Rotation GLB file in only one axis

Hi there,

I’m developing a three.js page which my object in .glb is rotation in a simple animation one axis. I also want to rotate with mouse events.

Something like this truck : Transmit 5

Can someone help me :frowning:

<!DOCTYPE html>
<html lang="en">
	<head>
		<title>three.js webgl - glTF loader</title>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
		<style>
			body {
				font-family: Monospace;
				color: #fff;
				margin: 0px;
				overflow: hidden;
			}
			#info {
				color: #fff;
				position: absolute;
				top: 10px;
				width: 100%;
				text-align: center;
				z-index: 100;
				display:block;
			}
			#info a {
				color: #75ddc1;
				font-weight: bold;
			}
		</style>
	</head>

	<body>
		<div id="info">
			<a href="http://threejs.org" target="_blank" rel="noopener">1995</a> <br />
			<a href="" target="_blank" rel="noopener">NEXT</a> <br />
			<h2>1995</h2><br />
		</div>

		<script src="three/three.js"></script>

		<script src="three/OrbitControls.js"></script>
		<script src="three/GLTFLoader.js"></script>
		<script src="three/CameraHelper.js"></script>

		<script src="three/WebGL.js"></script>
		<script src="three/stats.min.js"></script>

		<script>
            
            
			if ( WEBGL.isWebGLAvailable() === false ) {
				document.body.appendChild( WEBGL.getWebGLErrorMessage() );
			}
			var container, stats, controls;
			var camera, scene, renderer, light;
			init();
			animate();
        
            
            
			function init() {
				container = document.createElement( 'div' );
				document.body.appendChild( container );
				camera = new THREE.PerspectiveCamera( 0.5, window.innerWidth / window.innerHeight, 0.25, 1000 );
				//camera.position.set( -1.8, 0.9, 2.7 );
                camera.position.x = 0;
                camera.position.y = 150;
                camera.position.z = 10;
				controls = new THREE.OrbitControls( camera );
				controls.target.set(0, 0, 0);
                controls.enableRotate = false;
                controls.enableRotate.x = true;
                controls.enableZoom = false;
                controls.enableKeys = false;
				controls.update();
                
				scene = new THREE.Scene();
				light = new THREE.HemisphereLight( 0xbbbbff, 0x444422 );
				light.position.set( 1, 1, 0 );
				scene.add( light );
				// model
				var loader = new THREE.GLTFLoader();
				loader.load( 'models/19955.glb', function ( gltf ) {
					gltf.scene.traverse( function ( child ) {
						if ( child.isMesh ) {
						}
					} );
                    
					scene.add( gltf.scene );
                    
                                    console.log("nome:",gltf.scene );
                      mesh = gltf.scene;
                      scene.add(mesh);


                    
                    mesh.rotation.y += 0.006;
                    
                    render();

				}, undefined, function ( e ) {
					console.error( e );
				} );

				renderer = new THREE.WebGLRenderer( { alpha: true } );
                                renderer.setClearColor( 0x000000, 0 );

				renderer.setPixelRatio( window.devicePixelRatio );
				renderer.setSize( window.innerWidth, window.innerHeight );
				renderer.gammaOutput = true;
				container.appendChild( renderer.domElement );
				window.addEventListener( 'resize', onWindowResize, false );
				// stats
				stats = new Stats();
				container.appendChild( stats.dom );
			}
			function onWindowResize() {
				camera.aspect = window.innerWidth / window.innerHeight;
				camera.updateProjectionMatrix();
				renderer.setSize( window.innerWidth, window.innerHeight );
			}
			//
			function animate() {
             
				requestAnimationFrame( animate );
				renderer.render( scene, camera );
				stats.update();
			}
                
                
                function render(){
                    


                if (mesh) {
                    mesh.rotation.z -= 0.01;
                }

                    requestAnimationFrame(render);
                    //WebGLRenderer.render(scene, camera);
                }
             
		</script>

	</body>
</html>

You can use OrbitControls for this use case. Just set the autoRotate property to true and update the control object in the animation loop. You can adjust the speed of the rotation via autoRotateSpeed.

https://jsfiddle.net/f2Lommf5/15204/

Using OrbitControls like this means, you are moving the camera around a target. So the object (cube) itself is static and does not rotate.

But I don’t want to rotate the camera, I only rotate in one axis the object. I’m sorry if what I said it was a little be stupid :smile:

I suggested this solution since your posted website does it exactly like that :wink:

But how the website I posted can rotate in only one axis? :thinking:

See https://jsfiddle.net/f2Lommf5/15208/

You just restrict the min and max polar angle on a fix value.

My problem is my object are rotate but another axis also rotate, and I only what to rotate the y axis in horizontal :frowning:

But i changed this

controls.maxPolarAngle = controls.maxPolarAngle = Math.PI * 0.8;

to minPolarAngle and works in the Z axis, but in the another axis they continue

I’m afraid I don’t understand what you mean. Can you illustrate your intended result with a sketch? Or maybe explain in more detail?

I alreay solved :wink:

I used this
controls.minPolarAngle = Math.PI * 0.5;
controls.maxPolarAngle = Math.PI * 0.5;