forked from pmndrs/cannon-es-debugger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
340 lines (292 loc) · 10.3 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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>cannon-es-debugger</title>
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0" />
<style>
body {
margin: 0;
padding: 0;
overflow: hidden;
font-family: Monospace;
}
canvas {
outline: none;
}
.page-title {
position: fixed;
top: 0.75rem;
left: 0;
right: 0;
text-align: center;
color: white;
}
.page-title span {
color: #99ff4e;
}
</style>
</head>
<body>
<div class="page-title">Press the <span>d</span> key to toggle the debugger</div>
<!-- Import maps polyfill -->
<!-- Remove this when import maps will be widely supported -->
<script async src="https://unpkg.com/es-module-shims@1.3.6/dist/es-module-shims.js"></script>
<script type="importmap">
{
"imports": {
"cannon-es": "https://unpkg.com/cannon-es@0.18.0/dist/cannon-es.js",
"cannon-es-debugger": "./dist/cannon-es-debugger.js",
"three": "https://unpkg.com/three@0.136.0/build/three.module.js",
"three/examples/jsm/controls/OrbitControls": "https://unpkg.com/three@0.136.0/examples/jsm/controls/OrbitControls.js"
}
}
</script>
<script type="module">
import * as THREE from 'three'
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls'
import * as CANNON from 'cannon-es'
import CannonDebugger from 'cannon-es-debugger'
// three.js variables
let camera, scene, renderer, controls
let material
// cannon.js variables
let world
const mass = 7
const timeStep = 1 / 60
let lastCallTime
let cannonDebugger
// To be kept in sync
const meshes = []
const bodies = []
initThree()
initCannon()
initCannonDebugger()
addPlane()
addSphere()
addBox()
addCylinder()
addTrimesh()
addHeightfield()
animate()
function initThree() {
// Camera
camera = new THREE.PerspectiveCamera(30, window.innerWidth / window.innerHeight, 0.5, 1000)
camera.position.set(5, 4, 5)
// Scene
scene = new THREE.Scene()
scene.fog = new THREE.Fog(0x000000, 500, 1000)
// Renderer
renderer = new THREE.WebGLRenderer({ antialias: true })
renderer.setPixelRatio(window.devicePixelRatio)
renderer.setSize(window.innerWidth, window.innerHeight)
renderer.setClearColor(scene.fog.color)
renderer.outputEncoding = THREE.sRGBEncoding
renderer.shadowMap.enabled = true
renderer.shadowMap.type = THREE.PCFSoftShadowMap
document.body.appendChild(renderer.domElement)
// Orbit controls
controls = new OrbitControls(camera, renderer.domElement)
controls.enableDamping = true
controls.enablePan = false
controls.dampingFactor = 0.1
controls.minDistance = 1
controls.maxDistance = 50
// Generic material
material = new THREE.MeshStandardMaterial({ color: '#ccc' })
// Lights
const ambientLight = new THREE.AmbientLight(0xffffff, 0.1)
scene.add(ambientLight)
const spotLight = new THREE.SpotLight(0xffffff, 0.3, 0, Math.PI / 3, 1)
spotLight.position.set(0, 4, 0)
spotLight.castShadow = true
scene.add(spotLight)
window.addEventListener('resize', onWindowResize)
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight
camera.updateProjectionMatrix()
renderer.setSize(window.innerWidth, window.innerHeight)
}
function initCannon() {
// Setup world
world = new CANNON.World()
world.gravity.set(0, -9.81, 0)
}
function initCannonDebugger() {
cannonDebugger = new CannonDebugger(scene, world, {
onInit(body, mesh) {
// Toggle visibiliy on "d" press
document.addEventListener('keydown', (event) => {
if (event.key === 'd') {
mesh.visible = !mesh.visible
}
})
},
})
}
function animate() {
requestAnimationFrame(animate)
// Step the physics world
const time = performance.now() / 1000
if (!lastCallTime) {
world.step(timeStep)
} else {
const dt = time - lastCallTime
world.step(timeStep, dt)
}
lastCallTime = time
// Update the debugger
cannonDebugger.update()
// Update the visible meshes positions
updateMeshPositions()
controls.update()
renderer.render(scene, camera)
}
function updateMeshPositions() {
for (let i = 0; i < meshes.length; i++) {
meshes[i].position.copy(bodies[i].position)
meshes[i].quaternion.copy(bodies[i].quaternion)
}
}
function addPlane() {
// Physics
const shape = new CANNON.Plane()
const body = new CANNON.Body({ mass: 0 })
body.addShape(shape)
body.quaternion.setFromEuler(-Math.PI / 2, 0, 0)
world.addBody(body)
bodies.push(body)
// Graphics
const geometry = new THREE.PlaneGeometry(100, 100, 1, 1)
const material = new THREE.MeshStandardMaterial({ color: '#060606' })
const mesh = new THREE.Mesh(geometry, material)
// position and quaternion of the mesh are set by updateMeshPositions...
mesh.castShadow = true
mesh.receiveShadow = true
scene.add(mesh)
meshes.push(mesh)
}
function addBox() {
const size = 1
// Physics
const halfExtents = new CANNON.Vec3(size * 0.5, size * 0.5, size * 0.5)
const shape = new CANNON.Box(halfExtents)
const body = new CANNON.Body({ mass })
body.addShape(shape)
body.position.set(2, 2, 0.5)
world.addBody(body)
bodies.push(body)
// Graphics
const geometry = new THREE.BoxGeometry(size, size, size)
const mesh = new THREE.Mesh(geometry, material)
// position and quaternion of the mesh are set by updateMeshPositions...
mesh.castShadow = true
mesh.receiveShadow = true
scene.add(mesh)
meshes.push(mesh)
}
function addSphere() {
const size = 0.5
// Physics
const body = new CANNON.Body({ mass })
const shape = new CANNON.Sphere(size)
body.addShape(shape)
body.position.set(-0.5, 2, -1)
world.addBody(body)
bodies.push(body)
// Graphics
const geometry = new THREE.SphereGeometry(size)
const mesh = new THREE.Mesh(geometry, material)
// position and quaternion of the mesh are set by updateMeshPositions...
mesh.castShadow = true
mesh.receiveShadow = true
scene.add(mesh)
meshes.push(mesh)
}
function addCylinder() {
const size = 1
const radialSegments = 15
// Physics
const body = new CANNON.Body({ mass })
const shape = new CANNON.Cylinder(size * 0.5, size * 0.5, size, radialSegments)
body.addShape(shape)
body.position.set(0, 2, 1.5)
world.addBody(body)
bodies.push(body)
// Graphics
const geometry = new THREE.CylinderGeometry(size * 0.5, size * 0.5, size, radialSegments)
const mesh = new THREE.Mesh(geometry, material)
// position and quaternion of the mesh are set by updateMeshPositions...
mesh.castShadow = true
mesh.receiveShadow = true
scene.add(mesh)
meshes.push(mesh)
}
function addTrimesh() {
const radius = 1
const tube = 0.3
const radialSegments = 16
// Physics
const body = new CANNON.Body({ mass })
const shape = CANNON.Trimesh.createTorus(radius, tube, radialSegments, 16)
body.addShape(shape)
body.position.set(-3, 2, -1)
body.quaternion.setFromEuler(Math.PI * 0.1, 0, 0)
world.addBody(body)
bodies.push(body)
// Graphics
const geometry = new THREE.TorusGeometry(radius, tube, radialSegments, 100)
const mesh = new THREE.Mesh(geometry, material)
// position and quaternion of the mesh are set by updateMeshPositions...
mesh.castShadow = true
mesh.receiveShadow = true
scene.add(mesh)
meshes.push(mesh)
}
function addHeightfield() {
const sizeX = 20 // number of vertices in the X axis
const sizeY = 20 // number of vertices in the Y axis
const elementSize = 0.3 // cell width
const depth = 0.6
// Physics
const body = new CANNON.Body({ mass: 0 })
const matrix = []
for (let i = 0; i < sizeX; i++) {
matrix.push([])
for (let j = 0; j < sizeY; j++) {
const height = Math.cos((i / (sizeX - 1)) * Math.PI * 2) * Math.cos((j / (sizeY - 1)) * Math.PI * 2) * depth
matrix[i].push(height)
}
}
const shape = new CANNON.Heightfield(matrix, { elementSize })
body.addShape(shape, new CANNON.Vec3((-(sizeX - 1) / 2) * elementSize, (-(sizeY - 1) / 2) * elementSize, 0))
body.position.set(0, depth, -6)
body.quaternion.setFromEuler(-Math.PI / 2, 0, 0)
world.addBody(body)
bodies.push(body)
// Graphics
const geometry = new THREE.PlaneGeometry(
(sizeX - 1) * elementSize,
(sizeY - 1) * elementSize,
sizeX - 1,
sizeY - 1
)
for (let i = 0; i < sizeX; i++) {
for (let j = 0; j < sizeY; j++) {
const height = Math.cos((i / (sizeX - 1)) * Math.PI * 2) * Math.cos((j / (sizeY - 1)) * Math.PI * 2) * depth
geometry.attributes.position.setZ(i * sizeX + j, height)
}
}
geometry.computeBoundingSphere()
geometry.computeVertexNormals()
const mesh = new THREE.Mesh(geometry, material)
// position and quaternion of the mesh are set by updateMeshPositions...
mesh.castShadow = true
mesh.receiveShadow = true
scene.add(mesh)
meshes.push(mesh)
}
</script>
</body>
</html>