Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9270759
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T15:26:58+00:00 2026-06-18T15:26:58+00:00

I am currently trying to make a rotating cube with textures. I also want

  • 0

I am currently trying to make a rotating cube with textures. I also want the cube to rotate on it’s x- and y-axis (back and forth) by using controls (arrow images which rotate the cube on mouseover). When rotating it on the x-axis the back side of the cube is upside down.

I know that this has to do with the way UV mapping works. So I tried to correct that on cube initialization, which worked fine. But when I try to rotate the cube on it’s y-axis, the texture is upside down again. So I learned that I have to change the texture orientation on the fly – according to the rotation request.

But this doesn’t work for me. I found no way to change “faceVertexUvs[0][5]” after the cube is initialized. Trying to set “needsUpdate” for the cube geometry on initialization didn’t help.

Hope I made myself clear. Is there some sort of solution to that problem?

Thanks in advance
Michael

To illustrate my problem better: I post my source code here.

It cannot be demonstrated in JsFiddle as using texture images from another domain is not allowed (and CORS didn’t work). Just copy-paste the following code and save it as “test.html” locally. A texture image is availabe here: http://www.mikelmade.de/cors/t.png. It should be in the same directory as “test.html”. Open “test.html” in a WebGL-capable browser, then pass the mouse over “up” and you should see what I mean.

<html><head><script src="jquery.js"></script></head><body>
<script src="https://raw.github.com/mrdoob/three.js/master/build/three.min.js"></script>
<script src="https://raw.github.com/mrdoob/three.js/master/examples/js/libs/stats.min.js"> </script>

<script>
        var cubeGeo;
        var materials = [];
        var container, stats;
        var camera, scene, renderer;
        var cube, plane;

        var targetRotation = 0;
        var targetRotationOnMouseDown = 0;

        var mouseX = 0;
        var mouseXOnMouseDown = 0;

        var windowHalfX = window.innerWidth / 2;
        var windowHalfY = window.innerHeight / 2;

        init();
        animate();
        function init() {
            container = document.createElement( 'div' );
            document.body.appendChild( container );

            var info = document.createElement( 'div' );
            info.style.position = 'absolute';
            info.style.top = '10px';
            info.style.width = '100%';
            info.style.textAlign = 'center';
            info.innerHTML = 'Drag to spin the cube';
            container.appendChild( info );

            camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 1000 );
            camera.position.y = 150;
            camera.position.z = 500;

            scene = new THREE.Scene();

    // add subtle ambient lighting
    var ambientLight = new THREE.AmbientLight(0x555555);
    scene.add(ambientLight);

    // add directional light source
    var directionalLight = new THREE.DirectionalLight(0xEEEEEE);
    directionalLight.position.set(1, 1, 1).normalize();
    scene.add(directionalLight);

            // Cube
            for ( var i = 0; i < 6; i ++ ) {
                var img = new Image();
                img.src = 't.png';
                var tex = new THREE.Texture(img);
                img.tex = tex;
                img.onload = function() { this.tex.needsUpdate = true; };
                var mat = new THREE.MeshLambertMaterial({map: tex});
                materials.push(mat);
            }

            cubeGeo = new THREE.CubeGeometry(200,200,200,1,1,1);
            cubeGeo.uvsNeedUpdate = true;
            cubeGeo.dynamic = true;

            cube = new THREE.Mesh( cubeGeo, new THREE.MeshFaceMaterial( materials ) );

            cube.position.y = 150;
            scene.add( cube );

            renderer = new THREE.WebGLRenderer({antialias:true});
            renderer.setSize( window.innerWidth, window.innerHeight );

            // enable shadows on the renderer
            renderer.shadowMapEnabled = true;
            container.appendChild( renderer.domElement );

            stats = new Stats();
            stats.domElement.style.position = 'absolute';
            stats.domElement.style.top = '0px';
            container.appendChild( stats.domElement );

            document.addEventListener( 'mousedown', onDocumentMouseDown, false );
            document.addEventListener( 'touchstart', onDocumentTouchStart, false );
            document.addEventListener( 'touchmove', onDocumentTouchMove, 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 onDocumentMouseDown( event ) {
            event.preventDefault();

            document.addEventListener( 'mousemove', onDocumentMouseMove, false );
            document.addEventListener( 'mouseup', onDocumentMouseUp, false );
            document.addEventListener( 'mouseout', onDocumentMouseOut, false );

            mouseXOnMouseDown = event.clientX - windowHalfX;
            targetRotationOnMouseDown = targetRotation;
        }

        function onDocumentMouseMove( event ) {
            mouseX = event.clientX - windowHalfX;
            targetRotation = targetRotationOnMouseDown + ( mouseX - mouseXOnMouseDown ) * 0.02;
        }

        function onDocumentMouseUp( event ) {
            document.removeEventListener( 'mousemove', onDocumentMouseMove, false );
            document.removeEventListener( 'mouseup', onDocumentMouseUp, false );
            document.removeEventListener( 'mouseout', onDocumentMouseOut, false );
        }

        function onDocumentMouseOut( event ) {
            document.removeEventListener( 'mousemove', onDocumentMouseMove, false );
            document.removeEventListener( 'mouseup', onDocumentMouseUp, false );
            document.removeEventListener( 'mouseout', onDocumentMouseOut, false );
        }

        function onDocumentTouchStart( event ) {
            if ( event.touches.length === 1 ) {
                event.preventDefault();
                mouseXOnMouseDown = event.touches[ 0 ].pageX - windowHalfX;
                targetRotationOnMouseDown = targetRotation;
            }
        }

        function onDocumentTouchMove( event ) {
            if ( event.touches.length === 1 ) {
                event.preventDefault();
                mouseX = event.touches[ 0 ].pageX - windowHalfX;
                targetRotation = targetRotationOnMouseDown + ( mouseX - mouseXOnMouseDown ) * 0.05;
            }
        }


        function animatex(dir) {
            cubeGeo.faceVertexUvs[0][5] = [new THREE.UV(1, 0), new THREE.UV(1, 1), new THREE.UV(0, 1), new THREE.UV(0, 0)];
            animate2(dir);
        }

        var anim = 0;
        function animate2(dir) {
                requestAnimationFrame(function() {
                    cubeGeo.faceVertexUvs[0][5] = [new THREE.UV(1, 0), new THREE.UV(1, 1), new THREE.UV(0, 1), new THREE.UV(0, 0)];
                    if(anim == 1) {
                        animate2(dir);
                    }
                });
                render2();
                stats.update();
        }

        function render2() {
            cube.rotation.x = cube.rotation.x+0.06;
            renderer.render( scene, camera );
        }

        function animate() {
            requestAnimationFrame( animate );
            render();
            stats.update();

        }

        function render() {
            cube.rotation.y += ( targetRotation - cube.rotation.y ) * 0.05;
            $('#test').html(cube.rotation.y);
            renderer.render( scene, camera );
        }

    </script>
    <div id="up" style="position:absolute;z-index:500000;left:100px;top:20px;" class="nav">up</div>
    <div id="down" style="position:absolute;z-index:500000;left:100px;top:40px;" class="nav">down</div>
    <div id="left" style="position:absolute;z-index:500000;left:100px;top:60px;" class="nav">left</div>
    <div id="right" style="position:absolute;z-index:500000;left:100px;top:80px;" class="nav">right</div>
    <div id="test" style="position:absolute;z-index:500000;left:100px;top:100px;"> </div>
    <script>
        var _interval = 0;
        $(document).ready(function(){
        $('.nav').mouseover(function() {
            switch($(this).prop('id')) {
                case 'up':
                    anim = 1;
                    animatex('up');
                break;
            }
        });

        $('.nav').mouseout(
            function () {
                anim = 0;
            }
        );
        });
        </script>
    </body>
</html>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T15:27:00+00:00Added an answer on June 18, 2026 at 3:27 pm

    cubeGeo was a geometry, not a mesh, so the setting is.

    cubeGeo.uvsNeedUpdate = true;
    

    And a fiddle

    http://jsfiddle.net/crossphire/JebXL/2/

    Here is the modified example

    <html><head>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>  
    </head><body>
    <script src="https://raw.github.com/mrdoob/three.js/master/build/three.min.js"></script>
    <script src="https://raw.github.com/mrdoob/three.js/master/examples/js/libs/stats.min.js"> </script>
    
    <script>
            var cubeGeo;
            var materials = [];
            var container, stats;
            var camera, scene, renderer;
            var cube, plane;
    
            var targetRotation = 0;
            var targetRotationOnMouseDown = 0;
    
            var mouseX = 0;
            var mouseXOnMouseDown = 0;
    
            var windowHalfX = window.innerWidth / 2;
            var windowHalfY = window.innerHeight / 2;
    
            init();
            animate();
            function init() {
                container = document.createElement( 'div' );
                document.body.appendChild( container );
    
                var info = document.createElement( 'div' );
                info.style.position = 'absolute';
                info.style.top = '10px';
                info.style.width = '100%';
                info.style.textAlign = 'center';
                info.innerHTML = 'Drag to spin the cube';
                container.appendChild( info );
    
                camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 1000 );
                camera.position.y = 150;
                camera.position.z = 500;
    
                scene = new THREE.Scene();
    
        // add subtle ambient lighting
        var ambientLight = new THREE.AmbientLight(0x555555);
        scene.add(ambientLight);
    
        // add directional light source
        var directionalLight = new THREE.DirectionalLight(0xEEEEEE);
        directionalLight.position.set(1, 1, 1).normalize();
        scene.add(directionalLight);
    
                // Cube
                for ( var i = 0; i < 6; i ++ ) {
                    var img = new Image();
                    img.src = 'test.png';
                    var tex = new THREE.Texture(img);
                    img.tex = tex;
                    img.onload = function() { this.tex.needsUpdate = true; };
                    var mat = new THREE.MeshLambertMaterial({map: tex});
                    materials.push(mat);
                }
    
                cubeGeo = new THREE.CubeGeometry(200,200,200,1,1,1);
                cubeGeo.uvsNeedUpdate = true;
                cubeGeo.dynamic = true;
    
                cube = new THREE.Mesh( cubeGeo, new THREE.MeshFaceMaterial( materials ) );
    
                cube.position.y = 150;
                scene.add( cube );
    
                renderer = new THREE.WebGLRenderer({antialias:true});
                renderer.setSize( window.innerWidth, window.innerHeight );
    
                // enable shadows on the renderer
                renderer.shadowMapEnabled = true;
                container.appendChild( renderer.domElement );
    
                stats = new Stats();
                stats.domElement.style.position = 'absolute';
                stats.domElement.style.top = '0px';
                container.appendChild( stats.domElement );
    
                document.addEventListener( 'mousedown', onDocumentMouseDown, false );
                document.addEventListener( 'touchstart', onDocumentTouchStart, false );
                document.addEventListener( 'touchmove', onDocumentTouchMove, 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 onDocumentMouseDown( event ) {
                event.preventDefault();
    
                document.addEventListener( 'mousemove', onDocumentMouseMove, false );
                document.addEventListener( 'mouseup', onDocumentMouseUp, false );
                document.addEventListener( 'mouseout', onDocumentMouseOut, false );
    
                mouseXOnMouseDown = event.clientX - windowHalfX;
                targetRotationOnMouseDown = targetRotation;
            }
    
            function onDocumentMouseMove( event ) {
                mouseX = event.clientX - windowHalfX;
                targetRotation = targetRotationOnMouseDown + ( mouseX - mouseXOnMouseDown ) * 0.02;
            }
    
            function onDocumentMouseUp( event ) {
                document.removeEventListener( 'mousemove', onDocumentMouseMove, false );
                document.removeEventListener( 'mouseup', onDocumentMouseUp, false );
                document.removeEventListener( 'mouseout', onDocumentMouseOut, false );
            }
    
            function onDocumentMouseOut( event ) {
                document.removeEventListener( 'mousemove', onDocumentMouseMove, false );
                document.removeEventListener( 'mouseup', onDocumentMouseUp, false );
                document.removeEventListener( 'mouseout', onDocumentMouseOut, false );
            }
    
            function onDocumentTouchStart( event ) {
                if ( event.touches.length === 1 ) {
                    event.preventDefault();
                    mouseXOnMouseDown = event.touches[ 0 ].pageX - windowHalfX;
                    targetRotationOnMouseDown = targetRotation;
                }
            }
    
            function onDocumentTouchMove( event ) {
                if ( event.touches.length === 1 ) {
                    event.preventDefault();
                    mouseX = event.touches[ 0 ].pageX - windowHalfX;
                    targetRotation = targetRotationOnMouseDown + ( mouseX - mouseXOnMouseDown ) * 0.05;
                }
            }
    
    
            function animatex(dir) {
                cubeGeo.faceVertexUvs[0][5] = [new THREE.UV(1, 0), new THREE.UV(1, 1), new THREE.UV(0, 1), new THREE.UV(0, 0)];
                cubeGeo.uvsNeedUpdate = true;
                animate2(dir);
            }
    
            var anim = 0;
            function animate2(dir) {
                    requestAnimationFrame(function() {
                        cubeGeo.faceVertexUvs[0][5] = [new THREE.UV(1, 0), new THREE.UV(1, 1), new THREE.UV(0, 1), new THREE.UV(0, 0)];
                        cubeGeo.uvsNeedUpdate = true;
                        if(anim == 1) {
                            animate2(dir);
                        }
                    });
                    render2();
                    stats.update();
            }
    
            function render2() {
                cube.rotation.x = cube.rotation.x+0.06;
                renderer.render( scene, camera );
            }
    
            function animate() {
                requestAnimationFrame( animate );
                render();
                stats.update();
    
            }
    
            function render() {
                cube.rotation.y += ( targetRotation - cube.rotation.y ) * 0.05;
                $('#test').html(cube.rotation.y);
                renderer.render( scene, camera );
            }
    
        </script>
        <div id="up" style="position:absolute;z-index:500000;left:100px;top:20px;" class="nav">up</div>
        <div id="down" style="position:absolute;z-index:500000;left:100px;top:40px;" class="nav">down</div>
        <div id="left" style="position:absolute;z-index:500000;left:100px;top:60px;" class="nav">left</div>
        <div id="right" style="position:absolute;z-index:500000;left:100px;top:80px;" class="nav">right</div>
        <div id="test" style="position:absolute;z-index:500000;left:100px;top:100px;"> </div>
        <script>
            var _interval = 0;
            $(document).ready(function(){
            $('.nav').mouseover(function() {
                switch($(this).prop('id')) {
                    case 'up':
                        anim = 1;
                        animatex('up');
                    break;
                }
            });
    
            $('.nav').mouseout(
                function () {
                    anim = 0;
                }
            );
            });
            </script>
        </body>
    </html>
    
    
    
    
    mesh.geometry.uvsNeedUpdate = true;
    

    This link may help. https://github.com/mrdoob/three.js/issues/667

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Currently, I'm trying to rotate 3D Cube using orientation sensor values, using getRotation() method.
I’m currently trying to make my application using some Async methods. All my IO
so I'm currently trying to make an OpenID provider. I've tried using two Java
I'm currently trying to make a register form using mongoDB and nodeJS - I've
im currently trying to make a simple IRC Gui Client. Im using the SmartIrc4net
I am currently trying to make a simple crawler in python using Scrapey. What
I'm currently trying to make Log4J log into a JTextPane . I want to
So I'm currently trying to make a project using ravendb. And some basic moving
I am currently trying to make a e-zine using wordpress, and have most of
I'm currently trying to make a simple paint program in C# using a Windows

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.