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

52 Views Asked by At

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 tutorial. However, when I scale the glb file, the trimesh does not scale.

Some code that i have is as follows:

        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

0

There are 0 best solutions below