【问题标题】:copy THREE.js matrix from object从对象复制 THREE.js 矩阵
【发布时间】:2016-03-28 12:14:03
【问题描述】:

我需要在 Three.js 中多次使用相同的矩阵。我有一个 Object3D,并且在执行此操作时我得到了 console.log 的正确矩阵:

  console.log (scene.getObjectByName( "Pointer" ).matrix)

结果是这样的:

T…E.Matrix4 {元素:Float32Array[16]} 元素: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(...).. 工作正常!

【问题讨论】:

  • 你在对象名称上吗?指针首先调用 updateMatrix 我看到这是容器对象,如 THREE.Object3D
  • 我尝试了 object ubdateMatrix()... 但这没有帮助。是的,Pointer 是一个 containerObject..我放在 loadedJsonObject 的地方
  • child_clone.applyMatrix(container[i].object3d.children[j].parent.matrix); // 这是不好的 child_clone.applyMatrix(container[i].object3d.children[j].parent.parent.matrix); child_clone.applyMatrix(container[i].object3d.children[j].parent.parent.parent.matrix); child_clone.applyMatrix(container[i].object3d.children[j].parent.parent.parent.parent.matrix); child_clone.updateMatrixWorld() 道德是将矩阵从父容器应用到您的子容器,然后 updateMatrixWorld (实际上我的矩阵根本没有更新,原因是我正在创建自己的对象而不是 three.js Object3D 容器

标签: javascript matrix three.js


【解决方案1】:

three.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);

【讨论】:

  • 请勿设置matrixAutoUpdate = false,除非您熟悉库的内部工作并了解这样做的后果。
猜你喜欢
  • 2015-12-01
  • 2013-01-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多