Cannon-es creates trimesh for externally loaded GLB file, but the trimesh does not scale down when the glb file is scaled down

I am using the gltf loader from threejs to load a glb file on to the scene. I traverse the children of this model to create trimesh from the convex hull of each of these geometries. I am following this:
example. However, when I scale the glb file, the trimesh does not scale.

                const roomModel = await loadGltf('/assets/models/city.glb');
                sceneRef.current.add(roomModel);
                roomModel.rotation.y = Math.PI;                
                roomModel.scale.x = 0.005;
                roomModel.scale.y = 0.005;
                roomModel.scale.z = 0.005;
                
                  (roomModel as any).traverse((node) => {
                    if (node.isMesh) {
                      console.log("node", node);
                      // node.scale.set(0.005, 0.005, 0.005);
                      node.updateMatrixWorld();
                      node.geometry.attributes.position.needsUpdate = true;
                      const box = new THREE.Box3().setFromObject(node);
                      const size = new THREE.Vector3();
                      box.getSize(size);
                      const c = new THREE.Vector3();
                      node.getWorldPosition(c);
                      const nodeQuaternion = new THREE.Quaternion();
                      node.getWorldQuaternion(nodeQuaternion);
                      const meshPositions = node.geometry.getAttribute("position").array;
                      
                      const points = [];
                      for (let i = 0; i < meshPositions.length; i += 3) {
                        points.push(new THREE.Vector3(meshPositions[i], meshPositions[i + 1], meshPositions[i + 2]))
                      }
                      
                      const geometry = new ConvexGeometry( points );
                      const shape = CannonUtils.CreateTrimesh(geometry);
                      
                      const body = new Cannon.Body({ mass: 0 })
                      // body.allowSleep = true
                      body.addShape(shape)
                      body.position.x = c.x
                      body.position.y = c.y
                      body.position.z = c.z
                      body.quaternion.x = nodeQuaternion.x
                      body.quaternion.y = nodeQuaternion.y
                      body.quaternion.z = nodeQuaternion.z
                      body.quaternion.w = nodeQuaternion.w
  
                      triRef.current.push(shape);
                      if (physicsWorld.current) {
                        physicsWorld.current.addBody(body);
                      }
                    }
                  })

Have been struggling with this for a while now. Any help will be appreciated. I have tried scaling the local scale of each of the trimeshes. But in that case, the scale applied on the glb model and the scale applied on each of the trimeshes are not in sync

Scaling doesn’t scale the geometry.

When you copy out the points, you also need to scale them by the same amount when creating the vector

2 Likes

also If you want to capture all the nodes transformations, (position/rotation/scale) you can use localToWorld like this:

points.push(node.localToWorld(new THREE.Vector3(meshPositions[i], meshPositions[i + 1], meshPositions[i + 2]))