Three.js 360图像-纹理标记为更新,但图像不完整

时间:2019-07-08 13:46:38

标签: javascript three.js

这是我正在使用的代码-

var camera, scene, renderer;
        var Container = document.getElementById('container');
        var imageFile = Container.getAttribute('data');

        var isUserInteracting = false,
        onMouseDownMouseX = 0, onMouseDownMouseY = 0,
        lon = 0, onMouseDownLon = 0,
        lat = 0, onMouseDownLat = 0,
        phi = 0, theta = 0;

        init();
        animate();



        function closeModal() {

        }


        function init() {

            var container, mesh;

            container = document.getElementById( 'gallery-modal' );

            camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 1100 );
            camera.target = new THREE.Vector3( 0, 0, 0 );

            scene = new THREE.Scene();

            var geometry = new THREE.SphereGeometry( 500, 60, 40 );
            geometry.scale( - 1, 1, 1 );

            var material = new THREE.MeshBasicMaterial( {
                map: new THREE.TextureLoader().load(  imageFile )
            } );

            mesh = new THREE.Mesh( geometry, material );

            scene.add( mesh );

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


            // Controls to move the image
            document.addEventListener( 'mousedown', onDocumentMouseDown, false );
            document.addEventListener( 'mousemove', onDocumentMouseMove, false );
            document.addEventListener( 'mouseup', onDocumentMouseUp, false );
            document.addEventListener( 'wheel', onDocumentMouseWheel, false );

            document.addEventListener( 'dragover', function ( event ) {

                event.preventDefault();
                event.dataTransfer.dropEffect = 'copy';

            }, false );

            document.addEventListener( 'dragenter', function ( event ) {

                document.body.style.opacity = 0.5;

            }, false );

            document.addEventListener( 'dragleave', function ( event ) {

                document.body.style.opacity = 1;

            }, false );

            var galleryDOM = document.getElementById('gallery');
            var modal = document.getElementById('gallery-modal');

                galleryDOM.addEventListener('click', function(e) {

                    if(e.target.localName == 'img') {

                        //console.log(e.target.src);

                        modal.style.display = "block";

                        var reader = new FileReader();

                        reader.addEventListener('load', function(e) {

                           material.map.image.src = e.target.result;
                           material.map.needsUpdate = true;

                        }, false);

                    }
                });

                window.onclick = function(event) {
                    if (event.target == modal) {
                        modal.style.display = 'none';
                    }
                }


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

        }

        function onWindowResize() {

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

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

        }

        function onDocumentMouseDown( event ) {

            event.preventDefault();

            isUserInteracting = true;

            onPointerDownPointerX = event.clientX;
            onPointerDownPointerY = event.clientY;

            onPointerDownLon = lon;
            onPointerDownLat = lat;

        }

        function onDocumentMouseMove( event ) {

            if ( isUserInteracting === true ) {

                lon = ( onPointerDownPointerX - event.clientX ) * 0.1 + onPointerDownLon;
                lat = ( event.clientY - onPointerDownPointerY ) * 0.1 + onPointerDownLat;

            }

        }

        function onDocumentMouseUp( event ) {

            isUserInteracting = false;

        }

        function onDocumentMouseWheel( event ) {

            camera.fov += event.deltaY * 0.05;
            camera.updateProjectionMatrix();

        }

        function animate() {

            requestAnimationFrame( animate );
            update();

        }

        function update() {

            if ( isUserInteracting === false ) {

                lon += 0.1;

            }

            lat = Math.max( - 85, Math.min( 85, lat ) );
            phi = THREE.Math.degToRad( 90 - lat );
            theta = THREE.Math.degToRad( lon );

            camera.target.x = 500 * Math.sin( phi ) * Math.cos( theta );
            camera.target.y = 500 * Math.cos( phi );
            camera.target.z = 500 * Math.sin( phi ) * Math.sin( theta );

            camera.lookAt( camera.target );

            /*
            // distortion
            camera.position.copy( camera.target ).negate();
            */

            renderer.render( scene, camera );

        }

更新**我不再收到错误,但这也不会更改纹理图像吗?

我已经添加了完整的代码。我正在尝试创建一个画廊效果,单击一个占位符图像,使用360图像打开模态,但是我需要根据单击的占位符来切换360图像。

0 个答案:

没有答案