【问题标题】:THREE.js Migration r60 to r70: now cannot write: mesh.position = myVector3()THREE.js 迁移 r60 到 r70:现在无法写入:mesh.position = myVector3()
【发布时间】:2015-05-01 12:13:58
【问题描述】:

我正在将一个 THREE.js 应用程序从 r60 迁移到 r70。在其他更改中,我注意到以下形式的 r60 构造不再适用于 r70。

mesh.position.set(0,0,0);
myVector3 = new THREE.Vector3 (100,200,300);
mesh.position = myVector3;  

这适用于网格、pointLights,大概适用于所有 Object3D,但我没有进一步测试。

在上面的示例中,mesh.position x,y,z 值在 (0,0,0) 处保持不变。有关说明,请参见 this JSFiddle 并比较第 70 行和第 73 行。

    //...The next line DOES NOT update Sphere_mesh.position.x
    Sphere_mesh.position = NewPos_vector3;//...

    //...The next line DOES update Sphere_mesh.position.x
    Sphere_mesh.position.x = NewPos_vector3.x

在调试器中,在执行过程中不会发出控制台警告,指出分配没有起作用。在非常简短的 THREE.js migration notes for (r68 --> r69) 中,我看到 Object3D 的位置不再是不可变的,但我不知道这意味着什么。

无论如何,我的问题

是否有一个标准的三个构造或函数可以用来将 x,y,z 值从 Vector3 对象复制到 mesh.position 的 x,y,z 值,而不是有效但冗长的形式,例如

mesh.position.x = myVector3.x;
mesh.position.y = myVector3.y;
mesh.position.z = myVector3.z; ?

例如诸如

之类的东西
mesh.position = F_Get_Object3DPosition_from_Vector3(myVector3);   ?

我知道编写自己的函数很容易,但标准的 THREE 函数更有可能随着 THREE 的未来版本顺利发展。

【问题讨论】:

标签: three.js position migration mesh


【解决方案1】:

position beeeing immutable 意味着 position 属性不能改变。

所以

mesh.position = anything;

不会工作(但你已经发现了)

你可以做的不是改变位置,而是你必须改变位置值。

在你的情况下,最简单的方法是

mesh.position.copy (myVector3);

【讨论】:

  • 谢谢。 “.copy”方法正是我想要的,尽管 IMO 如果名称是“.copyValues”会更直观。
【解决方案2】:

我认为你的意思是 myVector3 而不是 myVector3() 在最后一行......无论如何,我虽然这也可以,但问题是,你正在将一个向量应用于应该是一个点/顶点的东西。即使这在我看来有效,但这也不是正确的方法。用一个简单的数组怎么样:

mesh.position.set(0,0,0);
new_position = [100,200,300]
mesh.position.fromArray(new_position,0)

其中 0 是起始索引。所以你可以在一个数组中有多个位置集

【讨论】:

  • 是的,谢谢你是对的,它应该是第三行代码中的vector3。我以前不知道 .fromArray 方法。有趣的。实际上我看到 Object3d.position 是一个 Vector3 对象(根据threejs.org/docs/#Reference/Core/Object3D 的文档)。在我的情况下,我已经将点存储为 Vector3 对象,因此 .fromArray 方法在这种情况下不适合。
猜你喜欢
  • 2015-05-06
  • 2019-02-09
  • 2019-11-08
  • 2019-06-27
  • 1970-01-01
  • 2017-06-20
  • 2019-07-08
  • 2018-02-20
  • 2012-05-01
相关资源
最近更新 更多