void OnEnable()
        {
            m_Weight           = serializedObject.FindProperty("m_Weight");
            m_TwistNodesToggle = serializedObject.FindProperty("m_TwistNodesGUIToggle");

            var data = serializedObject.FindProperty("m_Data");

            m_Source     = data.FindPropertyRelative("m_Source");
            m_TwistAxis  = data.FindPropertyRelative("m_TwistAxis");
            m_TwistNodes = data.FindPropertyRelative("m_TwistNodes");

            m_Constraint      = (TwistCorrection)serializedObject.targetObject;
            m_TwistNodesArray = m_Constraint.data.twistNodes;

            var dataType  = m_Constraint.data.GetType();
            var fieldInfo = dataType.GetField("m_TwistNodes", BindingFlags.NonPublic | BindingFlags.Instance);
            var range     = fieldInfo.GetCustomAttribute <RangeAttribute>();

            if (m_TwistNodesArray.Count == 0)
            {
                m_TwistNodesArray.Add(WeightedTransform.Default(0f));
                m_Constraint.data.twistNodes = m_TwistNodesArray;
            }

            m_ReorderableList = WeightedTransformHelper.CreateReorderableList(m_TwistNodes, ref m_TwistNodesArray, range);

            m_ReorderableList.onChangedCallback = (ReorderableList reorderableList) =>
            {
                m_Constraint.data.twistNodes = (WeightedTransformArray)reorderableList.list;
            };
        }
    private ConstraintTestData SetupConstraintRig()
    {
        var data = new ConstraintTestData();

        data.rigData = RuntimeRiggingTestFixture.SetupRigHierarchy();

        var multiRotationGO = new GameObject("multiPosition");
        var multiRotation   = multiRotationGO.AddComponent <MultiRotationConstraint>();

        multiRotation.Reset();

        multiRotationGO.transform.parent = data.rigData.rigGO.transform;

        multiRotation.data.constrainedObject = data.rigData.hipsGO.transform;
        data.constrainedObjectRestRotation   = multiRotation.data.constrainedObject.rotation;

        var sources = new WeightedTransformArray();
        var src0GO  = new GameObject("source0");
        var src1GO  = new GameObject("source1");

        src0GO.transform.parent = multiRotationGO.transform;
        src1GO.transform.parent = multiRotationGO.transform;
        sources.Add(new WeightedTransform(src0GO.transform, 0f));
        sources.Add(new WeightedTransform(src1GO.transform, 0f));
        multiRotation.data.sourceObjects = sources;

        src0GO.transform.rotation = data.rigData.hipsGO.transform.rotation;
        src1GO.transform.rotation = data.rigData.hipsGO.transform.rotation;

        data.rigData.rootGO.GetComponent <RigBuilder>().Build();

        data.constraint = multiRotation;

        return(data);
    }
        void OnEnable()
        {
            m_Weight = serializedObject.FindProperty("m_Weight");
            m_SourceObjectsToggle = serializedObject.FindProperty("m_SourceObjectsGUIToggle");
            m_SettingsToggle      = serializedObject.FindProperty("m_SettingsGUIToggle");

            var data = serializedObject.FindProperty("m_Data");

            m_ConstrainedObject       = data.FindPropertyRelative("m_ConstrainedObject");
            m_ConstrainedPositionAxes = data.FindPropertyRelative("m_ConstrainedPositionAxes");
            m_ConstrainedRotationAxes = data.FindPropertyRelative("m_ConstrainedRotationAxes");
            m_SourceObjects           = data.FindPropertyRelative("m_SourceObjects");
            m_MaintainPositionOffset  = data.FindPropertyRelative("m_MaintainPositionOffset");
            m_MaintainRotationOffset  = data.FindPropertyRelative("m_MaintainRotationOffset");

            m_Constraint         = (MultiParentConstraint)serializedObject.targetObject;
            m_SourceObjectsArray = m_Constraint.data.sourceObjects;

            var dataType  = m_Constraint.data.GetType();
            var fieldInfo = dataType.GetField("m_SourceObjects", BindingFlags.NonPublic | BindingFlags.Instance);
            var range     = fieldInfo.GetCustomAttribute <RangeAttribute>();

            if (m_SourceObjectsArray.Count == 0)
            {
                m_SourceObjectsArray.Add(WeightedTransform.Default(1f));
                m_Constraint.data.sourceObjects = m_SourceObjectsArray;
            }

            m_ReorderableList = WeightedTransformHelper.CreateReorderableList(m_SourceObjects, ref m_SourceObjectsArray, range);

            m_ReorderableList.onChangedCallback = (ReorderableList reorderableList) =>
            {
                m_Constraint.data.sourceObjects = (WeightedTransformArray)reorderableList.list;
            };
        }
        /// <inheritdoc />
        public override MultiRotationInverseConstraintJob Create(Animator animator, ref T data, Component component)
        {
            var job = new MultiRotationInverseConstraintJob();

            job.driven       = ReadOnlyTransformHandle.Bind(animator, data.constrainedObject);
            job.drivenParent = ReadOnlyTransformHandle.Bind(animator, data.constrainedObject.parent);
            job.drivenOffset = Vector3Property.Bind(animator, component, data.offsetVector3Property);

            WeightedTransformArray sourceObjects = data.sourceObjects;

            WeightedTransformArrayBinder.BindReadWriteTransforms(animator, component, sourceObjects, out job.sourceTransforms);
            WeightedTransformArrayBinder.BindWeights(animator, component, sourceObjects, data.sourceObjectsProperty, out job.sourceWeights);

            job.sourceOffsets = new NativeArray <Quaternion>(sourceObjects.Count, Allocator.Persistent, NativeArrayOptions.UninitializedMemory);

            job.weightBuffer = new NativeArray <float>(sourceObjects.Count, Allocator.Persistent, NativeArrayOptions.UninitializedMemory);

            Quaternion drivenRotInv = Quaternion.Inverse(data.constrainedObject.rotation);

            for (int i = 0; i < sourceObjects.Count; ++i)
            {
                job.sourceOffsets[i] = data.maintainOffset ?
                                       (drivenRotInv * sourceObjects[i].transform.rotation) : Quaternion.identity;
            }

            return(job);
        }
        public override MultiAimInverseConstraintJob Create(Animator animator, ref T data, Component component)
        {
            var job = new MultiAimInverseConstraintJob();

            job.driven       = ReadOnlyTransformHandle.Bind(animator, data.constrainedObject);
            job.drivenParent = ReadOnlyTransformHandle.Bind(animator, data.constrainedObject.parent);
            job.drivenOffset = Vector3Property.Bind(animator, component, data.offsetVector3Property);
            job.aimAxis      = data.aimAxis;

            WeightedTransformArray sourceObjects = data.sourceObjects;

            WeightedTransformArrayBinder.BindReadWriteTransforms(animator, component, sourceObjects, out job.sourceTransforms);
            WeightedTransformArrayBinder.BindWeights(animator, component, sourceObjects, data.sourceObjectsProperty, out job.sourceWeights);

            job.sourceOffsets = new NativeArray <Quaternion>(sourceObjects.Count, Allocator.Persistent, NativeArrayOptions.UninitializedMemory);
            for (int i = 0; i < sourceObjects.Count; ++i)
            {
                if (data.maintainOffset)
                {
                    var aimDirection = data.constrainedObject.rotation * data.aimAxis;
                    var dataToSource = sourceObjects[i].transform.position - data.constrainedObject.position;
                    var rot          = QuaternionExt.FromToRotation(dataToSource, aimDirection);
                    job.sourceOffsets[i] = Quaternion.Inverse(rot);
                }
                else
                {
                    job.sourceOffsets[i] = Quaternion.identity;
                }
            }

            job.weightBuffer = new NativeArray <float>(sourceObjects.Count, Allocator.Persistent, NativeArrayOptions.UninitializedMemory);

            return(job);
        }
        void OnEnable()
        {
            m_Weight = serializedObject.FindProperty("m_Weight");
            m_SourceObjectsToggle = serializedObject.FindProperty("m_SourceObjectsGUIToggle");
            m_SettingsToggle      = serializedObject.FindProperty("m_SettingsGUIToggle");

            var data = serializedObject.FindProperty("m_Data");

            m_ConstrainedObject = data.FindPropertyRelative("m_ConstrainedObject");
            m_AimAxis           = data.FindPropertyRelative("m_AimAxis");
            m_UpAxis            = data.FindPropertyRelative("m_UpAxis");
            m_WorldUpType       = data.FindPropertyRelative("m_WorldUpType");
            m_WorldUpAxis       = data.FindPropertyRelative("m_WorldUpAxis");
            m_WorldUpObject     = data.FindPropertyRelative("m_WorldUpObject");
            m_SourceObjects     = data.FindPropertyRelative("m_SourceObjects");
            m_MaintainOffset    = data.FindPropertyRelative("m_MaintainOffset");
            m_Offset            = data.FindPropertyRelative("m_Offset");
            m_ConstrainedAxes   = data.FindPropertyRelative("m_ConstrainedAxes");
            m_MinLimit          = data.FindPropertyRelative("m_MinLimit");
            m_MaxLimit          = data.FindPropertyRelative("m_MaxLimit");

            m_Constraint         = (MultiAimConstraint)serializedObject.targetObject;
            m_SourceObjectsArray = m_Constraint.data.sourceObjects;

            var dataType  = m_Constraint.data.GetType();
            var fieldInfo = dataType.GetField("m_SourceObjects", BindingFlags.NonPublic | BindingFlags.Instance);
            var range     = fieldInfo.GetCustomAttribute <RangeAttribute>();

            if (m_SourceObjectsArray.Count == 0)
            {
                m_SourceObjectsArray.Add(WeightedTransform.Default(1f));
                m_Constraint.data.sourceObjects = m_SourceObjectsArray;
            }

            m_ReorderableList = WeightedTransformHelper.CreateReorderableList(m_SourceObjects, ref m_SourceObjectsArray, range);

            m_ReorderableList.onChangedCallback = (ReorderableList reorderableList) =>
            {
                Undo.RegisterCompleteObjectUndo(m_Constraint, "Edit MultiAim");
                m_Constraint.data.sourceObjects = (WeightedTransformArray)reorderableList.list;
                if (PrefabUtility.IsPartOfPrefabInstance(m_Constraint))
                {
                    EditorUtility.SetDirty(m_Constraint);
                }
            };

            Undo.undoRedoPerformed += () =>
            {
                m_ReorderableList.list = m_Constraint.data.sourceObjects;
            };
        }
    private ConstraintTestData SetupConstraintRig()
    {
        var data = new ConstraintTestData();

        data.rigData = RuntimeRiggingTestFixture.SetupRigHierarchy();

        var twistCorrectionGO = new GameObject("twistCorrection");
        var twistCorrection   = twistCorrectionGO.AddComponent <TwistCorrection>();

        twistCorrection.Reset();

        twistCorrectionGO.transform.parent = data.rigData.rigGO.transform;

        var leftArm     = data.rigData.hipsGO.transform.Find("Chest/LeftArm");
        var leftForeArm = leftArm.Find("LeftForeArm");
        var leftHand    = leftForeArm.Find("LeftHand");

        // Force zero rotation to simplify testing
        leftHand.rotation    = Quaternion.identity;
        leftForeArm.rotation = Quaternion.identity;
        leftArm.rotation     = Quaternion.identity;

        twistCorrection.data.sourceObject = leftHand;
        twistCorrection.data.twistAxis    = TwistCorrectionData.Axis.X;
        data.restLocalRotation            = leftHand.localRotation;

        var twistNodes   = new WeightedTransformArray();
        var twistNode0GO = new GameObject("twistNode0");
        var twistNode1GO = new GameObject("twistNode1");

        twistNode0GO.transform.parent = leftForeArm;
        twistNode1GO.transform.parent = leftForeArm;
        twistNode0GO.transform.SetPositionAndRotation(Vector3.Lerp(leftForeArm.position, leftHand.position, 0.25f), leftHand.rotation);
        twistNode1GO.transform.SetPositionAndRotation(Vector3.Lerp(leftForeArm.position, leftHand.position, 0.75f), leftHand.rotation);
        twistNodes.Add(new WeightedTransform(twistNode0GO.transform, 0f));
        twistNodes.Add(new WeightedTransform(twistNode1GO.transform, 0f));
        twistCorrection.data.twistNodes = twistNodes;

        data.rigData.rootGO.GetComponent <RigBuilder>().Build();

        data.constraint = twistCorrection;

        return(data);
    }
    public static ConstraintTestData SetupConstraintRig()
    {
        var data = new ConstraintTestData();

        data.rigData = RuntimeRiggingTestFixture.SetupRigHierarchy();

        var multiParentGO = new GameObject("multiParent");
        var multiParent   = multiParentGO.AddComponent <MultiParentConstraint>();

        multiParent.Reset();

        multiParentGO.transform.parent = data.rigData.rigGO.transform;

        multiParent.data.constrainedObject = data.rigData.hipsGO.transform;
        data.constrainedObjectRestTx       = new AffineTransform(
            multiParent.data.constrainedObject.position,
            multiParent.data.constrainedObject.rotation
            );

        var sources = new WeightedTransformArray();
        var src0GO  = new GameObject("source0");
        var src1GO  = new GameObject("source1");

        src0GO.transform.parent = multiParentGO.transform;
        src1GO.transform.parent = multiParentGO.transform;
        sources.Add(new WeightedTransform(src0GO.transform, 0f));
        sources.Add(new WeightedTransform(src1GO.transform, 0f));
        multiParent.data.sourceObjects = sources;

        var pos = data.rigData.hipsGO.transform.position;
        var rot = data.rigData.hipsGO.transform.rotation;

        src0GO.transform.SetPositionAndRotation(pos, rot);
        src1GO.transform.SetPositionAndRotation(pos, rot);

        data.rigData.rootGO.GetComponent <RigBuilder>().Build();

        data.constraint = multiParent;

        return(data);
    }
        void OnEnable()
        {
            m_Weight           = serializedObject.FindProperty("m_Weight");
            m_TwistNodesToggle = serializedObject.FindProperty("m_TwistNodesGUIToggle");

            var data = serializedObject.FindProperty("m_Data");

            m_Source     = data.FindPropertyRelative("m_Source");
            m_TwistAxis  = data.FindPropertyRelative("m_TwistAxis");
            m_TwistNodes = data.FindPropertyRelative("m_TwistNodes");

            m_Constraint      = (TwistCorrection)serializedObject.targetObject;
            m_TwistNodesArray = m_Constraint.data.twistNodes;

            var dataType  = m_Constraint.data.GetType();
            var fieldInfo = dataType.GetField("m_TwistNodes", BindingFlags.NonPublic | BindingFlags.Instance);
            var range     = fieldInfo.GetCustomAttribute <RangeAttribute>();

            if (m_TwistNodesArray.Count == 0)
            {
                m_TwistNodesArray.Add(WeightedTransform.Default(0f));
                m_Constraint.data.twistNodes = m_TwistNodesArray;
            }

            m_ReorderableList = WeightedTransformHelper.CreateReorderableList(m_TwistNodes, ref m_TwistNodesArray, range);

            m_ReorderableList.onChangedCallback = (ReorderableList reorderableList) =>
            {
                Undo.RegisterCompleteObjectUndo(m_Constraint, "Edit TwistCorrection");
                m_Constraint.data.twistNodes = (WeightedTransformArray)reorderableList.list;
                if (PrefabUtility.IsPartOfPrefabInstance(m_Constraint))
                {
                    EditorUtility.SetDirty(m_Constraint);
                }
            };

            Undo.undoRedoPerformed += () =>
            {
                m_ReorderableList.list = m_Constraint.data.twistNodes;
            };
        }
        /// <inheritdoc />
        public override MultiParentInverseConstraintJob Create(Animator animator, ref T data, Component component)
        {
            var job = new MultiParentInverseConstraintJob();

            job.driven       = ReadOnlyTransformHandle.Bind(animator, data.constrainedObject);
            job.drivenParent = ReadOnlyTransformHandle.Bind(animator, data.constrainedObject.parent);

            WeightedTransformArray sourceObjects = data.sourceObjects;

            WeightedTransformArrayBinder.BindReadWriteTransforms(animator, component, sourceObjects, out job.sourceTransforms);
            WeightedTransformArrayBinder.BindWeights(animator, component, sourceObjects, data.sourceObjectsProperty, out job.sourceWeights);

            job.sourceOffsets = new NativeArray <AffineTransform>(sourceObjects.Count, Allocator.Persistent, NativeArrayOptions.UninitializedMemory);

            var drivenTx = new AffineTransform(data.constrainedObject.position, data.constrainedObject.rotation);

            for (int i = 0; i < sourceObjects.Count; ++i)
            {
                var sourceTransform = sourceObjects[i].transform;

                var srcTx     = new AffineTransform(sourceTransform.position, sourceTransform.rotation);
                var srcOffset = AffineTransform.identity;
                var tmp       = srcTx.InverseMul(drivenTx);

                if (data.maintainPositionOffset)
                {
                    srcOffset.translation = tmp.translation;
                }
                if (data.maintainRotationOffset)
                {
                    srcOffset.rotation = tmp.rotation;
                }

                srcOffset = srcOffset.Inverse();

                job.sourceOffsets[i] = srcOffset;
            }

            return(job);
        }
Example #11
0
    public static ConstraintTestData SetupConstraintRig()
    {
        var data = new ConstraintTestData();

        data.rigData = RuntimeRiggingTestFixture.SetupRigHierarchy();

        var multiAimGO = new GameObject("multiAim");
        var multiAim   = multiAimGO.AddComponent <MultiAimConstraint>();

        multiAim.Reset();

        multiAimGO.transform.parent = data.rigData.rigGO.transform;

        var head = data.rigData.hipsGO.transform.Find("Chest/Head");

        multiAim.data.constrainedObject = head;

        var sources = new WeightedTransformArray();
        var src0GO  = new GameObject("source0");
        var src1GO  = new GameObject("source1");

        src0GO.transform.parent = multiAimGO.transform;
        src1GO.transform.parent = multiAimGO.transform;
        sources.Add(new WeightedTransform(src0GO.transform, 0f));
        sources.Add(new WeightedTransform(src1GO.transform, 0f));
        multiAim.data.sourceObjects = sources;
        multiAim.data.aimAxis       = MultiAimConstraintData.Axis.Z;

        data.constrainedObjectRestTx = new AffineTransform(head.position, head.rotation);
        src0GO.transform.SetPositionAndRotation(data.constrainedObjectRestTx.translation + Vector3.forward, data.constrainedObjectRestTx.rotation);
        src1GO.transform.SetPositionAndRotation(data.constrainedObjectRestTx.translation + Vector3.forward, data.constrainedObjectRestTx.rotation);

        data.rigData.rootGO.GetComponent <RigBuilder>().Build();
        data.constraint = multiAim;

        return(data);
    }
Example #12
0
        public override MultiPositionInverseConstraintJob Create(Animator animator, ref T data, Component component)
        {
            var job = new MultiPositionInverseConstraintJob();

            job.driven       = ReadOnlyTransformHandle.Bind(animator, data.constrainedObject);
            job.drivenParent = ReadOnlyTransformHandle.Bind(animator, data.constrainedObject.parent);
            job.drivenOffset = Vector3Property.Bind(animator, component, data.offsetVector3Property);

            WeightedTransformArray sourceObjects = data.sourceObjects;

            WeightedTransformArrayBinder.BindReadWriteTransforms(animator, component, sourceObjects, out job.sourceTransforms);
            WeightedTransformArrayBinder.BindWeights(animator, component, sourceObjects, data.sourceObjectsProperty, out job.sourceWeights);

            job.sourceOffsets = new NativeArray <Vector3>(sourceObjects.Count, Allocator.Persistent, NativeArrayOptions.UninitializedMemory);

            Vector3 drivenPos = data.constrainedObject.position;

            for (int i = 0; i < sourceObjects.Count; ++i)
            {
                job.sourceOffsets[i] = data.maintainOffset ? (drivenPos - sourceObjects[i].transform.position) : Vector3.zero;
            }

            return(job);
        }
        public static ReorderableList CreateReorderableList(SerializedProperty property, ref WeightedTransformArray array, RangeAttribute range = null, bool draggable = true, bool displayHeader = false)
        {
            var reorderableList = new ReorderableList(array, typeof(WeightedTransform), draggable, displayHeader, true, true);

            reorderableList.drawElementBackgroundCallback = (Rect rect, int index, bool isActive, bool isFocused) =>
            {
                // Didn't find a way to register a callback before we repaint the reorderable list to toggle draggable flag.
                // Ideally, we'd need a callback canReorderElementCallback that would enable/disable draggable handle.
                reorderableList.draggable = !AnimationMode.InAnimationMode() && !Application.isPlaying;
            };

            reorderableList.drawElementCallback = (Rect rect, int index, bool isActive, bool isFocused) =>
            {
                var element = property.FindPropertyRelative("m_Item" + index);

                var offset = k_ElementHeightPadding * 0.5f;
                rect.y     += offset;
                rect.height = EditorGUIUtility.singleLineHeight;

                EditorGUI.BeginChangeCheck();

                WeightedTransformOnGUI(rect, element, GUIContent.none, range);

                if (EditorGUI.EndChangeCheck())
                {
                    var transformProperty = element.FindPropertyRelative("transform");
                    var weightProperty    = element.FindPropertyRelative("weight");

                    reorderableList.list[index] = new WeightedTransform(transformProperty.objectReferenceValue as Transform, weightProperty.floatValue);;
                }
            };

            reorderableList.onCanAddCallback = (ReorderableList list) =>
            {
                return(list.list.Count < WeightedTransformArray.k_MaxLength && !AnimationMode.InAnimationMode() && !Application.isPlaying);
            };

            reorderableList.onCanRemoveCallback = (ReorderableList list) =>
            {
                return(!AnimationMode.InAnimationMode() && !Application.isPlaying);
            };

            reorderableList.onAddCallback = (ReorderableList list) =>
            {
                list.list.Add(WeightedTransform.Default(1f));
            };

            reorderableList.elementHeight = EditorGUIUtility.singleLineHeight + k_ElementHeightPadding;

            if (!displayHeader)
            {
                reorderableList.headerHeight = k_NoHeaderHeight;
            }

            return(reorderableList);
        }