Javascript 将VideoTexture应用于加载的.obj不';t工作3.js

Javascript 将VideoTexture应用于加载的.obj不';t工作3.js,javascript,three.js,Javascript,Three.js,我加载从sketchup导出的一个.obj,它表示一个简单的矩形。 我想在loaded.obj的正面应用视频纹理,但它不起作用,并且在控制台上没有错误。我很困,问题可能来自紫外线,但我发现没有什么可以帮助我。我尝试使用视频纹理,但也遇到了同样的问题。 这是我的代码: <div id="ThreeJS" style="position: absolute; left:0px; top:0px"></div> <video id="video" aut

我加载从sketchup导出的一个.obj,它表示一个简单的矩形。 我想在loaded.obj的正面应用视频纹理,但它不起作用,并且在控制台上没有错误。我很困,问题可能来自紫外线,但我发现没有什么可以帮助我。我尝试使用视频纹理,但也遇到了同样的问题。 这是我的代码:

    <div id="ThreeJS" style="position: absolute; left:0px; top:0px"></div>

    <video id="video" autoplay muted loop crossOrigin="anonymous" webkit-playsinline style="display:none">
        <source src="assets/output.mp4" type='video/mp4'>
    </video>


    <script>
        var AMOUNT = 100;
        var container;
        var camera, scene, renderer;
        var video, image, imageContext,
        imageReflection, imageReflectionContext, imageReflectionGradient,
        texture, textureReflection;
        var mesh;
        var mouseX = 0;
        var mouseY = 0;
        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, 1, 10000 );
            camera.position.z = 1000;

            scene = new THREE.Scene();
            scene.background = new THREE.Color( 0xf0f0f0 );

            video = document.createElement( 'video' );
            // video.id = 'video';
            // video.type = ' video/ogg; codecs="theora, vorbis" ';
            video.src = "assets/output.mp4";
            video.load(); // must call after setting/changing source
            video.muted = true;
            video.play();
            //
            image = document.createElement( 'canvas' );
            image.width = 480;
            image.height = 204;
            imageContext = image.getContext( '2d' );
            imageContext.fillStyle = '#000000';
            imageContext.fillRect( 0, 0, 480, 204 );
            texture = new THREE.Texture( image );

            var material = new THREE.MeshBasicMaterial( { map: texture, overdraw: 0.5 } );
            imageReflection = document.createElement( 'canvas' );
            imageReflection.width = 480;
            imageReflection.height = 204;
            imageReflectionContext = imageReflection.getContext( '2d' );
            imageReflectionContext.fillStyle = '#000000';
            imageReflectionContext.fillRect( 0, 0, 480, 204 );
            imageReflectionGradient = imageReflectionContext.createLinearGradient( 0, 0, 0, 204 );
            imageReflectionGradient.addColorStop( 0.2, 'rgba(240, 240, 240, 1)' );
            imageReflectionGradient.addColorStop( 1, 'rgba(240, 240, 240, 0.8)' );

            textureReflection = new THREE.Texture( imageReflection );
            var materialReflection = new THREE.MeshBasicMaterial( { map: textureReflection, side: THREE.BackSide, overdraw: 0.5 } );

            // var plane = new THREE.PlaneBufferGeometry( 480, 204, 4, 4 );
            // mesh = new THREE.Mesh( plane, material );
            // mesh.scale.x = mesh.scale.y = mesh.scale.z = 1;
            // scene.add(mesh);
            // mesh = new THREE.Mesh( plane, materialReflection );
            // mesh.position.y = -306;
            // mesh.rotation.x = - Math.PI;
            // mesh.scale.x = mesh.scale.y = mesh.scale.z = 1.5;
            // scene.add( mesh );
            // this works

            //model
        var loader = new THREE.OBJLoader();
        loader.load( 'assets/model3D.obj', function (object) {
            var instance;
          object.traverse( function (child) {
              if ( child instanceof THREE.Mesh ) {
                child.material = materialReflection;
                child.material.needsUpdate = true;
                // this doesn't works
              }
            });

          object.position.y = - 80;
          scene.add( object );
        });


            renderer = new THREE.CanvasRenderer();
            renderer.setPixelRatio( window.devicePixelRatio );
            renderer.setSize( window.innerWidth, window.innerHeight );
            container.appendChild( renderer.domElement );

            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 );
            mouseY = ( event.clientY - windowHalfY ) * 0.2;
        }
        //
        function animate() {
            requestAnimationFrame( animate );
            render();
        }
        function render() {
            camera.position.x += ( mouseX - camera.position.x ) * 0.05;
            camera.position.y += ( - mouseY - camera.position.y ) * 0.05;
            camera.lookAt( scene.position );
            if ( video.readyState === video.HAVE_ENOUGH_DATA ) {
                imageContext.drawImage( video, 0, 0 );
                if ( texture ) texture.needsUpdate = true;
                if ( textureReflection ) textureReflection.needsUpdate = true;
            }
            imageReflectionContext.drawImage( image, 0, 0 );
            imageReflectionContext.fillStyle = imageReflectionGradient;
            imageReflectionContext.fillRect( 0, 0, 480, 204 );
            renderer.render( scene, camera );
        }
    </script>

附言:我是three.js的初学者

你有没有办法让它在JSFIDLE上运行?对于试图帮助您的人来说,这将使调试变得更加容易。另外,您应该尝试让它在一个简单的3JS生成的平面上工作。一旦你开始工作,你可以在OBJ导入上尝试。这可能会帮助您缩小问题的范围,无论是在代码中还是在OBJ中。我已经让它在一个简单的3JS平面上工作了。只有在使用obj导入时应用纹理(视频或img)时,问题才会出现。
# Alias OBJ Model File
# Exported from SketchUp, (c) 2000-2012 Trimble Navigation Limited
# File units = meters

g Mesh1 Model

usemtl FrontColor
v 12.4942 0 7.12249
vt 491.898 280.413
vn 0 -1 0
v 1.26421 0 7.12249
vt 49.7719 280.413
v 1.26421 0 0.472495
vt 49.7719 18.6021
v 12.4942 0 0.472495
vt 491.898 18.6021
f 1/1/1 2/2/1 3/3/1 4/4/1