-
Notifications
You must be signed in to change notification settings - Fork 97
/
simplify-physics-mesh.html
84 lines (72 loc) · 2.87 KB
/
simplify-physics-mesh.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta
name="viewport"
content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no, viewport-fit=cover"
/>
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Simplify Physics Mesh</title>
<link rel="stylesheet" href="/css/examples.css?ver=1.0.0" />
<script src="/js/examples.js?ver=1.1.1"></script>
<script src="/lib/enable3d/enable3d.framework.0.25.4.min.js"></script>
<script>
const { Project, Scene3D, PhysicsLoader, ExtendedObject3D, THREE } = ENABLE3D
</script>
<script src="/lib/BufferGeometryUtils.js"></script>
<script src="/lib/SimplifyModifier.js"></script>
</head>
<body>
<div id="info-text">The Physics Body on the left has fewer polygons than the one to the right.</div>
<script>
class MainScene extends Scene3D {
async create() {
this.warpSpeed()
this.camera.position.set(3, 3, 6)
this.physics.debug.enable()
const opacity = 0.8
const transparent = true
// normal duck
this.load.gltf('/assets/glb/Duck.glb').then(gltf => {
const object = gltf.scene.children[0]
object.position.x = 1
object.position.y = 1
object.rotation.y = -Math.PI / 2
object.traverse(child => {
if (child.isMesh && child.geometry) {
child.material.opacity = opacity
child.material.transparent = transparent
}
})
this.add.existing(object)
this.physics.add.existing(object, { shape: 'mesh' })
})
// duck with fewer polygons
this.load.gltf('/assets/glb/Duck.glb').then(gltf => {
const object = gltf.scene.children[0]
object.position.x = -1
object.position.y = 1
object.rotation.y = -Math.PI / 2
object.traverse(child => {
if (child.isMesh) {
const clone = child.clone(true)
clone.material.opacity = opacity
clone.material.transparent = transparent
child.material = child.material.clone()
child.material.visible = false
const modifier = new THREE.SimplifyModifier()
const count = Math.floor(child.geometry.attributes.position.count * 0.8) // number of vertices to remove
child.geometry = modifier.modify(child.geometry, count)
Promise.resolve().then(() => object.add(clone))
}
})
this.add.existing(object)
this.physics.add.existing(object, { shape: 'mesh' })
})
}
}
PhysicsLoader('/lib/ammo/kripken', () => new Project({ scenes: [MainScene] }))
</script>
</body>
</html>