Loading files
This file is pinned to the selected release. Download the original to open it in your design tool.
cd90feda4This file is pinned to the selected release. Download the original to open it in your design tool.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>加载带动画的glTF模型 - iThreeJS</title>
<style type="text/css">
* {margin:0;padding:0;}
html, body {overflow:hidden;}
#loading {position:absolute;left:50%;top:50%;z-index:99;
margin-left:-100px;margin-top:-100px;width:200px;height:200px;
line-height:200px;text-align:center;font-size:18px;color:#fff;}
#loadingPercent {font-size:96px;}
</style>
</head>
<body>
<div id="loading"><span id="loadingPercent">0</span>%</div>
<script type="text/javascript" src="../../assets/js/three.min.js"></script>
<script type="text/javascript" src="../../assets/js/ctrls/OrbitControls.js"></script>
<script type="text/javascript" src="../../assets/js/loaders/GLTFLoader.js"></script>
<script type="text/javascript" src="../../assets/js/utils/stats.min.js"></script>
<script type="text/javascript" src="../js/util.js"></script>
<script type="text/javascript">
const w = window.innerWidth;
const h = window.innerHeight;
let stats;
let renderer, scene, camera;
let mixer;
const clock = new THREE.Clock();
init();
function init() {
stats = initStats();
renderer = new THREE.WebGLRenderer({
antialias: true
});
renderer.setSize(w, h);
renderer.setClearColor(0x000000, 1);
document.body.appendChild(renderer.domElement);
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera(45, w / h, 1, 1000);
camera.position.set(-200, 400, -400);
camera.lookAt(scene.position);
const aLight = new THREE.AmbientLight(0xffffff, 1);
scene.add(aLight);
// const aHelper = new THREE.AxesHelper(500);
// scene.add(aHelper);
const ctrl = new THREE.OrbitControls(camera, renderer.domElement);
window.addEventListener('resize', onWindowResize);
loadModel();
render();
}
function loadModel() {
const loader = new THREE.GLTFLoader();
loader.load('../../assets/models/gltf/phoenix/scene.gltf',
function(model) {
model.scene.scale.set(0.3, 0.3, 0.3);
mixer = new THREE.AnimationMixer(model.scene);
let action = mixer.clipAction(model.animations[0]);
action.play();
scene.add(model.scene);
},
function(xhr) {
let per = (xhr.loaded / xhr.total * 100).toFixed(0);
if(per < 100) {
document.getElementById("loadingPercent").innerHTML = per;
}else {
if(document.getElementById("loading")) {
document.getElementById("loading").remove();
}
}
},
function(err) {
console.error('Error: ', err);
});
}
function render() {
requestAnimationFrame(render);
if(mixer) {
mixer.update(clock.getDelta());
}
renderer.render(scene, camera);
stats.update();
}
function onWindowResize() {
renderer.setSize(window.innerWidth, window.innerHeight);
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
render();
}
</script>
</body>
</html>