-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patha6.html
337 lines (287 loc) · 11.4 KB
/
a6.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
<html>
<head>
<meta charset="UTF-8">
<title>CPSC 314 Assignment 6 Jan 2018</title>
<style>
body { margin: 0; } canvas {width: 100%;
height: 100% }
</style>
</head>
<body>
<div id="canvas"></div>
<script src="js/three.js"></script>
<script src="js/OBJLoader.js"></script>
<script src="js/SourceLoader.js"></script>
<script src="js/OrbitControls.js"></script>
<script src="js/KeyboardState.js"></script> <!-- By Jerome Etienne: http://jetienne.com/ -->
<script id="raytracerVertShader" type="x-shader/x-vertex">
void main()
{
gl_Position = projectionMatrix * modelViewMatrix * vec4(position,1.0);
}
</script>
<script id="raytracerFragShader" type="x-shader/x-fragment">
uniform vec2 resolution;
uniform vec3 lightPosition;
uniform vec3 ambient_light_color;
uniform vec3 diffuse_light_color;
uniform float myFloat1;
uniform float myFloat2;
const int n_spheres = 3;
float epsilon = 0.00001;
float largeT = 1000000.0;
vec3 backgroundColor = vec3(0.5,0.5,1.0);
mat4 planeMatrix = mat4(vec4(1,0,0,0),vec4(0,0,1,0),vec4(0,1,0,0),vec4(0,0,0,1));
struct light {
vec3 color;
vec3 position;
};
struct ray {
vec3 origin;
vec3 direction;
int bounces;
};
struct material {
float kLocal;
float kSpec;
vec3 color;
};
struct sphere {
material mtrl;
vec3 position;
float radius;
};
struct plane {
material mtrl;
vec3 position;
float width;
float height;
};
sphere nearestSphere;
sphere scene_spheres[n_spheres];
/////////////////////////////////////////////////////////////////////////////////
// sphereIntersect(): returns smallest posistive t value for a sphere intersection;
// returns -1 if no intersection
/////////////////////////////////////////////////////////////////////////////////
float sphere_intersect(ray myRay, sphere sphr) {
float a = dot(myRay.direction, myRay.direction);
vec3 s0_r0 = myRay.origin - sphr.position;
float b = 2.0 * dot(myRay.direction, s0_r0);
float c = dot(s0_r0, s0_r0) - (sphr.radius * sphr.radius);
float d = sqrt(b*b-4.0*a*c); // compute the discriminant
if (d < 0.0) { // no solution to the quadratic equation?
return -1.0; // then flag as no intersection
} else {
float t1 = (-b - d)/(2.0*a); // compute both values of t
float t2 = (-b + d)/(2.0*a);
float tmin = min(t1,t2);
float tmax = max(t1,t2);
if (tmax<=0.0+epsilon) return -1.0;
if (tmin<=0.0+epsilon) return -1.0;
return (tmin); // return smallest positive value
}
}
float sphere_intersect_t2(ray myRay, sphere sphr) {
float a = dot(myRay.direction, myRay.direction);
vec3 s0_r0 = myRay.origin - sphr.position;
float b = 2.0 * dot(myRay.direction, s0_r0);
float c = dot(s0_r0, s0_r0) - (sphr.radius * sphr.radius);
float d = sqrt(b*b-4.0*a*c); // compute the discriminant
if (d < 0.0) { // no solution to the quadratic equation?
return -1.0; // then flag as no intersection
} else {
float t1 = (-b - d)/(2.0*a); // compute both values of t
float t2 = (-b + d)/(2.0*a);
float tmin = min(t1,t2);
float tmax = max(t1,t2);
if (tmax<=0.0+epsilon) return -1.0;
if (tmin<=0.0+epsilon) return -1.0;
return (tmax); // return greater positive value
}
}
/////////////////////////////////////////////////////////////////////////////////
// rayT(): cast a ray, and computes t for closest intersection in the direction of +t
// If there is no intersection, it returns largeT
/////////////////////////////////////////////////////////////////////////////////
float rayT(ray myRay)
{
float nearest_t = largeT;
float curr_t;
for (int i = 0; i < n_spheres; ++i) {
curr_t = sphere_intersect(myRay, scene_spheres[i]); // test against sphere
if (curr_t == -1.0) continue; // missed sphere?
else if (curr_t < nearest_t) { // closest sphere?
nearest_t = curr_t;
nearestSphere = scene_spheres[i];
}
}
return nearest_t;
}
/////////////////////////////////////////////////////////////////////////////////
// localShade(): compute local color for a surface point
/////////////////////////////////////////////////////////////////////////////////
vec3 localShade(vec3 P, vec3 N, vec3 I, vec3 surfColor) {
ray shadowRay;
/// TO DO:
// (1) compute and return a normal N.L diffuse shading color;
// surfColor is the assigned color of the surface.
float ka = 0.2;
float kd = 0.8;
float ks = 0.3;
vec3 finalColor = vec3(0, 0, 0);
vec3 ambientTerm = ambient_light_color * ka;
vec3 color = vec3(0, 0, 0);
vec3 L = normalize(lightPosition - P);
float i = dot(N, L);
if (i > 0.0){
color = surfColor * i;
}
vec3 diffuseTerm = color * kd;
vec3 specularTerm;
L = normalize(P - lightPosition);
vec3 R = normalize(reflect(L, N));
vec3 V = I * -1.0;
float rv = dot(R, V);
if (rv < 0.0){
rv = 0.0;
}
float n = 10.0;
specularTerm = surfColor * ks * pow(rv, n);
finalColor = ambientTerm + diffuseTerm + specularTerm;
// (2) now additionally check to see if the object is in shadow by building and casting
// a shadow ray. If the point is in shadow, return black. Otherwise return the diffuse shading.
shadowRay.origin = P;
shadowRay.direction = normalize(lightPosition - P);
float nearest_t = rayT(shadowRay);
if (nearest_t < largeT){ // hits another object
finalColor = vec3(0, 0, 0);
}
return finalColor;
}
/////////////////////////////////////////////////////////////////////////////////
// bgColor(ray): cast a ray, and compute a color, recursively if needed
/////////////////////////////////////////////////////////////////////////////////
vec3 bgColor(ray myRay)
{
// return backgroundColor;
vec4 origin = planeMatrix*vec4(myRay.origin, 1.0); // transform ray into the coord system of the plane
vec4 direction = planeMatrix*vec4(myRay.direction,0.0); //
float zPlane = -10.0; // in local coords, the plane occupies the xy-plane at z=-10
float t = (zPlane - origin.z)/direction.z; // intersect ray with plane, in local plane coords
if (t<0.0) return backgroundColor; // ray intersects behind the eye, so is looking away from the plane
vec3 P = origin.xyz + t*direction.xyz; // compute intersection point
float xf = floor(fract(0.1*P.x)+0.5); // 0 or 1 computations to compute checkerboard pattern
float yf = floor(fract(0.1*P.y)+0.5); // 0 or 1
float sum = xf + yf;
if (sum==1.0)
return vec3(0.3,0.3,0.3); // black square
else
return vec3(1,1,1); // white square
}
/////////////////////////////////////////////////////////////////////////////////
// rayCast2(): cast the reflected ray, and compute a color for it
/////////////////////////////////////////////////////////////////////////////////
vec3 rayCast2(ray myRay) // return the color for this reflected ray
{
// TODO: this will be a slightly simplified version of rayCast()
// (1) find the nearest intersection
float nearest_t = rayT(myRay);
// (2) if hit an object, then compute and return the local color;
// otherwise return black
if (nearest_t < largeT){
vec3 P = myRay.origin + myRay.direction * nearest_t;
vec3 N = normalize(P - nearestSphere.position);
vec3 I = myRay.direction;
vec3 localColor = localShade(P,N,I, nearestSphere.mtrl.color);
return localColor;
}
return bgColor(myRay); // return checkeboard texture
}
/////////////////////////////////////////////////////////////////////////////////
// rayCast(): cast a ray, and compute a color, recursively if needed
/////////////////////////////////////////////////////////////////////////////////
vec3 rayCast(ray myRay) // return color
{
// TODO:
// (1) find the nearest intersection
// (2) if hit an object, then compute and return the local color;
// otherwise return black
float nearest_t = rayT(myRay); // find closest object
float kSpec = nearestSphere.mtrl.kSpec; // keep these lines for use in step (7)
float kLocal = nearestSphere.mtrl.kLocal; // keep these lines for use in step (7)
if (nearest_t < largeT) { // hit an object?
//return vec3(1,1,1); // color this white for now (but replace this with steps 1--4 below)
//return nearestSphere.mtrl.color;
// TODO:
// (1) compute the actual intersection point, P, given the nearest_t value;
vec3 P = myRay.origin + myRay.direction * nearest_t;
// (2) compute the normal, N; the center of the sphere is given by nearestSphere.position
vec3 N = normalize(P - nearestSphere.position);
// (3) compute the incident direction, I
vec3 I = myRay.direction;
// (4) call the localShade function to compute the local shading
vec3 localColor = localShade(P,N,I, nearestSphere.mtrl.color); // local shading
// return localColor;
// (5) develop the parameters for the reflected ray
ray reflectedRay;
reflectedRay.origin = P;
reflectedRay.direction = normalize(reflect(I, N));
float refractive_index = 1.4;
ray refractedRay;
refractedRay.direction = normalize(refract(I, N, 1.0 / refractive_index));
refractedRay.origin = P - 0.1 * refractedRay.direction;
//second refraction
float t2 = sphere_intersect_t2(refractedRay, nearestSphere);
vec3 Pr = refractedRay.origin + refractedRay.direction * t2;
vec3 Nr = normalize(Pr - nearestSphere.position);
vec3 Ir = refractedRay.direction;
ray secondRefractedRay;
secondRefractedRay.origin = Pr;
secondRefractedRay.direction = normalize(refract(Ir, Nr, refractive_index));
// (6) compute the color of the reflected ray;
// Normally this would be a recursive call to
vec3 reflectedColor = rayCast2(reflectedRay);
vec3 refractedColor = rayCast2(secondRefractedRay);
// (7) return the sum of the local color and the reflected ray, weighted by kLocal and kSpec
return (kLocal*localColor + kSpec *reflectedColor + 0.1 * refractedColor);
}
return bgColor(myRay); // return checkeboard texture
}
void main()
{
// INIT SPHERES
sphere sphere0;
sphere0.mtrl.color = vec3(0.5, 1.0, 0.5);
sphere0.mtrl.kSpec = 0.7;
sphere0.mtrl.kLocal = 0.3;
sphere0.radius = 2.0;
sphere0.position = vec3(0,0,-7.5);
sphere0.position.x = -1.0 + myFloat1;
sphere0.position.y = -1.0 + myFloat2;
sphere sphere1;
sphere1.mtrl.color = vec3(1.0, 1.0, 1.0);
sphere1.mtrl.kSpec = 0.4;
sphere1.mtrl.kLocal = 0.6;
sphere1.radius = 1.0;
sphere1.position = vec3(3,1,-8);
sphere sphere2;
sphere2.mtrl.color = vec3(1.0, 0.0, 0.0);
sphere2.mtrl.kSpec = 0.0;
sphere2.mtrl.kLocal = 1.0;
sphere2.radius = 1.0;
sphere2.position = vec3(0,3,-8);
scene_spheres[0] = sphere0;
scene_spheres[1] = sphere1;
scene_spheres[2] = sphere2;
ray pixelRay;
// compute normalized screen coordinates for pixel: [-1,1] in y; [-a,a] in x, where a=aspect ratio
vec2 uv = (-1.0 + 2.0*gl_FragCoord.xy / resolution.xy) * vec2(resolution.x/resolution.y, 1.0);
pixelRay.origin = vec3(0,0,0); // ray starts at eye: origin
pixelRay.direction = normalize(vec3(0.5*uv,-1.0)); // look in the direction of a given pixel
vec3 rayColor = rayCast(pixelRay); // cast the initial ray
gl_FragColor = vec4(rayColor, 1.0); // assign color to fragment
}
</script>
<script src="a6.js"></script>
</body>
</html>