Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在three.js中将网格添加到场景之前,如何设置网格的位置_Javascript_Three.js - Fatal编程技术网

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

Javascript 在three.js中将网格添加到场景之前,如何设置网格的位置,javascript,three.js,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() 但这并没有影响任何事情 我该怎么办?我建议您查看这里的文档: 正如您在docu

在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()
但这并没有影响任何事情


我该怎么办?

我建议您查看这里的文档: 正如您在docu页面顶部看到的,网格继承自“Object3D”。这意味着您可以使用Object3D提供的所有方法或属性。因此,单击docu页面上的“Object3D”链接并检查属性列表。您将找到property“.position”。单击“.position”查看它是什么数据类型。帕哈..它的矢量

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

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

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

而且可以为个人做

mesh.position.setX(200);  
mesh.position.setZ(200); 
参考:

详细解释如下:

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

我更喜欢使用矢量3来设置位置

   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);

如果有人想从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


这对我有效,而chysmann的答案不起作用,尽管我无法解释为什么它不起作用。你能详细解释一下“在github上看到”是什么意思吗?我在github three.js页面上看到过。但是我现在找不到来源这是正确的答案。使用mesh.position.set(x,y,z),因为不需要强制更新,如果设置mesh.position=xxx,则仅更改对象属性,而不总是实时移动场景中的对象。position.set更为复杂,总是将对象移动到目标并刷新缓冲区、灯光、阴影等。。。马上回答。这是正确答案。从R69(Google CDN提供的最新发行版)开始,尝试将值分配给
网格。不应执行位置
,因为它是
只读
,从而导致
未捕获类型错误:无法分配给[对象]
的只读属性“位置”。这是正确答案。使用mesh.position.set(x,y,z),因为不需要强制更新,如果设置mesh.position=xxx,则仅更改对象属性,而不总是实时移动场景中的对象。position.set更为复杂,总是将对象移动到目标并刷新缓冲区、灯光、阴影等。。。有一次,我试过这个,但不知道它到底在做什么。如果我画一个点,它在正确的地方。但是如果我创建一个网格,并尝试设置位置,它只会出现在一些明显随机的地方。在我的例子中:mesh.position不会更改位置。但是Martin提到的mesh.position.set(x,y,z)非常有效。对于复杂网格,
position
指的是什么?在
Vector3
类定义中定义了某种“中心”
set
。为了找到它,你必须像这样导航:Mesh>Object3d>Object3d.position>Vector3>Vector3.set-幸运的是,作者还提供了一个工作代码的示例。你在哪里使用
position
?在Vector10,10,10中,这个示例将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)