| 61 | } |
| 62 | |
| 63 | SkinnedModel::SkeletonMapping SkinnedModel::GetSkeletonMapping(Asset* source, bool autoRetarget) |
| 64 | { |
| 65 | // Fast-path to use cached mapping |
| 66 | SkeletonMapping mapping; |
| 67 | mapping.TargetSkeleton = this; |
| 68 | SkeletonMappingData mappingData; |
| 69 | if (_skeletonMappingCache.TryGet(source, mappingData)) |
| 70 | { |
| 71 | mapping.SourceSkeleton = mappingData.SourceSkeleton; |
| 72 | mapping.NodesMapping = mappingData.NodesMapping; |
| 73 | return mapping; |
| 74 | } |
| 75 | mapping.SourceSkeleton = nullptr; |
| 76 | |
| 77 | if (WaitForLoaded() || !source || source->WaitForLoaded()) |
| 78 | return mapping; |
| 79 | PROFILE_CPU(); |
| 80 | ScopeLock lock(Locker); |
| 81 | if (!_skeletonMappingCache.TryGet(source, mappingData)) |
| 82 | { |
| 83 | // Initialize the mapping |
| 84 | SkeletonRetarget* retarget = nullptr; |
| 85 | const Guid sourceId = source->GetID(); |
| 86 | for (auto& e : _skeletonRetargets) |
| 87 | { |
| 88 | if (e.SourceAsset == sourceId) |
| 89 | { |
| 90 | retarget = &e; |
| 91 | break; |
| 92 | } |
| 93 | } |
| 94 | if (!retarget && !autoRetarget) |
| 95 | { |
| 96 | // Skip automatic retarget |
| 97 | return mapping; |
| 98 | } |
| 99 | const int32 nodesCount = Skeleton.Nodes.Count(); |
| 100 | mappingData.NodesMapping = Span<int32>((int32*)Allocator::Allocate(nodesCount * sizeof(int32)), nodesCount); |
| 101 | for (int32 i = 0; i < nodesCount; i++) |
| 102 | mappingData.NodesMapping[i] = -1; |
| 103 | if (const auto* sourceAnim = Cast<Animation>(source)) |
| 104 | { |
| 105 | const auto& channels = sourceAnim->Data.Channels; |
| 106 | if (retarget && retarget->SkeletonAsset) |
| 107 | { |
| 108 | // Map retarget skeleton nodes from animation channels |
| 109 | if (auto* skeleton = Content::Load<SkinnedModel>(retarget->SkeletonAsset)) |
| 110 | { |
| 111 | const SkeletonMapping skeletonMapping = GetSkeletonMapping(skeleton); |
| 112 | mappingData.SourceSkeleton = skeleton; |
| 113 | if (skeletonMapping.NodesMapping.Length() == nodesCount) |
| 114 | { |
| 115 | const auto& nodes = skeleton->Skeleton.Nodes; |
| 116 | for (int32 j = 0; j < nodesCount; j++) |
| 117 | { |
| 118 | if (skeletonMapping.NodesMapping[j] != -1) |
| 119 | { |
| 120 | const Char* nodeName = nodes[skeletonMapping.NodesMapping[j]].Name.GetText(); |
no test coverage detected