/**
* 把一个对象移动到另一个父容器,并应用变换
* @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)
}