Three.js中动态改变Mesh后的Raycasting [英] Raycasting after dynamically changing Mesh in Three.js
问题描述
我有一个从另一个网格克隆的网格.但克隆后,我翻译并旋转它.并对其进行光线投射,但它没有按预期工作.它在平移和旋转之前与原始位置保持相交.示例代码如下
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!
推荐答案
在变换后的网格中调用 updateMatrixWorld() 即可解决问题.归功于@prisoner849
Call updateMatrixWorld() from the mesh after transformation will solve the problem. Credit to @prisoner849
proposedModel.updateMatrixWorld()
原因是
一个对象的矩阵存储了对象相对于对象的父级;获取对象在世界中的变换坐标,您必须访问对象的 Object3D.matrixWorld.
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.
当父对象或子对象的变换发生变化时,你可以请求子对象的 matrixWorld 被更新调用 updateMatrixWorld().
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/介绍/矩阵变换
这篇关于Three.js中动态改变Mesh后的Raycasting的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!