Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(vrc): add support for vrc parentconstraints #434

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading