ThreeJS,将Subdivision Modifier添加到基于Objectloader的场景中

时间:2017-10-19 15:25:45

标签: javascript json three.js

我尝试在加载了对象加载器的场景中使用细分修改器。尝试此操作时出现以下javascript错误:

" BufferSubdivisionModifier.js:514 Uncaught TypeError:无法读取属性' array'未定义"

我的代码如下:使用细分修饰符加载对象。

            var loader = new THREE.ObjectLoader();
            loader.load("../js/brain2.json", function(object) {
                    var material = new THREE.MeshPhongMaterial( { color: 0x888888, specular: 0x222222, shininess: 20} );

                    object.traverse( function ( child ) {
                        if ( child instanceof THREE.Mesh ) {
                            var modifier = new THREE.BufferSubdivisionModifier( 1 );
                            child.material = material;
                            child.geometry.computeFaceNormals();
                            modifier.modify( child.geometry );
                            child.material.overdraw = 1
                        };  
                    });
                    object.scale.set(15, 15, 15);
                    object.position.x = 1;
                    object.position.y = 1;
                    object.position.z = 1;
                    object.rotation.set( 1, 1, 1 );
                    scene.add( object );
            });

如果我不得不猜测,细分修饰符不喜欢“child.geometry”#39;对象

1 个答案:

答案 0 :(得分:0)

解决方案是为孩子加载几何形状。

            var loader = new THREE.ObjectLoader();
            loader.load("../js/brain2.json", function(object) {
                    var material = new THREE.MeshPhongMaterial( { color: 0x888888, specular: 0x222222, shininess: 20} );

                    object.traverse( function ( child ) {
                        if ( child instanceof THREE.Mesh ) {
                            child.material = material;
                             var geometry = new THREE.Geometry().fromBufferGeometry( child.geometry );
                             geometry.computeFaceNormals();
                             geometry.mergeVertices();
                             geometry.computeVertexNormals();
                             child.geometry = new THREE.BufferGeometry().fromGeometry( geometry );
                            var modifier = new THREE.BufferSubdivisionModifier(1);
                            modifier.modify(geometry);
                            child.material.overdraw = 1
                        };  
                    });
                    object.scale.set(15, 15, 15);
                    object.position.x = 1;
                    object.position.y = 1;
                    object.position.z = 1;
                    object.rotation.set( 5, 1, 1 );
                    scene.add( object );
            });