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

前端 未结 3 2033
无人共我
无人共我 2020-12-25 12:19

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

I\'ve tried:

// mesh is a THREE.Mesh
scene is a THREE.Scene
scene.add(mesh)
scene.update         


        
相关标签:
3条回答
  • 2020-12-25 12:30

    I prefer to use Vector3 to set position.

       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);
    
    0 讨论(0)
  • 2020-12-25 12:38

    i would recommend you to check the documenation over here: http://threejs.org/docs/#Reference/Objects/Mesh As 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.

    So try to do the following:

    //scene is a THREE.Scene
    scene.add(mesh);
    mesh.position.set(100, 100, 100);
    
    0 讨论(0)
  • 2020-12-25 12:50

    i saw it on a github earlier. (three.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

    detailed explanation is below:

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

    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
    
    0 讨论(0)
提交回复
热议问题