-
Notifications
You must be signed in to change notification settings - Fork 112
/
images.js
81 lines (80 loc) · 3.1 KB
/
images.js
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
/**
* Convert an image-based heightmap into vertex-based height data.
*
* @param {Float32Array} g
* The geometry's z-positions to modify with heightmap data.
* @param {Object} options
* A map of settings that control how the terrain is constructed and
* displayed. Valid values are the same as those for the `options` parameter
* of {@link THREE.Terrain}().
*/
THREE.Terrain.fromHeightmap = function(g, options) {
var canvas = document.createElement('canvas'),
context = canvas.getContext('2d'),
rows = options.ySegments + 1,
cols = options.xSegments + 1,
spread = options.maxHeight - options.minHeight;
canvas.width = cols;
canvas.height = rows;
context.drawImage(options.heightmap, 0, 0, canvas.width, canvas.height);
var data = context.getImageData(0, 0, canvas.width, canvas.height).data;
for (var row = 0; row < rows; row++) {
for (var col = 0; col < cols; col++) {
var i = row * cols + col,
idx = i * 4;
g[i] = (data[idx] + data[idx+1] + data[idx+2]) / 765 * spread + options.minHeight;
}
}
};
/**
* Convert a terrain plane into an image-based heightmap.
*
* Parameters are the same as for {@link THREE.Terrain.fromHeightmap} except
* that if `options.heightmap` is a canvas element then the image will be
* painted onto that canvas; otherwise a new canvas will be created.
*
* @param {Float32Array} g
* The vertex position array for the geometry to paint to a heightmap.
* @param {Object} options
* A map of settings that control how the terrain is constructed and
* displayed. Valid values are the same as those for the `options` parameter
* of {@link THREE.Terrain}().
*
* @return {HTMLCanvasElement}
* A canvas with the relevant heightmap painted on it.
*/
THREE.Terrain.toHeightmap = function(g, options) {
var hasMax = typeof options.maxHeight !== 'undefined',
hasMin = typeof options.minHeight !== 'undefined',
max = hasMax ? options.maxHeight : -Infinity,
min = hasMin ? options.minHeight : Infinity;
if (!hasMax || !hasMin) {
var max2 = max,
min2 = min;
for (var k = 2, l = g.length; k < l; k += 3) {
if (g[k] > max2) max2 = g[k];
if (g[k] < min2) min2 = g[k];
}
if (!hasMax) max = max2;
if (!hasMin) min = min2;
}
var canvas = options.heightmap instanceof HTMLCanvasElement ? options.heightmap : document.createElement('canvas'),
context = canvas.getContext('2d'),
rows = options.ySegments + 1,
cols = options.xSegments + 1,
spread = max - min;
canvas.width = cols;
canvas.height = rows;
var d = context.createImageData(canvas.width, canvas.height),
data = d.data;
for (var row = 0; row < rows; row++) {
for (var col = 0; col < cols; col++) {
var i = row * cols + col,
idx = i * 4;
data[idx] = data[idx+1] = data[idx+2] = Math.round(((g[i * 3 + 2] - min) / spread) * 255);
data[idx+3] = 255;
}
}
context.putImageData(d, 0, 0);
return canvas;
};