forked from lo-th/Oimo.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_compound.html
369 lines (287 loc) · 11.1 KB
/
test_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
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
<!DOCTYPE html>
<html lang="en">
<head>
<title>Oimo.js Compound test</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="IE=11" />
<style>
body {
margin: 0px;
background-color: #000000;
overflow: hidden;
color: #eeeeee;
font-family: monospace;
font-size: 14px;
}
#interface{
position: absolute;
left:10px;
top:10px;
}
#info{
pointer-events:none;
position: absolute;
left:10px;
top:50px;
width: 400px;
height: 400px;
}
</style>
<script src="js/three.min.js"></script>
<script src="build/Oimo.js"></script>
<script src="js/controls/OrbitControls.js"></script>
</head>
<body>
<div id='container'></div>
<div id='interface'>
<input type="button" value="demo 1" onClick=populate(1)>
<input type="button" value="demo 2" onClick=populate(2)>
<input type="number" name="quantity" min="10" max="2000" value="200" id='MaxNumber'>
<input type="submit" onClick=populate()>
</div>
<div id='info'></div>
<a href="https://github.com/lo-th/Oimo.js"><img style="position: absolute; top: 0; right: 0; border: 0;" src="images/ribbon.png" alt="Fork me on GitHub"></a>
</body>
<script>
// three var
var camera, scene, light, renderer, container, cameraControls;
var meshs = [];
var grounds = [];
var geoBox, geoSphere;
var matBox, matSphere, matBoxSleep, matSphereSleep, matGround;
//oimo var
var world;
var bodys = [];
var fps=0, time, time_prev=0, fpsint = 0;
var ToRad = Math.PI / 180;
var type=1;
init();
animate();
function init() {
// three init
renderer = new THREE.WebGLRenderer({precision: "mediump", antialias:false, clearColor: 0x585858, clearAlpha: 0});
this.renderer.autoClear = false;
//renderer.setClearColor( 0x585858, 1 );
renderer.setSize( window.innerWidth, window.innerHeight );
container = document.getElementById("container");
container.appendChild( renderer.domElement );
camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 10000 );
camera.position.set( 0, 100, 400 );
cameraControls = new THREE.OrbitControls(camera, renderer.domElement);
cameraControls.target.set( 0, 40, 0);
cameraControls.maxDistance = 800;
cameraControls.minDistance = 10;
cameraControls.update();
scene = new THREE.Scene();
scene.add( new THREE.AmbientLight( 0x383838 ) );
light = new THREE.DirectionalLight( 0xffffff , 1.3);
light.position.set( 0.3, 1, 0.5 ).normalize();
scene.add( light );
geoSphere = new THREE.SphereGeometry( 1 , 20, 10 );
geoBox = new THREE.CubeGeometry( 1, 1, 1 );
matSphere = new THREE.MeshLambertMaterial( { map: basicTexture(0), name:'sph' } );
matBox = new THREE.MeshLambertMaterial( { map: basicTexture(2), name:'box' } );
matSphereSleep = new THREE.MeshLambertMaterial( { map: basicTexture(1), name:'ssph' } );
matBoxSleep = new THREE.MeshLambertMaterial( { map: basicTexture(3), name:'sbox' } );
matGround = new THREE.MeshLambertMaterial( { color: 0x151515 } );
// oimo init
world = new OIMO.World();
initChairGeometry();
populate(1);
//
window.addEventListener( 'resize', onWindowResize, false );
}
var types, sizes, positions, chairGeometry;
function addStaticBox(size, position, rotation) {
var mesh = new THREE.Mesh( geoBox, matGround );
mesh.scale.set( size[0], size[1], size[2] );
mesh.position.set( position[0], position[1], position[2] );
mesh.rotation.set( rotation[0]*ToRad, rotation[1]*ToRad, rotation[2]*ToRad );
scene.add( mesh );
grounds.push(mesh);
}
function initChairGeometry() {
types = [ 'box', 'box', 'box', 'box', 'box', 'box', 'box', 'box' ];
sizes = [ 30,5,30, 4,30,4, 4,30,4, 4,30,4, 4,30,4, 4,30,4, 4,30,4, 23,10,3 ];
positions = [ 0,0,0, 12,-16,12, -12,-16,12, 12,-16,-12, -12,-16,-12, 12,16,-12, -12,16,-12, 0,25,-12 ];
chairGeometry = new THREE.Geometry();
var mesh, n;
for(var i=0; i<types.length; i++){
n = i*3;
mesh = new THREE.Mesh( geoBox );
mesh.scale.set( sizes[n+0], sizes[n+1], sizes[n+2] );
mesh.position.set( positions[n+0], positions[n+1], positions[n+2] );
THREE.GeometryUtils.merge( chairGeometry, mesh );
}
}
function populate(n) {
var max = document.getElementById("MaxNumber").value;
if(n===1) type = 1
else if(n===2) type = 2;
//else if(n===3) type = 3;
// reset old
clearMesh();
world.clear();
if(type===1){// DEMO 1
var ground = new OIMO.Body({size:[100, 40, 400], pos:[0,-20,0], world:world});
var ground2 = new OIMO.Body({size:[400, 40, 400], pos:[0,-50,0], world:world});
addStaticBox([100, 40, 400], [0,-20,0], [0,0,0]);
addStaticBox([400, 40, 400], [0,-50,0], [0,0,0]);
var i = max;
var j;
while (i--){
positions[1] = 300+(i*160);
positions[0] = 0;
positions[2] = 0;
bodys[i] = new OIMO.Body({
type:types,
size:sizes,
pos:positions,
move:true,
world:world,
name:'box'+i,
config:[0.2, 0.4,0.1]
});
j = Math.round(Math.random()*2);
if(j===1)meshs[i] = new THREE.Mesh( chairGeometry, matBox );
else meshs[i] = new THREE.Mesh( chairGeometry, matSphere );
scene.add(meshs[i]);
}
} else if(type===2){// DEMO 2
var ground = new OIMO.Body({size:[1000, 40, 1000], pos:[0,-20,0], world:world});
addStaticBox([1000, 40, 1000], [0,-20,0], [0,0,0]);
var ground2 = new OIMO.Body({size:[400, 40, 400], pos:[0,130,-600], rot:[45,0,0], world:world});
addStaticBox([400, 40, 400], [0,130,-600], [45,0,0]);
var i = max;
var j, k=0, l=0;
while (i--){
positions[1] = 50;
positions[0] = -400+(50*l);
positions[2] = -400+(50*k);
l++;
if(l>16){k++; l=0}
bodys[i] = new OIMO.Body({
type:types,
size:sizes,
pos:positions,
move:true,
world:world,
name:'box'+i,
config:[0.2, 0.4,0.1]
});
j = Math.round(Math.random()*2);
if(j===1)meshs[i] = new THREE.Mesh( chairGeometry, matBox );
else meshs[i] = new THREE.Mesh( chairGeometry, matSphere );
scene.add(meshs[i]);
}
bodys[max] = new OIMO.Body({type:'sphere', size:[80], pos:[0,1000,-600], move:true, world:world});
meshs[max] = new THREE.Mesh( geoSphere, matSphere );
meshs[max].scale.set( 80, 80, 80 );
scene.add(meshs[max]);
}
}
function clearMesh(){
var i = meshs.length;
while (i--){
scene.remove(meshs[ i ]);
}
i = grounds.length;
while (i--){
scene.remove(grounds[ i ]);
}
grounds = [];
meshs = [];
bodys=[];
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
}
function animate() {
requestAnimationFrame( animate );
world.step();
var p, r, m, x, y, z;
var mtx = new THREE.Matrix4();
var i = bodys.length;
var mesh;
while (i--){
mesh = meshs[i];
if(!bodys[i].sleep()){
m = bodys[i].getMatrix();
mtx.fromArray(m);
mesh.position.setFromMatrixPosition( mtx );
mesh.rotation.setFromRotationMatrix( mtx );
// change material
if(mesh.material.name === 'sbox') mesh.material = matBox;
if(mesh.material.name === 'ssph') mesh.material = matSphere;
// reset position
if(m[13]<-100){
x = -100 + Math.random()*200;
z = -100 + Math.random()*200;
y = 100 + Math.random()*1000;
bodys[i].setPosition(x,y,z);
}
} else {
if(mesh.material.name === 'box') mesh.material = matBoxSleep;
if(mesh.material.name === 'sph') mesh.material = matSphereSleep;
}
}
cameraControls.update();
renderer.render( scene, camera );
displayInfo();
}
function displayInfo(){
time = Date.now();
if (time - 1000 > time_prev) {
time_prev = time; fpsint = fps; fps = 0;
} fps++;
var info =[
"Oimo.js DEV.1.1.0a<br><br>",
"FPS: " + fpsint +" fps<br><br>",
"Rigidbody: "+world.numRigidBodies+"<br>",
"Contact: "+world.numContacts+"<br>",
"Pair Check: "+world.broadPhase.numPairChecks+"<br>",
"Contact Point: "+world.numContactPoints+"<br>",
"Island: " + world.numIslands +"<br><br>",
"Broad-Phase Time: " + world.performance.broadPhaseTime + " ms<br>",
"Narrow-Phase Time: " + world.performance.narrowPhaseTime + " ms<br>",
"Solving Time: " + world.performance.solvingTime + " ms<br>",
"Updating Time: " + world.performance.updatingTime + " ms<br>",
"Total Time: " + world.performance.totalTime + " ms "
].join("\n");
document.getElementById("info").innerHTML = info;
}
function basicTexture(n){
var canvas = document.createElement( 'canvas' );
canvas.width = canvas.height = 64;
var ctx = canvas.getContext( '2d' );
var colors = [];
if(n===0){ // sphere
colors[0] = "#58AA80";
colors[1] = "#58FFAA";
}
if(n===1){ // sphere sleep
colors[0] = "#383838";
colors[1] = "#38AA80";
}
if(n===2){ // box
colors[0] = "#AA8058";
colors[1] = "#FFAA58";
}
if(n===3){ // box sleep
colors[0] = "#383838";
colors[1] = "#AA8038";
}
ctx.fillStyle = colors[0];
ctx.fillRect(0, 0, 64, 64);
ctx.fillStyle = colors[1];
ctx.fillRect(0, 0, 32, 32);
ctx.fillRect(32, 32, 32, 32);
var tx = new THREE.Texture(canvas);
tx.needsUpdate = true;
return tx;
}
</script>
</html>