javascript - Three.JS 错误 - 纹理不是二的幂/无法更改纹理

标签 javascript three.js blob

快速运行 - 我正在尝试构建一个包含 360 度图像的画廊,当点击一个画廊图像时,模态打开并在内部显示 360 度查看器,但我希望图像根据被点击的画廊图像动态变化.

THREE.WebGLRenderer: image is not power of two (1920x960). Resized to 2048x1024 
blob:http://nolan.bfdevserver.com/7ca91892-2e3b-4fff-b672-ec250ef13498 new image blob
blob:http://nolan.bfdevserver.com/7788013c-9611-497a-bc12-66a06bb2244f old image blob
THREE.WebGLRenderer: image is not power of two (0x0). Resized to 0x0 
THREE.WebGLRenderer: Texture is not power of two. Texture.minFilter should be set to THREE.NearestFilter or THREE.LinearFilter. 
THREE.Texture
WebGL: INVALID_VALUE: texImage2D: no canvas

这是我在控制台中收到的错误。正如您在下面的代码中看到的:

material.minFilter = THREE.LinearFilter;

所以我不确定为什么会这样。

   <div id="container" data="<?php $ThreeSixtyImage = the_field('image'); ?>"></div>

<div id="gallery-modal" class="modal">
</div> 

<div id="gallery" class="gallery">
    <div class="gallery-item">
        <img src="https://www.bluefire360.com/wp-content/uploads/V4260450-min.jpg" />
    </div>
    <div class="gallery-item">
        <img src="http://nolan.bfdevserver.com/wp-content/uploads/pano_2048.jpg" />
    </div>
    <div class="gallery-item">
        <img src="http://nolan.bfdevserver.com/wp-content/uploads/pano_2048.jpg" />
    </div>
</div>

        <script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r79/three.min.js"></script>

  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 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 )
            } );

            material.minFilter = THREE.LinearFilter;

            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 );


            /// ADDITIONAL CODE STARTS HERE

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

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

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

                        // Show Modal
                        modal.style.display = "block";

                        // Create blob from image url
                        var blob = new Blob([e.target.src], {type: 'url'});             
                        var newestImage = window.URL.createObjectURL(blob);

                        console.log(newestImage,  "new image blob")
                        console.log(material.map.image.src,  "old image blob")

                        // Apply newestImage to material object in THREEjs
                        if(newestImage) {
                            material.map.image.src = newestImage;
                            material.map.needsUpdate = true;

                        }

                    }
                });


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

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

        }

        function dataURItoBlob(dataURI) {
            var mime = dataURI.split(',')[0].split(':')[1].split(';')[0];
            var binary = atob(dataURI.replace(/^data:image\/(png|jpeg|jpg);base64,/, ''));
            var array = [];
            for (var i = 0; i < binary.length; i++) {
                array.push(binary.charCodeAt(i));
            }
            return new Blob([new Uint8Array(array)], {type: mime});
        }

        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 );

        }

我已经关注这个几个小时了,所以任何帮助甚至只是类(class)指导都将不胜感激。提前致谢!

最佳答案

要修正二次方错误,需要修改这一行:

material.minFilter = THREE.LinearFilter;

material.map.minFilter = THREE.LinearFilter;

为了更新纹理,回到标准图像 url 与 blob url 并使用:

if(newestImage) {
   material.map = new THREE.TextureLoader().load(newestImage);
}

关于javascript - Three.JS 错误 - 纹理不是二的幂/无法更改纹理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56941798/

相关文章:

javascript - 如何在 JavaScript 中的事件图像 slider 的左侧和右侧包含尖括号

javascript - meteor / react : How to integrate react component in main template

three.js - 如何在运行时在框架中加载3D对象?

Python:使用请求读取图像(url),然后将其作为BLOB数据保存到MySQL

javascript - 为什么我的 CSS 不适用于我的 React 组件?

javascript - 使用 React 的动态表

javascript - 完全显示 Three.js sprite 和 PlaneGeometry 图像,无论其大小

javascript - Collada 装载机 : Is there anyway to get several meshes from the scene object?

javascript - 无法将 “custom blob” 保存到 Firestore - ADMIN SDK

java - 如何在 Java 中操作 BLOB?