-
Notifications
You must be signed in to change notification settings - Fork 97
/
railway-with-csg.html
84 lines (69 loc) · 2.99 KB
/
railway-with-csg.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>Railway with CSG and HACD</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>
</head>
<body>
<div id="info-text">Make a Railway using CSG and HACD Body</div>
<script>
const { Project, Scene3D, PhysicsLoader, ExtendedObject3D, THREE } = ENABLE3D
class MainScene extends Scene3D {
isRolling = false
wheel
async create() {
this.warpSpeed('-ground')
this.camera.position.set(20, 20, 40)
this.camera.lookAt(0, 0, 0)
this.physics.debug?.enable()
const distance = 10
let railLeft = this.add.box({ depth: 80 })
railLeft.position.x -= distance
this.physics.add.existing(railLeft, { collisionFlags: 1, mass: 0 })
railLeft.body.setRestitution(1)
let railRight = this.add.box({ depth: 80 })
railRight.position.x += distance
this.physics.add.existing(railRight, { collisionFlags: 1, mass: 0 })
railRight.body.setRestitution(1)
let group = []
const data = [
{ radiusSegments: 24, radiusTop: 4, radiusBottom: 4, height: 2, z: 0, y: 10 },
{ radiusSegments: 24, radiusTop: 4, radiusBottom: 4, height: 2, z: 0, y: -10 },
{ radiusSegments: 24, radiusTop: 5.5, radiusBottom: 5.5, height: 0.3, z: 0, y: 9 },
{ radiusSegments: 24, radiusTop: 5.5, radiusBottom: 5.5, height: 0.3, z: 0, y: -9 },
{ radiusSegments: 4, radiusTop: 1, radiusBottom: 1, height: 20, z: 0, y: 0 }
]
data.forEach(d => group.push(this.make.cylinder(d)))
this.wheel = group[0]
for (let i = 1; i < group.length; i++) {
this.wheel = this.csg.union(this.wheel, group[i])
// [bug] you do not have to add this line in version >= 0.20.1
this.scene.remove(this.wheel)
}
this.wheel.material = new THREE.MeshLambertMaterial({ color: 0xcccccc })
this.wheel.castShadow = this.wheel.receiveShadow = true
this.wheel.rotateZ(Math.PI / 2)
this.wheel.position.z -= 20
this.wheel.position.y -= 4
this.add.existing(this.wheel)
this.physics.add.existing(this.wheel, { shape: 'hacd', autoCenter: true, mass: 1000 })
setTimeout(() => {
this.isRolling = true
}, 2000)
}
update() {
if (this.isRolling) this.wheel.body.setAngularVelocityX(1)
}
}
PhysicsLoader('/lib/ammo/moz', () => new Project({ scenes: [MainScene], antialias: true }))
</script>
</body>
</html>