-
Notifications
You must be signed in to change notification settings - Fork 98
/
Copy pathhollow-cylinder.html
109 lines (90 loc) · 3.7 KB
/
hollow-cylinder.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
<!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>Hollow Cylinder</title>
<link rel="stylesheet" href="/css/examples.css?ver=1.0.0" />
<script src="/js/examples.js?ver=1.1.1"></script>
</head>
<body>
<script type="importmap">
{
"imports": {
"three": "/lib/threejs/r171/three.module.min.js",
"orbit-controls": "/lib/threejs/r171/OrbitControls.module.min.js",
"enable3d": "/lib/enable3d/enable3d.ammoPhysics.0.26.0_dev0.module.min.js"
}
}
</script>
<script type="module">
import * as THREE from 'three'
import { AmmoPhysics, PhysicsLoader } from 'enable3d'
import { OrbitControls } from 'orbit-controls'
const MainScene = () => {
const scene = new THREE.Scene()
scene.background = new THREE.Color(0xf0f0f0)
const camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 0.1, 1000)
camera.position.set(10, 10, 20)
const renderer = new THREE.WebGLRenderer()
renderer.setSize(window.innerWidth, window.innerHeight)
document.body.appendChild(renderer.domElement)
const DPR = window.devicePixelRatio
renderer.setPixelRatio(Math.min(2, DPR))
const controls = new OrbitControls(camera, renderer.domElement)
controls.target.set(0, 2, 0)
camera.lookAt(0, 2, 0)
scene.add(new THREE.HemisphereLight(0xffffff, 0x000000, 1))
scene.add(new THREE.AmbientLight(0xffffff, 1))
const light = new THREE.DirectionalLight(0xffffff, 1)
light.position.set(50, 200, 100)
light.position.multiplyScalar(1.3)
const physics = new AmmoPhysics(scene)
physics.debug.enable(true)
// balls
physics.add.sphere({ z: -1 }, { lambert: { color: 'olivegreen' } })
physics.add.sphere({ z: 0 }, { lambert: { color: 'orchid' } })
physics.add.sphere({ z: -1, y: 2.5 }, { lambert: { color: 'firebrick' } })
// static ground
physics.add.ground({ width: 20, height: 20 }, { lambert: { color: 'cadetblue' } })
// ------------------------
// add outer cylinder using extrude (https://stackoverflow.com/a/31107090)
// ------------------------
const extrudeSettings = {
depth: 2,
steps: 1,
bevelEnabled: false,
curveSegments: 8
}
const arcShape = new THREE.Shape()
arcShape.absarc(0, 0, 4, 0, Math.PI * 2, 0, false)
const holePath = new THREE.Path()
holePath.absarc(0, 0, 3.5, 0, Math.PI * 2, true)
arcShape.holes.push(holePath)
const geo = new THREE.ExtrudeGeometry(arcShape, extrudeSettings)
const mat = new THREE.MeshBasicMaterial({ color: 'khaki' })
const mesh = new THREE.Mesh(geo, mat)
geo.translate(0, 0, -1) // somehow this has an offset as well :/
mesh.rotateX(Math.PI / 2)
mesh.position.y = 15
scene.add(mesh)
physics.add.existing(mesh, { shape: 'hacd' })
// ------------------------
const clock = new THREE.Clock()
const animate = () => {
physics.update(clock.getDelta() * 1000)
physics.updateDebugger()
renderer.render(scene, camera)
requestAnimationFrame(animate)
}
requestAnimationFrame(animate)
}
PhysicsLoader('/lib/ammo/moz', () => MainScene())
console.log(`three.js version "${THREE.REVISION}"`)
</script>
</body>
</html>