问题描述
我使用objloader加载多个对象。我正在尝试移动其中一个对象,并需要更新顶点位置。加载对象时,我将缓冲几何转换为几何并运行一些函数。我检查了所有更新缓冲区几何顶点的样本。我是否需要将其转换回缓冲区?
我需要在移动时计算一些其他函数的实时位置,所以我不想继续从缓冲区转换到几何,反之亦然。
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.
推荐答案
OBJLoader
returns BufferGeometry
。您可以像这样更新顶点位置:
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
研究
相反,您可以转换为几何
。在你的情况下,在加载器回调中执行以下操作:
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;
如果几何图形只设置 needsUpdate
已经被提交。
Only set the needsUpdate
flag if the geometry has been previously rendered.
three.js r.71
three.js r.71
这篇关于如何更新几何顶点位置Objloader的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!