javascript - Three.js 中动态更改网格后的光线转换

标签 javascript three.js raycasting

我有一个从另一个网格克隆的网格。但克隆后,我对其进行平移和旋转。并对它进行光线转换,但它没有按预期工作。在平移和旋转之前保持与原始位置相交。示例代码如下

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

提议的Model.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

关于javascript - Three.js 中动态更改网格后的光线转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48662643/

相关文章:

javascript - 循环对象数组以返回正确的结果

google-chrome - 在 Chrome 但在 Android 浏览器中创建 WebGL 上下文时出错

c# - Unity 上的虚线弹跳

c# - Unity Physics2D.Raycast 命中自身

javascript - 为什么我的 reducer 在 react/redux 中返回一个空数组?

javascript - 禁用浏览器的重排\重画

javascript - 将外部数据加载到SproutCore应用程序中

three.js - 可以反向播放皮肤动画吗? [三.js]

javascript - 三.js 触发旋转的MouseUp事件

javascript - 如何使用 three.js 'wrap' 球体上的平面?