修改脚本

This commit is contained in:
2026-03-22 09:33:56 +08:00
parent 1e1fec02cd
commit ba5da38dde
127 changed files with 31514 additions and 22327 deletions

View File

@@ -6,43 +6,31 @@ namespace CC
{
public class CopyPose : MonoBehaviour
{
private Transform[] SourceHierarchy;
private Transform[] TargetHierarchy;
public List<Transform> SourceBones = new List<Transform>();
public List<Transform> TargetBones = new List<Transform>();
private void Start()
public void ParseBones(Dictionary<string, Transform> mainMeshBoneMap)
{
//Get meshes
var mainScript = GetComponentInParent<CharacterCustomization>();
var sourceMesh = mainScript.MainMesh;
var targetMeshes = gameObject.GetComponentsInChildren<SkinnedMeshRenderer>();
var targetHierarchy = GetRootBone(GetComponentInChildren<SkinnedMeshRenderer>().rootBone).GetComponentsInChildren<Transform>();
//Copy bounds from character
var LODGroup = GetComponentInChildren<LODGroup>();
var mainLODGroup = mainScript.GetComponent<LODGroup>();
if (LODGroup != null) { LODGroup.RecalculateBounds(); LODGroup.size = (mainLODGroup == null) ? 1.5f : mainLODGroup.size; }
var targetBonesDict = targetHierarchy.ToDictionary(t => t.name, t => t);
foreach (var mesh in targetMeshes)
foreach (var (boneName, sourceBone) in mainMeshBoneMap)
{
mesh.localBounds = sourceMesh.localBounds;
if (!targetBonesDict.TryGetValue(boneName, out var targetBone)) continue;
//Re-parent & reset local transform
targetBone.SetParent(sourceBone);
targetBone.localPosition = Vector3.zero;
targetBone.localRotation = Quaternion.identity;
targetBone.localScale = Vector3.one;
if (!TargetBones.Contains(targetBone)) TargetBones.Add(targetBone);
}
//Get bone hierarchies
SourceHierarchy = sourceMesh.rootBone.GetComponentsInChildren<Transform>();
TargetHierarchy = GetRootBone(targetMeshes[0].rootBone).GetComponentsInChildren<Transform>();
var targetBonesDict = TargetHierarchy.ToDictionary(t => t.name, t => t);
//Only copy bones that are found in both hierarchies, also ensures order is the same
foreach (Transform child in SourceHierarchy)
//Rename bones to avoid duplicate object names
foreach (var bone in targetHierarchy)
{
//Check if a bone with the same name exists in the target hierarchy using the dictionary
if (targetBonesDict.TryGetValue(child.name, out var targetBone))
{
SourceBones.Add(child);
TargetBones.Add(targetBone);
}
bone.name += "_" + GetInstanceID();
}
}
@@ -52,14 +40,16 @@ namespace CC
return GetRootBone(bone.parent);
}
private void LateUpdate()
private void OnDestroy()
{
//Copy bone transform
for (int i = 0; i < SourceBones.Count; i++)
if (TargetBones == null) return;
foreach (Transform t in TargetBones)
{
TargetBones[i].localPosition = SourceBones[i].localPosition;
TargetBones[i].localRotation = SourceBones[i].localRotation;
TargetBones[i].localScale = SourceBones[i].localScale;
if (t != null)
{
Destroy(t.gameObject);
}
}
}
}