如何在Three.js中使用由ObjLoader加载的对象的顶点

时间:2018-10-23 16:51:46

标签: javascript three.js objloader

我有一个对象由ObjLoader加载到场景中。但是然后我需要阅读该对象的顶点。但是我仍然找不到解决方法。在我使用Three.js制作的普通多维数据集上,我可以毫无问题地读取和更改顶点,但是在由ObjLoader加载的对象上则不起作用。我还尝试使用FaceNormalsHelper,它也可以在多维数据集上使用,但不能在我的对象上使用。

这是我的代码:

    //My cube this works
    var geometry = new THREE.BoxGeometry(20, 20, 20);
    var color = new THREE.Color("rgba(188, 141, 190, 1)");
    var material = new THREE.MeshNormalMaterial({color: color.getHex()});
    var cube = new THREE.Mesh(geometry, material);

    var helpeer = new THREE.FaceNormalsHelper( cube, 20, 0x00ff00, 1 );
    var vert = cube.geometry.vertices[1].y;

    scene.add( cube );
    scene.add( helpeer );

    //Loading my object 
    var manager = new THREE.LoadingManager();
    manager.onProgress = function ( item, loaded, total ) {
        console.log( item, loaded, total );
    };
    var texture = new THREE.Texture();
    var onProgress = function ( xhr ) {
        if ( xhr.lengthComputable ) {
            var percentComplete = xhr.loaded / xhr.total * 100;
            console.log( Math.round(percentComplete, 2) + '% downloaded' );
        }
    };
    var onError = function ( xhr ) {
    };
    var loader = new THREE.ImageLoader( manager );
    loader.load( 'objects/textura.jpg', function ( image ) {
        texture.image = image;
        texture.needsUpdate = true;
    } );

    var loader = new THREE.OBJLoader( manager );
    loader.load( 'objects/scan_leg_repair.obj', function ( object ) {
        object.traverse( function ( child ) {
            if ( child instanceof THREE.Mesh ) {
                child.material.map = texture;
            }
        } );

        object.position.y = - 100;
        object.rotation.x = -1.5;
        object.rotation.z = 3;

        scene.add( object );

        //That is what i tried. Without success :(
        //var vert2 = object.geometry.vertices[1].y;
        //var vert3 = object.children[0].geometry.vertices[1].y;
        //var helper = new THREE.FaceNormalsHelper( object, 20, 0x00ff00, 1 );
        //var helper = new THREE.FaceNormalsHelper( object.children[0], 20, 0x00ff00, 1 );
        //scene.add( helper );

    }, onProgress, onError );

非常感谢

1 个答案:

答案 0 :(得分:1)

OBJLoader创建对象为THREE.group。 因此,您可以通过以下方式访问顶点。

object.children[0].geometry.attributes.position