问题
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.
回答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
来源:https://stackoverflow.com/questions/31859819/how-to-update-the-geometry-vertex-position-objloader