Raycasting after dynamically changing Mesh in Three.js

北慕城南 提交于 2020-01-14 05:37:13

问题


I have a mesh cloned from another mesh. But after cloning, I translate and rotate it. And do a ray-casting a point to it but it is not working as expected. It is keep intersecting with the original position before translation and rotation. Sample code is as below

const raycaster = THREE.Raycaster()
const proposedModel = model.clone()
proposedModel.translateX(1)
proposedModel.translateY(1)
proposedModel.translateZ(1)
const q = new THREE.Quaternion(
    -0.847,
    -0.002,
    -0.505,
     0.168
)
proposedModel.applyQuaternion(q)

const point = new THREE.Vector3(1,1,1)
raycaster.set(point, new THREE.Vector3(1,1,1))
const intersects = raycaster.intersectObject(object) // It keep intersecting with original position

Glad if any help, Thanks!


回答1:


Call updateMatrixWorld() from the mesh after transformation will solve the problem. Credit to @prisoner849

proposedModel.updateMatrixWorld()

The reason is

An object's matrix stores the object's transformation relative to the object's parent; to get the object's transformation in world coordinates, you must access the object's Object3D.matrixWorld.

When either the parent or the child object's transformation changes, you can request that the child object's matrixWorld be updated by calling updateMatrixWorld().

Check detail here https://threejs.org/docs/#manual/introduction/Matrix-transformations



来源:https://stackoverflow.com/questions/48662643/raycasting-after-dynamically-changing-mesh-in-three-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!