-
Notifications
You must be signed in to change notification settings - Fork 0
/
raytracer.js
373 lines (333 loc) · 11.1 KB
/
raytracer.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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
// Ray tracer adapted from TypeScript
// Original code: https://github.com/Microsoft/TypeScriptSamples/tree/master/raytracer
var Vector = Create(struct, 'Vector', {
x: 0,
y: 0,
z: 0,
times: function times(k) {
return copy(vector, {x:k * this.x}, {y:k * this.y}, {z:k * this.z});
},
minus: function minus(v) {
return copy(vector, {x:this.x - v.x}, {y:this.y - v.y}, {z: this.z - v.z});
},
plus: function plus(v) {
return copy(vector, {x:this.x + v.x}, {y:this.y + v.y}, {z: this.z + v.z});
},
dot: function dot(v) {
return this.x * v.x + this.y * v.y + this.z * v.z;
},
mag: function mag() {
return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
},
norm: function norm() {
var mag = this.mag();
var div = (mag === 0) ? Infinity : 1.0 / mag;
return this.times(div);
},
cross: function cross(v) {
return copy(vector,
{x: this.y * v.z - this.z * v.y},
{y: this.z * v.x - this.x * v.z},
{z: this.x * v.y - this.y * v.x});
}
});
var vector = Vector.instance;
var Color = Create(struct, 'Color', {
r: 0.0,
g: 0.0,
b: 0.0,
scale: function scale(k) {
return copy(color, {r:k * this.r}, {g:k * this.g}, {b:k * this.b});
},
plus: function plus(c) {
return copy(color, {r:this.r + c.r}, {g:this.g + c.g}, {b:this.b + c.b});
},
times: function times(c) {
return copy(color, {r:this.r * c.r}, {g:this.g * c.g}, {b:this.b * c.b});
},
toDrawingColor: function toDrawingColor() {
var legalize = function legalize(d) {
return (d > 1.0) ? 1.0 : d;
};
return copy(color,
{r: Math.floor(legalize(this.r) * 255)},
{g: Math.floor(legalize(this.g) * 255)},
{b: Math.floor(legalize(this.b) * 255)});
}
});
var color = Color.instance;
var white = copy(color, {r: 1.0}, {g: 1.0}, {b: 1.0});
var grey = copy(color, {r: 0.5}, {g: 0.5}, {b: 0.5});
var black = color;
var background = black;
var Camera = Create(struct, 'Camera', {
forward: vector,
right: vector,
up: vector,
pos: vector,
$init(pos, lookAt) {
this.pos = pos;
var down = copy(vector, {y: -1.0});
this.forward = lookAt.minus(pos).norm();
this.right = this.forward.cross(down).norm().times(1.5);
this.up = this.forward.cross(this.right).norm().times(1.5);
}
});
var interfac = struct;
var Ray = Create(interfac, 'Ray', {
start: vector,
dir: vector
});
var Surface = Create(interfac, 'Surface', {
diffuse: function(pos) {return color;},
specular: function(pos) {return color;},
reflect: function(pos) {return 0;},
roughness: 0
});
var Thing = Create(interfac, 'Thing', {
intersect: function(ray) {return Intersection.instance;},
normal: function(pos) {return vector;},
surface: Surface.instance
});
var Intersection = Create(interfac, 'Intersection', {
thing: Thing.instance,
ray: Ray.instance,
dist: 0
});
var Light = Create(interfac, 'Light', {
pos: vector,
color: color
});
var Scene = Create(interfac, 'Scene', {
things: new Array(),
lights: new Array(),
camera: Camera.instance
});
var Sphere = Create(Thing.instance, 'Sphere', {
radius2: 1.0,
center: vector,
$init: function initSphere(center, radius, surface) {
this.radius2 = radius * radius;
this.center = center;
this.surface = surface;
},
$normal: function normal(pos) {
dodo.checkType('pos (Sphere.normal)', pos, vector);
return pos.minus(this.center).norm();
},
$intersect: function intersect(ray) {
var eo = this.center.minus(ray.start);
var v = eo.dot(ray.dir);
var dist = 0;
if (v > 0)
{
var disc = this.radius2 - (eo.dot(eo) - v * v);
if (disc > 0)
{
dist = v - Math.sqrt(disc);
}
}
if (dist === 0)
{
return null;
}
else
{
return copy(Intersection(), {thing:this}, {ray:ray}, {dist:dist});
}
}
});
var sphere = Sphere.instance;
var Plane = Create(Thing.instance, 'Plane', {
offset: 0.0,
$intersect: function intersect(ray) {
var norm = this.normal()
var denom = norm.dot(ray.dir);
if (denom > 0)
{
return null;
}
else
{
var dist = (norm.dot(ray.start) + this.offset) / (-denom);
return copy(Intersection(), {thing:this}, {ray:ray}, {dist:dist});
}
},
$init: function initPlane(norm, offset, surface) {
this.normal = function normal(pos) {
return norm;
};
this.surface = surface;
}
});
var module = struct;
var surfaces = extend(module, {
shiny: copy(Surface(),
{diffuse: function diffuse(pos) {return white;}},
{specular: function specular(pos) {return grey;}},
{reflect: function reflect(pos) {return 0.7;}},
{roughness: 250}
),
checkerboard: copy(Surface(),
{diffuse: function diffuse(pos) {
if ((Math.floor(pos.z) + Math.floor(pos.x)) % 2 !== 0)
{
return white;
}
else
{
return black;
}
}},
{specular: function specular(pos) {
return white;
}},
{reflect: function reflect(pos) {
if ((Math.floor(pos.z) + Math.floor(pos.x)) % 2 !== 0)
{
return 0.1;
}
else
{
return 0.7;
}
}},
{roughness: 150}
)
});
var RayTracer = Create(struct, 'RayTracer', {
_maxDepth: 5,
_intersections: function intersections(ray, scene) {
var closest = +Infinity;
var closestInter = null;
for (var i in scene.things)
{
var inter = scene.things[i].intersect(ray);
if (inter !== null && inter.dist < closest)
{
closestInter = inter;
closest = inter.dist;
}
}
return closestInter;
},
_testRay: function testRay(ray, scene) {
var isect = this._intersections(ray, scene);
if (isect === null)
{
return null;
}
return isect.dist;
},
_traceRay: function traceRay(ray, scene, depth) {
var isect = this._intersections(ray, scene);
if (isect === null) {
return background;
} else {
return this._shade(isect, scene, depth);
}
},
_shade: function shade(isect, scene, depth) {
var d = isect.ray.dir;
var pos = d.times(isect.dist).plus(isect.ray.start);
var normal = isect.thing.normal(pos);
var reflectDir = d.minus(normal.times(normal.dot(d)).times(2.0));
var naturalColor = background.plus(this._getNaturalColor(isect.thing, pos, normal, reflectDir, scene));
var reflectedColor = (depth >= this._maxDepth)
? grey
: this._getReflectionColor(isect.thing, pos, normal, reflectDir, scene, depth);
return naturalColor.plus(reflectedColor);
},
_getReflectionColor: function getReflectionColor(thing, pos, normal, rd, scene, depth) {
return this._traceRay(copy(Ray(), {start:pos}, {dir:rd}), scene, depth + 1).scale(thing.surface.reflect(pos));
},
_getNaturalColor: function getNaturalColor(thing, pos, norm, rd, scene) {
var self = this;
var addLight = function addLight(col, light) {
var ldis = light.pos.minus(pos);
var livec = ldis.norm();
var neatIsect = self._testRay(copy(Ray(), {start:pos}, {dir:livec}), scene);
var isInShadow = (neatIsect === null)
? false
: (neatIsect <= ldis.mag());
if (isInShadow)
{
return col;
}
else
{
var illum = livec.dot(norm);
var lcolor = (illum > 0)
? light.color.scale(illum)
: color;
var specular = livec.dot(rd.norm());
var scolor = (specular > 0)
? light.color.scale(Math.pow(specular, thing.surface.roughness))
: color;
return col.plus(thing.surface.diffuse(pos).times(lcolor).plus(thing.surface.specular(pos).times(scolor)));
}
};
return scene.lights.reduce(addLight, color);
},
render: function render(scene, ctx, screenWidth, screenHeight) {
var getPoint = function getPoint(x, y, camera) {
var recenterX = function(x) {
return (x - screenWidth / 2.0) / 2.0 / screenWidth;
};
var recenterY = function(y) {
return (screenHeight / 2.0 - y) / 2.0 / screenHeight;
};
return camera.forward.plus(camera.right.times(recenterX(x)).plus(camera.up.times(recenterY(y)))).norm();
};
for (var y = 0; y < screenHeight; y++)
{
for (var x = 0; x < screenWidth; x++)
{
var scolor = this._traceRay(copy(Ray(), {start:scene.camera.pos}, {dir:getPoint(x, y, scene.camera)}), scene, 0);
var c = scolor.toDrawingColor();
ctx.fillStyle = "rgb(" + String(c.r) + ", " + String(c.g) + ", " + String(c.b) + ")";
ctx.fillRect(x, y, x + 1, y + 1);
}
}
}
});
var rayTracer = RayTracer.instance;
function defaultScene() {
return copy(Scene(),
{things: [
Plane(copy(vector, {y:1.0}), 0.0, surfaces.checkerboard),
Sphere(copy(vector, {y:1.0}, {z:-0.25}), 1.0, surfaces.shiny),
Sphere(copy(vector, {x:0.5}, {y:1.75}, {z:2.1}), 0.25, surfaces.shiny),
Sphere(copy(vector, {x:-1.0}, {y:0.7}, {z:1.5}), 0.5, surfaces.shiny)
]},
{lights: [
copy(Light(),
{pos: copy(vector, {x:-2.0}, {y:2.5})},
{color: copy(color, {r:0.49}, {g:0.07}, {b:0.07})}
),
copy(Light(),
{pos: copy(vector, {x:1.5}, {y:2.5}, {z:1.5})},
{color: copy(color, {r:0.07}, {g:0.07}, {b:0.49})}
),
copy(Light(),
{pos: copy(vector, {x:1.5}, {y:2.5}, {z:-1.5})},
{color: copy(color, {r:0.57}, {g:0.8}, {b:0.071})}
),
copy(Light(),
{pos: copy(vector, {y:3.5})},
{color: copy(color, {r:0.21}, {g:0.21}, {b:0.35})}
)
]},
{camera: Camera(copy(vector, {x:3.0}, {y:2.0}, {z:4.0}), copy(vector, {x:-1.0}, {y:0.5}))}
);
}
function execute() {
var canv = document.createElement("canvas");
canv.width = 300;
canv.height = 300;
document.body.appendChild(canv);
var ctx = canv.getContext("2d");
return rayTracer.render(defaultScene(), ctx, canv.width, canv.height);
}
execute();
dodo.dumpLog();