ThreeJS导入gltf模型和切换动画

更新时间:2023-07-13 19:29:57 阅读: 评论:0

ThreeJS导⼊gltf模型和切换动画
最近尝试把带有动画的gltf模型导⼊,并控制多个动画的切换播放。
效果如下:
主要代码如下,没有定义的函数都是《Three.js开发指南》⾥的,借⽤了⼀下。
function init(){
//场景设置
var stats =initStats();
var renderer =initRenderer();
renderer.tClearColor(0x262626);
var camera =initCamera();
var scene =new THREE.Scene();
scene.add(new THREE.AmbientLight(0xFFFFFF,0.3));
var dirLight =new THREE.DirectionalLight(0xBBFFFF,2);
dirLight.position.t(0,0,10);
scene.add(dirLight);
camera.position.t(0,0,20);
var orbitControls =new initOrbitControls(camera, renderer);
var clock =new THREE.Clock();
var actions =[];
var mixer =new THREE.AnimationMixer();
var clipAction
var animationClip
var mesh
var controls
考核标准
var mixerControls ={
time:0,
timeScale:1,
stopAllAction:function(){ mixer.stopAllAction()},
animation:'None',
}
//导⼊模型和动画
var loader =new THREE.GLTFLoader();
var loader =new THREE.GLTFLoader();
loader.load('../../asts/punk_girl/scene.gltf',function(result){
//调整
result.scene.scale.t(8,8,8);
anslateY(-10);
scene.add(result.scene)
// tup the mixer
mixer =new THREE.AnimationMixer(result.scene);
//⽤actions储存所有动画
for(var i =0; i < result.animations.length; i++){
actions[i]= mixer.clipAction(result.animations[i]);
}
// add the animation controls
enableControls();
});
function enableControls(){
var gui =new dat.GUI();//⽤dat gui来实现控制组件
//⼀些控制选项
var mixerFolder = gui.addFolder("AnimationMixer")
mixerFolder.add(mixerControls,"time").listen()
mixerFolder.add(mixerControls,"timeScale",0,5).onChange(function(timeScale){ mixer.timeScale = timeScale });
mixerFolder.add(mixerControls,"stopAllAction").listen()
gui.add(mixerControls,"animation",['','Dancing','Idle','PettingAnimal','Talking','Walk','WavingGesture','None']).onChange(function(anim ation){
for(var i =0; i < actions.length; i++){
if(animation == actions[i].getClip().name){
actions[i].play();
}
el{
actions[i].stop();
}
}
})
睡虎地秦墓竹简}
render();
function render(){
stats.update();
var delta = Delta();
orbitControls.update(delta);
requestAnimationFrame(render);
mixer.update(delta);
}
}
借⽤的函数:
/**
* Initialize the statistics domelement
感受生命
*
* @param {Number} type 0: fps, 1: ms, 2: mb, 3+: custom
* @returns stats javascript object
*/
function initStats(type){鸡蛋番茄汤
var panelType =(typeof type !=='undefined'&& type)&&(!isNaN(type))?parInt(type):0;
var stats =new Stats();
stats.showPanel(panelType);// 0: fps, 1: ms, 2: mb, 3+: custom
document.body.appendChild(stats.dom);
return stats;
}
/
**
* Initialize a simple default renderer and binds it to the "webgl-output" dom
会计事务所实习* element.
*
* @param additionalProperties Additional properties to pass into the renderer
*/
function initRenderer(additionalProperties){
蝙蝠和雷达课文var props =(typeof additionalProperties !=='undefined'&& additionalProperties)? additionalProperties :{}; var renderer =new THREE.WebGLRenderer(props);
abled =true;
renderer.shadowMapSoft =true;
pe =THREE.PCFSoftShadowMap;
renderer.tClearColor(new THREE.Color(0x000000));
renderer.tSize(window.innerWidth, window.innerHeight);
abled =true;
return renderer;
backspace是哪个键
}
/**
* Initialize a simple camera and point it at the center of a scene
*
* @param {THREE.Vector3} [initialPosition]
*/
function initCamera(initialPosition){
var position =(initialPosition !==undefined)? initialPosition :new THREE.Vector3(-30,40,30);
var camera =new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight,0.1,1000);    py(position);
王颁camera.lookAt(new THREE.Vector3(0,0,0));
return camera;
}
function initOrbitControls(camera, renderer){
var orbitControls =new THREE.OrbitControls(camera, renderer.domElement);
orbitControls.panSpeed =0.8;
orbitControls.staticMoving =true;
orbitControls.dynamicDampingFactor =0.3;
orbitControls.keys =[65,83,68];
return orbitControls;
}

本文发布于:2023-07-13 19:29:57,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/89/1080207.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:动画   控制   切换   模型   没有   定义   实习   指南
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图