Skip to content

Commit

Permalink
feat(vrc): add support for vrc parentconstraints
Browse files Browse the repository at this point in the history
  • Loading branch information
poi-vrc committed Oct 17, 2024
1 parent bdb5d0b commit a0bbcbd
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 18 deletions.
81 changes: 65 additions & 16 deletions Editor/Passes/Modifiers/ObjectMappingPass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,72 @@ private static void RemoveExistingPrefixSuffix(Transform trans)
}
}

private static void ScanParentConstraints(Transform transform, Dictionary<Transform, HashSet<Component>> foundPc)
{
if (transform.TryGetComponent<ParentConstraint>(out var pcComp))
{
if (!foundPc.TryGetValue(transform, out var comps))
{
comps = foundPc[transform] = new HashSet<Component>();
}
comps.Add(pcComp);
}

#if DT_VRCSDK3A_3_7_0_OR_LATER
if (transform.TryGetComponent<VRC.SDK3.Dynamics.Constraint.Components.VRCParentConstraint>(out var vrcPcComp))
{
var targetTrans = vrcPcComp.TargetTransform == null ? vrcPcComp.transform : vrcPcComp.TargetTransform;
if (!foundPc.TryGetValue(targetTrans, out var comps))
{
comps = foundPc[targetTrans] = new HashSet<Component>();
}
comps.Add(vrcPcComp);
}
#endif

foreach (Transform child in transform)
{
ScanParentConstraints(child, foundPc);
}
}

private static void AddParentConstraint(Context ctx, Transform sourceTransform, Transform targetTransform, Dictionary<Transform, HashSet<Component>> foundPc)
{
if (foundPc.TryGetValue(sourceTransform, out var existingComps))
{
ctx.Report.LogWarn(LogLabel, $"Existing ParentConstraint (count {existingComps.Count}) in {sourceTransform.name} detected, destroying it to avoid issues.");
foreach (var existingComp in existingComps)
{
Object.DestroyImmediate(existingComp);
}
}

#if DT_VRCSDK3A_3_7_0_OR_LATER
var vrcPcComp = sourceTransform.gameObject.AddComponent<VRC.SDK3.Dynamics.Constraint.Components.VRCParentConstraint>();
vrcPcComp.IsActive = true;
vrcPcComp.TargetTransform = targetTransform;
vrcPcComp.Sources.Add(new VRC.Dynamics.VRCConstraintSource
{
SourceTransform = targetTransform,
Weight = 1
});
#else
var pcComp = sourceTransform.gameObject.AddComponent<ParentConstraint>();
pcComp.constraintActive = true;
var source = new ConstraintSource
{
sourceTransform = targetTransform,
weight = 1
};
pcComp.AddSource(source);
#endif
}

private static void ApplyObjectMappings(Context ctx, DTObjectMapping objMappingComp)
{
var foundPc = new Dictionary<Transform, HashSet<Component>>();
ScanParentConstraints(ctx.AvatarGameObject.transform, foundPc);

var pathRemapper = ctx.Feature<PathRemapper>();
foreach (var mapping in objMappingComp.Mappings)
{
Expand Down Expand Up @@ -121,22 +185,7 @@ private static void ApplyObjectMappings(Context ctx, DTObjectMapping objMappingC
}
else if (mapping.Type == DTObjectMapping.Mapping.MappingType.ParentConstraint)
{
if (mapping.SourceTransform.TryGetComponent<ParentConstraint>(out var existingPcComp))
{
ctx.Report.LogWarn(LogLabel, $"Existing ParentConstraint in {mapping.SourceTransform.name} detected, destroying it to avoid issues.");
Object.DestroyImmediate(existingPcComp);
}

var pcComp = mapping.SourceTransform.gameObject.AddComponent<ParentConstraint>();

pcComp.constraintActive = true;

var source = new ConstraintSource
{
sourceTransform = targetTransform,
weight = 1
};
pcComp.AddSource(source);
AddParentConstraint(ctx, mapping.SourceTransform, targetTransform, foundPc);
}
else if (mapping.Type == DTObjectMapping.Mapping.MappingType.IgnoreTransform)
{
Expand Down
8 changes: 7 additions & 1 deletion Editor/com.chocopoi.vrc.dressingtools.Editor.asmdef
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"name": "com.chocopoi.vrc.dressingtools.Editor",
"rootNamespace": "",
"references": [
"GUID:85e555d1cc87ace4d8e14fcfe9062453",
"GUID:97650872dc498bb4597e0bbf23109900",
Expand Down Expand Up @@ -30,7 +31,12 @@
"name": "nadena.dev.ndmf",
"expression": "[1.3.0,2.0.0)",
"define": "DT_NDMF"
},
{
"name": "com.vrchat.avatars",
"expression": "3.7.0",
"define": "DT_VRCSDK3A_3_7_0_OR_LATER"
}
],
"noEngineReferences": false
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "com.chocopoi.vrc.dressingtools",
"displayName": "DressingTools",
"version": "2.5.1-beta.1",
"version": "2.6.0-beta.1",
"unity": "2019.4",
"description": "A simple but advanced, non-destructive cabinet system.",
"author": {
Expand Down

0 comments on commit a0bbcbd

Please sign in to comment.