如何在使用THREE.OBJLoader加载的对象中应用THREE.EdgesGeometry?

时间:2019-01-17 03:29:30

标签: javascript three.js

我尝试使用OBJLoader在模型加载器中实现边缘,但无法做到。

Mloader = new THREE.MTLLoader();
            Mloader.setPath( dir );
            Mloader.load( mtl_dir, function ( materials ) {
                materials.preload();
                OLoader = new THREE.OBJLoader();
                OLoader.setMaterials( materials );
                OLoader.setPath( dir );
                OLoader.load( name_file, function ( object ) {
                    object.scale.set( scale, scale, scale );
                    scene.add( object );
                    var edges = new THREE.EdgesGeometry( object, 11 );
                    var line = new THREE.LineSegments( edges, new THREE.LineBasicMaterial( {color: 0x111111 } ) );
                    line.scale.set( scale, scale, scale );
                    scene.add( line )
                } );
            } );

模型可以很好地加载,但是边缘却不能。 当使用STLloader加载模型时,几何的边缘可以很好地渲染,但是我需要使用.obs文件来完成。

var loader = new THREE.STLLoader();
loader.load(dir, function (geometry) {

material = new THREE.MeshPhongMaterial({
    color: 0xAAAAAA,
    specular: 0x111111,
    shininess: 200
});
var edges = new THREE.EdgesGeometry(geometry, 11);

var line = new THREE.LineSegments(edges, new THREE.LineBasicMaterial({color: 0x111111}));   
line.scale.set(scale, scale, scale);

scene.add(line)

});

STL vs OBJ Loader

1 个答案:

答案 0 :(得分:0)

谢谢,是的。解决方案的代码:

Mloader = new THREE.MTLLoader();
            Mloader.setPath( dir );
            Mloader.load( mtl_dir, function ( materials ) {
                materials.preload();
                OLoader = new THREE.OBJLoader();
                OLoader.setMaterials( materials );
                OLoader.setPath( dir );
                OLoader.load( name_file, function ( object ) {
                    object.traverse( function ( child ) {
                        if ( child instanceof THREE.Mesh ) {
                            child.castShadow = true;
                            edges = new THREE.EdgesGeometry( child.geometry,11);
                            line = new THREE.LineSegments( edges, new THREE.LineBasicMaterial( {
                            color: 0x111111
                        } ) );
                            line.scale.set( scale, scale, scale );
                            line.position.set( 0, 0.7, 0 );
                            scene.add( line );
                        }
                    } );

                        object.scale.set( scale, scale, scale );
                        object.position.set( 0, 0.7, 0 );
                        scene.add( object );
                    } );
            } );