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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14223249/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 16:05:13  来源:igfitidea点击:

How can I set the position of a mesh before I add it to the scene in three.js

javascriptthree.js

提问by user1783292

In three.js, I want to add a mesh to a position in the scene

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

I've tried:

我试过了:

// 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()

BUT it didn't affect anything.

但它没有影响任何东西。

What should I do ?

我该怎么办 ?

回答by chsymann

i would recommend you to check the documenation over here: http://threejs.org/docs/#Reference/Objects/MeshAs you can see on the top of the docu-page, Mesh inherits from "Object3D". That means that you can use all methods or properties that are provided by Object3D. So click on the "Object3D" link on the docu-page and check the properties list. You will find propertie ".position". Click on ".position" to see what data-type it is. Paha..its Vector3.

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

So try to do the following:

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

//scene is a THREE.Scene
scene.add(mesh);
mesh.position.set(100, 100, 100);

回答by bh_earth0

i saw it on a github earlier. (three.js r71 )

之前在github上看到过。(三.js r71)

mesh.position.set(100, 100, 100);

and can be done for individuals

并且可以为个人完成

mesh.position.setX(200);  
mesh.position.setZ(200); 

reference: https://threejs.org/docs/#api/math/Vector3

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

detailed explanation is below:

详细解释如下:

since mesh.position is "Vector3". Vector3() has setX() setY() and setZ() methods. we can use it like this.

因为 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

回答by Hitesh Sahu

I prefer to use Vector3to set position.

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

   let group = new THREE.Group();

   // position of box
   let position = 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);