即使启用了阴影也没有阴影

时间:2014-12-01 20:51:54

标签: javascript three.js

我写了下面的代码,以便在飞机上获得斯坦福龙的影子。但我没有得到它。我已经通过对象和平面启用阴影来接收阴影。我的代码是:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
        <style>
            body {
                font-family: Monospace;
                background-color: #000;
                color: #fff;
                margin: 0px;
                overflow: hidden;
            }
            #info {
                color: #fff;
                position: absolute;
                top: 10px;
                width: 100%;
                text-align: center;
                z-index: 100;
                display:block;
            }
            #info a, .button { color: #f00; font-weight: bold; text-decoration: underline; cursor: pointer }
        </style>
    </head>

    <body>

        <script src="http://mrdoob.github.com/three.js/build/three.min.js"></script>

    <script src="http://mrdoob.github.com/three.js/examples/js/shaders/CopyShader.js"></script>
    <script src="http://mrdoob.github.com/three.js/examples/js/shaders/SSAOShader.js"></script>
    <script type="text/javascript" src="http://mrdoob.github.com/three.js/examples/js/shaders/ConvolutionShader.js"></script>
    <script type="text/javascript" src="http://mrdoob.github.com/three.js/examples/js/shaders/FXAAShader.js"></script>

    <script src="http://mrdoob.github.com/three.js/examples/js/postprocessing/EffectComposer.js"></script>
    <script src="http://mrdoob.github.com/three.js/examples/js/postprocessing/RenderPass.js"></script>
    <script src="http://mrdoob.github.com/three.js/examples/js/postprocessing/MaskPass.js"></script>
    <script src="http://mrdoob.github.com/three.js/examples/js/postprocessing/ShaderPass.js"></script>
        <script src="http://mrdoob.github.com/three.js/examples/js/loaders/DDSLoader.js"></script>
        <script src="http://mrdoob.github.com/three.js/examples/js/loaders/MTLLoader.js"></script>
        <script src="http://mrdoob.github.com/three.js/examples/js/loaders/OBJMTLLoader.js"></script>
        <script type="text/javascript" src="http://mrdoob.github.com/three.js/examples/js/postprocessing/BloomPass.js"></script>

        <script src="http://mrdoob.github.com/three.js/examples/js/Detector.js"></script>
        <script src="http://mrdoob.github.com/three.js/examples/js/libs/stats.min.js"></script>

        <script>

            if ( window.innerWidth === 0 ) { window.innerWidth = parent.innerWidth; window.innerHeight = parent.innerHeight; }

            var camera, scene, renderer;
            var group;
            var depthMaterial, depthTarget, composer;
            var object;
            init();
            animate();



            function init() {
                renderer = new THREE.WebGLRenderer();
                renderer.setSize( window.innerWidth, window.innerHeight );
                renderer.setClearColor(new THREE.Color(0x000000,0.1));
                renderer.shadowMapType = THREE.PCFSoftShadowMap;
                renderer.shadowMapEnabled = true;   
                renderer.shadowMapSoft = true;
                document.body.appendChild( renderer.domElement );

                camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 2000 );
                //camera.position.x = 100;
                camera.position.y = 100;
                camera.position.z = 100;
                // scene

                scene = new THREE.Scene();
                var planeGeometry = new THREE.PlaneGeometry(150,150);
                var planeMaterial =    new THREE.MeshLambertMaterial({color: 0xaaaaaa});
                var plane = new THREE.Mesh(planeGeometry,planeMaterial);
                plane.receiveShadow  = true;
                plane.rotation.x=-0.5*Math.PI;
                plane.position.x=0;
                plane.position.y=0;
                plane.position.z=0;
                 // add the plane to the scene
                scene.add(plane);
                var ambient = new THREE.AmbientLight( 0xffffff );
                scene.add( ambient );

                var directionalLight = new THREE.DirectionalLight( 0xffeedd );
                directionalLight.position.set( 0, 0, 1 ).normalize();
                scene.add( directionalLight );

                var spotLight = new THREE.SpotLight( 0xffffff );
                spotLight.position.set( -80, 60, -10 );
                spotLight.shadowMapWidth =2048;
                spotLight.shadowMapHeight = 2048;
                spotLight.castShadow = true;
                spotLight.shadowMapEnabled = true;
                scene.add( spotLight );
                //Camera




                // model

                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 ) {
                };


                THREE.Loader.Handlers.add( /\.dds$/i, new THREE.DDSLoader() );

                var loader = new THREE.OBJMTLLoader();
                loader.load( 'dragon.obj', 'dragon.mtl', function ( object ) {

                    object.position.y = 0;
                    object.position.x = 0;
                    object.position.z = 10;
                    object.castShadow=true;
                    object.receiveShadow= true;
                    object.scale.set(4,4,4); 
                    scene.add( object );

                }, onProgress, onError );

                //

                var depthShader = THREE.ShaderLib[ "normal" ];
                var depthUniforms = THREE.UniformsUtils.clone( depthShader.uniforms );


                depthMaterial = new THREE.ShaderMaterial( { fragmentShader: depthShader.fragmentShader, vertexShader: depthShader.vertexShader, uniforms: depthUniforms } );
                depthMaterial.blending = THREE.NoBlending;
                // postprocessing

                composer = new THREE.EffectComposer( renderer );
                composer.addPass( new THREE.RenderPass( scene, camera ));

                depthTarget = new THREE.WebGLRenderTarget( window.innerWidth, window.innerHeight, { minFilter: THREE.NearestFilter, magFilter: THREE.NearestFilter, format: THREE.RGBAFormat } );

                var effect = new THREE.ShaderPass( THREE.SSAOShader );
                effect.uniforms[ 'tDepth' ].value = depthTarget;
                effect.uniforms[ 'size' ].value.set(  window.innerWidth, window.innerHeight );
                effect.uniforms[ 'cameraNear' ].value = camera.near;
                effect.uniforms[ 'cameraFar' ].value = camera.far;
                effect.uniforms[ 'aoClamp' ].value = 0.9;
                effect.uniforms[ 'onlyAO' ].value=0;
                effect.renderToScreen = true;
                composer.addPass( effect );


                camera.lookAt( scene.position );

            }
            function animate() {

                requestAnimationFrame( animate );


                scene.overrideMaterial = depthMaterial;
                renderer.render( scene, camera, depthTarget );

                scene.overrideMaterial = null;
                composer.render();


            }

        </script>

    </body>
</html>

请帮助我找出我做错了什么。我对JavaScript也比较陌生。 我想在上面的信息中添加的另一件事是它只适用于firefox而不是chrome。 提前致谢。

0 个答案:

没有答案