-
Notifications
You must be signed in to change notification settings - Fork 18
/
fluid-effects.html
376 lines (286 loc) · 10.5 KB
/
fluid-effects.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
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<script src="three147.min.js"></script>
<link rel="stylesheet" href="../etudes.css">
<style>
.lil-gui {
--width: 10em;
--name-width: 100%;
--font-size: 100%;
--widget-height: 2em;
position: fixed;
z-index:2;
top: 4em !important;
}
.lil-gui button {
text-align: left;
padding-left: 1em;
}
</style>
<script src="lil-gui.umd.js"></script>
</head>
<body>
<h1 class="white">Fluid effects<span id="caption"></span><a href="https://boytchev.github.io/etudes/">←</a></h1>
<script>
// if you are here only to learn how the fluid effects are created,
// go to the bottom of the file - there is a function for each effect
// construct and setup the scene
var renderer = new THREE.WebGLRenderer( {antialias:true} );
renderer.shadowMap.enabled = true;
document.body.appendChild( renderer.domElement );
document.body.style.margin = 0;
document.body.style.overflow = 'hidden';
var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera( 60, 1, 10, 1000 );
camera.position.set( 0, 0, 100 );
camera.lookAt( new THREE.Vector3(0, 0, 0) );
var light = new THREE.SpotLight( 'white', 0.4 );
light.castShadow = true;
light.penumbra = 1;
light.angle = 0.8;
light.position.set( -10, -10, 100 );
light.target = scene;
light.shadow.camera.near = 10;
light.shadow.camera.far = 1100;
light.shadow.radius = 3;
var sublight = new THREE.SpotLight( 'white', 0.6 );
sublight.penumbra = 1;
sublight.angle = 0.8;
sublight.position.set( 60, 120, 300 );
sublight.target = scene;
scene.add( light, sublight );
// interactive panel for selecting fluid effects
var fluidEffect = NoEffect; // initial fluid effect
var caption = document.getElementById( 'caption' );
var gui = new lil.GUI({title:'Select effect'});
var options = {
NoEffect: function(){ fluidEffect = NoEffect, caption.innerHTML = ' – No effect'},
DiagonalWaves: function(){ fluidEffect = DiagonalWaves, caption.innerHTML = ' – Diagonal waves'},
CircularWaves: function(){ fluidEffect = CircularWaves, caption.innerHTML = ' – Circular waves'},
SquareWaves: function(){ fluidEffect = SquareWaves, caption.innerHTML = ' – Square waves'},
WindowWipe: function(){ fluidEffect = WindowWipe, caption.innerHTML = ' – Window wipe'},
Wiggle: function(){ fluidEffect = Wiggle, caption.innerHTML = ' – Wiggle'},
PageTurn: function(){ fluidEffect = PageTurn, caption.innerHTML = ' – Page turn'},
GlassBricks: function(){ fluidEffect = GlassBricks, caption.innerHTML = ' – Glass bricks'},
CentralShiver: function(){ fluidEffect = CentralShiver, caption.innerHTML = ' – Central shiver'},
PeripherialShiver: function(){ fluidEffect = PeripherialShiver, caption.innerHTML = ' – Peripheral shiver'},
ClockTimer: function(){ fluidEffect = ClockTimer, caption.innerHTML = ' – Clock timer'},
Drops: function(){ fluidEffect = Drops, caption.innerHTML = ' – Drops'},
RainDrops: function(){ fluidEffect = RainDrops, caption.innerHTML = ' – Rain drops'},
Tachycardia: function(){ fluidEffect = Tachycardia, caption.innerHTML = ' – Tachycardia'},
BlackHolePortal: function(){ fluidEffect = BlackHolePortal, caption.innerHTML = ' – Black hole portal'},
RollingBlinds: function(){ fluidEffect = RollingBlinds, caption.innerHTML = ' – Rolling blinds'},
};
gui.add( options, 'NoEffect' ).name( 'No effect' );
gui.add( options, 'DiagonalWaves' ).name( 'Diagonal waves' );
gui.add( options, 'CircularWaves' ).name( 'Circular waves' );
gui.add( options, 'SquareWaves' ).name( 'Square waves' );
gui.add( options, 'WindowWipe' ).name( 'Window wipe' );
gui.add( options, 'Wiggle' ).name( 'Wiggle' );
gui.add( options, 'PageTurn' ).name( 'Page turn' );
gui.add( options, 'GlassBricks' ).name( 'Glass bricks' );
gui.add( options, 'CentralShiver' ).name( 'Central shiver' );
gui.add( options, 'PeripherialShiver' ).name( 'Peripherial shiver' );
gui.add( options, 'ClockTimer' ).name( 'Clock timer' );
gui.add( options, 'Drops' ).name( 'Drops' );
gui.add( options, 'RainDrops' ).name( 'Rain drops' );
gui.add( options, 'Tachycardia' ).name( 'Tachycardia' );
gui.add( options, 'BlackHolePortal' ).name( 'Black hole portal' );
gui.add( options, 'RollingBlinds' ).name( 'Rolling blinds' );
gui.open();
// process window resizes
window.addEventListener( 'resize', onWindowResize, false );
onWindowResize();
function onWindowResize( event )
{
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight, true );
}
// background wall of squares
var wallMap = new THREE.TextureLoader().load( "fluid-effects/wall.jpg" );
wallMap.wrapS = THREE.RepeatWrapping;
wallMap.wrapT = THREE.RepeatWrapping;
wallMap.repeat.set( 10, 6 );
var wall = new THREE.Mesh(
new THREE.PlaneGeometry( 1000, 600 ),
new THREE.MeshStandardMaterial( {color: 'CadetBlue', map: wallMap} )
);
wall.position.z = -600;
wall.receiveShadow = true;
scene.add( wall );
// main object in the scene
var ball = new THREE.Mesh(
new THREE.IcosahedronGeometry( 70, 1 ),
new THREE.MeshStandardMaterial( {
color: 'Gray',
metalness: 1,
roughness: 0.5,
flatShading: true,
} )
);
ball.castShadow = true;
ball.position.z = -200;
scene.add( ball );
var ballDecor = new THREE.Mesh(
new THREE.IcosahedronGeometry( 68, 2 ),
new THREE.MeshStandardMaterial( {
color: 'Orange',
metalness: 0,
roughness: 0.2,
flatShading: true,
emissive: 'Orange',
emissiveIntensity: 0.2,
} )
);
ball.add( ballDecor );
// a flock of small satellites flying around the main ball
var satGeo = new THREE.DodecahedronGeometry( 5.2, 1 ),
satMat = new THREE.MeshLambertMaterial( {color: 'white'} ),
satDecorGeo = new THREE.DodecahedronGeometry( 5.45 ),
satDecorMat = new THREE.MeshLambertMaterial( {color: 'black'} );
for( var i=0; i<40; i++ )
{
var sat = new THREE.Mesh( satGeo, satMat );
sat.add( new THREE.Mesh( satDecorGeo, satDecorMat ) );
sat.castShadow = true;
ballDecor.add( sat );
}
// create a refractive glass surface - any wrinkles
// in this surface cause the various fluids effects
var M = 90, // number of horizontal vertices
N = 60; // number of vertical vertices
var glass = new THREE.Mesh(
new THREE.PlaneGeometry( 300, 200, M, N ),
new THREE.MeshPhysicalMaterial( {
transmission: 1,
roughness: 0,
thickness: 30,
specularIntensity: 0,
} )
);
scene.add( glass );
// helper variables for glass geometry and vertices
var geo = glass.geometry,
pos = geo.getAttribute( 'position' );
// animation loop function
function animate( t )
{
// rotate the big ball
ball.rotation.set( t/1700, t/1800, t/1900 );
// orbit the small satellite balls
for( var i in ballDecor.children )
{
// set values for spherical coordinates that look chaotic
var time = t + 50*i,
dist = 90 + 10*Math.sin(i*i-i+time/1000),
phi = i**3 + time/1200,
theta = i*i - time/1300;
ballDecor.children[i].position.setFromSphericalCoords( dist, phi, theta );
}
// update the glass surface with the current fluid effect function
for( var i=0; i<pos.count; i++ )
{
var x = pos.getX( i ),
y = pos.getY( i ),
z = fluidEffect( x, y, t );
pos.setZ( i, z );
}
pos.needsUpdate = true;
geo.computeVertexNormals( );
// we are done, render
renderer.render( scene, camera );
}
renderer.setAnimationLoop( animate );
// functions for fluid effects - z(x,y,t)
// each function uses coordinates (X,Y) and time T
// calculates and returns the Z coordinate
function NoEffect( x, y, t )
{
return 0;
}
function DiagonalWaves( x, y, t )
{
return 2*Math.sin( 0.1*x + 0.1*y - t/150 );
}
function CircularWaves( x, y, t )
{
var d = Math.sqrt( x**2 + y**2 );
return 2*Math.sin( d/10+t/100);
}
function SquareWaves( x, y, t )
{
return Math.sin( 0.3*Math.max( Math.abs(x+y/10), Math.abs(y+x/10) ) + t/100 );
}
function WindowWipe( x, y, t )
{
return 0.2*x*Math.sin( t/100 ) + 0.2*y*Math.cos( t/100 );
}
function Wiggle( x, y, t )
{
return 5*(
Math.exp(Math.sin(x/26-t/430)) * Math.exp(Math.cos(y/23-t/590))
+
Math.exp(Math.cos(x/25+t/480)) * Math.exp(Math.sin(y/21+t/510))
+
Math.exp(Math.cos(x/22+t/550)) * Math.exp(Math.cos(y/27+t/410))
);
}
function PageTurn( x, y, t )
{
return (
2*Math.exp(x/4-y/4) * Math.sin(t/1000)**100
+
2*Math.exp(-x/4+y/4) * Math.cos(t/1000)**100
);
}
function GlassBricks( x, y, t )
{
var k = 40 + 20*Math.sin(t/1000);
return ((Math.round(x/k) + Math.round(y/k)) % 2) ? 5 : -5;
}
function CentralShiver( x, y, t )
{
var d = (x**2 + y**2) / 5000;
return Math.min( 0.1*Math.random()/(0.0001+d), 5 );
}
function PeripherialShiver( x, y, t )
{
var d = (x**2 + 4*y**2) / 5000;
return d*(Math.cos( x+t/10 )**2 + Math.cos( y+t/10 )**2);
}
function ClockTimer( x, y, t )
{
var a = (Math.atan2(y,x)/Math.PI + 1) / 2;
return 2 * ((a+t/1000) % 1);
}
function Drops( x, y, t )
{
var d = Math.sqrt( x**2 + y**2 );
t = THREE.MathUtils.clamp( (-(t%1500)/20+d/2)%30-10, -Math.PI, Math.PI );
return 2*Math.cos( t );
}
function RainDrops( x, y, t )
{
var d = Math.sqrt( x**2 + y**2 );
return 0.01 * Math.tan( d+2*t/10000 );
}
function Tachycardia( x, y, t )
{
var d = Math.sqrt( x**2 + y**2 );
return d*Math.sin( t/50 )*Math.cos( t/150 );
}
function BlackHolePortal( x, y, t )
{
var d = Math.sqrt( x**2 + y**2 );
return 500/(0.1+d)+ 20*(1.5+0.5*Math.sin( d/100-(t/1000)));
}
function RollingBlinds( x, y, t )
{
return 1/(Math.sin(t/1000)+Math.abs(x/100)) + 1/(-Math.sin(t/1000)+Math.abs(y/100));
}
</script>
</body>
</html>