化身 | 将从中读写HumanPose的化身。该化身必须是类人型的。 |
根 | 类人形avatar 中定义的骨架层次结构中最顶层的父级。这必须与avatar 定义相匹配。 |
jointPaths | 定义avatar 联合路径的列表。每个关节路径从根变换之后的节点开始,并沿着化身骨架层次结构向下。根变换关节路径是空字符串。 |
从avatar
和root
变换以及一系列关节路径中创建一个人体姿势处理器。
指定一个avatar
和一个root
变换以从骨架层次结构(化身)创建一个人体姿势。指定一个avatar
和一个关节路径列表以创建一个不绑定到骨架变换层次结构的人体姿势处理器。您可以使用从关节路径创建的人体姿势处理器将人体姿势转换为局部关节变换(jointPaths)的数组,或从中转换过来。
using UnityEngine; using System.Collections; using System.Collections.Generic; using Unity.Collections;
public class ExampleClass : MonoBehaviour { public Avatar avatar; public Transform avatarRoot;
void ParseAvatarTransformRecursive(Transform t, string parentPath, List<string> jointPaths, List<Transform> transforms) { string jointPath = parentPath.Length == 0 ? t.gameObject.name : parentPath + "/" + t.gameObject.name; jointPaths.Add(jointPath); transforms.Add(t);
foreach (Transform child in t) { ParseAvatarTransformRecursive(t, jointPath, jointPaths, transforms); } }
void ParseAvatarRootTransform(Transform rootTransform, List<string> jointPaths, List<Transform> transforms) { jointPaths.Add(""); // root tranform path is the empty string transforms.Add(rootTransform);
foreach (Transform t in rootTransform) { ParseAvatarTransformRecursive(t, "", jointPaths, transforms); } }
void Start() { List<string> jointPaths = new List<string>(); List<Transform> avatarTransforms = new List<Transform>(); ParseAvatarRootTransform(avatarRoot, jointPaths, avatarTransforms);
HumanPoseHandler humanPoseHandler = new HumanPoseHandler(avatar, jointPaths.ToArray()); NativeArray<float> avatarPose = new NativeArray<float>(jointPaths.Count * 7, Allocator.Persistent);
for (int i = 0; i < jointPaths.Count; ++i) { Vector3 position = avatarTransforms[i].localPosition; Quaternion rotation = avatarTransforms[i].localRotation; avatarPose[7 * i] = position.x; avatarPose[7 * i + 1] = position.y; avatarPose[7 * i + 2] = position.z; avatarPose[7 * i + 3] = rotation.x; avatarPose[7 * i + 4] = rotation.y; avatarPose[7 * i + 5] = rotation.z; avatarPose[7 * i + 6] = rotation.w; }
humanPoseHandler.SetInternalAvatarPose(avatarPose);
avatarPose.Dispose(); humanPoseHandler.Dispose(); } }
以上示例从一个化身和一个化身关节列表中创建了一个人体姿势处理器。该示例从化身根部变换中读取姿势,并将该姿势写入人体姿势处理器。