Skip to content

Convex Geometry to CANNON.Trimesh

Description

Demonstrates using the ConvexGeometry class to create a simplified object shape that can be used in Cannon physics.

The example,

  • loads a model, OBJ in this case.
  • Takes the geometry from the model, converts it to and array of THREE.Vector3 and then creates a convex hull mesh from it.
  • The Convex Hull is then converted into a CANNON.Trimesh and added to the CANNON.World.
<>

Code

./src/client/client.ts

  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
import * as THREE from 'three'
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls'
import { OBJLoader } from 'three/examples/jsm/loaders/OBJLoader'
import Stats from 'three/examples/jsm/libs/stats.module'
import { ConvexGeometry } from 'three/examples/jsm/geometries/ConvexGeometry'
import * as CANNON from 'cannon-es'
import CannonUtils from './utils/cannonUtils'

const scene = new THREE.Scene()

const light1 = new THREE.SpotLight(0xffffff, 100)
light1.position.set(2.5, 5, 5)
light1.angle = Math.PI / 4
light1.penumbra = 0.5
light1.castShadow = true
light1.shadow.mapSize.width = 2048
light1.shadow.mapSize.height = 2048
light1.shadow.camera.near = 0.5
light1.shadow.camera.far = 20
scene.add(light1)

const light = new THREE.SpotLight(0xffffff, 100)
light.position.set(-2.5, 5, 5)
light.angle = Math.PI / 4
light.penumbra = 0.5
light.castShadow = true
light.shadow.mapSize.width = 2048
light.shadow.mapSize.height = 2048
light.shadow.camera.near = 0.5
light.shadow.camera.far = 20
scene.add(light)

const camera = new THREE.PerspectiveCamera(
    85,
    window.innerWidth / window.innerHeight,
    0.1,
    1000
)
camera.position.set(-0.9, 0.5, 2)

const renderer = new THREE.WebGLRenderer({ antialias: true })
renderer.setSize(window.innerWidth, window.innerHeight)
renderer.shadowMap.enabled = true
document.body.appendChild(renderer.domElement)

const controls = new OrbitControls(camera, renderer.domElement)
controls.enableDamping = true

const material = new THREE.MeshPhysicalMaterial({})
material.thickness = 3.0
material.roughness = 0.9
material.clearcoat = 0.1
material.clearcoatRoughness = 0
material.transmission = 0.99
material.ior = 1.25
material.envMapIntensity = 25

const texture = new THREE.TextureLoader().load('img/grid.png')
material.map = texture
const envTexture = new THREE.CubeTextureLoader().load(
    [
        'img/px_25.jpg',
        'img/nx_25.jpg',
        'img/py_25.jpg',
        'img/ny_25.jpg',
        'img/pz_25.jpg',
        'img/nz_25.jpg',
    ],
    () => {
        material.envMap = envTexture
    }
)

const world = new CANNON.World()
world.gravity.set(0, -9.82, 0)
world.allowSleep = true

let monkey: THREE.Mesh
let convexHull: THREE.Mesh
let body: CANNON.Body
let sphereMesh: THREE.Mesh
let sphereBody: CANNON.Body

const objLoader = new OBJLoader()
objLoader.load(
    'models/monkey.obj',
    (object) => {
        monkey = object.children[0] as THREE.Mesh
        monkey.material = material
        monkey.position.y = 1.5
        monkey.rotation.x = 0.4
        monkey.rotation.z = -0.4
        monkey.castShadow = true
        scene.add(monkey)

        setTimeout(() => {
            createConvexHull()
        }, 2000)
    },
    (xhr) => {
        console.log((xhr.loaded / xhr.total) * 100 + '% loaded')
    },
    (error) => {
        console.log(error)
    }
)

function createConvexHull() {
    const position = monkey.geometry.attributes.position.array
    const points: THREE.Vector3[] = []
    for (let i = 0; i < position.length; i += 3) {
        points.push(
            new THREE.Vector3(position[i], position[i + 1], position[i + 2])
        )
    }
    const convexGeometry = new ConvexGeometry(points)
    convexHull = new THREE.Mesh(
        convexGeometry,
        new THREE.MeshBasicMaterial({
            color: 0x00ff00,
            wireframe: true,
        })
    )
    monkey.add(convexHull)

    setTimeout(() => {
        addFloor()
    }, 2000)
}

function addFloor() {
    const planeGeometry = new THREE.PlaneGeometry(25, 25)
    const texture = new THREE.TextureLoader().load('img/grid.png')
    const plane: THREE.Mesh = new THREE.Mesh(
        planeGeometry,
        new THREE.MeshPhongMaterial({ map: texture })
    )
    plane.rotateX(-Math.PI / 2)
    plane.position.y = -1
    plane.receiveShadow = true
    scene.add(plane)

    const planeShape = new CANNON.Plane()
    const planeBody = new CANNON.Body({ mass: 0 })
    planeBody.addShape(planeShape)
    planeBody.quaternion.setFromAxisAngle(
        new CANNON.Vec3(1, 0, 0),
        -Math.PI / 2
    )
    planeBody.position.y = plane.position.y
    world.addBody(planeBody)

    const sphereGeometry = new THREE.SphereGeometry(0.5)
    sphereMesh = new THREE.Mesh(sphereGeometry, material)
    sphereMesh.position.x = -0.2
    sphereMesh.position.z = -0.5
    sphereMesh.castShadow = true
    scene.add(sphereMesh)
    const sphereShape = new CANNON.Sphere(0.5)
    sphereBody = new CANNON.Body({ mass: 1 })
    sphereBody.addShape(sphereShape)
    sphereBody.position.x = sphereMesh.position.x
    sphereBody.position.y = sphereMesh.position.y
    sphereBody.position.z = sphereMesh.position.z
    world.addBody(sphereBody)

    setTimeout(() => {
        convertConvexHullToTrimesh()
    }, 2000)
}

function convertConvexHullToTrimesh() {
    const shape = CannonUtils.CreateTrimesh(convexHull.geometry)
    body = new CANNON.Body({ mass: 1 })
    body.allowSleep = true
    body.addShape(shape)
    body.position.x = monkey.position.x
    body.position.y = monkey.position.y
    body.position.z = monkey.position.z
    body.quaternion.x = monkey.quaternion.x
    body.quaternion.y = monkey.quaternion.y
    body.quaternion.z = monkey.quaternion.z
    body.quaternion.w = monkey.quaternion.w
    world.addBody(body)
}

window.addEventListener('resize', onWindowResize, false)
function onWindowResize() {
    camera.aspect = window.innerWidth / window.innerHeight
    camera.updateProjectionMatrix()
    renderer.setSize(window.innerWidth, window.innerHeight)
    render()
}

const stats = new Stats()
document.body.appendChild(stats.dom)

const clock = new THREE.Clock()
let delta

function animate() {
    requestAnimationFrame(animate)

    controls.update()
    if (monkey) {
        controls.target.copy(monkey.position)
    }

    delta = Math.min(clock.getDelta(), 0.1)
    world.step(delta)

    if (body) {
        monkey.position.set(body.position.x, body.position.y, body.position.z)
        monkey.quaternion.set(
            body.quaternion.x,
            body.quaternion.y,
            body.quaternion.z,
            body.quaternion.w
        )
    }
    if (sphereBody) {
        sphereMesh.position.set(
            sphereBody.position.x,
            sphereBody.position.y,
            sphereBody.position.z
        )
        sphereMesh.quaternion.set(
            sphereBody.quaternion.x,
            sphereBody.quaternion.y,
            sphereBody.quaternion.z,
            sphereBody.quaternion.w
        )
    }
    render()

    stats.update()
}

function render() {
    renderer.render(scene, camera)
}

animate()

Comments