Search code examples
javascriptanimationthree.jsazimuth

Three.js: rotate an object back and forth between two azimuth angles


I have a 3D object loaded with three.js that should be seen only from the front since it is a single plane and from the back it is transparent ...

With the orbitContronls I limit the excursion of the azimuth and the polar angle ...

To make the 3D appealing it should start rotating ...

function animate() {
    if ( mesh ) {
        mesh.rotation.y += .005;
    }
    requestAnimationFrame( animate );
    render();
}

how do I limit the motion between -90° and 90° back and forth ?


Solution

  • You can use Math.sin()

    function animate() {
        requestAnimationFrame( animate );
    
        if ( mesh ) {
            mesh.rotation.y = Math.sin(Date.now() * 0.001) * Math.PI * 0.5;
        }
    
        render();
    }