| | 1 | | using System.Collections.Generic; |
| | 2 | | using System.Linq; |
| | 3 | | using UnityEngine; |
| | 4 | |
|
| | 5 | | public class AvatarAnchorPoints : IAvatarAnchorPoints |
| | 6 | | { |
| 1 | 7 | | private static readonly Dictionary<AvatarAnchorPointIds, string> boneMapping = new Dictionary<AvatarAnchorPointIds, |
| | 8 | | { |
| | 9 | | { AvatarAnchorPointIds.LeftHand, "Avatar_LeftHand" }, |
| | 10 | | { AvatarAnchorPointIds.RightHand, "Avatar_RightHand" }, |
| | 11 | | }; |
| | 12 | |
|
| 687 | 13 | | private readonly Dictionary<AvatarAnchorPointIds, Transform> boneTransformMapping = new Dictionary<AvatarAnchorPoint |
| | 14 | |
|
| | 15 | | private Transform avatarTransform; |
| | 16 | | private float nameTagY; |
| | 17 | |
|
| | 18 | | void IAvatarAnchorPoints.Prepare(Transform avatarTransform, Transform[] bones, float nameTagY) |
| | 19 | | { |
| 0 | 20 | | this.avatarTransform = avatarTransform; |
| 0 | 21 | | this.nameTagY = nameTagY; |
| | 22 | |
|
| 0 | 23 | | boneTransformMapping.Clear(); |
| | 24 | |
|
| 0 | 25 | | foreach (var bone in bones) |
| | 26 | | { |
| 0 | 27 | | if (TryGetIdFromBoneName(bone.name, out AvatarAnchorPointIds anchorPointId)) |
| | 28 | | { |
| 0 | 29 | | boneTransformMapping.Add(anchorPointId, bone); |
| | 30 | | } |
| | 31 | | } |
| 0 | 32 | | } |
| | 33 | | (Vector3 position, Quaternion rotation, Vector3 scale) IAvatarAnchorPoints.GetTransform(AvatarAnchorPointIds anchorP |
| | 34 | | { |
| 0 | 35 | | if (anchorPointId == AvatarAnchorPointIds.Position && avatarTransform != null) |
| | 36 | | { |
| 0 | 37 | | return (avatarTransform.position, avatarTransform.rotation, Vector3.one); |
| | 38 | | } |
| | 39 | |
|
| 0 | 40 | | if (anchorPointId == AvatarAnchorPointIds.NameTag && avatarTransform != null) |
| | 41 | | { |
| 0 | 42 | | return (avatarTransform.position + Vector3.up * nameTagY, avatarTransform.rotation, Vector3.one); |
| | 43 | | } |
| | 44 | |
|
| 0 | 45 | | if (boneTransformMapping.TryGetValue(anchorPointId, out Transform bone)) |
| | 46 | | { |
| 0 | 47 | | if (bone != null) |
| | 48 | | { |
| 0 | 49 | | return (bone.position, bone.rotation, bone.lossyScale); |
| | 50 | | } |
| | 51 | | } |
| 0 | 52 | | return (Vector3.zero, Quaternion.identity, Vector3.one); |
| | 53 | | } |
| | 54 | |
|
| | 55 | | private static bool TryGetIdFromBoneName(string boneName, out AvatarAnchorPointIds id) |
| | 56 | | { |
| 0 | 57 | | var result = boneMapping.FirstOrDefault(pair => pair.Value == boneName); |
| 0 | 58 | | id = result.Key; |
| 0 | 59 | | return !string.IsNullOrEmpty(result.Value); |
| | 60 | | } |
| | 61 | | } |