1

I am using objloader to load multiple objects. I am trying to move one of the objects and need to have the updated vertex positions. while loading the objects I converted the buffergeometry to geometry and run some functions. I checked some samples all updating the vertices of the buffergeometry. Do I need to convert it back to buffergeometry or not ? I need to have the real time positions while moving to calculate some other functions, so I prefer not to keep on converting from buffer to geometry and vice versa.

Here is a piece of code:

          var tool= new THREE.OBJLoader();
          tool.load( '../obj/tool.obj', function ( object ) {
          var material = new THREE.MeshLambertMaterial({color:0xA0A0A0});             
          object.traverse( function ( child ) {
          if ( child instanceof THREE.Mesh ) {
               child.material = material;
               Geometry = new THREE.Geometry().fromBufferGeometry(child.geometry);
              }

          console.log(Geometry.vertices[220]);
          Geometry.position.x += 0.01;
          Geometry.verticesNeedUpdate = true;
          console.log(Geometry.vertices[220]);

Besides, I checked the migration document of the latest versions and checked them out.

Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
Arezoo Tony
  • 69
  • 1
  • 8

1 Answers1

1

OBJLoader returns BufferGeometry. You can update a vertex position like so:

geometry.attributes.position.setX( index, x );

geometry.attributes.position.setXYZ( index, x, y, z ); // alternate

geometry.attributes.position.needsUpdate = true; // only required if geometry previously-rendered

Study http://threejs.org/docs/#Reference/Core/BufferAttribute


Instead, you can convert to Geometry. In your case, do the following in the loader callback:

child.geometry = new THREE.Geometry().fromBufferGeometry( child.geometry );

You then update a vertex position using this pattern:

geometry.vertices[ 0 ].set( x, y, z );
geometry.verticesNeedUpdate = true;

Only set the needsUpdate flag if the geometry has been previously rendered.

three.js r.71

WestLangley
  • 102,557
  • 10
  • 276
  • 276
  • Well , thanks for the reply but the problem is that I converted from buffergeometry to geometry first to have access to the vertices and other attributes, Rendered the object and I want to move it by mouse movement next and get the real time vertices positions of the object. in this way I need the needUpdate . So I do not know how to have the updated positions while rendering in " Function render () " . – Arezoo Tony Aug 06 '15 at 17:56
  • 1. I answered the question that you asked in your post. 2. I explained in my answer how to properly replace the child geometry in the loader callback. 3. If you have another issue -- such as how to update the positions in the render loop -- then please make a new post. – WestLangley Aug 06 '15 at 18:14
  • [This post](http://stackoverflow.com/a/31411794/1461008) shows how update `BufferGeometry` in the render loop. – WestLangley Aug 06 '15 at 18:17
  • Thanks for your help. The topic of the question is even Geometry vertex position updates. As mentioned before, I converted the buffergeometry to geometry to access the vertices and for the rendering and moving the object I need to have the updates of the Geometry vertices positions and I need to know how to have updated positions.The example and your previous reply is based on buffergeometry. Thanks again. – Arezoo Tony Aug 07 '15 at 18:33
  • Thanks for the update but still it is not working, so can you please explain what does this code do exactly ? geometry.vertices[ 0 ].set( x, y, z ); – Arezoo Tony Aug 19 '15 at 17:35
  • You need to learn how to answer that question yourself. Are you using the non-minified version of three.js library "three.js"? For development, you should be. Do you know how to step through the code with a debugger? Do you know how to read the errors in the console window? – WestLangley Aug 19 '15 at 19:04
  • Well, Actually I am using the three.min.js , and I am reading the errors that it says X is undefined. Actually, I want to get all the updated vertices positions while moving the object while rendering. – Arezoo Tony Aug 19 '15 at 19:10
  • Do not use three.min.js for development... Use the latest version of three.js. If you are really stuck, then make a new post and provide a live link to your _simple_ example -- not your entire project. – WestLangley Aug 19 '15 at 19:20