JSFiddle - React, Tailwind, and code Playground
HTML
<script src="https://cdn.jsdelivr.net/npm/three@0.123/build/three.js"></script>
<script src="https://cdn.jsdelivr.net/npm/three@0.123/examples/js/loaders/GLTFLoader.js"></script>
<script src="https://cdn.jsdelivr.net/npm/three@0.123/examples/js/controls/OrbitControls.js"></script>
JavaScript
let camera, scene, renderer, clock, mixer;
init();
animate();
function init() {
camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.01, 10 );
camera.position.set( 2, 2, - 2 );
clock = new THREE.Clock();
scene = new THREE.Scene();
scene.background = new THREE.Color( 0xffffff );
const light = new THREE.HemisphereLight( 0xbbbbff, 0x444422 );
light.position.set( 0, 1, 0 );
scene.add( light );
// model
const loader = new THREE.GLTFLoader();
loader.load( 'https://threejs.org/examples/models/gltf/Soldier.glb', function ( gltf ) {
const model = gltf.scene;
mixer = new THREE.AnimationMixer( model );
const idleClip = gltf.animations[ 0 ];
const walkClip = gltf.animations[ 3 ];
const idleAction = mixer.clipAction( idleClip );
const walkAction = mixer.clipAction( walkClip );
idleAction.play();
setTimeout( function() {
walkAction.play();
idleAction.crossFadeTo( walkAction, 1 );
}, 2000 );
scene.add( model );
} );
renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.gammaOutput = true;
renderer.gammaFactor = 2.2;
document.body.appendChild( renderer.domElement );
window.addEventListener( 'resize', onWindowResize, false );
const controls = new THREE.OrbitControls( camera, renderer.domElement );
controls.target.set( 0, 1, 0 );
controls.update();
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
}
function animate() {
requestAnimationFrame( animate );
if ( mixer ) {
mixer.update( clock.getDelta() );
}
renderer.render( scene, camera );
}