forked from sschoenholz/WebGL-Raytracer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
404 lines (306 loc) · 11.3 KB
/
index.html
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Raytracer</title>
<link href="stylesheets/screen.css" media="all" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="javascripts/resources/webgl-utils.js"></script>
<script language="javascript" src="javascripts/engine/opengl.js" type="text/javascript"></script>
<script language="javascript" src="javascripts/engine/geometry.js" type="text/javascript"></script>
<script type="text/javascript" src="javascripts/resources/gl-matrix.js"></script>
</head>
<body>
<canvas id="canvas" width = "600" height = "600"></canvas>
<script id="shader-fs" type="x-shader/x-fragment">
precision highp float;
uniform vec2 uResolution;
//constants
#define MAX_OBJECTS 10
#define MAX_LIGHTS 10
#define EPSILON 0.001
#define MAX_RECURSION 4
//types of objects as defines
#define SPHERE 0
#define PLANE 1
//the scene as 2d textures
//the id of objects as well as the type.
uniform sampler2D objects;
//the position of the objects a vec4 to allow for one free parameter
uniform sampler2D objectDefinitions;
//the materials of the object format to be decided.
uniform sampler2D objectMaterials;
//more material properties so that each pixel represents a unique object
uniform sampler2D objectMaterialsExtended;
//the light positions
uniform sampler2D lights;
//the light colors
uniform sampler2D lightMaterials;
//the number of objects in the scene
uniform int numObjects;
uniform float objectTextureSize;
uniform int numLights;
uniform float lightTextureSize;
//Function to intersect a ray with a sphere
void iSphere(in vec2 ID, in vec3 origin,in vec3 direction, in vec4 sphere, inout float closestIntersection, inout vec2 closestID)
{
//(o + td - c)^2 = r^2
//(o-c).(o-c) - r^2 + 2t(o-c).d + t^2 = 0
float b = 2.0*dot(origin-sphere.xyz,direction);
float c = dot(origin-sphere.xyz,origin-sphere.xyz) - sphere.w*sphere.w;
float disc = b*b - 4.0*c;
if(disc < 0.0)
return;
float tmp = -(b+sqrt(disc))/2.0;
if(tmp > 0.0 && tmp < closestIntersection){
closestIntersection = tmp;
closestID = ID;
}
}
vec3 nSphere(in vec3 position, in vec4 sphere)
{
return normalize(position - sphere.xyz);
}
//Function to intersect a ray with a plane
void iPlane(in vec2 ID, in vec3 origin, in vec3 direction,in vec4 normal,inout float closestIntersection, inout vec2 closestID)
{
//(o + td - p0).n = 0 --> (o+td).n = p0.n ---> t = p0.n-
float tmp = (normal.w-dot(origin,normal.xyz))/dot(direction,normal.xyz);
if(tmp > 0.0 && tmp < closestIntersection){
closestIntersection = tmp;
closestID = ID;
}
}
vec3 nPlane(in vec3 position, in vec4 plane)
{
return plane.xyz;
}
//function to go through the scene and do all of the ray intersections
void intersect(in vec3 origin, in vec3 direction,inout float closestIntersection, inout vec2 closestID)
{
float it = 1.0/objectTextureSize/2.0;
float ity = 1.0/objectTextureSize/2.0;
float step = 1.0/objectTextureSize;
for(int i = 0 ; i < MAX_OBJECTS ; i++)
{
if(i >= numObjects)
break;
int objectType = int(texture2D(objects,vec2(it,ity)).y*256.0);
if(objectType == SPHERE)
iSphere(vec2(it,ity),origin,direction,texture2D(objectDefinitions,vec2(it,ity)),closestIntersection,closestID);
else if(objectType ==PLANE)
iPlane(vec2(it,ity),origin,direction,texture2D(objectDefinitions,vec2(it,ity)),closestIntersection,closestID);
it+=step;
if(it > 1.0)
{
ity += step;
it = step/2.0;
}
}
}
//function to get the normal from an intersection
vec3 normal(in vec3 position,in vec2 ID)
{
if(int(texture2D(objects,ID).y*256.0) == SPHERE)
return nSphere(position,texture2D(objectDefinitions,ID));
else if(int(texture2D(objects,ID).y*256.0) == PLANE)
return nPlane(position,texture2D(objectDefinitions,ID));
return vec3(0.0,0.0,0.0);
}
//function to handle the lighting
vec4 computeLighting(in vec3 origin, in vec3 direction, in float t, in vec2 ID)
{
vec3 intersection = origin + t*direction;
vec3 norm = normal(intersection,ID);
vec4 color = vec4(0.01*texture2D(objectMaterials,ID).xyz,1.0);
float it = 1.0/lightTextureSize/2.0;
float ity = 1.0/lightTextureSize/2.0;
float step = 1.0/lightTextureSize;
//go through each light and add its contribution to the illumination
for(int i = 0 ; i < MAX_LIGHTS ; i++)
{
if(i >= numLights)
break;
//if the point can see the light source
vec3 lightdir = normalize(texture2D(lights,vec2(it,ity)).xyz-intersection);
vec2 shadowID = vec2(-1.0,-1.0);
float shadowT = 1000.0;
intersect(intersection+lightdir*EPSILON,lightdir,shadowT,shadowID);
if(shadowID.x < 0.0 || shadowT < 0.01){
//diffuse lighting
float dp = dot(norm,lightdir);
if(dp>0.0)
color += texture2D(objectMaterials,ID).w*dp*vec4(texture2D(objectMaterials,ID).xyz,1.0)*texture2D(lightMaterials,vec2(it,ity));
//specular lighting
vec3 R = lightdir - 2.0 * dot(lightdir,norm) * norm;
dp = dot(direction,R);
if(dp > 0.0) {
color += texture2D(objectMaterialsExtended,ID).x * pow(dp,20.0) * vec4(texture2D(objectMaterials,ID).xyz,1.0);
}
}
it+=step;
if(it > 1.0)
{
ity += step;
it = step/2.0;
}
}
return color;
}
vec4 reflection(in vec3 origin, in vec3 direction, in float t, in vec2 ID) {
vec4 color[MAX_RECURSION];
float reflCoefficient[MAX_RECURSION];
int recursion = 0;
for(int i = 0 ; i < MAX_RECURSION ; i++)
{
recursion = i;
vec3 intersection = origin + t*direction;
vec3 norm = normal(intersection,ID);
direction = direction - 2.0*dot(direction,norm)*norm;
origin = intersection + direction * EPSILON;
vec2 reflID = vec2(-1.0,-1.0);
float reflT = 1000.0;
//intersect the reflected ray with the scene
intersect(origin,direction,reflT,reflID);
//if we intersected an object
if(reflID.x < 0.0) {
color[i] = vec4(0.0,0.0,0.4,1.0);
reflCoefficient[i] = texture2D(objectMaterialsExtended,ID).y;
break;
}
color[i] = computeLighting(origin,direction,reflT,reflID);
reflCoefficient[i] = texture2D(objectMaterialsExtended,ID).y;
if(reflCoefficient[i] < EPSILON)
break;
ID = reflID;
t = reflT;
}
vec4 sum = vec4(0.0,0.0,0.0,1.0);
for(int i = 0 ; i < MAX_RECURSION ; i++)
{
if(i > recursion)
break;
vec4 prod = vec4(1.0,1.0,1.0,1.0);
for(int j = 0 ; j < MAX_RECURSION ; j++){
if(j > i)
break;
prod*=reflCoefficient[j];
}
sum+=color[i]*prod;
}
return sum;
}
//main function
void main(void) {
vec3 origin = vec3(0.0,1.0,-8.0);
vec3 direction = normalize(vec3((gl_FragCoord.xy/uResolution-0.5)*2.0,1.0));
float t = 1000.0;
vec2 ID = vec2(-1.0,-1.0);
intersect(origin,direction,t,ID);
//if we intersected with an object
if(ID.x > 0.0)
{
vec4 color = computeLighting(origin,direction,t,ID);
//if the object is reflective then add a reflective contribution
if(texture2D(objectMaterialsExtended,ID).y > EPSILON)
color += reflection(origin,direction,t,ID);
gl_FragColor = color;
}else{
gl_FragColor = vec4(0.0,0.0,0.4,1.0);
}
}
</script>
<script id="shader-vs" type="x-shader/x-vertex">
attribute vec2 aVertexPosition;
void main(void) {
gl_Position = vec4(aVertexPosition, 0.0, 1.0);
}
</script>
<script type='text/javascript'>
//<![CDATA[
var FPS = 60;
var glCanvas;
var raytracer;
//Function to initialize data
initialize();
//Start the main loop
tick();
//Main game loop.
function tick() {
requestAnimFrame(tick);
update();
draw();
}
function initialize()
{
glCanvas = InitializeWebGL(document.getElementById("canvas"));
if(glCanvas)
{
//Initialize basic attributes
glCanvas.clearColor(0.0,0.0,0.0,1.0);
glCanvas.enable(glCanvas.DEPTH_TEST);
glCanvas.depthFunc(glCanvas.LEQUAL);
glCanvas.clear(glCanvas.COLOR_BUFFER_BIT|glCanvas.DEPTH_BUFFER_BIT);
//Initialize the ray tracer
raytracer = new Raytracer(glCanvas);
raytracer.build();
//Add geometry to the scene
raytracer.objects.push(new Sphere());
raytracer.objects[0].position = [0.0,1.01,0.0,1.0];
raytracer.objects[0].material = [0.2,1.0,0.2,0.75];
raytracer.objects[0].materialExtended = [0.3,0.6,0.0,0.0];
raytracer.objects.push(new Sphere());
raytracer.objects[1].position = [2.0,1.01,0.0,1.0];
raytracer.objects[1].material = [0.0,0.0,1.0,0.75];
raytracer.objects[1].materialExtended = [0.3,0.6,0.0,0.0];
raytracer.objects.push(new Sphere());
raytracer.objects[2].position = [-2.0,1.01,0.0,1.0];
raytracer.objects[2].material = [1.0,0.0,0.0,0.75];
raytracer.objects[2].materialExtended = [0.3,0.6,0.0,0.0];
raytracer.objects.push(new Sphere());
raytracer.objects[3].position = [0.0,1.01,2.0,1.0];
raytracer.objects[3].material = [1.0,1.0,0.0,0.75];
raytracer.objects[3].materialExtended = [0.3,0.6,0.0,0.0];
raytracer.objects.push(new Sphere());
raytracer.objects[4].position = [0.0,1.01,2.0,1.0];
raytracer.objects[4].material = [1.0,0.0,1.0,0.75];
raytracer.objects[4].materialExtended = [0.3,0.6,0.0,0.0];
raytracer.objects.push(new Plane());
raytracer.objects[5].position = [0.0,1.0,0.0,0.0];
raytracer.objects[5].material = [0.25,0.25,0.25,1.0];
raytracer.objects[5].materialExtended = [0.0,0.4,0.0,0.0];
raytracer.lights.push(new Light());
raytracer.lights[0].position = [0.0,6.0,-2.0,0.0];
raytracer.lights[0].material = [1.0,1.0,1.0,1.0];
raytracer.lights.push(new Light());
raytracer.lights[1].position = [0.0,6.0,2.0,0.0];
raytracer.lights[1].material = [1.0,1.0,1.0,1.0];
//build the geometry
raytracer.buildObjects();
raytracer.buildLights();
}
}
//function to update logic
function update()
{
}
//function to draw to the world
function draw() {
if(glCanvas){
//draw the world
glCanvas.viewport(0.0,0.0,glCanvas.viewportWidth,glCanvas.viewportHeight);
glCanvas.clear(glCanvas.COLOR_BUFFER_BIT|glCanvas.DEPTH_BUFFER_BIT);
//update the object positions
time = new Date().getTime();
raytracer.objects[0].position = [0.0,2.0+Math.sin(0.1*time/1000),0.0,1.0];
raytracer.objects[1].position = [2.0*Math.cos(time/1000.0),2.0+Math.sin(time/1000),2.0*Math.sin(time/1000.0),0.4];
raytracer.objects[2].position = [-2.0*Math.cos(time/1000.0),2.0+Math.sin(time/1000),-2.0*Math.sin(time/1000.0),0.4];
raytracer.objects[3].position = [2.0*Math.sin(time/1000.0),2.0+Math.sin(time/1000),-2.0*Math.cos(time/1000.0),0.4];
raytracer.objects[4].position = [-2.0*Math.sin(time/1000.0),2.0+Math.sin(time/1000),2.0*Math.cos(time/1000.0),0.4];
//rebuild and draw the scene
raytracer.buildObjects();
raytracer.draw();
}
}
//]]>
</script>
</body>
</html>