-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
230 lines (152 loc) · 7.64 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
<html>
<head>
<meta charset="utf-8">
<title>JS 3D Physics</title>
<style>
body { margin: 0; }
</style>
</head>
<body>
<script src="js/three/three.js"></script>
<script src="js/ammo/ammo.js"></script>
<script>
//variable declaration section
let physicsWorld, scene, camera, renderer, rigidBodies = []
//Ammojs Initialization
Ammo().then(start)
function start (){
tmpTrans = new Ammo.btTransform();
setupPhysicsWorld();
setupGraphics();
createBlock();
createBall();
renderFrame();
}
function setupPhysicsWorld(){
let collisionConfiguration = new Ammo.btDefaultCollisionConfiguration(),
dispatcher = new Ammo.btCollisionDispatcher(collisionConfiguration),
overlappingPairCache = new Ammo.btDbvtBroadphase(),
solver = new Ammo.btSequentialImpulseConstraintSolver();
physicsWorld = new Ammo.btDiscreteDynamicsWorld(dispatcher, overlappingPairCache, solver, collisionConfiguration);
physicsWorld.setGravity(new Ammo.btVector3(0, -10, 0));
}
function setupGraphics(){
//create clock for timing
clock = new THREE.Clock();
//create the scene
scene = new THREE.Scene();
scene.background = new THREE.Color( 0xbfd1e5 );
//create camera
camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 0.2, 5000 );
camera.position.set( 0, 30, 70 );
camera.lookAt(new THREE.Vector3(0, 0, 0));
//Add hemisphere light
let hemiLight = new THREE.HemisphereLight( 0xffffff, 0xffffff, 0.1 );
hemiLight.color.setHSL( 0.6, 0.6, 0.6 );
hemiLight.groundColor.setHSL( 0.1, 1, 0.4 );
hemiLight.position.set( 0, 50, 0 );
scene.add( hemiLight );
//Add directional light
let dirLight = new THREE.DirectionalLight( 0xffffff , 1);
dirLight.color.setHSL( 0.1, 1, 0.95 );
dirLight.position.set( -1, 1.75, 1 );
dirLight.position.multiplyScalar( 100 );
scene.add( dirLight );
dirLight.castShadow = true;
dirLight.shadow.mapSize.width = 2048;
dirLight.shadow.mapSize.height = 2048;
let d = 50;
dirLight.shadow.camera.left = -d;
dirLight.shadow.camera.right = d;
dirLight.shadow.camera.top = d;
dirLight.shadow.camera.bottom = -d;
dirLight.shadow.camera.far = 13500;
//Setup the renderer
renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.setClearColor( 0xbfd1e5 );
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
renderer.gammaInput = true;
renderer.gammaOutput = true;
renderer.shadowMap.enabled = true;
}
function renderFrame(){
let deltaTime = clock.getDelta();
updatePhysics( deltaTime );
renderer.render( scene, camera );
requestAnimationFrame( renderFrame );
}
function createBlock(){
let pos = {x: 0, y: 0, z: 0};
let scale = {x: 50, y: 2, z: 50};
let quat = {x: 0, y: 0, z: 0, w: 1};
let mass = 0;
//threeJS Section
let blockPlane = new THREE.Mesh(new THREE.BoxBufferGeometry(), new THREE.MeshPhongMaterial({color: 0xa0afa4}));
blockPlane.position.set(pos.x, pos.y, pos.z);
blockPlane.scale.set(scale.x, scale.y, scale.z);
blockPlane.castShadow = true;
blockPlane.receiveShadow = true;
scene.add(blockPlane);
//Ammojs Section
let transform = new Ammo.btTransform();
transform.setIdentity();
transform.setOrigin( new Ammo.btVector3( pos.x, pos.y, pos.z ) );
transform.setRotation( new Ammo.btQuaternion( quat.x, quat.y, quat.z, quat.w ) );
let motionState = new Ammo.btDefaultMotionState( transform );
let colShape = new Ammo.btBoxShape( new Ammo.btVector3( scale.x * 0.5, scale.y * 0.5, scale.z * 0.5 ) );
colShape.setMargin( 0.05 );
let localInertia = new Ammo.btVector3( 0, 0, 0 );
colShape.calculateLocalInertia( mass, localInertia );
let rbInfo = new Ammo.btRigidBodyConstructionInfo( mass, motionState, colShape, localInertia );
let body = new Ammo.btRigidBody( rbInfo );
physicsWorld.addRigidBody( body );
}
function createBall(){
let pos = {x: 0, y: 20, z: 0};
let radius = 2;
let quat = {x: 0, y: 0, z: 0, w: 1};
let mass = 1;
//threeJS Section
let ball = new THREE.Mesh(new THREE.SphereBufferGeometry(radius), new THREE.MeshPhongMaterial({color: 0xff0505}));
ball.position.set(pos.x, pos.y, pos.z);
ball.castShadow = true;
ball.receiveShadow = true;
scene.add(ball);
//Ammojs Section
let transform = new Ammo.btTransform();
transform.setIdentity();
transform.setOrigin( new Ammo.btVector3( pos.x, pos.y, pos.z ) );
transform.setRotation( new Ammo.btQuaternion( quat.x, quat.y, quat.z, quat.w ) );
let motionState = new Ammo.btDefaultMotionState( transform );
let colShape = new Ammo.btSphereShape( radius );
colShape.setMargin( 0.05 );
let localInertia = new Ammo.btVector3( 0, 0, 0 );
colShape.calculateLocalInertia( mass, localInertia );
let rbInfo = new Ammo.btRigidBodyConstructionInfo( mass, motionState, colShape, localInertia );
let body = new Ammo.btRigidBody( rbInfo );
physicsWorld.addRigidBody( body );
ball.userData.physicsBody = body;
rigidBodies.push(ball);
}
function updatePhysics( deltaTime ){
// Step world
physicsWorld.stepSimulation( deltaTime, 10 );
// Update rigid bodies
for ( let i = 0; i < rigidBodies.length; i++ ) {
let objThree = rigidBodies[ i ];
let objAmmo = objThree.userData.physicsBody;
let ms = objAmmo.getMotionState();
if ( ms ) {
ms.getWorldTransform( tmpTrans );
let p = tmpTrans.getOrigin();
let q = tmpTrans.getRotation();
objThree.position.set( p.x(), p.y(), p.z() );
objThree.quaternion.set( q.x(), q.y(), q.z(), q.w() );
}
}
}
</script>
</body>
</html>