-
Notifications
You must be signed in to change notification settings - Fork 0
/
Terrain Heightmap Grid.sketchplugin
171 lines (128 loc) · 4.37 KB
/
Terrain Heightmap Grid.sketchplugin
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// Terrain Heightmap Grid
// v.03
//
// This command creates a square bitmap image containing a random heightmap.
//
// The default settings can be adjusted within the script.
//
// Based on the original article:
// http://www.playfuljs.com/realistic-terrain-in-130-lines/
//
// Copyright (c) 2014 Les R. Titze. All rights reserved.
// Twitter: @lrtitze
// USER MODIFIABLE
var blockSize = 10;
var roughness = 0.75; // larger values for more scattering
var detail = 5; // number of blocks per side will be 1 + 2^detail
// CODE FOLLOWS
var blockCount = Math.pow(2, detail) + 1;
// Get current "canvas".
var page = [doc currentPage],
artboard = [[doc currentPage] currentArtboard],
currentCanvas = artboard ? artboard : page;
var imgRect = NSMakeRect(0, 0, blockSize*blockCount, blockSize*blockCount);
function Terrain(detail) {
this.size = Math.pow(2, detail) + 1;
this.max = this.size - 1;
this.map = new Array(this.size * this.size);
// this.map = new Float32Array(this.size * this.size);
}
Terrain.prototype.get = function(x, y) {
if (x < 0 || x > this.max || y < 0 || y > this.max) return -1;
return this.map[x + this.size * y];
};
Terrain.prototype.set = function(x, y, val) {
this.map[x + this.size * y] = val;
};
Terrain.prototype.generate = function(roughness) {
var self = this;
this.set( 0, 0, self.max);
this.set(this.max, 0, self.max / 2);
this.set(this.max, this.max, 0);
//this.set(this.max, this.max, self.max / 2);
this.set( 0, this.max, self.max / 2);
divide(this.max);
function divide(size) {
var x, y, half = size / 2;
var scale = roughness * size;
if (half < 1) return;
for (y = half; y < self.max; y += size) {
for (x = half; x < self.max; x += size) {
square(x, y, half, Math.random() * scale * 2 - scale);
}
}
for (y = 0; y <= self.max; y += half) {
for (x = (y + half) % size; x <= self.max; x += size) {
diamond(x, y, half, Math.random() * scale * 2 - scale);
}
}
divide(size / 2);
}
function average(values) {
var valid = values.filter(function(val) { return val !== -1; });
var total = valid.reduce(function(sum, val) { return sum + val; }, 0);
return total / valid.length;
}
function square(x, y, size, offset) {
var ave = average([
self.get(x - size, y - size), // upper left
self.get(x + size, y - size), // upper right
self.get(x + size, y + size), // lower right
self.get(x - size, y + size) // lower left
]);
self.set(x, y, ave + offset);
}
function diamond(x, y, size, offset) {
var ave = average([
self.get(x, y - size), // top
self.get(x + size, y), // right
self.get(x, y + size), // bottom
self.get(x - size, y) // left
]);
self.set(x, y, ave + offset);
}
};
Terrain.prototype.draw = function(size, img) {
var self = this;
log("draw");
//var img = [NSImage imageWithSize:size];
var littleBox = NSMakeRect(0, 0, blockSize, blockSize);
var x = 0;
var y = 0;
var ix = 0;
var level = 0.0;
var useColor = [NSColor colorWithRed:0 green:0 blue:0 alpha:1.0];
[img lockFocus]
for (var row = 0; row < this.size; row++) {
for (var col = 0; col < this.size; col++) {
x = col * blockSize;
y = row * blockSize;
littleBox.origin.x = x;
littleBox.origin.y = y;
level = (this.get(col, row)) / this.max;
useColor = [NSColor colorWithWhite:level alpha:1.0];
[useColor set];
[NSBezierPath fillRect:littleBox];
}
}
[img unlockFocus];
return img;
}
// Creates a new bitmap image and adds it to the Layer group
function addImgToCanvas (el, img, layerName) {
var imageCollection = el.documentData().images();
var imageData = imageCollection.addImage_name_convertColourspace(img, layerName, false);
var newImage = MSBitmapLayer.alloc().initWithImage_parentFrame_name(imageData, el.frame(), layerName);
el.addLayer(newImage);
return newImage;
}
var terrain = new Terrain(detail);
terrain.generate(roughness); // 0.7
log ("imgRect.size is: " + imgRect.size);
var img = [NSImage imageWithSize:imgRect.size];
var pattImage = terrain.draw(imgRect.size, img);
var layerName = 'Pattern ' + blockCount + " x " + blockCount;
var layer = addImgToCanvas(currentCanvas, pattImage, layerName);
var frame = layer.frame();
frame.setX(0);
frame.setY(0);