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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
|
var container, stats;
var camera, controls, scene, renderer;
var mesh, texture, material;
//FIXME experiment with these fixed params
var worldWidth = 256, worldDepth = 256, worldHalfWidth = worldWidth / 2, worldHalfDepth = worldDepth / 2;
var textureFile = 'img/Texture.jpg';
var heightmapFile = 'img/Heightmap.jpg';
var pos_x, pos_y, pos_z, rot_x, rot_y, rot_z;
var wf = false;
function runWebGLSimulation(){
//Detect WebGL
if (!Detector.webgl) {
Detector.addGetWebGLMessage();
document.getElementById('container').innerHTML = "";
}
//Start Graphics
initGraphics(textureFile, heightmapFile, function(){ animate() });
//Start Scene Animation
}
function initGraphics(textureFile, heightmapFile, cb) {
container = document.getElementById('container');
//Set camera
camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 1, 20000);
//Set scene
scene = new THREE.Scene();
//Get heightmap data
//Generates the heightmap data from the heightmap image
var size = worldWidth * worldDepth, data = new Float32Array(size);
var canvas = document.createElement('canvas');
canvas.width = worldWidth;
canvas.height = worldDepth;
context = canvas.getContext('2d');
context.fillStyle = '#000';
context.fillRect(0, 0, worldWidth, worldDepth);
var img = new Image();
img.src = heightmapFile;
img.onerror = function() {
alert("The following url did not work: \n"+heightmapFile.slice(15));
is_generating = false;
toggle_background();
};
img.onload = function(){
context.drawImage(img, 0, 0);
image = context.getImageData(0, 0, worldWidth, worldDepth);
var imageData = image.data;
var pixels = size;
for (var i=0; i<pixels; i++){
// Get RGB
red = imageData[4 * i + 0];
green = imageData[4 * i + 1];
blue = imageData[4 * i + 2];
// Get grayscale
gray = (red + green + blue) / 3;
// Put heightmap value
data[i] = gray;
}
//Set camera position
camera.position.y = data[worldHalfWidth + worldHalfDepth * worldWidth] + 500;
scene.add(camera);
//Create geometry data used for mesh
var geometry = new THREE.PlaneGeometry(7500, 7500, worldWidth - 1, worldDepth - 1);
for ( var i = 0, l = geometry.vertices.length; i < l; i++) {
geometry.vertices[i].position.z = data[i] * 3;
}
//Load texture
texture = THREE.ImageUtils.loadTexture(textureFile, {}, function() {
renderer.render(scene, camera);
});
texture.needsUpdate = true;
//Create mesh from heightmap and texture
material = new THREE.MeshBasicMaterial({
map : texture,
wireframe: wf
});
mesh = new THREE.Mesh(geometry, material);
mesh.rotation.x = -90 * Math.PI / 180;
scene.add(mesh);
//Create renderer
renderer = new THREE.WebGLRenderer({
preserveDrawingBuffer : true // required to support .toDataURL()
});
renderer.setSize(window.innerWidth, window.innerHeight - 150); //FIXME, add height of the controls
container.innerHTML = "";
container.appendChild(renderer.domElement);
//Get default values
pos_x = mesh.position.x;
pos_y = mesh.position.y;
pos_z = mesh.position.z;
rot_x = mesh.rotation.x;
rot_y = mesh.rotation.y;
rot_z = mesh.rotation.z;
is_generating = false;
toggle_background();
cb();
};
}
var requestId;
function loop() {
render();
requestId = window.requestAnimationFrame(loop);
}
function animate() {
if (!requestId) {
loop();
}
}
function stop_animating() {
if (requestId) {
window.cancelAnimationFrame(requestId);
requestId = undefined;
}
}
function render() {
renderer.render(scene, camera);
}
function init_controls2(){
var listener = new window.keypress.Listener();
listener.simple_combo("space", function() {
wf = !wf;
material.wireframe = wf;
});
listener.simple_combo("shift up", function() {
mesh.rotation.x += 0.1;
});
listener.simple_combo("shift down", function() {
mesh.rotation.x -= 0.1;
});
listener.simple_combo("shift left", function() {
mesh.rotation.z += 0.1;
});
listener.simple_combo("shift right", function() {
mesh.rotation.z -= 0.1;
});
listener.simple_combo("pageup", function() {
mesh.position.z += 150;
});
listener.simple_combo("pagedown", function() {
mesh.position.z -= 150;
});
listener.simple_combo("up",function() {
mesh.position.y += 150;
});
//Down
listener.simple_combo("down",function() {
mesh.position.y -= 150;
});
//Left
listener.simple_combo("left",function() {
mesh.position.x -= 150;
});
//Right
listener.simple_combo("right",function() {
mesh.position.x += 150;
});
//Reset
listener.simple_combo("delete",function() {
mesh.position.x = pos_x;
mesh.position.y = pos_y;
mesh.position.z = pos_z;
mesh.rotation.x = rot_x;
mesh.rotation.y = rot_y;
mesh.rotation.z = rot_z;
});
}
function init_controls(){
//{{{init controls
//Toggle wire-frame
shortcut.add("Space",function() {
wf = !wf;
material.wireframe = wf;
});
//Rotate Up
shortcut.add("Shift+Up",function() {
mesh.rotation.x += 0.1;
});
//Rotate Down
shortcut.add("Shift+Down",function() {
mesh.rotation.x -= 0.1;
});
//Rotate Left
shortcut.add("Shift+Left",function() {
mesh.rotation.z += 0.1;
});
//Rotate Right
shortcut.add("Shift+Right",function() {
mesh.rotation.z -= 0.1;
});
//Zoom In
shortcut.add("Page_up",function() {
mesh.position.z += 150;
});
//Zoom Out
shortcut.add("Page_down",function() {
mesh.position.z -= 150;
});
//Up
shortcut.add("Up",function() {
mesh.position.y += 150;
});
//Down
shortcut.add("Down",function() {
mesh.position.y -= 150;
});
//Left
shortcut.add("Left",function() {
mesh.position.x -= 150;
});
//Right
shortcut.add("Right",function() {
mesh.position.x += 150;
});
//Reset
shortcut.add("Delete",function() {
mesh.position.x = pos_x;
mesh.position.y = pos_y;
mesh.position.z = pos_z;
mesh.rotation.x = rot_x;
mesh.rotation.y = rot_y;
mesh.rotation.z = rot_z;
});
//}}}
}
window.onload= function(e){
runWebGLSimulation();
init_controls2();
}
|