Three.js-移动对象到新的父容器并应用变换
/** * 把一个对象移动到另一个父容器,并应用变换 * @param mesh 移动的对象 * @param newParent 要移动到的父容器 */ const moveMesh = (mesh: THREE.Object3D, newParent: THREE.Object3D) => { // 1. 获取世界矩阵 mesh.updateWorldMatrix(true, true) const worldMatrix = mesh.matrixWorld.clone() // 2. 解除父级关系 mesh.parent?.remove(mesh) // 3. 添加到新父级 newParent.add(mesh) // 4. 更新新父级矩阵 newParent.updateWorldMatrix(true, true) // 5. 计算相对矩阵(核心) const inverseParentMatrix = new THREE.Matrix4().copy(newParent.matrixWorld).invert() goods.matrix.multiplyMatrices(inverseParentMatrix, worldMatrix) goods.matrix.decompose(goods.position, goods.quaternion, goods.scale) }
鸑鷟虽孤,不匹鹜雏。

浙公网安备 33010602011771号