THREE.js-BufferGeometry(ObjLoader)的顶点坐标

时间:2018-07-06 18:34:57

标签: three.js vertices buffer-geometry objloader

我是Three.js的新手,在获取2个顶点的坐标时遇到问题。我需要他们的x,y,z坐标来计算和绘制尺寸。

我使用了ObjLoader,并且我确切知道了顶点的ID(索引)-11889和11877。

我尝试使用BufferGeometry的某些属性,但是显然是错误的。这是我要使用的简单代码。

                var coord1 = [11889,0,0,0], coord2 = [11877,0,0,0];

            var mtlLoader = new THREE.MTLLoader();      
            mtlLoader.load(
                'bath1.mtl',
                function( materials ) 
                {
                    materials.preload();
                    oLoader.setMaterials( materials );
                    oLoader.load('bath1.obj', function ( bath ) 
                    {
                        bath.name="bath1";
                        bath.position.y -=1; bath.position.x -=0.25; bath.position.z -=1;
                        scene.add( bath ); 
                    },
                    function ( xhr ) {},
                    function ( error ) {console.log( 'An error happened' );}
                    );

                }
            );

我阅读了一些类似的问题,但是我无法根据自己的问题改编答案。我应该怎么做才能获得坐标?谢谢!

1 个答案:

答案 0 :(得分:0)

作为一种选择,您可以使用THREE.Vector3()的{​​{3}}方法,如下所示:

var geo = new THREE.PlaneBufferGeometry(5, 5);
var bath = new THREE.Mesh(geo, new THREE.MeshBasicMaterial());

var vert1 = new THREE.Vector3().fromArray(bath.geometry.attributes.position.array, 1 * 3);
var vert2 = new THREE.Vector3().fromArray(bath.geometry.attributes.position.array, 2 * 3);

console.log(vert1, vert2);
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/94/three.min.js"></script>

另一种方法是使用THREE.BufferAttribute()的{​​{3}}方法:

var geom = new THREE.PlaneBufferGeometry(5, 5);
var bath = new THREE.Mesh(geom, new THREE.MeshBasicMaterial());

var position = bath.geometry.attributes.position;
var x = position.getX(1);
var y = position.getY(1);
var z = position.getZ(1);

console.log(x, y, z);
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/94/three.min.js"></script>