-
Notifications
You must be signed in to change notification settings - Fork 1
/
ball2.html
367 lines (329 loc) · 12.5 KB
/
ball2.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
<!DOCTYPE html>
<html lang="en">
<div id="container">
<div id="nogl">
<p>This experiment requires WebGL to run. Please enable it, or come back and visit with a <a href="http://caniuse.com/webgl" target="_blank">compatible browser</a>.</p>
</div>
</div>
<header class="info">
<hgroup class="about">
<h1>Graphics Project</h1>
<h2>WebGL implementation of a multi-ball system</h2>
</hgroup>
<nav class="more">
<a href="ball.html">Transparent Balls</a> <br>
<a href="ocean.html">The Sea</a> <br>
<a href="merge.html">Isosurface & Metaballs</a>
</nav>
</header>
<body></body>
<link rel="stylesheet" type="text/css" href="style.css">
<script src="three.min.js"></script>
<script src="js/controls/OrbitControls.js"></script>
<script src="js/Mirror.js"></script>
<script src="js/WaterShader.js"></script>
<script src="js/libs/stats.min.js"></script>
<script src="js/Detector.js"></script>
<script>
if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
var cameraRTT;
var container;
var camera, scene, renderer;
var waterNormals;
var cameraCube, sceneCube;
var mesh, lightMesh, geometry;
var spheres = [];
var directionalLight, pointLight;
var mouseX = 0, mouseY = 0;
var windowHalfX = window.innerWidth / 2;
var windowHalfY = window.innerHeight / 2;
var deg = 0;
var parameters = {
width: 2000,
height: 2000,
widthSegments: 250,
heightSegments: 250,
depth: 1500,
param: 4,
filterparam: 1
};
document.addEventListener( 'mousemove', onDocumentMouseMove, false );
// Main
init();
animate();
function init() {
container = document.createElement( 'div' );
document.body.appendChild( container );
//camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 1, 100000 );
//camera.position.z = 3200;
camera = new THREE.PerspectiveCamera( 55, window.innerWidth / window.innerHeight, 0.5, 3000000 );
camera.position.set( 2000, 750, 2000 );
//render to texture RTT stuff
cameraRTT = new THREE.CubeCamera( 1, 30000000, 256 );
cameraRTT.position.y = 750;
//cameraCube = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 1, 30000000000 );
scene = new THREE.Scene();
//sceneCube = new THREE.Scene();
scene.add(cameraRTT);
//renderer
renderer = new THREE.WebGLRenderer();
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.autoClear = false;
container.appendChild( renderer.domElement );
//controls
controls = new THREE.OrbitControls( camera, renderer.domElement );
controls.enablePan = false;
controls.minDistance = 1000.0;
controls.maxDistance = 5000.0;
controls.maxPolarAngle = Math.PI * 0.495;
controls.target.set( 0, 500, 0 );
//scene.add( new THREE.AmbientLight( 0x444444 ) );
var light = new THREE.DirectionalLight( 0xffffbb, 1 );
light.position.set( - 1, 1, - 1 );
scene.add( light );
//lens flares
var textureLoader = new THREE.TextureLoader();
var textureFlare0 = textureLoader.load( "textures/lensflare/lensflare0.png" );
var textureFlare2 = textureLoader.load( "textures/lensflare/lensflare2.png" );
var textureFlare3 = textureLoader.load( "textures/lensflare/lensflare3.png" );
addLight( 0.08, 0.8, 0.5, -3000, 3000, -3000 );
function addLight( h, s, l, x, y, z ) {
var light = new THREE.PointLight( 0xffffff, 1.5, 2000 );
light.color.setHSL( h, s, l );
light.position.set( x, y, z );
scene.add( light );
var flareColor = new THREE.Color( 0xffffff );
flareColor.setHSL( h, s, l + 0.5 );
var lensFlare = new THREE.LensFlare( textureFlare0, 700, 0.0, THREE.AdditiveBlending, flareColor );
lensFlare.add( textureFlare2, 512, 0.0, THREE.AdditiveBlending );
lensFlare.add( textureFlare2, 512, 0.0, THREE.AdditiveBlending );
lensFlare.add( textureFlare2, 512, 0.0, THREE.AdditiveBlending );
lensFlare.add( textureFlare3, 60, 0.6, THREE.AdditiveBlending );
lensFlare.add( textureFlare3, 70, 0.7, THREE.AdditiveBlending );
lensFlare.add( textureFlare3, 120, 0.9, THREE.AdditiveBlending );
lensFlare.add( textureFlare3, 70, 1.0, THREE.AdditiveBlending );
lensFlare.customUpdateCallback = lensFlareUpdateCallback;
lensFlare.position.copy( light.position );
scene.add( lensFlare );
}
function lensFlareUpdateCallback( object ) {
var f, fl = object.lensFlares.length;
var flare;
var vecX = -object.positionScreen.x * 2;
var vecY = -object.positionScreen.y * 2;
for( f = 0; f < fl; f++ ) {
flare = object.lensFlares[ f ];
flare.x = object.positionScreen.x + vecX * flare.distance;
flare.y = object.positionScreen.y + vecY * flare.distance;
flare.rotation = 0;
}
object.lensFlares[ 2 ].y += 0.025;
object.lensFlares[ 3 ].rotation = object.positionScreen.x * 0.5 + THREE.Math.degToRad( 45 );
}
//water stuff
waterNormals = new THREE.TextureLoader().load( 'textures/waternormals.jpg' );
waterNormals.wrapS = waterNormals.wrapT = THREE.RepeatWrapping;
water = new THREE.Water( renderer, camera, scene, {
textureWidth: 512,
textureHeight: 512,
waterNormals: waterNormals,
alpha: 1.0,
sunDirection: light.position.clone().normalize(),
sunColor: 0xffffff,
waterColor: 0x001e0f,
distortionScale: 50.0,
} );
mirrorMesh = new THREE.Mesh(
new THREE.PlaneBufferGeometry( parameters.width * 500, parameters.height * 500 ),
water.material
);
mirrorMesh.add( water );
mirrorMesh.rotation.x = - Math.PI * 0.5;
scene.add( mirrorMesh );
// load skybox
var cubeMap = new THREE.CubeTexture( [] );
cubeMap.format = THREE.RGBFormat;
var loader = new THREE.ImageLoader();
loader.load( 'textures/skyboxsun25degtest.png', function ( image ) {
var getSide = function ( x, y ) {
var size = 1024;
var canvas = document.createElement( 'canvas' );
canvas.width = size;
canvas.height = size;
var context = canvas.getContext( '2d' );
context.drawImage( image, - x * size, - y * size );
return canvas;
};
cubeMap.images[ 0 ] = getSide( 2, 1 ); // px
cubeMap.images[ 1 ] = getSide( 0, 1 ); // nx
cubeMap.images[ 2 ] = getSide( 1, 0 ); // py
cubeMap.images[ 3 ] = getSide( 1, 2 ); // ny
cubeMap.images[ 4 ] = getSide( 1, 1 ); // pz
cubeMap.images[ 5 ] = getSide( 3, 1 ); // nz
cubeMap.needsUpdate = true;
} );
var cubeShader = THREE.ShaderLib[ 'cube' ];
cubeShader.uniforms[ 'tCube' ].value = cubeMap;
var skyBoxMaterial = new THREE.ShaderMaterial( {
fragmentShader: cubeShader.fragmentShader,
vertexShader: cubeShader.vertexShader,
uniforms: cubeShader.uniforms,
depthWrite: false,
side: THREE.BackSide
} );
var skyBox = new THREE.Mesh(
new THREE.BoxGeometry( 1000000, 1000000, 1000000 ),
skyBoxMaterial
);
scene.add( skyBox );
//adding the balls
var num_ball = 3;
var POS = 500, VEL = 500;
var sphere_geometry = new THREE.SphereBufferGeometry( 100, 20, 20 ); //radius, hori, vert
//var sphere_material = new THREE.MeshBasicMaterial( { color: 0xffffff, envMap: cubeMap, refractionRatio: 0.8 } );
//reflection and refraction
var cubeTarget = cameraRTT.renderTarget;
cubeTarget.mapping = THREE.CubeRefractionMapping;
/*
//cubeTarget.mapping = THREE.CubeReflectionMapping;
var sphere_material = new THREE.MeshBasicMaterial( {
color: 0xffffff,
envMap: cubeTarget,
refractionRatio: 0.3
} );
*/
for ( var i = 0; i < num_ball; i ++ ) {
//var cameraRTT = new THREE.CubeCamera( 1, 30000000, 256 );
//var cubeTarget = cameraRTT.renderTarget;
//cubeTarget.mapping = THREE.CubeRefractionMapping;
//cubeTarget.mapping = THREE.CubeReflectionMapping;
//cubeMap.mapping = THREE.CubeRefractionMapping
var sphere_material = new THREE.MeshBasicMaterial( {
color: 0xffffff,
envMap: cubeTarget,
refractionRatio: 0.8
} );
var ball = {};
var sphere_mesh = new THREE.Mesh( sphere_geometry, sphere_material );
ball.obj = sphere_mesh;
//ball.camera = cameraRTT;
ball.x = POS*Math.random() - 9; // set random ball position
ball.y = POS*Math.random() - 9;
ball.z = POS*Math.random() - 9;
ball.dx = Math.random() * VEL + 3; // set random ball velocity, in units per second
ball.dy = Math.random() * VEL + 3;
ball.dz = Math.random() * VEL + 3;
if (Math.random() < 0.5) ball.dx = -ball.dx;
if (Math.random() < 0.5) ball.dy = -ball.dy;
if (Math.random() < 0.5) ball.dz = -ball.dz;
ball.obj.position.set( ball.x, ball.y, ball.z);
//ball.camera.position.set(ball.x, ball.y, ball.z);
scene.add(ball.obj);
//scene.add(cameraRTT);
spheres.push( ball );
}
//start animation
window.addEventListener( 'resize', onWindowResize, false );
startAnimation();
}
function onWindowResize() {
windowHalfX = window.innerWidth / 2,
windowHalfY = window.innerHeight / 2,
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
//cameraCube.aspect = window.innerWidth / window.innerHeight;
//cameraCube.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
}
////////////////
/* Animations */
////////////////
var clock; // Keeps track of elapsed time of animation.
var animating = false;
function animate() {
updateForFrame();
requestAnimationFrame( animate );
render();
}
function doFrame() {
if (animating) {
clock.frameNumber++;
updateForFrame();
render();
requestAnimationFrame(doFrame);
}
}
function startAnimation() {
if (!animating) {
if (!clock) {
clock = new THREE.Clock(false);
clock.frameNumber = 0;
clock.getFrameNumber = function() { return this.frameNumber }
}
clock.start();
animating = true;
requestAnimationFrame(doFrame);
}
}
function updateForFrame() {
//update ball positions and velocity
var dt = clock.getDelta(); // time since last update
if (dt > 0.5) return; //very long never update, animation paused for some reason
for (var i = 0; i < spheres.length; i++) {
var ball = spheres[i];
ball.x += ball.dx * dt;
ball.y += ball.dy * dt;
ball.z += ball.dz * dt;
var right = 1000, left = -1000;
var top = 1000, bottom = 100;
var front = 1000, back = -1000;
if (ball.x > right) {
ball.x -= 2*(ball.x - right);
ball.dx = -Math.abs(ball.dx);
}else if (ball.x < left) {
ball.x += 2*(left - ball.x);
ball.dx = Math.abs(ball.dx);
}
if (ball.y > top) {
ball.y -= 2*(ball.y - top);
ball.dy = -Math.abs(ball.dy);
}else if (ball.y < bottom) {
ball.y += 2*(bottom - ball.y);
ball.dy = Math.abs(ball.dy);
}
if (ball.z > front) {
ball.z -= 2*(ball.z - front);
ball.dz = -Math.abs(ball.dz);
}else if (ball.z < back) {
ball.z += 2*(back - ball.z);
ball.dz = Math.abs(ball.dz);
}
//ball.camera.position.set(ball.x, ball.y, ball.z);
ball.obj.position.set(ball.x, ball.y, ball.z);
//ball.camera.updateCubeMap( renderer, scene );
}
}
function onDocumentMouseMove(event) {
mouseX = ( event.clientX - windowHalfX ) * 10;
mouseY = ( event.clientY - windowHalfY ) * 10;
}
function render() {
var timer = 0.0001 * Date.now();
//camera.position.x += ( mouseX - camera.position.x ) * .05;
//camera.position.y += ( - mouseY - camera.position.y ) * .05;
//camera.lookAt( scene.position );
//deg = (deg + 10) % 360;
//camera.rotateOnAxis((new THREE.Vector3(0, 1, 0)).normalize(), deg);
//cameraCube.rotation.copy( camera.rotation );
//renderer.render( sceneCube, cameraCube );
//renderer.render( scene, camera );
//camera.rotateOnAxis((new THREE.Vector3(0, 1, 0)).normalize(), degInRad(1));
cameraRTT.updateCubeMap( renderer, scene );
water.material.uniforms.time.value += 1.0 / 60.0;
controls.update();
water.render();
renderer.render( scene, camera );
}
</script>
</html>