我需要在 Three.js 中对相同的矩阵进行乘法运算。我有一个 Object3D,在执行此操作时我得到了 console.log 的正确矩阵:
console.log (scene.getObjectByName( "Pointer" ).matrix)
结果是这样的:
T…E.Matrix4 {elements: Float32Array[16]} elements: Float32Array[16] 0: 11: 02: 03: 04: 05: 16: 07: 08: 09: 0 10: 11 1: 0 12: -150 13: 0 14: 0 15: 1
请注意,第 12 个元素的值为 -150(在 obj.translationX(-150) 之后)。
var newMat = new THREE.Matrix4();
console.log(scene.getObjectByName("Pointer").matrix.elements)
// output: [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]
newMat = newMat.copy(scene.getObjectByName("Pointer").matrix);
console.log(newMat);
// output:elements: Float32Array[16] 1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1
返回一个单位矩阵(意味着第 12 个元素是:0)
这里出了什么问题?
更新:在渲染循环内... newMat.copy(...).. 工作正常!
最佳答案
two.js 在渲染页面时会根据对象位置、缩放、旋转来更新对象矩阵。所以当你设置对象矩阵时,它很快就会被重写。 要手动设置对象矩阵,您必须将 autoupdate 设置为 false。
object.matrixAutoUpdate = false;
然后使用您的代码。
var newMat = new THREE.Matrix4();
console.log(scene.getObjectByName("Pointer").matrix.elements)
// output: [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]
newMat = newMat.copy(scene.getObjectByName("Pointer").matrix);
console.log(newMat);
关于javascript - 从对象复制 THREE.js 矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36262075/