-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathripple.html
436 lines (341 loc) · 13.3 KB
/
ripple.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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Three.js Water Test</title>
<style>canvas {
width: 100%;
height: 100%;
}</style>
</head>
<body>
<!-- Main THREE includes -->
<script src="js/three.js"></script>
<script src="js/DAT.GUI.js"></script>
<script src="js/ORBIT.js"></script>
<!-------------------->
<!-- Shaders -->
<!-------------------->
<!-- Render texture vertex shader. Does not modify anything. -->
<script id="vs_rt" type="x-shader/x-vertex">
varying vec2 vUv;
void main()
{
vUv = uv;
gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
}
</script>
<!-- Render texture fragment shader. Runs a simulation step. -->
<script id="fs_rt" type="x-shader/x-fragment">
uniform sampler2D texture;
uniform vec2 delta, mousePoint;
uniform int mouseActive;
uniform float damping, strength, radius;
varying vec2 vUv;
const float v = 3.14159;
void main()
{
// get current frag value
vec4 info = texture2D(texture, vUv);
// Draw ripples with mouse
if(mouseActive >= 1)
{
float m = max(0.0, 1.0 - length(vec2(mousePoint.x, 1.0 - mousePoint.y) - vUv) / radius);
m = 0.5 -cos(m * v) * 0.5;
info.r -= m * strength;
}
// Main wave propagation
vec2 dx = vec2(delta.x, 0.0);
vec2 dy = vec2(0.0, delta.y);
float average = (
texture2D(texture, vUv - dx).r +
texture2D(texture, vUv - dy).r +
texture2D(texture, vUv + dx).r +
texture2D(texture, vUv + dy).r
) * 0.25;
info.g += (average - info.r) * 2.0;
// attenuate the velocity a little so waves do not last forever
info.g *= damping;
// move the vertex along the velocity
info.r += info.g;
// damp the final value
info.r *= damping;
// set the new vertex height (VS uses color to determine height)
gl_FragColor = info;
}
</script>
<!-- Normal map vertex shader. Sends heightmap UV to fragment shader. -->
<script id="vs_normal" type="x-shader/x-fragment">
varying vec2 vUv;
void main()
{
vUv = uv;
gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
}
</script>
<!-- Normal map fragment shader. Outputs to normalMap. -->
<script id="fs_normal" type="x-shader/x-fragment">
uniform float height;
uniform vec2 delta;
uniform sampler2D texture;
varying vec2 vUv;
void main() {
float val = texture2D( texture, vUv ).r;
float valU = texture2D( texture, vUv + vec2( delta.x, 0.0 ) ).r;
float valV = texture2D( texture, vUv + vec2( 0.0, delta.y ) ).r;
gl_FragColor = vec4( ( 0.5 * normalize( vec3( val - valU, val - valV, height ) ) + 0.5 ), 1.0 );
}
</script>
<!-- Final pass vertex shader. Sets final mesh deformation from heightmap.
Calculates light and eye vectors and sends them to fragment shader. -->
<script id="vs_setHeight" type="x-shader/x-vertex">
uniform sampler2D heightMap;
uniform float scale;
uniform vec3 lightPos;
varying vec3 lightVec;
varying vec3 eyeVec;
varying vec2 texCoord;
void main(void)
{
texCoord = uv;
vec4 info = texture2D(heightMap, texCoord);
vec3 newpos = position;
// Multiply new height so we can actually see the difference.
newpos.z = info.r * scale;
gl_Position = projectionMatrix * modelViewMatrix * vec4( newpos, 1.0 );
vec3 n = normalize(normalMatrix * normal);
vec3 t = normalize(normalMatrix * vec3(1.0, 0.0, 0.0));
vec3 b = cross(n, t);
vec3 vVertex = vec3(modelViewMatrix * vec4(newpos, 1.0)).xyz;
vec3 tmpVec = vec3(modelViewMatrix * vec4(lightPos.x, -lightPos.z, lightPos.y, 1.0)).xyz - vVertex;
lightVec.x = dot(tmpVec, t);
lightVec.y = dot(tmpVec, b);
lightVec.z = dot(tmpVec, n);
tmpVec = -vVertex;
eyeVec.x = dot(tmpVec, t);
eyeVec.y = dot(tmpVec, b);
eyeVec.z = dot(tmpVec, n);
}
</script>
<!-- Final pass fragment shader. Uses normal map and light/eye positions to determine
final shaded color. Adds ambient lighting and specular highlights. -->
<script id="fs_setColor" type="x-shader/x-fragment">
uniform sampler2D colorMap;
uniform sampler2D normalMap;
uniform float invRadius;
uniform vec3 ambient;
uniform vec3 diffuse;
uniform vec3 specular;
uniform float alpha;
uniform float shininess;
varying vec3 lightVec;
varying vec3 eyeVec;
varying vec2 texCoord;
void main (void)
{
float distSqr = dot(lightVec, lightVec);
float att = clamp(1.0 - invRadius * sqrt(distSqr), 0.0, 1.0);
vec3 lVec = lightVec * inversesqrt(distSqr);
vec3 vVec = normalize(eyeVec);
vec4 base = texture2D(colorMap, texCoord);
vec3 bump = normalize( texture2D(normalMap, texCoord).xyz * 2.0 - 1.0);
vec4 vAmbient = vec4(ambient, alpha) * vec4(ambient, alpha);
float diffuse2 = max( dot(lVec, bump), 0.0 );
vec4 vDiffuse = vec4(diffuse, alpha) * vec4(diffuse, alpha) * diffuse2;
float specular2 = pow(clamp(dot(reflect(-lVec, bump), vVec), 0.0, 1.0), shininess );
vec4 vSpecular = vec4(specular, alpha) * vec4(specular, alpha) * specular2;
gl_FragColor = vec4((( vAmbient*base + vDiffuse*base + vSpecular ) * att).xyz, alpha);
}
</script>
<!-------------------->
<!-- Main Logic -->
<!-------------------->
<script>
//------------------------------------------
// Globals
//------------------------------------------
var stats, renderStats, cameraRTT, camera, sceneRTT, scene, renderer;
var rtTexture, rtTexture2, normalMap;
var simRes = 256;
var rtUniforms, mainUniforms, normalUniforms;
var water, waterMat;
var mouseDown;
var gui, guiParams, guiNeedsUpdate, mainFolder, lightFolder, waterColorFolder;
var normalMat, screenMat;
var light, lensFlare;
var animateLight = false;
var controls, clock;
var renderTargetLinearFloatParams = {
minFilter: THREE.LinearFilter,
magFilter: THREE.LinearFilter,
wrapS: THREE.RenderTargetWrapping,
wrapT: THREE.RenderTargetWrapping,
format: THREE.RGBAFormat,
stencilBuffer: false,
depthBuffer: false,
type: THREE.FloatType
};
//------------------------------------------
// Main init and loop
//------------------------------------------
start();
update();
//------------------------------------------
// Initialization
//------------------------------------------
function start() {
scene = new THREE.Scene();
sceneRTT = new THREE.Scene();
clock = new THREE.Clock();
renderer = new THREE.WebGLRenderer({antialias: true});
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setClearColor(0x808080);
renderer.autoClear = false;
//renderer.shadowMapEnabled = true;
document.body.appendChild(renderer.domElement);
setupRTTScene();
setupMainScene();
setupControls();
controls = new THREE.OrbitControls(camera);
}
// Setup materials
setupMats();
//------------------------------------------
// Setup the render target textures and materials
//------------------------------------------
function setupMats() {
// create buffers
rtTexture = new THREE.WebGLRenderTarget(simRes, simRes, renderTargetLinearFloatParams);
rtTexture2 = new THREE.WebGLRenderTarget(simRes, simRes, renderTargetLinearFloatParams);
normalMap = new THREE.WebGLRenderTarget(simRes, simRes, renderTargetLinearFloatParams);
var delta = 1.0 / simRes;
// main render-to-texture material
rtUniforms = {
texture: {type: "t", value: rtTexture2},
delta: {type: "v2", value: new THREE.Vector2(delta, delta)},
mousePoint: {type: "v2", value: new THREE.Vector2(-1, -1)},
mouseActive: {type: "i", value: 0},
damping: {type: "f", value: guiParams.damping},
strength: {type: "f", value: guiParams.strength},
radius: {type: "f", value: guiParams.radius}
};
screenMat = new THREE.ShaderMaterial({
uniforms: rtUniforms,
vertexShader: document.getElementById('vs_rt').textContent,
fragmentShader: document.getElementById('fs_rt').textContent
});
// main water material and mesh
mainUniforms = {
heightMap: {type: "t", value: rtTexture2},
scale: {type: "f", value: guiParams.scale},
alpha: {type: "f", value: guiParams.alpha},
colorMap: {type: "t", value: THREE.ImageUtils.loadTexture('images/eerieYpos.png')},
normalMap: {type: "t", value: normalMap},
ambient: {type: "c", value: new THREE.Color(0x111111)},
diffuse: {type: "c", value: new THREE.Color(0xffffff)},
specular: {type: "c", value: new THREE.Color(0xffffff)},
invRadius: {type: "f", value: 0.0},
shininess: {type: "f", value: 50.0},
lightPos: {type: "v3", value: light.position}
};
waterMat = new THREE.ShaderMaterial({
uniforms: mainUniforms,
vertexShader: document.getElementById('vs_setHeight').textContent,
fragmentShader: document.getElementById('fs_setColor').textContent,
transparent: true,
side: THREE.DoubleSide
//wireframe: true
});
// Setup normal material
normalUniforms = {
texture: {type: "t", value: rtTexture2},
delta: {type: "v2", value: new THREE.Vector2(delta, delta)},
height: {type: "f", value: 0.05}
};
normalMat = new THREE.ShaderMaterial({
uniforms: normalUniforms,
vertexShader: document.getElementById('vs_normal').textContent,
fragmentShader: document.getElementById('fs_normal').textContent
});
}
//------------------------------------------
// Setup the render-to-texture scene
//------------------------------------------
function setupRTTScene() {
cameraRTT = new THREE.OrthographicCamera(simRes / -2, simRes / 2, simRes / 2, simRes / -2, -10000, 10000);
var screenGeo = new THREE.PlaneGeometry(simRes, simRes);
screenQuad = new THREE.Mesh(screenGeo, screenMat);
screenQuad.position.z = -100;
sceneRTT.add(screenQuad);
}
//------------------------------------------
// Setup the main scene
//------------------------------------------
function setupMainScene() {
camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 1, 10000);
camera.position.y = 450;
camera.position.z = 450;
camera.rotation.x = THREE.Math.degToRad(-45);
var waterGeo = new THREE.PlaneGeometry(512, 512, simRes - 1, simRes - 1);
water = new THREE.Mesh(waterGeo, waterMat);
water.rotation.x = THREE.Math.degToRad(-90);
scene.add(water);
// lower 'floor' plane
var floorTexture = THREE.ImageUtils.loadTexture('img/checkerboard.jpg');
floorTexture.wrapS = floorTexture.wrapT = THREE.RepeatWrapping;
floorTexture.repeat.set(5, 5);
var floorPlaneGeo = new THREE.PlaneGeometry(512, 512, 1, 1);
var floorPlane = new THREE.Mesh(floorPlaneGeo, new THREE.MeshLambertMaterial({
color: 0xffffff,
map: floorTexture
}));
floorPlane.position.set(0, -50, 0);
floorPlane.rotation.x = THREE.Math.degToRad(-90);
scene.add(floorPlane);
// debug the render texture by applying it to a basic plane in the upper-left
// area of the scene
var rtDbgGeo = new THREE.PlaneGeometry(150, 150, 1, 1);
var rtDbgMat = new THREE.MeshBasicMaterial({color: 0xffffff, map: normalMap});
var rtDbg = new THREE.Mesh(rtDbgGeo, rtDbgMat);
rtDbg.position.set(-300, 200, 0);
scene.add(rtDbg);
// sphere to test that lighting is working
var sp = new THREE.SphereGeometry(50, 30, 15);
var spm = new THREE.Mesh(sp, new THREE.MeshLambertMaterial({color: 0xff0000}));
spm.position.set(300, 200, 0);
scene.add(spm);
}
//------------------------------------------
// Setup mouse event handlers
//------------------------------------------
function setupControls() {
// Add mouse controls
renderer.domElement.onmousedown = function (event) {
if (event.button === 0) {
mouseDown = true;
}
};
renderer.domElement.onmouseup = function () {
mouseDown = false;
rtUniforms.mouseActive.value = 0;
};
renderer.domElement.onmouseout = function () {
rtUniforms.mouseActive.value = 0;
};
renderer.domElement.onclick = function (event) {
rtUniforms.mouseActive.value = 2;
};
renderer.domElement.onmousemove = function (a) {
rtUniforms.mousePoint.value.set(a.clientX / window.innerWidth, a.clientY / window.innerHeight);
if (1 > Math.abs(rtUniforms.mousePoint.value.x) && 1 > Math.abs(rtUniforms.mousePoint.value.y))
rtUniforms.mouseActive.value = mouseDown ? 2 : 0;
};
}
//------------------------------------------
// Main loop
//------------------------------------------
function update() {
requestAnimationFrame(update);
render();
</body>
</html>