-
Notifications
You must be signed in to change notification settings - Fork 453
/
compound.html
267 lines (221 loc) · 6.5 KB
/
compound.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
<!DOCTYPE html>
<html>
<head>
<title>Compound Shapes - Physijs</title>
<link rel="stylesheet" type="text/css" href="css/styles.css" />
<script type="text/javascript" src="js/three.min.js"></script>
<script type="text/javascript" src="js/stats.js"></script>
<script type="text/javascript" src="../physi.js"></script>
<script type="text/javascript">
'use strict';
Physijs.scripts.worker = '../physijs_worker.js';
Physijs.scripts.ammo = 'examples/js/ammo.js';
var initScene, render, renderer, render_stats, physics_stats, scene, ground, light, camera, spawnChair,
ground_material, chair_material, loader;
initScene = function() {
renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.shadowMap.enabled = true;
renderer.shadowMapSoft = true;
document.getElementById( 'viewport' ).appendChild( renderer.domElement );
render_stats = new Stats();
render_stats.domElement.style.position = 'absolute';
render_stats.domElement.style.top = '0px';
render_stats.domElement.style.zIndex = 100;
document.getElementById( 'viewport' ).appendChild( render_stats.domElement );
physics_stats = new Stats();
physics_stats.domElement.style.position = 'absolute';
physics_stats.domElement.style.top = '50px';
physics_stats.domElement.style.zIndex = 100;
document.getElementById( 'viewport' ).appendChild( physics_stats.domElement );
scene = new Physijs.Scene;
scene.setGravity(new THREE.Vector3( 0, -50, 0 ));
scene.addEventListener(
'update',
function() {
scene.simulate( undefined, 2 );
physics_stats.update();
}
);
camera = new THREE.PerspectiveCamera(
35,
window.innerWidth / window.innerHeight,
1,
1000
);
camera.position.set( 60, 50, 60 );
camera.lookAt( scene.position );
scene.add( camera );
// Light
light = new THREE.DirectionalLight( 0xFFFFFF );
light.position.set( 20, 40, -15 );
light.target.position.copy( scene.position );
light.castShadow = true;
light.shadowCameraLeft = -60;
light.shadowCameraTop = -60;
light.shadowCameraRight = 60;
light.shadowCameraBottom = 60;
light.shadowCameraNear = 20;
light.shadowCameraFar = 200;
light.shadowBias = -.0001
light.shadowMapWidth = light.shadowMapHeight = 2048;
light.shadowDarkness = .7;
scene.add( light );
// Loader
loader = new THREE.TextureLoader();
// Materials
ground_material = Physijs.createMaterial(
new THREE.MeshLambertMaterial({ map: loader.load( 'images/rocks.jpg' ) }),
.8, // high friction
.4 // low restitution
);
ground_material.map.wrapS = ground_material.map.wrapT = THREE.RepeatWrapping;
ground_material.map.repeat.set( 3, 3 );
chair_material = Physijs.createMaterial(
new THREE.MeshLambertMaterial({ map: loader.load( 'images/wood.jpg' ) }),
.6, // medium friction
.2 // low restitution
);
chair_material.map.wrapS = chair_material.map.wrapT = THREE.RepeatWrapping;
chair_material.map.repeat.set( .25, .25 );
// Ground
ground = new Physijs.BoxMesh(
new THREE.BoxGeometry(100, 1, 100),
ground_material,
0 // mass
);;
ground.receiveShadow = true;
scene.add( ground );
spawnChair();
requestAnimationFrame( render );
scene.simulate();
};
spawnChair = (function() {
var buildBack, buildLegs, doSpawn;
buildBack = function() {
var back, _object;
back = new Physijs.BoxMesh(
new THREE.BoxGeometry( 5, 1, .5 ),
chair_material
);
back.position.y = 5;
back.position.z = -2.5;
back.castShadow = true;
back.receiveShadow = true;
// rungs - relative to back
_object = new Physijs.BoxMesh(
new THREE.BoxGeometry( 1, 5, .5 ),
chair_material
);
_object.position.y = -3;
_object.position.x = -2;
_object.castShadow = true;
_object.receiveShadow = true;
back.add( _object );
_object = new Physijs.BoxMesh(
new THREE.BoxGeometry( 1, 5, .5 ),
chair_material
);
_object.position.y = -3;
_object.castShadow = true;
_object.receiveShadow = true;
back.add( _object );
_object = new Physijs.BoxMesh(
new THREE.BoxGeometry( 1, 5, .5 ),
chair_material
);
_object.position.y = -3;
_object.position.x = 2;
_object.castShadow = true;
_object.receiveShadow = true;
back.add( _object );
return back;
};
buildLegs = function() {
var leg, _leg;
// back left
leg = new Physijs.BoxMesh(
new THREE.BoxGeometry( .5, 4, .5 ),
chair_material
);
leg.position.x = 2.25;
leg.position.z = -2.25;
leg.position.y = -2.5;
leg.castShadow = true;
leg.receiveShadow = true;
// back right - relative to back left leg
_leg = new Physijs.BoxMesh(
new THREE.BoxGeometry( .5, 4, .5 ),
chair_material
);
_leg.position.x = -4.5;
_leg.castShadow = true;
_leg.receiveShadow = true;
leg.add( _leg );
// front left - relative to back left leg
_leg = new Physijs.BoxMesh(
new THREE.BoxGeometry( .5, 4, .5 ),
chair_material
);
_leg.position.z = 4.5;
_leg.castShadow = true;
_leg.receiveShadow = true;
leg.add( _leg );
// front right - relative to back left leg
_leg = new Physijs.BoxMesh(
new THREE.BoxGeometry( .5, 4, .5 ),
chair_material
);
_leg.position.x = -4.5;
_leg.position.z = 4.5;
_leg.castShadow = true;
_leg.receiveShadow = true;
leg.add( _leg );
return leg;
};
doSpawn = function() {
var chair, back, legs;
// seat of the chair
chair = new Physijs.BoxMesh(
new THREE.BoxGeometry( 5, 1, 5 ),
chair_material
);
chair.castShadow = true;
chair.receiveShadow = true;
// back - relative to chair ( seat )
back = buildBack();
chair.add( back );
// legs - relative to chair ( seat )
legs = buildLegs();
chair.add( legs );
chair.position.y = 20;
chair.position.x = Math.random() * 50 - 25;
chair.position.z = Math.random() * 50 - 25;
chair.rotation.set(
Math.random() * Math.PI * 2,
Math.random() * Math.PI * 2,
Math.random() * Math.PI * 2
);
chair.addEventListener( 'ready', spawnChair );
scene.add( chair );
};
return function() {
setTimeout( doSpawn, 500 );
};
})();
render = function() {
requestAnimationFrame( render );
renderer.render( scene, camera );
render_stats.update();
};
window.onload = initScene;
</script>
</head>
<body>
<div id="heading">
<h1>Compound Shapes</h1>
<p>Using the object hierarchy in Three.js to create compound shapes.</p>
</div>
<div id="viewport"></div>
</body>
</html>