| | 1 | | using System.Collections.Generic; |
| | 2 | | using UnityEngine; |
| | 3 | |
|
| | 4 | | public class AvatarAnchorPoints : IAvatarAnchorPoints |
| | 5 | | { |
| 1 | 6 | | private static readonly Dictionary<AvatarAnchorPointIds, string> boneMapping = new () |
| | 7 | | { |
| | 8 | | { AvatarAnchorPointIds.LeftHand, "Avatar_LeftHand" }, |
| | 9 | | { AvatarAnchorPointIds.RightHand, "Avatar_RightHand" }, |
| | 10 | | { AvatarAnchorPointIds.Head, "Avatar_Head" }, |
| | 11 | | { AvatarAnchorPointIds.Neck, "Avatar_Neck" }, |
| | 12 | | { AvatarAnchorPointIds.Spine, "Avatar_Spine" }, |
| | 13 | | { AvatarAnchorPointIds.Spine1, "Avatar_Spine1" }, |
| | 14 | | { AvatarAnchorPointIds.Spine2, "Avatar_Spine2" }, |
| | 15 | | { AvatarAnchorPointIds.Hip, "Avatar_Hip" }, |
| | 16 | | { AvatarAnchorPointIds.LeftShoulder, "Avatar_LeftShoulder" }, |
| | 17 | | { AvatarAnchorPointIds.LeftArm, "Avatar_LeftArm" }, |
| | 18 | | { AvatarAnchorPointIds.LeftForearm, "Avatar_LeftForearm" }, |
| | 19 | | { AvatarAnchorPointIds.LeftHandIndex, "Avatar_LeftHandIndex" }, |
| | 20 | | { AvatarAnchorPointIds.RightShoulder, "Avatar_RightShoulder" }, |
| | 21 | | { AvatarAnchorPointIds.RightArm, "Avatar_RightArm" }, |
| | 22 | | { AvatarAnchorPointIds.RightForearm, "Avatar_RightForearm" }, |
| | 23 | | { AvatarAnchorPointIds.RightHandIndex, "Avatar_RightHandIndex" }, |
| | 24 | | { AvatarAnchorPointIds.LeftUpLeg, "Avatar_LeftUpLeg" }, |
| | 25 | | { AvatarAnchorPointIds.LeftLeg, "Avatar_LeftLeg" }, |
| | 26 | | { AvatarAnchorPointIds.LeftFoot, "Avatar_LeftFoot" }, |
| | 27 | | { AvatarAnchorPointIds.LeftToeBase, "Avatar_LeftToeBase" }, |
| | 28 | | { AvatarAnchorPointIds.RightUpLeg, "Avatar_RightUpLeg" }, |
| | 29 | | { AvatarAnchorPointIds.RightLeg, "Avatar_RightLeg" }, |
| | 30 | | { AvatarAnchorPointIds.RightFoot, "Avatar_RightFoot" }, |
| | 31 | | { AvatarAnchorPointIds.RightToeBase, "Avatar_RightToeBase" }, |
| | 32 | | }; |
| | 33 | |
|
| 314 | 34 | | private readonly Dictionary<AvatarAnchorPointIds, Transform> boneTransformMapping = new (); |
| | 35 | |
|
| | 36 | | private Transform avatarTransform; |
| | 37 | | private float nameTagY; |
| | 38 | |
|
| | 39 | | void IAvatarAnchorPoints.Prepare(Transform avatarTransform, (string AnchorName, Transform Bone)[] anchors, float nam |
| | 40 | | { |
| 0 | 41 | | this.avatarTransform = avatarTransform; |
| 0 | 42 | | this.nameTagY = nameTagY; |
| | 43 | |
|
| 0 | 44 | | if (anchors == null) |
| 0 | 45 | | return; |
| | 46 | |
|
| 0 | 47 | | foreach ((string AnchorName, Transform Bone) anchor in anchors) |
| 0 | 48 | | foreach (KeyValuePair<AvatarAnchorPointIds, string> boneMap in boneMapping) |
| 0 | 49 | | if (anchor.AnchorName == boneMap.Value) |
| 0 | 50 | | boneTransformMapping[boneMap.Key] = anchor.Bone; |
| 0 | 51 | | } |
| | 52 | |
|
| | 53 | | (Vector3 position, Quaternion rotation, Vector3 scale) IAvatarAnchorPoints.GetTransform(AvatarAnchorPointIds anchorP |
| | 54 | | { |
| 0 | 55 | | if (anchorPointId == AvatarAnchorPointIds.Position && avatarTransform != null) |
| | 56 | | { |
| 0 | 57 | | return (avatarTransform.position, avatarTransform.rotation, Vector3.one); |
| | 58 | | } |
| | 59 | |
|
| 0 | 60 | | if (anchorPointId == AvatarAnchorPointIds.NameTag && avatarTransform != null) |
| | 61 | | { |
| 0 | 62 | | return (avatarTransform.position + Vector3.up * nameTagY, avatarTransform.rotation, Vector3.one); |
| | 63 | | } |
| | 64 | |
|
| 0 | 65 | | if (boneTransformMapping.TryGetValue(anchorPointId, out Transform bone)) |
| | 66 | | { |
| 0 | 67 | | if (bone != null) |
| | 68 | | { |
| 0 | 69 | | return (bone.position, bone.rotation, bone.lossyScale); |
| | 70 | | } |
| | 71 | | } |
| 0 | 72 | | return (Vector3.zero, Quaternion.identity, Vector3.one); |
| | 73 | | } |
| | 74 | | } |