【问题标题】:Three.js scene with Obj and Cubemap, receiving THbindTexture: textures can not be used with multiple targetsThree.js 场景与 Obj 和 Cubemap,接收 THbindTexture: 纹理不能与多个目标一起使用
【发布时间】:2016-03-31 12:06:38
【问题描述】:

我正在尝试创建一个带有立方体贴图全景背景的 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 上有一个演示(你会看到我的模型是黑色的)

【问题讨论】:

  • 您的代码缩进令人困惑...

标签: javascript three.js webgl


【解决方案1】:

bindTexture:纹理不能用于多个目标

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

【讨论】:

    猜你喜欢
    • 2013-11-13
    • 2015-08-30
    • 1970-01-01
    • 1970-01-01
    • 2015-12-25
    • 2023-04-08
    • 2021-08-30
    • 2016-06-24
    • 1970-01-01
    相关资源
    最近更新 更多