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
<!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>