-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathbasic.html
More file actions
146 lines (123 loc) · 5.22 KB
/
basic.html
File metadata and controls
146 lines (123 loc) · 5.22 KB
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
<!DOCTYPE html>
<script src='vendor/three.js/build/three.min.js'></script>
<script src='../vendor/oimo.js'></script>
<script src='../threex.oimo.js'></script>
<body style='margin: 0px; background-color: #bbbbbb; overflow: hidden;'><script>
var renderer = new THREE.WebGLRenderer();
renderer.setClearColor('lightgrey')
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
var onRenderFcts= [];
var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.01, 1000)
camera.position.z = 100
//////////////////////////////////////////////////////////////////////////////////
// oimo world //
//////////////////////////////////////////////////////////////////////////////////
var world = new OIMO.World(1/120, 2, 8)
setInterval(function(){
world.step()
}, 1000/60);
//////////////////////////////////////////////////////////////////////////////////
// Ground //
//////////////////////////////////////////////////////////////////////////////////
var geometry = new THREE.BoxGeometry(100,100,400);
var material = new THREE.MeshNormalMaterial();
var mesh = new THREE.Mesh( geometry, material );
mesh.position.y = -geometry.parameters.height/2
scene.add(mesh)
var ground = THREEx.Oimo.createBodyFromMesh(world, mesh, {
move : false
})
//////////////////////////////////////////////////////////////////////////////////
// add an object and make it move //
//////////////////////////////////////////////////////////////////////////////////
;(function(){
for(var i = 0; i < 300; i++ ){
(function(){
//////////////////////////////////////////////////////////////////////////////////
// create the mesh
//////////////////////////////////////////////////////////////////////////////////
if( Math.random() < 0.05 ){
var width = 3 + (Math.random()-0.5)*1
var height = 3 + (Math.random()-0.5)*1
var depth = 3 + (Math.random()-0.5)*1
var geometry = new THREE.BoxGeometry(width, height, depth)
}else{
var radius = 3 + (Math.random()-0.5)*2
var geometry = new THREE.SphereGeometry( radius )
}
var material = new THREE.MeshNormalMaterial()
var mesh = new THREE.Mesh( geometry, material )
scene.add( mesh )
mesh.position.x = (Math.random()-0.5)*40
mesh.position.y = 50 + (Math.random()-0.5)*40
mesh.position.z = (Math.random()-0.5)*40
//////////////////////////////////////////////////////////////////////////////////
// create a body for this mesh
//////////////////////////////////////////////////////////////////////////////////
// create IOMO.Body from mesh
var body = THREEx.Oimo.createBodyFromMesh(world, mesh)
// add an updater for them
onRenderFcts.push(function(delta){
THREEx.Oimo.updateObject3dWithBody(mesh, body)
})
//////////////////////////////////////////////////////////////////////////////////
// if body is too low, reset it
//////////////////////////////////////////////////////////////////////////////////
// if the position.y < 20, reset the position
onRenderFcts.push(function(delta){
if( mesh.position.y < -20 ){
mesh.position.x = (Math.random()-0.5)*20
mesh.position.y = 100 + (Math.random()-0.5)*15
mesh.position.z = (Math.random()-0.5)*20
body.resetPosition(mesh.position.x, mesh.position.y, mesh.position.z);
}
})
})()
}
})()
//////////////////////////////////////////////////////////////////////////////////
// comment //
//////////////////////////////////////////////////////////////////////////////////
var iomoStats = new THREEx.Oimo.Stats(world)
document.body.appendChild(iomoStats.domElement)
onRenderFcts.push(function(delta){
iomoStats.update()
})
//////////////////////////////////////////////////////////////////////////////////
// Camera Controls //
//////////////////////////////////////////////////////////////////////////////////
var mouse = {x : 0, y : 0}
document.addEventListener('mousemove', function(event){
mouse.x = (event.clientX / window.innerWidth ) - 0.5
mouse.y = (event.clientY / window.innerHeight) - 0.5
}, false)
onRenderFcts.push(function(delta, now){
camera.position.x += (mouse.x*50 - camera.position.x) * (delta*3)
camera.position.y += (mouse.y*50 - (camera.position.y - 5)) * (delta*3)
camera.lookAt( scene.position )
})
//////////////////////////////////////////////////////////////////////////////////
// render the scene //
//////////////////////////////////////////////////////////////////////////////////
onRenderFcts.push(function(){
renderer.render( scene, camera );
})
//////////////////////////////////////////////////////////////////////////////////
// loop runner //
//////////////////////////////////////////////////////////////////////////////////
var lastTimeMsec= null
requestAnimationFrame(function animate(nowMsec){
// keep looping
requestAnimationFrame( animate );
// measure time
lastTimeMsec = lastTimeMsec || nowMsec-1000/60
var deltaMsec = Math.min(200, nowMsec - lastTimeMsec)
lastTimeMsec = nowMsec
// call each update function
onRenderFcts.forEach(function(onRenderFct){
onRenderFct(deltaMsec/1000, nowMsec/1000)
})
})
</script></body>