-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
248 lines (192 loc) · 9.38 KB
/
index.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
<!doctype html>
<html>
<head>
<title>D-Kapstory</title>
<meta charset="utf-8">
<script src="./vendor/three.js/three.min.js"></script>
<script src="./js/dk.js"></script>
<link href="css/main.css" rel="stylesheet" />
</head>
<body>
<div id="container" width="512" height="512"></div>
<script type="text/javascript">
/*global THREE getClass DFK*/
// this factory hold some 3D Objects
var factory = new DFK.Factory();
factory.add("mesh::caps", function()
{
var material = new THREE.MeshLambertMaterial({ color: 0xFC0C20 });
var geometry;
if(!factory.contains("geom::caps"))
{
geometry = new THREE.CylinderGeometry(6, 6.2, 2);
factory.add("geom::caps", geometry);
}
var caps = new THREE.Mesh(factory.create("geom::caps"), material);
caps.castShadow = true;
return caps;
});
factory.add("mesh::bottle", function()
{
var baseradius = 10;
var topradius = 5;
var baseheight = 30;
var topheight = 10;
var ringheight = 2;
var ringradius = 5.5;
var base, top, ring;
var material = new THREE.MeshLambertMaterial({ color: 0xCCFCC0 });
var bottle = new THREE.Object3D();
base = new THREE.Mesh(new THREE.CylinderGeometry(baseradius, baseradius, baseheight, 10, 10), material);
base.position.y = baseheight / 2;
top = new THREE.Mesh(new THREE.CylinderGeometry(topradius, baseradius, topheight, 10, 10), material);
top.position.y = baseheight + topheight / 2;
ring = new THREE.Mesh(new THREE.CylinderGeometry(ringradius, ringradius, ringheight, 10, 10), material);
ring.position.y = baseheight + topheight + ringheight / 2;
base.castShadow = true;
top.castShadow = true;
ring.castShadow = true;
base.receiveShadow = true;
top.receiveShadow = true;
ring.receiveShadow = true;
bottle.add(top);
bottle.add(base);
bottle.add(ring);
return bottle;
});
factory.add("mesh::floor", function(floorRadius){
var geometry = new THREE.CylinderGeometry(floorRadius, floorRadius, 0.1, 20, 8);
var material = new THREE.MeshPhongMaterial({ color: 0x42AF33 });
var mesh = new THREE.Mesh(geometry, material);
mesh.receiveShadow = true;
return mesh;
});
// create a simple gravity particule system with capsCount elements centered on pos
function df_caps_animator(capsCount, pos) {
var d = {};
d.obj = new THREE.Object3D();
d.particules = [];
for (var i = 0; i < capsCount; ++i) {
d.obj.add(factory.create("mesh::caps")());
// create the physics engine for each particule
var part = new DFK.particule(
0.32,
DFK.P,
{
onDie : function (part) {
var min = 5;
if (part.v.y < min) // Full reset
part.reset(DFK.v3(pos.x + DFK.rnd(-1, 1),
pos.y + DFK.rnd(0, 1),
pos.z),
DFK.v3(Math.sin(DFK.rnd(-3.1415, 3.1415)),
4 + DFK.rnd(-1, 1.5),
Math.cos(DFK.rnd(-3.1415, 3.1415))));
else // bounce
{
part.p.y = min;
part.reset(part.p, DFK.v3(2*part.v.x,
0.7*part.v.y,
2*part.v.z));
}
},
shouldDie : function(p, v) { return p.y < 0; }
});
d.particules.push(part);
}
d.animate = function (dt) {
for (var i = 0; i < d.particules.length; ++i) {
// get the new particule position
var new_pos = d.particules[i].next(dt);
var mesh = d.obj.children[i];
//update the mesh position and little hack on the rotation
mesh.position.set(new_pos.x, new_pos.y, new_pos.z);
mesh.rotation.x = new_pos.z / 10;
mesh.rotation.z = new_pos.y / 10;
}
}
return d;
}
// create a light and mak sure the the floor kept in the fustrum
function df_dlight(lightPos, lightTarget, floorRadius) {
var dist = DFK.v3dist(lightPos, lightTarget);
var light = new THREE.DirectionalLight(0x444444);
light.position.set(lightPos.x, lightPos.y, lightPos.z);
light.target.position.set(lightTarget.x, lightTarget.y, lightTarget.z);
light.castShadow = true;
light.shadowDarkness = 0.5;
light.shadowCameraNear = dist - floorRadius;
light.shadowCameraFar = dist + floorRadius;
light.shadowCameraLeft = -floorRadius;
light.shadowCameraRight = floorRadius;
light.shadowCameraTop = floorRadius;
light.shadowCameraBottom = -floorRadius;
return light;
}
var renderer, scene, camera, animator;
init();
animate();
function init() {
var width = 512;
var height = 512;
// on initialise le moteur de rendu
renderer = new THREE.WebGLRenderer();
// si WebGL ne fonctionne pas sur votre navigateur vous pouvez utiliser le moteur de rendu Canvas à la place
//renderer = new THREE.CanvasRenderer();
renderer.setSize(width, height);//window.innerWidth, window.innerHeight);
document.getElementById('container').appendChild(renderer.domElement);
// on initialise la scène
scene = new THREE.Scene();
// setting up the camera
camera = new THREE.PerspectiveCamera(50, width / height, 1, 1000);
camera.position.set(0, 150, 250);
camera.lookAt(new THREE.Vector3(0.0, 50.0, 0.0));
scene.add(camera);
// add a bottle first
scene.add(factory.create("mesh::bottle")());
// then create 500 animated caps on top of the bottle
animator = new df_caps_animator(500, new THREE.Vector3(0, 48, 0));
// and add them to the scene
scene.add(animator.obj);
// Finally add a floor to don't lost the others 3D objects in the space
var floorRadius = 200;
scene.add(factory.create("mesh::floor")(200));
// adding 4 lights around the bottle
var lightPos = DFK.v3(floorRadius + 50, 2*floorRadius, 0.5*floorRadius + 50);
scene.add(df_dlight(lightPos, DFK.v3(0, 30, 0), floorRadius));
lightPos.x *= -1;
scene.add(df_dlight(lightPos, DFK.v3(0, 30, 0), floorRadius));
lightPos.z *= -1;
scene.add(df_dlight(lightPos, DFK.v3(0, 30, 0), floorRadius));
lightPos.x *= -1;
scene.add(df_dlight(lightPos, DFK.v3(0, 30, 0), floorRadius));
// Enabling the shadow
renderer.shadowMapEnabled = true;
renderer.shadowMapSoft = true;
renderer.shadowCameraNear = camera.near;
renderer.shadowCameraFar = camera.far;
renderer.shadowCameraFov = 50;
renderer.shadowMapBias = 0.0039;
renderer.shadowMapDarkness = 0.5;
renderer.shadowMapWidth = 1024;
renderer.shadowMapHeight = 1024;
// on effectue le rendu de la scène
//renderer.render( scene, camera );
}
var gt = 0; // a global timer
function animate() {
requestAnimationFrame(animate);
// updating the caps
animator.animate(0.10510);
gt += 0.01;//0.02510;
camera.position.set((150 + 50*Math.cos(gt/3))*Math.sin(gt), 150 + 100*(1+Math.sin(gt)), (250 + 50*Math.sin(gt/3))*Math.cos(gt));
camera.lookAt(new THREE.Vector3(0.0, 50.0, 0.0));
render();
}
function render() {
renderer.setClearColor(0);
renderer.render(scene, camera);
}
</script>
</body>
</html>