具有Obj和Cubemap的Three.js场景,接收THbindTexture:纹理不能与多个目标一起使用

时间:2016-03-31 02:23:27

标签: javascript three.js webgl

我试图创建一个带有立方体贴图全景背景的three.js场景和一些放置在场景中的.mtl纹理的.obj文件。但是,我收到此WebGL错误,我无法找出以下来源: THbindTexture: textures can not be used with multiple targets 有谁知道可能出错的地方?以下是相关代码:

    var camera, cubeCamera, scene, object, renderer;
        var cube, sphere, torus;
        var fov = 70,
        isUserInteracting = false,
        onMouseDownMouseX = 0, onMouseDownMouseY = 0,
        lon = 0, onMouseDownLon = 0,
        lat = 0, onMouseDownLat = 0,
        phi = 0, theta = 0;
        var skyLoader = new THREE.TextureLoader();
        skyLoader.load( 'pano.jpg', function ( texture ) {
            texture.mapping = THREE.UVMapping;
            init( texture );
            animate();
        } );



        function init( texture ) {
            camera = new THREE.PerspectiveCamera( fov, window.innerWidth / window.innerHeight, 1, 1000 );
            scene = new THREE.Scene();

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


        var mesh = new THREE.Mesh( new THREE.SphereGeometry( 500, 60, 40 ), new THREE.MeshBasicMaterial( {  map: texture } ) );
        mesh.scale.x = -1;
            scene.add( mesh );
            renderer = new THREE.WebGLRenderer( { antialias: true } );
            renderer.setPixelRatio( window.devicePixelRatio );
            renderer.setSize( window.innerWidth, window.innerHeight );
        cubeCamera = new THREE.CubeCamera( 1, 1000, 256 );

            scene.add( cubeCamera );
            document.body.appendChild( renderer.domElement );

            document.addEventListener( 'mousedown', onDocumentMouseDown, false );
            document.addEventListener( 'mousewheel', onDocumentMouseWheel, false );
            document.addEventListener( 'MozMousePixelScroll', onDocumentMouseWheel, false);
            window.addEventListener( 'resize', onWindowResized, false );
            onWindowResized( null );

            var loader = new THREE.OBJLoader();
loader.load( 'nsa_sanantonio.obj', function ( object ) {
  object.traverse( function ( child ) {
      if ( child instanceof THREE.Mesh ) {
          child.material.envMap = texture;
          // add any other properties you want here. check the docs.
      }
  } );

  scene.add( object );

    } );
        }          
        function onWindowResized( event ) {
            renderer.setSize( window.innerWidth, window.innerHeight );
            camera.projectionMatrix.makePerspective( fov, window.innerWidth / window.innerHeight, 1, 1100 );
        }
        function onDocumentMouseDown( event ) {
            event.preventDefault();
            onPointerDownPointerX = event.clientX;
            onPointerDownPointerY = event.clientY;
            onPointerDownLon = lon;
            onPointerDownLat = lat;
            document.addEventListener( 'mousemove', onDocumentMouseMove, false );
            document.addEventListener( 'mouseup', onDocumentMouseUp, false );
        }
        function onDocumentMouseMove( event ) {
            lon = ( event.clientX - onPointerDownPointerX ) * 0.1 + onPointerDownLon;
            lat = ( event.clientY - onPointerDownPointerY ) * 0.1 + onPointerDownLat;
        }
        function onDocumentMouseUp( event ) {
            document.removeEventListener( 'mousemove', onDocumentMouseMove, false );
            document.removeEventListener( 'mouseup', onDocumentMouseUp, false );
        }
        function onDocumentMouseWheel( event ) {
            // WebKit
            if ( event.wheelDeltaY ) {
                fov -= event.wheelDeltaY * 0.05;
            // Opera / Explorer 9
            } else if ( event.wheelDelta ) {
                fov -= event.wheelDelta * 0.05;
            // Firefox
            } else if ( event.detail ) {
                fov += event.detail * 1.0;
            }
            camera.projectionMatrix.makePerspective( fov, window.innerWidth / window.innerHeight, 1, 1100 );
        }
        function animate() {
            requestAnimationFrame( animate );
            render();
        }
        function render() {
            var time = Date.now();
            lon += .15;
            lat = Math.max( - 85, Math.min( 85, lat ) );
            phi = THREE.Math.degToRad( 90 - lat );
            theta = THREE.Math.degToRad( lon );

            camera.position.x = 100 * Math.sin( phi ) * Math.cos( theta );
            camera.position.y = 100 * Math.cos( phi );
            camera.position.z = 100 * Math.sin( phi ) * Math.sin( theta );
            camera.lookAt( scene.position );

            cubeCamera.updateCubeMap( renderer, scene );

            renderer.render( scene, camera );
        }

我不明白一次如何应用两个纹理。 我从here.获得了Objloader代码 但它也不适用于普通的obj / stl加载器。我在我的网站http://www.zakziebell.com/nsa上有一个演示(你会看到我的模特是黑色的)

1 个答案:

答案 0 :(得分:0)

  

bindTexture:纹理不能与多个目标一起使用

意味着您尝试以两种不同的方式使用相同的纹理,一次作为2D纹理,另一种作为立方体贴图。纹理只能是一个或另一个而不是两者。