在Three.js中动态更改Mesh后的光线投射 [英] Raycasting after dynamically changing Mesh in Three.js

查看:737
本文介绍了在Three.js中动态更改Mesh后的光线投射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从另一个网格克隆的网格。但克隆后,我翻译并旋转它。然后做一个光线投射点,但它没有按预期工作。它在平移和旋转之前与原始位置保持交叉。示例代码如下

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()

原因是


对象的矩阵存储对象相对于
对象父对象的转换;要在world
坐标中获取对象的转换,您必须访问对象的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.

当父对象或子对象的转换发生变化时,
你可以请求
调用updateMatrixWorld()更新子对象的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

这篇关于在Three.js中动态更改Mesh后的光线投射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆