【问题标题】:Javascript object property not updating on variable setJavascript对象属性未在变量集上更新
【发布时间】:2017-09-11 17:52:49
【问题描述】:

我正在使用 Three.js 四元数,我无法为四元数保存我的对象属性。

GLmol.prototype.initializeScene = function() {
   this.scene = new THREE.Scene();
   this.scene.fog = new THREE.Fog(this.bgColor, 100, 200);

   this.modelGroup = new THREE.Object3D();
   this.rotationGroup = new THREE.Object3D();
   this.rotationGroup.useQuaternion = true;

   this.rotationGroup.quaternion = new THREE.Quaternion(0.7235552851867599, -0.004228243257681183 , 0.004646778667168487, 0.6902378421133389);
   console.log(this.rotationGroup.quaternion)

   this.rotationGroup.add(this.modelGroup);

   this.scene.add(this.rotationGroup);
   this.setupLights(this.scene);
};

我遇到的问题是,当我设置 this.rotationGroup.quaternion = new THREE.Quaternion() 时,它不会保存 Quaternion 对象。当我查看输出时,我得到了

THREE.Quaternion (w:1 x:0 y:0 z:0)

你认为为什么会这样?

【问题讨论】:

  • 你调用initializeScene()函数了吗?
  • 是的,之所以调用它是因为我从 console.log(this.rotationGroup.quaternion) 获得了输出,但是它不是之前在该行中设置的值。
  • 另外,如果我创建一个名为 var test = new THREE.Quaternion(3, 3, 3, 3) 和 console.log(test) 的变量,它可以正常工作。

标签: javascript three.js


【解决方案1】:

Object3D.quaternion 属性是不可变的,所以这段代码无效:

rotationGroup.quaternion = new THREE.Quaternion( x, y, z, w );

改为使用.quaternion.copy( q ).quaternion.set( x, y, z, w )

另见this answer

three.js r.84

【讨论】:

  • 谢谢,我用了set,​​一切正常。我正在修改代码,他们尝试了我所做的方式,但是,它实际上是在代码的前面设置的,而我正在复制的行什么也没做。谢谢。
猜你喜欢
  • 2012-03-16
  • 2016-07-13
  • 1970-01-01
  • 2021-08-12
  • 2019-05-02
  • 2020-01-19
  • 1970-01-01
  • 2016-12-19
  • 1970-01-01
相关资源
最近更新 更多