在将网格添加到three.js中的场景之前,如何设置网格的位置

use*_*292 27 javascript three.js

在three.js中,我想将一个网格添加到场景中的一个位置

我试过了:

// mesh is a THREE.Mesh
scene is a THREE.Scene
scene.add(mesh)
scene.updateMatrixWorld(true)
mesh.matrixWorld.setPosition(new THREE.Vector3(100, 100, 100))
scene.updateMatrix()
Run Code Online (Sandbox Code Playgroud)

但它没有影响任何事情.

我该怎么办 ?

chs*_*ann 74

我建议你查看这里的文档:http://threejs.org/docs/#Reference/Objects/Mesh 正如你在文档页面顶部看到的,Mesh继承自" Object3D ".这意味着您可以使用Object3D提供的所有方法或属性.因此,单击docu -page 上的" Object3D "链接并检查属性列表.你会发现属性" .position ".单击" .position "以查看它是什么数据类型.Paha .. Vector3.

因此,请尝试执行以下操作:

//scene is a THREE.Scene
scene.add(mesh);
mesh.position.set(100, 100, 100);
Run Code Online (Sandbox Code Playgroud)

  • 你不仅提供答案.您还教授如何使用文档! (16认同)
  • 这是正确的答案.使用mesh.position.set(x,y,z)因为你不需要强制更新,如果你设置mesh.position = xxx,你只需要改变对象属性,而不是总是在场景中实时移动对象.position.set更加复杂,总是将对象移动到目标并刷新缓冲区,灯光,阴影等......一次. (4认同)
  • 就我而言:mesh.position不会更改位置。但是马丁提到的mesh.position.set(x,y,z)可以完美地工作。 (2认同)

bh_*_*th0 28

我之前在github上看过它.(three.js r71)

mesh.position.set(100, 100, 100);
Run Code Online (Sandbox Code Playgroud)

并且可以为个人完成

mesh.position.setX(200);  
mesh.position.setZ(200); 
Run Code Online (Sandbox Code Playgroud)

参考:https://threejs.org/docs/#api/math/Vector3

详细说明如下:

因为mesh.position是"Vector3".Vector3()有setX()setY()和setZ()方法.我们可以像这样使用它.

mesh.position = new THREE.Vector3() ; //see position is Vector3()
vector1 = new THREE.Vector3();   

mesh.position.setX(100);  //or  this
vector1.setX(100)         // because all of them is Vector3()
camera1.position.setZ(100); // or this
light1.position.setY(100)   // applicable to any object.position
Run Code Online (Sandbox Code Playgroud)

  • 这是正确的答案.使用mesh.position.set(x,y,z)因为你不需要强制更新,如果你设置mesh.position = xxx,你只需要改变对象属性,而不是总是在场景中实时移动对象.position.set更加复杂,总是将对象移动到目标并刷新缓冲区,灯光,阴影等......一次. (3认同)

Hit*_*ahu 7

我更喜欢使用Vector3来设置位置。

   let group = new THREE.Group();

   // position of box
   let vector = new THREE.Vector3(10, 10, 10);
   
     // add wooden Box
   let woodenBox = new THREE.Mesh(boxGeometry, woodMaterial);

    //update postion
    woodenBox.position.copy(vector);

  // add to scene
   group.add(woodenBox)
   this.scene.add(group);
Run Code Online (Sandbox Code Playgroud)