-
Notifications
You must be signed in to change notification settings - Fork 236
/
renderer.js
353 lines (347 loc) · 12.8 KB
/
renderer.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
/*
* WebGL Water
* http://madebyevan.com/webgl-water/
*
* Copyright 2011 Evan Wallace
* Released under the MIT license
*/
var helperFunctions = '\
const float IOR_AIR = 1.0;\
const float IOR_WATER = 1.333;\
const vec3 abovewaterColor = vec3(0.25, 1.0, 1.25);\
const vec3 underwaterColor = vec3(0.4, 0.9, 1.0);\
const float poolHeight = 1.0;\
uniform vec3 light;\
uniform vec3 sphereCenter;\
uniform float sphereRadius;\
uniform sampler2D tiles;\
uniform sampler2D causticTex;\
uniform sampler2D water;\
\
vec2 intersectCube(vec3 origin, vec3 ray, vec3 cubeMin, vec3 cubeMax) {\
vec3 tMin = (cubeMin - origin) / ray;\
vec3 tMax = (cubeMax - origin) / ray;\
vec3 t1 = min(tMin, tMax);\
vec3 t2 = max(tMin, tMax);\
float tNear = max(max(t1.x, t1.y), t1.z);\
float tFar = min(min(t2.x, t2.y), t2.z);\
return vec2(tNear, tFar);\
}\
\
float intersectSphere(vec3 origin, vec3 ray, vec3 sphereCenter, float sphereRadius) {\
vec3 toSphere = origin - sphereCenter;\
float a = dot(ray, ray);\
float b = 2.0 * dot(toSphere, ray);\
float c = dot(toSphere, toSphere) - sphereRadius * sphereRadius;\
float discriminant = b*b - 4.0*a*c;\
if (discriminant > 0.0) {\
float t = (-b - sqrt(discriminant)) / (2.0 * a);\
if (t > 0.0) return t;\
}\
return 1.0e6;\
}\
\
vec3 getSphereColor(vec3 point) {\
vec3 color = vec3(0.5);\
\
/* ambient occlusion with walls */\
color *= 1.0 - 0.9 / pow((1.0 + sphereRadius - abs(point.x)) / sphereRadius, 3.0);\
color *= 1.0 - 0.9 / pow((1.0 + sphereRadius - abs(point.z)) / sphereRadius, 3.0);\
color *= 1.0 - 0.9 / pow((point.y + 1.0 + sphereRadius) / sphereRadius, 3.0);\
\
/* caustics */\
vec3 sphereNormal = (point - sphereCenter) / sphereRadius;\
vec3 refractedLight = refract(-light, vec3(0.0, 1.0, 0.0), IOR_AIR / IOR_WATER);\
float diffuse = max(0.0, dot(-refractedLight, sphereNormal)) * 0.5;\
vec4 info = texture2D(water, point.xz * 0.5 + 0.5);\
if (point.y < info.r) {\
vec4 caustic = texture2D(causticTex, 0.75 * (point.xz - point.y * refractedLight.xz / refractedLight.y) * 0.5 + 0.5);\
diffuse *= caustic.r * 4.0;\
}\
color += diffuse;\
\
return color;\
}\
\
vec3 getWallColor(vec3 point) {\
float scale = 0.5;\
\
vec3 wallColor;\
vec3 normal;\
if (abs(point.x) > 0.999) {\
wallColor = texture2D(tiles, point.yz * 0.5 + vec2(1.0, 0.5)).rgb;\
normal = vec3(-point.x, 0.0, 0.0);\
} else if (abs(point.z) > 0.999) {\
wallColor = texture2D(tiles, point.yx * 0.5 + vec2(1.0, 0.5)).rgb;\
normal = vec3(0.0, 0.0, -point.z);\
} else {\
wallColor = texture2D(tiles, point.xz * 0.5 + 0.5).rgb;\
normal = vec3(0.0, 1.0, 0.0);\
}\
\
scale /= length(point); /* pool ambient occlusion */\
scale *= 1.0 - 0.9 / pow(length(point - sphereCenter) / sphereRadius, 4.0); /* sphere ambient occlusion */\
\
/* caustics */\
vec3 refractedLight = -refract(-light, vec3(0.0, 1.0, 0.0), IOR_AIR / IOR_WATER);\
float diffuse = max(0.0, dot(refractedLight, normal));\
vec4 info = texture2D(water, point.xz * 0.5 + 0.5);\
if (point.y < info.r) {\
vec4 caustic = texture2D(causticTex, 0.75 * (point.xz - point.y * refractedLight.xz / refractedLight.y) * 0.5 + 0.5);\
scale += diffuse * caustic.r * 2.0 * caustic.g;\
} else {\
/* shadow for the rim of the pool */\
vec2 t = intersectCube(point, refractedLight, vec3(-1.0, -poolHeight, -1.0), vec3(1.0, 2.0, 1.0));\
diffuse *= 1.0 / (1.0 + exp(-200.0 / (1.0 + 10.0 * (t.y - t.x)) * (point.y + refractedLight.y * t.y - 2.0 / 12.0)));\
\
scale += diffuse * 0.5;\
}\
\
return wallColor * scale;\
}\
';
function Renderer() {
this.tileTexture = GL.Texture.fromImage(document.getElementById('tiles'), {
minFilter: gl.LINEAR_MIPMAP_LINEAR,
wrap: gl.REPEAT,
format: gl.RGB
});
this.lightDir = new GL.Vector(2.0, 2.0, -1.0).unit();
this.causticTex = new GL.Texture(1024, 1024);
this.waterMesh = GL.Mesh.plane({ detail: 200 });
this.waterShaders = [];
for (var i = 0; i < 2; i++) {
this.waterShaders[i] = new GL.Shader('\
uniform sampler2D water;\
varying vec3 position;\
void main() {\
vec4 info = texture2D(water, gl_Vertex.xy * 0.5 + 0.5);\
position = gl_Vertex.xzy;\
position.y += info.r;\
gl_Position = gl_ModelViewProjectionMatrix * vec4(position, 1.0);\
}\
', helperFunctions + '\
uniform vec3 eye;\
varying vec3 position;\
uniform samplerCube sky;\
\
vec3 getSurfaceRayColor(vec3 origin, vec3 ray, vec3 waterColor) {\
vec3 color;\
float q = intersectSphere(origin, ray, sphereCenter, sphereRadius);\
if (q < 1.0e6) {\
color = getSphereColor(origin + ray * q);\
} else if (ray.y < 0.0) {\
vec2 t = intersectCube(origin, ray, vec3(-1.0, -poolHeight, -1.0), vec3(1.0, 2.0, 1.0));\
color = getWallColor(origin + ray * t.y);\
} else {\
vec2 t = intersectCube(origin, ray, vec3(-1.0, -poolHeight, -1.0), vec3(1.0, 2.0, 1.0));\
vec3 hit = origin + ray * t.y;\
if (hit.y < 2.0 / 12.0) {\
color = getWallColor(hit);\
} else {\
color = textureCube(sky, ray).rgb;\
color += vec3(pow(max(0.0, dot(light, ray)), 5000.0)) * vec3(10.0, 8.0, 6.0);\
}\
}\
if (ray.y < 0.0) color *= waterColor;\
return color;\
}\
\
void main() {\
vec2 coord = position.xz * 0.5 + 0.5;\
vec4 info = texture2D(water, coord);\
\
/* make water look more "peaked" */\
for (int i = 0; i < 5; i++) {\
coord += info.ba * 0.005;\
info = texture2D(water, coord);\
}\
\
vec3 normal = vec3(info.b, sqrt(1.0 - dot(info.ba, info.ba)), info.a);\
vec3 incomingRay = normalize(position - eye);\
\
' + (i ? /* underwater */ '\
normal = -normal;\
vec3 reflectedRay = reflect(incomingRay, normal);\
vec3 refractedRay = refract(incomingRay, normal, IOR_WATER / IOR_AIR);\
float fresnel = mix(0.5, 1.0, pow(1.0 - dot(normal, -incomingRay), 3.0));\
\
vec3 reflectedColor = getSurfaceRayColor(position, reflectedRay, underwaterColor);\
vec3 refractedColor = getSurfaceRayColor(position, refractedRay, vec3(1.0)) * vec3(0.8, 1.0, 1.1);\
\
gl_FragColor = vec4(mix(reflectedColor, refractedColor, (1.0 - fresnel) * length(refractedRay)), 1.0);\
' : /* above water */ '\
vec3 reflectedRay = reflect(incomingRay, normal);\
vec3 refractedRay = refract(incomingRay, normal, IOR_AIR / IOR_WATER);\
float fresnel = mix(0.25, 1.0, pow(1.0 - dot(normal, -incomingRay), 3.0));\
\
vec3 reflectedColor = getSurfaceRayColor(position, reflectedRay, abovewaterColor);\
vec3 refractedColor = getSurfaceRayColor(position, refractedRay, abovewaterColor);\
\
gl_FragColor = vec4(mix(refractedColor, reflectedColor, fresnel), 1.0);\
') + '\
}\
');
}
this.sphereMesh = GL.Mesh.sphere({ detail: 10 });
this.sphereShader = new GL.Shader(helperFunctions + '\
varying vec3 position;\
void main() {\
position = sphereCenter + gl_Vertex.xyz * sphereRadius;\
gl_Position = gl_ModelViewProjectionMatrix * vec4(position, 1.0);\
}\
', helperFunctions + '\
varying vec3 position;\
void main() {\
gl_FragColor = vec4(getSphereColor(position), 1.0);\
vec4 info = texture2D(water, position.xz * 0.5 + 0.5);\
if (position.y < info.r) {\
gl_FragColor.rgb *= underwaterColor * 1.2;\
}\
}\
');
this.cubeMesh = GL.Mesh.cube();
this.cubeMesh.triangles.splice(4, 2);
this.cubeMesh.compile();
this.cubeShader = new GL.Shader(helperFunctions + '\
varying vec3 position;\
void main() {\
position = gl_Vertex.xyz;\
position.y = ((1.0 - position.y) * (7.0 / 12.0) - 1.0) * poolHeight;\
gl_Position = gl_ModelViewProjectionMatrix * vec4(position, 1.0);\
}\
', helperFunctions + '\
varying vec3 position;\
void main() {\
gl_FragColor = vec4(getWallColor(position), 1.0);\
vec4 info = texture2D(water, position.xz * 0.5 + 0.5);\
if (position.y < info.r) {\
gl_FragColor.rgb *= underwaterColor * 1.2;\
}\
}\
');
this.sphereCenter = new GL.Vector();
this.sphereRadius = 0;
var hasDerivatives = !!gl.getExtension('OES_standard_derivatives');
this.causticsShader = new GL.Shader(helperFunctions + '\
varying vec3 oldPos;\
varying vec3 newPos;\
varying vec3 ray;\
\
/* project the ray onto the plane */\
vec3 project(vec3 origin, vec3 ray, vec3 refractedLight) {\
vec2 tcube = intersectCube(origin, ray, vec3(-1.0, -poolHeight, -1.0), vec3(1.0, 2.0, 1.0));\
origin += ray * tcube.y;\
float tplane = (-origin.y - 1.0) / refractedLight.y;\
return origin + refractedLight * tplane;\
}\
\
void main() {\
vec4 info = texture2D(water, gl_Vertex.xy * 0.5 + 0.5);\
info.ba *= 0.5;\
vec3 normal = vec3(info.b, sqrt(1.0 - dot(info.ba, info.ba)), info.a);\
\
/* project the vertices along the refracted vertex ray */\
vec3 refractedLight = refract(-light, vec3(0.0, 1.0, 0.0), IOR_AIR / IOR_WATER);\
ray = refract(-light, normal, IOR_AIR / IOR_WATER);\
oldPos = project(gl_Vertex.xzy, refractedLight, refractedLight);\
newPos = project(gl_Vertex.xzy + vec3(0.0, info.r, 0.0), ray, refractedLight);\
\
gl_Position = vec4(0.75 * (newPos.xz + refractedLight.xz / refractedLight.y), 0.0, 1.0);\
}\
', (hasDerivatives ? '#extension GL_OES_standard_derivatives : enable\n' : '') + '\
' + helperFunctions + '\
varying vec3 oldPos;\
varying vec3 newPos;\
varying vec3 ray;\
\
void main() {\
' + (hasDerivatives ? '\
/* if the triangle gets smaller, it gets brighter, and vice versa */\
float oldArea = length(dFdx(oldPos)) * length(dFdy(oldPos));\
float newArea = length(dFdx(newPos)) * length(dFdy(newPos));\
gl_FragColor = vec4(oldArea / newArea * 0.2, 1.0, 0.0, 0.0);\
' : '\
gl_FragColor = vec4(0.2, 0.2, 0.0, 0.0);\
' ) + '\
\
vec3 refractedLight = refract(-light, vec3(0.0, 1.0, 0.0), IOR_AIR / IOR_WATER);\
\
/* compute a blob shadow and make sure we only draw a shadow if the player is blocking the light */\
vec3 dir = (sphereCenter - newPos) / sphereRadius;\
vec3 area = cross(dir, refractedLight);\
float shadow = dot(area, area);\
float dist = dot(dir, -refractedLight);\
shadow = 1.0 + (shadow - 1.0) / (0.05 + dist * 0.025);\
shadow = clamp(1.0 / (1.0 + exp(-shadow)), 0.0, 1.0);\
shadow = mix(1.0, shadow, clamp(dist * 2.0, 0.0, 1.0));\
gl_FragColor.g = shadow;\
\
/* shadow for the rim of the pool */\
vec2 t = intersectCube(newPos, -refractedLight, vec3(-1.0, -poolHeight, -1.0), vec3(1.0, 2.0, 1.0));\
gl_FragColor.r *= 1.0 / (1.0 + exp(-200.0 / (1.0 + 10.0 * (t.y - t.x)) * (newPos.y - refractedLight.y * t.y - 2.0 / 12.0)));\
}\
');
}
Renderer.prototype.updateCaustics = function(water) {
if (!this.causticsShader) return;
var this_ = this;
this.causticTex.drawTo(function() {
gl.clear(gl.COLOR_BUFFER_BIT);
water.textureA.bind(0);
this_.causticsShader.uniforms({
light: this_.lightDir,
water: 0,
sphereCenter: this_.sphereCenter,
sphereRadius: this_.sphereRadius
}).draw(this_.waterMesh);
});
};
Renderer.prototype.renderWater = function(water, sky) {
var tracer = new GL.Raytracer();
water.textureA.bind(0);
this.tileTexture.bind(1);
sky.bind(2);
this.causticTex.bind(3);
gl.enable(gl.CULL_FACE);
for (var i = 0; i < 2; i++) {
gl.cullFace(i ? gl.BACK : gl.FRONT);
this.waterShaders[i].uniforms({
light: this.lightDir,
water: 0,
tiles: 1,
sky: 2,
causticTex: 3,
eye: tracer.eye,
sphereCenter: this.sphereCenter,
sphereRadius: this.sphereRadius
}).draw(this.waterMesh);
}
gl.disable(gl.CULL_FACE);
};
Renderer.prototype.renderSphere = function() {
water.textureA.bind(0);
this.causticTex.bind(1);
this.sphereShader.uniforms({
light: this.lightDir,
water: 0,
causticTex: 1,
sphereCenter: this.sphereCenter,
sphereRadius: this.sphereRadius
}).draw(this.sphereMesh);
};
Renderer.prototype.renderCube = function() {
gl.enable(gl.CULL_FACE);
water.textureA.bind(0);
this.tileTexture.bind(1);
this.causticTex.bind(2);
this.cubeShader.uniforms({
light: this.lightDir,
water: 0,
tiles: 1,
causticTex: 2,
sphereCenter: this.sphereCenter,
sphereRadius: this.sphereRadius
}).draw(this.cubeMesh);
gl.disable(gl.CULL_FACE);
};