Three.js - VRML Loader

时间:2014-10-29 16:50:38

标签: javascript three.js vrml

我试图将Three.js与VRML(WRL)模型一起使用。我想知道的是模型加载了各种颜色(彩色)。如何将模型以纯色加载为原始文件?

我的例子是:http://dev.logicarts.com.br/semapro/projeto-3d/teste-1.html

代码:

<div id="info">
    <a href="http://threejs.org" target="_blank">three.js</a> -
    vrml format loader test -
    <!--model from <a href="http://cs.iupui.edu/~aharris/webDesign/vrml/" target="_blank">VRML 2.0 Tutorial</a>,-->
    </div>

    <script src="js/three.min.js"></script>

    <script src="js/OrbitControls.js"></script>

    <script src="js/VRMLLoader.js"></script>

    <script src="js/Detector.js"></script>
    <script src="js/stats.min.js"></script>

    <script>

        if ( ! Detector.webgl ) Detector.addGetWebGLMessage();

        var container, stats;

        var camera, controls, scene, renderer;

        var cross;

        init();
        animate();

        function init() {

            camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 0.01, 1e10 );
            camera.position.z = 6;

            controls = new THREE.OrbitControls( camera );

            controls.rotateSpeed = 5.0;
            controls.zoomSpeed = 5;

            controls.noZoom = false;
            controls.noPan = false;

            scene = new THREE.Scene();
            scene.add( camera );

            // light

            var dirLight = new THREE.DirectionalLight( 0xffffff );
            dirLight.position.set( 200, 200, 1000 ).normalize();

            camera.add( dirLight );
            camera.add( dirLight.target );

            var loader = new THREE.VRMLLoader();
            loader.addEventListener( 'load', function ( event ) {

                scene.add(event.content);

            } );
            loader.load( "3d/conjunto-carcaca.wrl" );

            // renderer

            renderer = new THREE.WebGLRenderer( { antialias: false } );
            renderer.setSize( window.innerWidth, window.innerHeight );

            container = document.createElement( 'div' );
            document.body.appendChild( container );
            container.appendChild( renderer.domElement );

            stats = new Stats();
            stats.domElement.style.position = 'absolute';
            stats.domElement.style.top = '0px';
            container.appendChild( stats.domElement );

            //

            window.addEventListener( 'resize', onWindowResize, false );

        }

        function onWindowResize() {

            camera.aspect = window.innerWidth / window.innerHeight;
            camera.updateProjectionMatrix();

            renderer.setSize( window.innerWidth, window.innerHeight );

            controls.handleResize();

        }

        function animate() {

            requestAnimationFrame( animate );

            controls.update();
            renderer.render( scene, camera );

            stats.update();

        }

    </script>

1 个答案:

答案 0 :(得分:2)

如果您在scene.add(event.content);语句下添加此代码,则场景中的所有对象都将变为红色。

scene.traverse (function (object)
{
    if (object instanceof THREE.Mesh) {
        object.material.color.setHex( 0xff0000 );
    }
});