在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()
但它没有影响任何事情。
我该怎么办?
答案 0 :(得分:65)
我建议你查看这里的文件: http://threejs.org/docs/#Reference/Objects/Mesh 正如您在文档页面顶部所看到的,Mesh继承自“ Object3D ”。这意味着您可以使用Object3D提供的所有方法或属性。因此,单击docu-page上的“ Object3D ”链接并检查属性列表。你会发现属性“ .position ”。单击“ .position ”以查看它是什么数据类型。 Paha..its Vector3 。
因此,请尝试执行以下操作:
//scene is a THREE.Scene
scene.add(mesh);
mesh.position.set(100, 100, 100);
答案 1 :(得分:26)
我之前在github上看过它。 (three.js r71)
mesh.position.set(100, 100, 100);
可以为个人完成
mesh.position.setX(200);
mesh.position.setZ(200);
参考: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
答案 2 :(得分:0)
我更喜欢使用 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);
答案 3 :(得分:0)
如果有人正在寻找从 Vector3 更新位置的方法
const V3 = new THREE.Vector3(0,0,0) // Create variable in zero position
const box = new THREE.Mesh(geometry, material) // Create an object
Object.assign(box.position, V3) // Put the object in zero position
或
const V3 = new THREE.Vector3(0,0,0) // Create variable in zero position
const box = new THREE.Mesh(geometry, material) // Create an object
box.position.copy(V3)