Three.js奇怪的透明神器

时间:2016-05-06 23:05:04

标签: javascript 3d three.js rendering

我最近刚刚开始为我正在创建的网站创建一个3D模型..由于某种原因,角色的一个部分是透明的,但只有部分就像你在里面看到的那样。如下所示: Transparent arm我无法弄明白。我试着追回我的一些代码无济于事,我尝试在blender中渲染它(因为它是.obj导入),它仍然有同样的问题。有没有人碰巧知道这个问题的解决办法?

<body>

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

    <script src="./JS/DDSLoader.js"></script>
    <script src="./JS/MTLLoader.js"></script>
    <script src="./JS/OBJLoader.js"></script>
    <script src="./JS/OrbitControls.js"></script>  
    <script src="./JS/Detector.js"></script>
    <script src="./JS/stats.min.js"></script>

    <script>

        var container, stats;

        var camera, scene, renderer;

        var mouseX = 0, mouseY = 0;

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


        init();
        animate();


        function init() {

            container = document.createElement( 'div' );
            document.body.appendChild( container );
             renderer = new THREE.WebGLRenderer( { 
              alpha: true,
              antialias: true
              } );
            renderer.setPixelRatio( window.devicePixelRatio );
            renderer.setSize( 200,300);
            container.appendChild( renderer.domElement );


            camera = new THREE.PerspectiveCamera( 14, window.innerWidth / window.innerHeight, .3, 1000 );
            camera.position.z = 1;
            camera.position.y = 8;
            //camera.rotation.y = -90*(180/3.14159265)


            var orbit = new THREE.OrbitControls( camera, renderer.domElement );
              orbit.enableZoom = false;
              orbit.enablePan = false;
              orbit.autoRotate = true;
            // scene

            scene = new THREE.Scene();

            var ambient = new THREE.AmbientLight( 0xffffff );
            scene.add( ambient );

             var dirLight = new THREE.DirectionalLight(0xffffff, .41);
            dirLight.position.set(100, 100, 50);
            scene.add(dirLight);


            var light = new THREE.PointLight( 0xf8f8ff, 0.25, 10000 );
            light.position.set( 0, 100,-75);
            scene.add( light );

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

            // 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 mtlLoader = new THREE.MTLLoader();
            mtlLoader.setBaseUrl( './Avatar/' );
            mtlLoader.setPath( './Avatar/' );
            mtlLoader.load( 'Avatar.mtl', function( materials ) {

                materials.preload();

                var objLoader = new THREE.OBJLoader();
                objLoader.setMaterials( materials );
                objLoader.setPath( './Avatar/' );
                objLoader.load( 'Avatar.obj', function ( object )
                 {
                    object.alphaTest = 0;
                    object.transparent = false;
                     object.side = THREE.DoubleSide;
                    //object.rotation.y = -25*(180/3.14159265)
                    //object.rotation.x = -35*(180/3.14159265)
                    object.position.y = 0;
                    scene.add( object );

                }, onProgress, onError );

            });

            //


            document.addEventListener( 'mousemove', onDocumentMouseMove, false );

            //

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

        }

        function onWindowResize() {

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

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

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

        }

        function onDocumentMouseMove( event ) {

            mouseX = ( event.clientX - windowHalfX ) / 2;
            mouseY = ( event.clientY - windowHalfY ) / 2;

        }

        //

        function animate() {

            requestAnimationFrame( animate );
            render();

        }

        function render() {

            //camera.position.x += ( mouseX - camera.position.x ) * .005;
            //camera.position.y += ( - mouseY - camera.position.y ) * .005;

            camera.lookAt( scene.position );

            renderer.render( scene, camera );

        }

    </script>

我此时有点失落,可以真正使用你的帮助!提前致谢。如果您有任何疑问,或者对我的代码中的某些内容不确定,请告诉我。

1 个答案:

答案 0 :(得分:0)

手臂的脸部法线可能会翻转到背面。您可能知道,如果面法线面向背面,则不会渲染,只会渲染背面,即使在3d建模器中也是如此,例如搅拌器。

您也可以使用FaceNormalsHelper检查three.js中的正常方向。

帮助者的文件:

如果法线方向错误,请在3d建模应用中翻转它们。