-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathjelly-balls.html
144 lines (106 loc) · 3.42 KB
/
jelly-balls.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
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<script src="three.min.js"></script>
<link rel="stylesheet" href="../etudes.css">
</head>
<body>
<h1>Jelly balls <a href="https://boytchev.github.io/etudes/">←</a></h1>
<script>
// construct and setup the scene
var renderer = new THREE.WebGLRenderer( {antialias:true} );
renderer.setAnimationLoop( animate );
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
document.body.appendChild( renderer.domElement );
document.body.style.margin = 0;
document.body.style.overflow = 'hidden';
var scene = new THREE.Scene();
scene.background = new THREE.Color( 'skyblue' );
var camera = new THREE.PerspectiveCamera( 40, 1, 0.1, 1000 );
camera.position.set( 0, 60, 300 );
camera.lookAt( new THREE.Vector3( 0, -10, 0 ) );
var light = new THREE.PointLight( 'white', 0.5 );
light.position.set( 0, 0, 0 );
light.castShadow = false;
scene.add( light );
var light = new THREE.PointLight();
light.position.set( 0, 100, 0 );
light.castShadow = true;
scene.add( light );
// create the balls
var n = 3, // number of balls
r = 15, // size of balls
balls = [];
colors = [ 0x907000, 0x2040A0, 0xA02040 ],
geometry = new THREE.SphereBufferGeometry( r, 32, 32 );
for (var i=0; i<n; i++)
{
balls[i] = new THREE.Mesh(
geometry,
new THREE.MeshPhongMaterial({
shininess: 100,
color: colors[i],
emissive: colors[i],
})
);
balls[i].castShadow = true;
balls[i].position.set( -50+50*i, 0, 0 );
balls[i].v = THREE.Math.randFloat( 1.5, 2.5 ); // velocity
balls[i].o = THREE.Math.randFloat( 0, 2*Math.PI ); // offset
}
scene.add( ...balls );
// create the box
var ground = new THREE.Mesh(
new THREE.BoxGeometry( 160.2, 2.2, 100.2 ),
new THREE.MeshPhongMaterial({ color:'slateblue', shininess: 2, specular: 0xffffff})
);
ground.position.y = -51;
ground.receiveShadow = true;
scene.add( ground );
var glass = new THREE.Mesh(
new THREE.BoxGeometry( 160, 100, 100 ),
new THREE.MeshPhongMaterial({color:'lightcyan', opacity:0.3,transparent:true, shininess: 0, specular: 0xffffff, side:THREE.BackSide})
);
glass.receiveShadow = true;
scene.add( glass );
var frame = new THREE.BoxHelper( glass );
frame.material.color.set( 'white' );
scene.add( frame );
// maintain full screen
window.addEventListener( 'resize', onWindowResize, false );
onWindowResize();
function onWindowResize( event )
{
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight, true );
}
var clock = new THREE.Clock();
var t, y, k, topY;
function animate()
{
t = clock.getElapsedTime();
scene.rotation.y = t/2;
for (var i=0; i<n; i++)
{
y = -55+90*Math.abs( Math.sin( balls[i].v*t+balls[i].o ) );
if ( y-r < -50 )
{
topY = y+r;
y = (topY+(-50))/2;
k = (topY-y)/r;
}
else
{
k = 1;
}
balls[i].scale.set( 2-k, k, 2-k );
balls[i].position.y = y;
}
renderer.render( scene, camera );
}
</script>
</body>
</html>