当前位置: 首页 > article >正文

【动画】unity中实现骨骼蒙皮动画

我是一名资深的游戏客户端,没事的时候我就想手搓轮子

本文目标

搓一个骨骼动画的核心实现,促进理解骨骼动画本质

骨骼动画简介

官方解释上网搜或者问豆包

快速理解

想知道骨骼动画怎么个事要先知道模型是怎么个事
简单来说:模型 = 顶点数据 + 三角形 + 材质
骨骼动画 = 模型 + 骨骼权重数据
骨骼权重数据是编辑器下由美术编辑出来的数据,指明某个顶点受到几根骨骼影响,每个骨骼权重是什么
在unity里,骨骼就是transform

思路

  • 在unity里自己实现一个组件,自己做骨骼对顶点的影响,完成蒙皮
  • 骨骼的transform驱动还靠unity的Animation,在自己的组件里获取骨骼transform就够了
  • 参照SkinnedMeshRenderer设计自己的组件
  • 骨骼动画核心公式,求顶点位置:skinnedVertex = 加权求和(骨骼local2world矩阵 * 顶点初始位置矩阵 * vertex * weight)
  • 只要能拿到顶点,骨骼transform,顶点初始位置矩阵,权重数据,就可以求出来模型某个顶点在动画中某一帧的位置

准备数据

  • 模型,fbx模型文件的mesh
  • 获取骨骼
深度遍历根骨骼的子节点是不行的,里面有用不上的transform不是骨骼,得解析fbx模型文件
FBX模型文件里有骨骼数据形如:Deformer: "SubDeformer::Cluster DummyMesh B-hips", "Cluster"
解析fbx费事,不是核心代码,用SkinnedMeshRenderer的接口bones获取
  • 权重数据,用Mesh的boneWeights接口,一个顶点最多受到4个骨骼影响,基本够用
  • 顶点初始位置矩阵,用Mesh的bindposes接口获得

开搓

using UnityEngine;namespace HotPlayer.Demos
{[ExecuteInEditMode]public class HotSkinnedMeshRenderer : MonoBehaviour{public Mesh mesh;public Material material;public bool castShadow = true;public bool receiveShadow = true;public bool useLightProbes = true;private Mesh skinnedMesh;private Vector3[] vertices;private BoneWeight[] boneWeights;private Transform[] bones;void Start(){skinnedMesh = Instantiate(mesh);skinnedMesh.name = mesh.name + "_Skinned";boneWeights = skinnedMesh.boneWeights;vertices = skinnedMesh.vertices;bones = gameObject.GetComponent<SkinnedMeshRenderer>().bones;}private void OnDestroy(){if (skinnedMesh != null){if (Application.isPlaying){Destroy(skinnedMesh);}else{DestroyImmediate(skinnedMesh);}}}private void UpdateVertices(){for (var i = 0; i < boneWeights.Length; i++){BoneWeight boneWeight = boneWeights[i];Vector3 vertex = mesh.vertices[i];Vector3 skinnedVertex = Vector3.zero;var index = boneWeight.boneIndex0;Transform bone;if (index > 0){bone = bones[index];var boneMatrix = bone.localToWorldMatrix * mesh.bindposes[index];skinnedVertex += boneMatrix.MultiplyPoint3x4(vertex) * boneWeight.weight0;}index = boneWeight.boneIndex1;if (index > 0){bone = bones[index];var boneMatrix = bone.localToWorldMatrix * mesh.bindposes[index];skinnedVertex += boneMatrix.MultiplyPoint3x4(vertex) * boneWeight.weight1;}index = boneWeight.boneIndex2;if (index > 0){bone = bones[index];var boneMatrix = bone.localToWorldMatrix * mesh.bindposes[index];skinnedVertex += boneMatrix.MultiplyPoint3x4(vertex) * boneWeight.weight2;}index = boneWeight.boneIndex3;if (index > 0){bone = bones[index];var boneMatrix = bone.localToWorldMatrix * mesh.bindposes[index];skinnedVertex += boneMatrix.MultiplyPoint3x4(vertex) * boneWeight.weight3;}vertices[i] = skinnedVertex;}skinnedMesh.vertices = vertices;skinnedMesh.RecalculateBounds();skinnedMesh.RecalculateNormals();}void Update(){#if UNITY_EDITORif (Application.isPlaying)DrawSkinMesh();elseGraphics.DrawMesh(mesh, transform.localToWorldMatrix, material, 0, null, 0, null, castShadow, receiveShadow, useLightProbes);
#elseDrawSkinMesh();
#endif}private void DrawSkinMesh(){UpdateVertices();Graphics.DrawMesh(skinnedMesh, transform.localToWorldMatrix, material, 0, null, 0, null, castShadow, receiveShadow, useLightProbes);}}
}

结语

点赞超64就再搓一个GPU版骨骼蒙皮动画

所以说学习好啊,得学啊,因为真男人必会手搓轮子

http://www.lryc.cn/news/2397493.html

相关文章:

  • VSCODE的终端无法执行npm命令
  • Langchian - 自定义提示词模板 提取结构化的数据
  • 【机器学习基础】机器学习入门核心:Jaccard相似度 (Jaccard Index) 和 Pearson相似度 (Pearson Correlation)
  • QT之头像剪裁效果实现
  • apptrace 视角下移动端深度链接技术与优势​
  • 微前端之micro-app数据通信
  • 【GPT入门】第40课 vllm与ollama特性对比,与模型部署
  • unity开发棋牌游戏
  • Nat Commun项目文章 ▏小麦CUTTag助力解析转录因子TaTCP6调控小麦氮磷高效利用机制
  • Qt OpenGL 相机实现
  • 云原生时代 Kafka 深度实践:03进阶特性与最佳实践
  • 基于关联表字段映射的批量数据更新 SQL 实现方案(AIGC)
  • Hadoop复习(二)
  • C 语言开发中常见的开发环境
  • vscode命令行debug
  • Matlab作图之 subplot
  • Springboot 项目一启动就获取HttpSession
  • PostgreSQL的扩展 insert_username
  • 【机器学习基础】机器学习入门核心算法:层次聚类算法(AGNES算法和 DIANA算法)
  • Google Play的最新安全变更可能会让一些高级用户无法使用App
  • 深度学习篇---人脸识别中的face-recognition库和深度学习
  • (11)java+ selenium->元素定位之By_tag_name
  • React---day5
  • Java开发之定时器学习
  • HealthBench医疗AI评估基准:技术路径与核心价值深度分析(上)
  • Windows+VSCode搭建小智(xiaozhi)开发环境
  • VueScan Pro v9.8.45.08 一款图像扫描软件,中文绿色便携版
  • FreeRTOS通俗理解指南:基础概念 + 架构+ 内核组件+练手实验
  • Python后端开发实战:从0到1搭建高可用API服务
  • 房屋租赁系统 Java+Vue.js+SpringBoot,包括房屋信息、看房申请、租赁合同、房屋报修、收租信息、维修数据、租客管理、公告管理模块