Skip to content

Commit ef26231

Browse files
sohailshafiiWkfacebook-github-bot
authored andcommitted
feat(Runtime): Support user-definable CustomProcessor
Summary: This change adds support for a `CustomProcessor`, which needs a reference to a class that inherits from `CustomProcessorBehavior`. Each custom `CustomProcessorBehavior` implementation can be implemented by a user so that they can define their own IK. Reviewed By: andkim-meta Differential Revision: D75054156 Privacy Context Container: L1117747 fbshipit-source-id: 89196c4fa366df0654eea05ba893e3a5a1570ed5
1 parent 80c3adc commit ef26231

9 files changed

Lines changed: 228 additions & 1 deletion

File tree

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// Copyright (c) Meta Platforms, Inc. and affiliates.
2+
3+
using UnityEditor;
4+
using UnityEngine;
5+
6+
namespace Meta.XR.Movement.Retargeting.Editor
7+
{
8+
/// <summary>
9+
/// Renders an instance of <see cref="CustomProcessor"/>.
10+
/// </summary>
11+
[CustomPropertyDrawer(typeof(CustomProcessor))]
12+
public class CustomProcessorDrawer : PropertyDrawer
13+
{
14+
private static readonly (string propertyName, GUIContent label)[] _propertiesToDraw =
15+
{
16+
("_weight", new GUIContent("Weight")),
17+
("_customBehavior", new GUIContent("Custom Behavior"))
18+
};
19+
20+
/// <inheritdoc cref="PropertyDrawer.OnGUI"/>
21+
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
22+
{
23+
EditorGUI.BeginProperty(position, label, property);
24+
var foldoutRect = new Rect(
25+
position.x,
26+
position.y - EditorGUIUtility.singleLineHeight * 1.1f,
27+
position.width,
28+
EditorGUIUtility.singleLineHeight);
29+
var isFoldoutExpanded = property.FindPropertyRelative("_isFoldoutExpanded");
30+
isFoldoutExpanded.boolValue = EditorGUI.Foldout(foldoutRect, isFoldoutExpanded.boolValue, GUIContent.none);
31+
32+
if (!isFoldoutExpanded.boolValue)
33+
{
34+
EditorGUI.EndProperty();
35+
return;
36+
}
37+
38+
EditorGUI.indentLevel++;
39+
var elementVerticalSpacing = GetVerticalSpacing();
40+
for (var i = 0; i < _propertiesToDraw.Length; i++)
41+
{
42+
var propertyRect = new Rect(
43+
position.x,
44+
position.y + elementVerticalSpacing * i,
45+
position.width,
46+
EditorGUIUtility.singleLineHeight);
47+
48+
var serializedProperty = property.FindPropertyRelative(_propertiesToDraw[i].propertyName);
49+
var serializedLabel = _propertiesToDraw[i].label;
50+
if (i == 0)
51+
{
52+
EditorGUI.PropertyField(propertyRect, serializedProperty, serializedLabel);
53+
}
54+
else
55+
{
56+
EditorGUI.ObjectField(propertyRect, serializedProperty, serializedLabel);
57+
}
58+
}
59+
60+
EditorGUI.indentLevel--;
61+
EditorGUI.EndProperty();
62+
}
63+
64+
/// <inheritdoc cref="PropertyDrawer.GetPropertyHeight"/>
65+
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
66+
{
67+
if (!property.FindPropertyRelative("_isFoldoutExpanded").boolValue)
68+
{
69+
return EditorGUIUtility.standardVerticalSpacing;
70+
}
71+
72+
var verticalSpacing = GetVerticalSpacing();
73+
var numberOfProperties = _propertiesToDraw.Length;
74+
return verticalSpacing * numberOfProperties;
75+
}
76+
77+
private float GetVerticalSpacing()
78+
{
79+
return EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
80+
}
81+
}
82+
}

Editor/Native/Scripts/Retargeting/Processors/CustomProcessorDrawer.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Editor/Native/Scripts/Retargeting/Processors/TargetProcessorContainerDrawer.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ private SerializedProperty GetProcessorTypeProperty(SerializedProperty property)
4141
return property.FindPropertyRelative("_locomotionProcessor");
4242
case TargetProcessor.ProcessorType.CCDIK:
4343
return property.FindPropertyRelative("_ccdProcessor");
44+
case TargetProcessor.ProcessorType.Custom:
45+
return property.FindPropertyRelative("_customProcessor");
4446
default:
4547
return null;
4648
}

Runtime/Native/Scripts/Retargeting/TargetProcessor.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ public enum ProcessorType
2525
Animation,
2626
HipPinning,
2727
Locomotion,
28-
CCDIK
28+
CCDIK,
29+
Custom
2930
}
3031

3132
/// <summary>

Runtime/Native/Scripts/Retargeting/TargetProcessorContainer.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ public class TargetProcessorContainer
5252
[SerializeField]
5353
protected CCDSkeletalProcessor _ccdProcessor;
5454

55+
/// <summary>
56+
/// A user-defined, custom processor.
57+
/// </summary>
58+
[SerializeField]
59+
protected CustomProcessor _customProcessor;
60+
5561
/// <summary>
5662
/// Returns the current <see cref="TargetProcessor"/> based on the current type
5763
/// saved.
@@ -71,6 +77,8 @@ public TargetProcessor GetCurrentProcessor()
7177
return _locomotionProcessor;
7278
case TargetProcessor.ProcessorType.CCDIK:
7379
return _ccdProcessor;
80+
case TargetProcessor.ProcessorType.Custom:
81+
return _customProcessor;
7482
default:
7583
return null;
7684
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// (c) Meta Platforms, Inc. and affiliates. Confidential and proprietary.
2+
3+
using System;
4+
using Unity.Collections;
5+
using UnityEngine;
6+
using static Meta.XR.Movement.MSDKUtility;
7+
8+
namespace Meta.XR.Movement.Retargeting
9+
{
10+
/// <summary>
11+
/// A custom processor that a user can define.
12+
/// </summary>
13+
[Serializable]
14+
public class CustomProcessor : TargetProcessor
15+
{
16+
/// <summary>
17+
/// Component that is associated with custom processor implementation.
18+
/// </summary>
19+
[SerializeField]
20+
protected CustomProcessorBehavior _customBehavior;
21+
22+
/// <inheritdoc />
23+
public override void Initialize(CharacterRetargeter retargeter)
24+
{
25+
_customBehavior.Initialize(retargeter);
26+
}
27+
28+
/// <inheritdoc />
29+
public override void Destroy()
30+
{
31+
_customBehavior.Destroy();
32+
}
33+
34+
/// <inheritdoc />
35+
public override void UpdatePose(ref NativeArray<NativeTransform> pose)
36+
{
37+
_customBehavior.UpdatePose(ref pose, _weight);
38+
}
39+
40+
/// <inheritdoc />
41+
public override void LateUpdatePose(
42+
ref NativeArray<NativeTransform> currentPose,
43+
ref NativeArray<NativeTransform> targetPoseLocal)
44+
{
45+
_customBehavior.LateUpdatePose(ref currentPose, ref targetPoseLocal, _weight);
46+
}
47+
}
48+
}

Runtime/Native/Scripts/Retargeting/TargetProcessors/CustomProcessor.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// (c) Meta Platforms, Inc. and affiliates. Confidential and proprietary.
2+
3+
using Unity.Collections;
4+
using UnityEngine;
5+
using static Meta.XR.Movement.MSDKUtility;
6+
7+
namespace Meta.XR.Movement.Retargeting
8+
{
9+
/// <summary>
10+
/// Behavior class referenced by <see cref="CustomProcessor"/>. Inherit to define your own.
11+
/// </summary>
12+
public class CustomProcessorBehavior : MonoBehaviour
13+
{
14+
/// <summary>
15+
/// Initializes the processor.
16+
/// </summary>
17+
/// <param name="retargeter">The native retargeter.</param>
18+
public virtual void Initialize(CharacterRetargeter retargeter)
19+
{
20+
}
21+
22+
/// <summary>
23+
/// Destroys the processor.
24+
/// </summary>
25+
public virtual void Destroy()
26+
{
27+
}
28+
29+
/// <summary>
30+
/// Processes the retargeted pose values in Update.
31+
/// </summary>
32+
/// <param name="pose">The pose that the constraint needs to affect.</param>
33+
/// <param name="weight">Weight.</param>
34+
public virtual void UpdatePose(
35+
ref NativeArray<NativeTransform> pose,
36+
float weight)
37+
{
38+
}
39+
40+
/// <summary>
41+
/// Processes the target pose values in LateUpdate.
42+
/// </summary>
43+
/// <param name="currentPose">The current pose.</param>
44+
/// <param name="targetPose">The target pose to be updated by the constraint.</param>
45+
/// <param name="weight">Weight.</param>
46+
public virtual void LateUpdatePose(
47+
ref NativeArray<NativeTransform> currentPose,
48+
ref NativeArray<NativeTransform> targetPose,
49+
float weight)
50+
{
51+
}
52+
}
53+
}

Runtime/Native/Scripts/Retargeting/TargetProcessors/CustomProcessorBehavior.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)