forked from lo-th/Oimo.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_basic.html
339 lines (271 loc) · 11.6 KB
/
test_basic.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
<!DOCTYPE html>
<html lang="en">
<head>
<title>Oimo.js Basic</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" />
<link rel="shortcut icon" href="favicon.ico">
<link rel="stylesheet" href="css/demo.css">
<script src="js_new/three.min.js"></script>
<script src="js_new/OrbitControls.js"></script>
<script src="build/oimo.min.js"></script>
</head>
<body>
<canvas id="canvas"></canvas>
<div id='interface'>
<input type="button" value="sphere" onClick=populate(1)>
<input type="button" value="boxe" onClick=populate(2)>
<input type="button" value="cylinder" onClick=populate(3)>
<input type="button" value="mix" onClick=populate(4)>
<input type="number" name="quantity" min="10" max="2000" value="400" id='MaxNumber'>
<input type="submit" onClick=populate()>
<input type="number" name="gravity" min="-20" max="20" value="-10" id='gravity' onChange=gravity() >
</div>
<div id='info'></div>
<div id='link'>
<a href="test_basic.html">Basic</a>
<a href="test_compound.html">Compound</a>
<a href="test_compound2.html">Compound2</a>
<a href="test_ragdoll.html">Ragdoll</a>
<a href="test_collision.html">Collision</a>
<a href="test_moving.html">Moving</a>
<a href="test_terrain.html">Terrain</a>
<a href="test_car">Vehicle</a>
</div>
<img style="position: absolute; top: 0; right: 0; border: 0;" src="images/ribbon.png"></a>
<a id="ribbon" href="https://github.com/lo-th/Oimo.js"></a>
<script>
// three var
var camera, scene, light, renderer, canvas, controls;
var meshs = [];
var grounds = [];
var isMobile = false;
var antialias = true;
var geos = {};
var mats = {};
//oimo var
var world = null;
var bodys = [];
var fps = [0,0,0,0];
var ToRad = 0.0174532925199432957;
var type = 1;
var infos;
init();
loop();
function init() {
var n = navigator.userAgent;
if (n.match(/Android/i) || n.match(/webOS/i) || n.match(/iPhone/i) || n.match(/iPad/i) || n.match(/iPod/i) || n.match(/BlackBerry/i) || n.match(/Windows Phone/i)){ isMobile = true; antialias = false; document.getElementById("MaxNumber").value = 200; }
infos = document.getElementById("info");
canvas = document.getElementById("canvas");
camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 1, 5000 );
camera.position.set( 0, 160, 400 );
controls = new THREE.OrbitControls( camera, canvas );
controls.target.set(0, 20, 0);
controls.update();
scene = new THREE.Scene();
renderer = new THREE.WebGLRenderer({ canvas:canvas, precision: "mediump", antialias:antialias });
renderer.setSize( window.innerWidth, window.innerHeight );
var materialType = 'MeshBasicMaterial';
if(!isMobile){
scene.add( new THREE.AmbientLight( 0x3D4143 ) );
light = new THREE.DirectionalLight( 0xffffff , 1.4);
light.position.set( 300, 1000, 500 );
light.target.position.set( 0, 0, 0 );
light.castShadow = true;
light.shadowCameraNear = 500;
light.shadowCameraFar = 1600;
light.shadowCameraFov = 70;
light.shadowBias = 0.0001;
light.shadowDarkness = 0.7;
//light.shadowCameraVisible = true;
light.shadowMapWidth = light.shadowMapHeight = 1024;
scene.add( light );
materialType = 'MeshPhongMaterial';
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFShadowMap;//THREE.BasicShadowMap;
}
// background
var buffgeoBack = new THREE.BufferGeometry();
buffgeoBack.fromGeometry( new THREE.IcosahedronGeometry(3000,2) );
var back = new THREE.Mesh( buffgeoBack, new THREE.MeshBasicMaterial( { map:gradTexture([[0.75,0.6,0.4,0.25], ['#1B1D1E','#3D4143','#72797D', '#b0babf']]), side:THREE.BackSide, depthWrite: false, fog:false } ));
//back.geometry.applyMatrix(new THREE.Matrix4().makeRotationZ(15*ToRad));
scene.add( back );
// geometrys
geos['sphere'] = new THREE.BufferGeometry().fromGeometry( new THREE.SphereGeometry(1,16,10));
geos['box'] = new THREE.BufferGeometry().fromGeometry( new THREE.BoxGeometry(1,1,1));
geos['cylinder'] = new THREE.BufferGeometry().fromGeometry(new THREE.CylinderGeometry(1,1,1));
// materials
mats['sph'] = new THREE[materialType]( {shininess: 10, map: basicTexture(0), name:'sph' } );
mats['box'] = new THREE[materialType]( {shininess: 10, map: basicTexture(2), name:'box' } );
mats['cyl'] = new THREE[materialType]( {shininess: 10, map: basicTexture(4), name:'cyl' } );
mats['ssph'] = new THREE[materialType]( {shininess: 10, map: basicTexture(1), name:'ssph' } );
mats['sbox'] = new THREE[materialType]( {shininess: 10, map: basicTexture(3), name:'sbox' } );
mats['scyl'] = new THREE[materialType]( {shininess: 10, map: basicTexture(5), name:'scyl' } );
mats['ground'] = new THREE[materialType]( {shininess: 10, color:0x3D4143, transparent:true, opacity:0.5 } );
// events
window.addEventListener( 'resize', onWindowResize, false );
// physics
initOimoPhysics();
}
function loop() {
updateOimoPhysics();
renderer.render( scene, camera );
requestAnimationFrame( loop );
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
}
function addStaticBox(size, position, rotation) {
var mesh = new THREE.Mesh( geos.box, mats.ground );
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);
mesh.castShadow = true;
mesh.receiveShadow = true;
}
function clearMesh(){
var i=meshs.length;
while (i--) scene.remove(meshs[ i ]);
i = grounds.length;
while (i--) scene.remove(grounds[ i ]);
grounds = [];
meshs = [];
}
//----------------------------------
// OIMO PHYSICS
//----------------------------------
function initOimoPhysics(){
// world setting:( TimeStep, BroadPhaseType, Iterations )
// BroadPhaseType can be
// 1 : BruteForce
// 2 : Sweep and prune , the default
// 3 : dynamic bounding volume tree
world = new OIMO.World(1/60, 2, 8);
populate(1);
//setInterval(updateOimoPhysics, 1000/60);
}
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;
else if(n===4) type = 4;
// reset old
clearMesh();
world.clear();
bodys=[];
//add ground
var ground0 = world.add({size:[40, 40, 390], pos:[-180,20,0], world:world});
var ground1 = world.add({size:[40, 40, 390], pos:[180,20,0], world:world});
var ground2 = world.add({size:[400, 80, 400], pos:[0,-40,0], world:world});
addStaticBox([40, 40, 390], [-180,20,0], [0,0,0]);
addStaticBox([40, 40, 390], [180,20,0], [0,0,0]);
addStaticBox([400, 80, 400], [0,-40,0], [0,0,0]);
//add object
var x, y, z, w, h, d;
var i = max;
while (i--){
if(type===4) t = Math.floor(Math.random()*3)+1;
else t = type;
x = -100 + Math.random()*200;
z = -100 + Math.random()*200;
y = 100 + Math.random()*1000;
w = 10 + Math.random()*10;
h = 10 + Math.random()*10;
d = 10 + Math.random()*10;
if(t===1){
bodys[i] = world.add({type:'sphere', size:[w*0.5], pos:[x,y,z], move:true, world:world});
meshs[i] = new THREE.Mesh( geos.sphere, mats.sph );
meshs[i].scale.set( w*0.5, w*0.5, w*0.5 );
} else if(t===2){
bodys[i] = world.add({type:'box', size:[w,h,d], pos:[x,y,z], move:true, world:world});
meshs[i] = new THREE.Mesh( geos.box, mats.box );
meshs[i].scale.set( w, h, d );
} else if(t===3){
bodys[i] = world.add({type:'cylinder', size:[w*0.5,h], pos:[x,y,z], move:true, world:world});
meshs[i] = new THREE.Mesh( geos.cylinder, mats.cyl );
meshs[i].scale.set( w*0.5, h, w*0.5 );
}
meshs[i].castShadow = true;
meshs[i].receiveShadow = true;
scene.add( meshs[i] );
}
}
function updateOimoPhysics() {
if(world==null) return;
world.step();
var x, y, z, mesh, body, i = bodys.length;
while (i--){
body = bodys[i];
mesh = meshs[i];
if(!body.sleeping){
mesh.position.copy(body.getPosition());
mesh.quaternion.copy(body.getQuaternion());
// change material
if(mesh.material.name === 'sbox') mesh.material = mats.box;
if(mesh.material.name === 'ssph') mesh.material = mats.sph;
if(mesh.material.name === 'scyl') mesh.material = mats.cyl;
// reset position
if(mesh.position.y<-100){
x = -100 + Math.random()*200;
z = -100 + Math.random()*200;
y = 100 + Math.random()*1000;
body.resetPosition(x,y,z);
}
} else {
if(mesh.material.name === 'box') mesh.material = mats.sbox;
if(mesh.material.name === 'sph') mesh.material = mats.ssph;
if(mesh.material.name === 'cyl') mesh.material = mats.scyl;
}
}
infos.innerHTML = world.performance.show();
}
function gravity(g){
nG = document.getElementById("gravity").value
world.gravity = new OIMO.Vec3(0, nG, 0);
}
//----------------------------------
// TEXTURES
//----------------------------------
function gradTexture(color) {
var c = document.createElement("canvas");
var ct = c.getContext("2d");
var size = 1024;
c.width = 16; c.height = size;
var gradient = ct.createLinearGradient(0,0,0,size);
var i = color[0].length;
while(i--){ gradient.addColorStop(color[0][i],color[1][i]); }
ct.fillStyle = gradient;
ct.fillRect(0,0,16,size);
var texture = new THREE.Texture(c);
texture.needsUpdate = true;
return texture;
}
function basicTexture(n){
var canvas = document.createElement( 'canvas' );
canvas.width = canvas.height = 64;
var ctx = canvas.getContext( '2d' );
var color;
if(n===0) color = "#3884AA";// sphere58AA80
if(n===1) color = "#61686B";// sphere sleep
if(n===2) color = "#AA6538";// box
if(n===3) color = "#61686B";// box sleep
if(n===4) color = "#AAAA38";// cyl
if(n===5) color = "#61686B";// cyl sleep
ctx.fillStyle = color;
ctx.fillRect(0, 0, 64, 64);
ctx.fillStyle = "rgba(0,0,0,0.2)";
ctx.fillRect(0, 0, 32, 32);
ctx.fillRect(32, 32, 32, 32);
var tx = new THREE.Texture(canvas);
tx.needsUpdate = true;
return tx;
}
</script>
</body>
</html>