-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathvoxel.js
343 lines (305 loc) · 15.5 KB
/
voxel.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
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/*
* javascript voxel demo
*
* Copyright (c) 2009 Selim Arsever (voxel.onaluf.org)
* licensed under the MIT (MIT-LICENSE.txt)
*/
var voxel = (function() {
// local variables:
var texture, heightmap, offscreenCanvas, offscreenContext, onscreenCanvas, onscreenContext, frame, startTime, frameCounter;
var constants = {
highres: false,
screen: {
height: 100,
width: 160,
zoom: 4
},
pov: {
verticalOpening: 0.4,
depthOfField: 900
},
color: {
fog: [216, 247, 255]
},
init: function() {
constants.pov.horizontalOpening = Math.atan(constants.pov.verticalOpening) * constants.screen.width / constants.screen.height;
constants.screen.distance = constants.screen.width / 2 / Math.tan(constants.pov.horizontalOpening);
}
};
//you app's private functions comme here
var imageToImageData = function (image){
// draw the image to to the canvas
var canvas = document.createElement('canvas');
canvas.width = image.width;
canvas.height = image.height;
var context = canvas.getContext("2d");
context.drawImage(image, 0, 0);
// return the imageData
return context.getImageData(0, 0, image.width, image.height);
};
var imageDataToArrayR = function (imageData) {
// create an exportable array instead of the data part
// but with only the red component
var temp = {
width: imageData.width,
height: imageData.height,
data: []
};
for(var i = 0; i < imageData.data.length / 4; i++) {
temp.data[i] = imageData.data[i*4];
}
return temp;
};
var imageDataToArray = function (imageData) {
// create an exportable array instead of the data part
var temp = {
width: imageData.width,
height: imageData.height,
data: []
};
for(var i = 0; i < imageData.data.length; i++) {
temp.data[i] = imageData.data[i];
}
return temp;
};
var position = {
x: 450,
y: 0,
z: 180,
a: Math.PI/2,
antialiasing: false
};
frameCounter = 0;
var loadHeightmap = function(heighmapFilename, textureFilename) {
var img = new Image();
img.onload = function() {
// convert the image to an ImageData
var imageData = imageToImageData(img);
// convert the imageData to an array
heightmap = imageDataToArrayR(imageData);
// Load the texture
loadTexture(textureFilename);
};
img.src = heighmapFilename;
};
var loadTexture = function(textureFilename) {
var img = new Image();
img.onload = function() {
// draw the image to to the canvas
var imageData = imageToImageData(img);
// convert the imageData to an array
texture = imageDataToArray(imageData);
// now we can continue
imagesLoaded();
};
img.src = textureFilename;
};
var imagesLoaded = function() {
// initialize constants:
constants.init();
// create a sort of double buffer (two context):
// first a offscreen context
offscreenCanvas = document.createElement('canvas');
offscreenCanvas.width = constants.screen.width;
offscreenCanvas.height = constants.screen.height;
offscreenContext = offscreenCanvas.getContext("2d");
// second the onscreen context
onscreenCanvas = document.getElementById("canvas");
onscreenCanvas.width = constants.screen.width * constants.screen.zoom;
onscreenCanvas.height = constants.screen.height * constants.screen.zoom;
onscreenContext = onscreenCanvas.getContext("2d");
// get an ImageData to draw each frame
frame = offscreenContext.getImageData(0, 0, constants.screen.width, constants.screen.height);
// send all important data to the worker:
startTime = new Date().getTime();
renderFrame();
};
var renderFrame = function() {
// Clean the frame to the sky color
offscreenContext.fillStyle = "rgb("+constants.color.fog[0]+","+constants.color.fog[1]+","+constants.color.fog[2]+")";
offscreenContext.fillRect(0, 0, constants.screen.width, constants.screen.height);
frame = offscreenContext.getImageData(0, 0, constants.screen.width, constants.screen.height);
var image = frame.data;
var doff = constants.pov.depthOfField / 4;
var c1 = constants.screen.height /2;
var c2 = constants.screen.distance * position.z;
for (var i = 0; i < constants.screen.width; i++){
var orientation = position.a - constants.pov.horizontalOpening*(1-i*2/constants.screen.width);
var progression = {x: Math.cos(orientation), y: Math.sin(orientation)};
var distanceProbed = 0;
var screenProjectedTop = 0;
var oldHeight = 0;
var oldRenderCache = false;
var summit = false;
while(distanceProbed < constants.pov.depthOfField && screenProjectedTop < constants.screen.height) {
// 1) find the projection of the current point on the screen space
distanceProbed += (distanceProbed < doff)? 2 : (distanceProbed < 2 * doff)? 4 : (distanceProbed < 3 * doff)? 8 : 16;
var probe = { //warp for texture
x: Math.abs(Math.ceil(position.x + distanceProbed * progression.x)),
y: Math.abs(Math.ceil(position.y + distanceProbed * progression.y))
};
var dataIndex = heightmap.width * (probe.y % heightmap.height) + (probe.x % heightmap.width);
// This is a small optimisation to skip some projection computation
var height = heightmap.data[dataIndex];
if(height < oldHeight){
oldHeight = height;
continue;
}
oldHeight = height;
var projectedHeight = Math.min(Math.ceil(c1 - (c2 - constants.screen.distance*height) / distanceProbed), constants.screen.height);
// 2) if visible we draw it
if (projectedHeight > screenProjectedTop) {
var textureDataIndex = (texture.width * (probe.y % texture.height) + (probe.x % texture.width)) * 4;
var textureCache = [texture.data[textureDataIndex], texture.data[textureDataIndex + 1], texture.data[textureDataIndex + 2]];
var fillGoal = Math.max(constants.screen.height-projectedHeight, 0);
var fogFactor = Math.min(distanceProbed, constants.pov.depthOfField)/constants.pov.depthOfField;
var invFogFactor = (1 - fogFactor);
// antialiasing
var renderCache = {
r: invFogFactor * textureCache[0] + fogFactor * constants.color.fog[0],
g: invFogFactor * textureCache[1] + fogFactor * constants.color.fog[1],
b: invFogFactor * textureCache[2] + fogFactor * constants.color.fog[2]
};
if(position.antialiasing && oldRenderCache && summit) {
var previousIndex = (i + constants.screen.width * (constants.screen.height-screenProjectedTop+1))*4;
image[previousIndex] = Math.ceil(0.5*renderCache.r + 0.5*oldRenderCache.r);
image[previousIndex+1] = Math.ceil(0.5*renderCache.g + 0.5*oldRenderCache.g);
image[previousIndex+2] = Math.ceil(0.5*renderCache.b + 0.5*oldRenderCache.b);
}
oldRenderCache = renderCache;
// render
for (var j = (constants.screen.height - screenProjectedTop); j > fillGoal; j--) {
image[(i+j*constants.screen.width)*4] = Math.ceil(renderCache.r);
image[(i+j*constants.screen.width)*4+1] = Math.ceil(renderCache.g);
image[(i+j*constants.screen.width)*4+2] = Math.ceil(renderCache.b);
image[(i+j*constants.screen.width)*4+3] = 255;
}
screenProjectedTop = projectedHeight;
summit = false;
} else if (screenProjectedTop > projectedHeight){
summit = true;
}
}
// 3) if the top is lower than the top of the screen we fill it
if(position.antialiasing){
if((constants.screen.height - screenProjectedTop + 1) >= 0) {
var j = (i + (constants.screen.height - screenProjectedTop + 1 ) * constants.screen.width)*4;
image[j] = Math.ceil(0.5*image[j] + 0.5*constants.color.fog[0]);
image[j+1] = Math.ceil(0.5*image[j+1] + 0.5*constants.color.fog[1]);
image[j+2] = Math.ceil(0.5*image[j+2] + 0.5*constants.color.fog[2]);
}
}
}
frame.data = image;
offscreenContext.putImageData(frame, 0, 0);
onscreenContext.drawImage(offscreenCanvas, 0, 0, constants.screen.zoom*constants.screen.width, constants.screen.zoom*constants.screen.height);
// mesure framerate
frameCounter++;
document.getElementById("fps").innerHTML = (Math.floor(frameCounter/(new Date().getTime()-startTime)*10000)/10)+"fps";
if(frameCounter > 30) {
frameCounter = 0;
startTime = new Date().getTime();
}
setTimeout(renderFrame, 1);
};
var pressed = false;
var oldClientX = false;
return {
//you app's visible functions comme here
eventHandler: function(e) {
var increment = 10;
switch(e.keyCode) {
case 37: //left
position.a -= 0.03;
break;
case 38: //up
position.x += Math.cos(position.a)*increment;
position.y += Math.sin(position.a)*increment;
break;
case 39: //right
position.a += 0.03;
break;
case 40: //down
position.x -= Math.cos(position.a)*increment;
position.y -= Math.sin(position.a)*increment;
break;
case 87: // w = UP
position.z += 10;
break;
case 83: // s = DOWN
position.z -= 10;
break;
case 65: // a : antialiasing
position.antialiasing = !position.antialiasing;
break;
case 81: //q : swich resolution
constants.highres = !constants.highres;
if(constants.highres){
constants.screen.height = 400;
constants.screen.width = 640;
constants.screen.zoom = 1;
} else {
constants.screen.height = 100;
constants.screen.width = 160;
constants.screen.zoom = 4;
}
// initialize constants:
constants.init();
// create a sort of double buffer (two context):
// first a offscreen context
offscreenCanvas = document.createElement('canvas');
offscreenCanvas.width = constants.screen.width;
offscreenCanvas.height = constants.screen.height;
offscreenContext = offscreenCanvas.getContext("2d");
// second the onscreen context
onscreenCanvas = document.getElementById("canvas");
onscreenCanvas.width = constants.screen.width * constants.screen.zoom;
onscreenCanvas.height = constants.screen.height * constants.screen.zoom;
onscreenContext = onscreenCanvas.getContext("2d");
// get an ImageData to draw each frame
frame = offscreenContext.getImageData(0, 0, constants.screen.width, constants.screen.height);
}
voxel.keyTrap(e);
return false;
},
mouseHandler: function(event) {
if(event.type === "mousedown"){
if(event.button === 0) {
pressed = true;
}
} else if (event.type === "mouseup") {
if(event.button === 0) {
pressed = false;
}
} else if (event.type === "mousemove") {
if(pressed) {
var xdiff = event.clientX - oldClientX;
position.a += 0.01*xdiff;
var ydiff = event.clientY - oldClientY;
position.x -= Math.cos(position.a)*2*ydiff;
position.y -= Math.sin(position.a)*2*ydiff;
}
}
oldClientX = event.clientX;
oldClientY = event.clientY;
},
keyTrap: function (event) {
event = event || window.event;
switch (event.keyCode) {
case 37:
case 38:
case 39:
case 40:
if (event.preventDefault) {
event.preventDefault();
} else {
event.returnValue = false;
}
break;
}
},
start: function(heighmapFilename, textureFilename) {
loadHeightmap(heighmapFilename, textureFilename);
}
};
})();