在 Three.js 中动态更改网格后进行光线投射
Raycasting after dynamically changing Mesh in Three.js
我有一个从另一个网格克隆的网格。但是在克隆之后,我平移并旋转了它。并对其进行光线投射,但它没有按预期工作。它与平移和旋转之前的原始位置保持相交。示例代码如下
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
很高兴有任何帮助,谢谢!
变换后从网格调用updateMatrixWorld() 将解决问题。感谢@prisoner849
proposedModel.updateMatrixWorld()
原因是
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().
在此处查看详细信息https://threejs.org/docs/#manual/introduction/Matrix-transformations
我有一个从另一个网格克隆的网格。但是在克隆之后,我平移并旋转了它。并对其进行光线投射,但它没有按预期工作。它与平移和旋转之前的原始位置保持相交。示例代码如下
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
很高兴有任何帮助,谢谢!
变换后从网格调用updateMatrixWorld() 将解决问题。感谢@prisoner849
proposedModel.updateMatrixWorld()
原因是
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().
在此处查看详细信息https://threejs.org/docs/#manual/introduction/Matrix-transformations