导入的3D对象不会使用three.js投射阴影

时间:2014-04-06 14:05:49

标签: javascript three.js

我目前正在围绕three.js缠绕我的大脑,我已成功通过三个.OBJMTLLoader导入我在C4D中制作的3D模型,但是我无法让对象投下阴影。我已经使用了object.castShadow = true但是它没有用,但我可以在three.js中创建几何体以投射阴影,所以我知道场景设置正常。

测试场景目前位于http://kirkd.co.uk/dev/,现在已根据下面的建议进行了更新。

代码在下面,如果有人可以指出我做错了什么,或者即使导入的对象可以投射阴影,那么我也会永远感激。

的Ta。

        <script>


        var container;

        var controls;

        var camera, scene, renderer;

        var windowHalfX = window.innerWidth / 2;
        var windowHalfY = window.innerHeight / 2;

        init();
        animate();

        function init() {

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

            camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 2000);
            camera.position.z = 500;
            camera.position.y = 500;

            scene = new THREE.Scene();

            controls = new THREE.OrbitControls( camera );
            controls.addEventListener( 'change', render );


            var ambientLight = new THREE.AmbientLight(0x0c0c0c);
            scene.add(ambientLight);

            var spotLight = new THREE.SpotLight( 0xffffff );
            spotLight.position.set( 500, 1000, 500 );
            spotLight.castShadow = true;

            spotLight.shadowMapWidth = 1024;
            spotLight.shadowMapHeight = 1024;
            scene.add( spotLight );

            var companion = new THREE.OBJMTLLoader();
            companion.load( 'companion2.obj', 'companion.mtl', function ( object ) {
                object.position.set(0,-20,0);
                object.scale.set( 0.8, 0.8, 0.8 );
                object.castShadow = true;
                scene.add( object );
            });


            var floorGeometry = new THREE.CubeGeometry(1000,4,1000);
            var floorMaterial = new THREE.MeshLambertMaterial({color: 0xff0000});
            var floor = new THREE.Mesh(floorGeometry, floorMaterial);
            floor.position.x=0;
            floor.position.y=-130;
            floor.position.z=0;
            floor.receiveShadow = true;
            scene.add(floor);

            var geometry = new THREE.BoxGeometry( 100, 100, 100 );
            mesh = new THREE.Mesh( geometry);
            scene.add( mesh );
            mesh.position.set(-100,200,10);
            mesh.castShadow = true;

            renderer = new THREE.WebGLRenderer();
            renderer.setClearColor(0xEEEEEE, 1.0);
            renderer.setSize(window.innerWidth, window.innerHeight);
            container.appendChild( renderer.domElement );
            renderer.shadowMapEnabled = true;
            renderer.shadowMapSoft = true;
            spotLight.shadowCameraVisible = true;

            var step=0;
            render();
        };

        function render() {
          camera.lookAt(scene.position);
          renderer.render(scene, camera);
      }


      function animate() {
        requestAnimationFrame( animate );
        render();

    }

    </script>

1 个答案:

答案 0 :(得分:6)

您的对象包含子网格,每个网格都需要将castShadow设置为true

在你的加载器回调中,添加:

object.traverse( function( node ) { if ( node instanceof THREE.Mesh ) { node.castShadow = true; } } );

three.js r.66