ThreeJS导入gltf模型和切换动画

2024-02-12 12:30

本文主要是介绍ThreeJS导入gltf模型和切换动画,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

最近尝试把带有动画的gltf模型导入,并控制多个动画的切换播放。
效果如下:
请添加图片描述主要代码如下,没有定义的函数都是《Three.js开发指南》里的,借用了一下。

function init() {
//场景设置var stats = initStats();var renderer = initRenderer();renderer.setClearColor(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.set(0, 0, 10);scene.add(dirLight);camera.position.set(0, 0, 20);var orbitControls = new initOrbitControls(camera, renderer);var clock = new THREE.Clock();var actions = [];var mixer = new THREE.AnimationMixer();var clipActionvar animationClipvar meshvar controlsvar mixerControls = {time: 0,timeScale: 1,stopAllAction: function () { mixer.stopAllAction() },animation: 'None',}//导入模型和动画var loader = new THREE.GLTFLoader();loader.load('../../assets/punk_girl/scene.gltf', function (result) {//调整result.scene.scale.set(8, 8, 8);result.scene.translateY(-10);scene.add(result.scene)// setup the mixermixer = 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 controlsenableControls();});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", ['mixamo.com', 'Dancing', 'Idle', 'PettingAnimal', 'Talking', 'Walk', 'WavingGesture', 'None']).onChange(function (animation) {for (var i = 0; i < actions.length; i++) {if (animation == actions[i].getClip().name) {actions[i].play();}else {actions[i].stop();}}})}render();function render() {stats.update();var delta = clock.getDelta();orbitControls.update(delta);requestAnimationFrame(render);renderer.render(scene, camera)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)) ? parseInt(type) : 0;var stats = new Stats();stats.showPanel(panelType); // 0: fps, 1: ms, 2: mb, 3+: customdocument.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);renderer.shadowMap.enabled = true;renderer.shadowMapSoft = true;renderer.shadowMap.type = THREE.PCFSoftShadowMap;renderer.setClearColor(new THREE.Color(0x000000));renderer.setSize(window.innerWidth, window.innerHeight);renderer.shadowMap.enabled = true;document.getElementById("webgl-output").appendChild(renderer.domElement);return renderer;
}
/*** 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);camera.position.copy(position);camera.lookAt(new THREE.Vector3(0, 0, 0));return camera;
}function initOrbitControls(camera, renderer) {var orbitControls = new THREE.OrbitControls(camera, renderer.domElement);orbitControls.rotateSpeed = 1.0;orbitControls.zoomSpeed = 1.2;orbitControls.panSpeed = 0.8;orbitControls.noZoom = false;orbitControls.noPan = false;orbitControls.staticMoving = true;orbitControls.dynamicDampingFactor = 0.3;orbitControls.keys = [65, 83, 68];return orbitControls;
}

这篇关于ThreeJS导入gltf模型和切换动画的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/702593

相关文章

浅析Python中的绝对导入与相对导入

《浅析Python中的绝对导入与相对导入》这篇文章主要为大家详细介绍了Python中的绝对导入与相对导入的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1 Imports快速介绍2 import语句的语法2.1 基本使用2.2 导入声明的样式3 绝对import和相对i

C#集成DeepSeek模型实现AI私有化的流程步骤(本地部署与API调用教程)

《C#集成DeepSeek模型实现AI私有化的流程步骤(本地部署与API调用教程)》本文主要介绍了C#集成DeepSeek模型实现AI私有化的方法,包括搭建基础环境,如安装Ollama和下载DeepS... 目录前言搭建基础环境1、安装 Ollama2、下载 DeepSeek R1 模型客户端 ChatBo

SpringBoot快速接入OpenAI大模型的方法(JDK8)

《SpringBoot快速接入OpenAI大模型的方法(JDK8)》本文介绍了如何使用AI4J快速接入OpenAI大模型,并展示了如何实现流式与非流式的输出,以及对函数调用的使用,AI4J支持JDK8... 目录使用AI4J快速接入OpenAI大模型介绍AI4J-github快速使用创建SpringBoot

0基础租个硬件玩deepseek,蓝耘元生代智算云|本地部署DeepSeek R1模型的操作流程

《0基础租个硬件玩deepseek,蓝耘元生代智算云|本地部署DeepSeekR1模型的操作流程》DeepSeekR1模型凭借其强大的自然语言处理能力,在未来具有广阔的应用前景,有望在多个领域发... 目录0基础租个硬件玩deepseek,蓝耘元生代智算云|本地部署DeepSeek R1模型,3步搞定一个应

Deepseek R1模型本地化部署+API接口调用详细教程(释放AI生产力)

《DeepseekR1模型本地化部署+API接口调用详细教程(释放AI生产力)》本文介绍了本地部署DeepSeekR1模型和通过API调用将其集成到VSCode中的过程,作者详细步骤展示了如何下载和... 目录前言一、deepseek R1模型与chatGPT o1系列模型对比二、本地部署步骤1.安装oll

Spring AI Alibaba接入大模型时的依赖问题小结

《SpringAIAlibaba接入大模型时的依赖问题小结》文章介绍了如何在pom.xml文件中配置SpringAIAlibaba依赖,并提供了一个示例pom.xml文件,同时,建议将Maven仓... 目录(一)pom.XML文件:(二)application.yml配置文件(一)pom.xml文件:首

如何在本地部署 DeepSeek Janus Pro 文生图大模型

《如何在本地部署DeepSeekJanusPro文生图大模型》DeepSeekJanusPro模型在本地成功部署,支持图片理解和文生图功能,通过Gradio界面进行交互,展示了其强大的多模态处... 目录什么是 Janus Pro1. 安装 conda2. 创建 python 虚拟环境3. 克隆 janus

本地私有化部署DeepSeek模型的详细教程

《本地私有化部署DeepSeek模型的详细教程》DeepSeek模型是一种强大的语言模型,本地私有化部署可以让用户在自己的环境中安全、高效地使用该模型,避免数据传输到外部带来的安全风险,同时也能根据自... 目录一、引言二、环境准备(一)硬件要求(二)软件要求(三)创建虚拟环境三、安装依赖库四、获取 Dee

Oracle数据库如何切换登录用户(system和sys)

《Oracle数据库如何切换登录用户(system和sys)》文章介绍了如何使用SQL*Plus工具登录Oracle数据库的system用户,包括打开登录入口、输入用户名和口令、以及切换到sys用户的... 目录打开登录入口登录system用户总结打开登录入口win+R打开运行对话框,输php入:sqlp

DeepSeek模型本地部署的详细教程

《DeepSeek模型本地部署的详细教程》DeepSeek作为一款开源且性能强大的大语言模型,提供了灵活的本地部署方案,让用户能够在本地环境中高效运行模型,同时保护数据隐私,在本地成功部署DeepSe... 目录一、环境准备(一)硬件需求(二)软件依赖二、安装Ollama三、下载并部署DeepSeek模型选