From 9bbf0f63e7634fd5f32c214628f3b9239257bf7b Mon Sep 17 00:00:00 2001 From: elamhut Date: Wed, 5 Jan 2022 01:45:46 -0300 Subject: [PATCH 1/7] ShapeKey Driver Start --- Assets/Editor/Animation/ShapeKeyDriver.meta | 8 + .../ShapeKeyDriver/ShapeKeyDrawer.cs | 50 +++ .../ShapeKeyDriver/ShapeKeyDrawer.cs.meta | 11 + .../ShapeKeyRotationConfigDrawer.cs | 87 ++++ .../ShapeKeyRotationConfigDrawer.cs.meta | 11 + Assets/Editor/AssetImporter/EventsImporter.cs | 4 +- Assets/Editor/Extensions/RectExtensions.cs | 6 +- Assets/Editor/Helpers/EditorHelper.cs | 4 +- .../Editor/Presets/FBXImporter_Generic.preset | 387 ++++++++++++++++++ .../Presets/FBXImporter_Generic.preset.meta | 8 + Assets/Runtime/Animation/ShapeKeyDriver.meta | 8 + .../ShapeKeyDriver/ShapeKeyDefinition.cs | 51 +++ .../ShapeKeyDriver/ShapeKeyDefinition.cs.meta | 11 + .../ShapeKeyDriver/ShapeKeyDriver.cs | 26 ++ .../ShapeKeyDriver/ShapeKeyDriver.cs.meta | 11 + Packages/manifest.json | 2 +- Packages/packages-lock.json | 2 +- ProjectSettings/PresetManager.asset | 4 + ProjectSettings/ProjectVersion.txt | 4 +- 19 files changed, 686 insertions(+), 9 deletions(-) create mode 100644 Assets/Editor/Animation/ShapeKeyDriver.meta create mode 100644 Assets/Editor/Animation/ShapeKeyDriver/ShapeKeyDrawer.cs create mode 100644 Assets/Editor/Animation/ShapeKeyDriver/ShapeKeyDrawer.cs.meta create mode 100644 Assets/Editor/Animation/ShapeKeyDriver/ShapeKeyRotationConfigDrawer.cs create mode 100644 Assets/Editor/Animation/ShapeKeyDriver/ShapeKeyRotationConfigDrawer.cs.meta create mode 100644 Assets/Editor/Presets/FBXImporter_Generic.preset create mode 100644 Assets/Editor/Presets/FBXImporter_Generic.preset.meta create mode 100644 Assets/Runtime/Animation/ShapeKeyDriver.meta create mode 100644 Assets/Runtime/Animation/ShapeKeyDriver/ShapeKeyDefinition.cs create mode 100644 Assets/Runtime/Animation/ShapeKeyDriver/ShapeKeyDefinition.cs.meta create mode 100644 Assets/Runtime/Animation/ShapeKeyDriver/ShapeKeyDriver.cs create mode 100644 Assets/Runtime/Animation/ShapeKeyDriver/ShapeKeyDriver.cs.meta diff --git a/Assets/Editor/Animation/ShapeKeyDriver.meta b/Assets/Editor/Animation/ShapeKeyDriver.meta new file mode 100644 index 0000000..bcc5a94 --- /dev/null +++ b/Assets/Editor/Animation/ShapeKeyDriver.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 4e74e16a6ca98044cbeef5940c4a6b50 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Editor/Animation/ShapeKeyDriver/ShapeKeyDrawer.cs b/Assets/Editor/Animation/ShapeKeyDriver/ShapeKeyDrawer.cs new file mode 100644 index 0000000..bb6b7bc --- /dev/null +++ b/Assets/Editor/Animation/ShapeKeyDriver/ShapeKeyDrawer.cs @@ -0,0 +1,50 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using UnityEditor; +using UnityEngine; + +namespace BitStrap +{ + [CustomPropertyDrawer(typeof(ShapeKeyDefinition))] + public class ShapeKeyDrawer : PropertyDrawer + { + public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) + { + PropertyDrawerHelper.LoadAttributeTooltip( this, label ); + + var behaviour = property.serializedObject.targetObject as MonoBehaviour; + + SkinnedMeshRenderer skinnedMeshRenderer = null; + var nameProperty = property.GetMemberProperty( p => p.name ); + + if( behaviour != null ) + { + skinnedMeshRenderer = behaviour.GetComponent(); + } + + if (skinnedMeshRenderer != null) + { + var blendShapeCount = skinnedMeshRenderer.sharedMesh.blendShapeCount; + var blendShapeNames = new List(); + for (int i = 0; i < blendShapeCount; i++) + { + blendShapeNames.Add(skinnedMeshRenderer.sharedMesh.GetBlendShapeName(i)); + } + + var popupOptions = blendShapeNames.Select( x => new GUIContent( x ) ).ToArray(); + int currentIndex = blendShapeNames.IndexOf(nameProperty.stringValue); + + EditorGUI.BeginChangeCheck(); + currentIndex = EditorGUI.Popup( position, label, currentIndex, popupOptions ); + + if( EditorGUI.EndChangeCheck() ) + { + nameProperty.stringValue = blendShapeNames[currentIndex]; + property.serializedObject.ApplyModifiedProperties(); + } + } + } + } +} diff --git a/Assets/Editor/Animation/ShapeKeyDriver/ShapeKeyDrawer.cs.meta b/Assets/Editor/Animation/ShapeKeyDriver/ShapeKeyDrawer.cs.meta new file mode 100644 index 0000000..9d065b8 --- /dev/null +++ b/Assets/Editor/Animation/ShapeKeyDriver/ShapeKeyDrawer.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 464b20d8e40d6e6478403921bffbb7b3 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Editor/Animation/ShapeKeyDriver/ShapeKeyRotationConfigDrawer.cs b/Assets/Editor/Animation/ShapeKeyDriver/ShapeKeyRotationConfigDrawer.cs new file mode 100644 index 0000000..07d286c --- /dev/null +++ b/Assets/Editor/Animation/ShapeKeyDriver/ShapeKeyRotationConfigDrawer.cs @@ -0,0 +1,87 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using UnityEditor; +using UnityEngine; + +namespace BitStrap +{ + [CustomPropertyDrawer(typeof(ShapeKeyRotationConfig))] + public class ShapeKeyRotationConfigDrawer : PropertyDrawer + { + public override float GetPropertyHeight( SerializedProperty property, GUIContent label ) + { + if (property.isExpanded) + return EditorHelper.singleLineHeight * 8; + + return EditorHelper.singleLineHeight; + } + public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) + { + PropertyDrawerHelper.LoadAttributeTooltip( this, label ); + var row0 = position.Row(0); + var row1 = position.Row(1); + var row2 = position.Row(2); + var row3 = position.Row(3); + var row4 = position.Row(4); + var row5 = position.Row(5); + + property.isExpanded = EditorGUI.Foldout(row0, property.isExpanded, label); + + if (!property.isExpanded) + return; + + var indentLevel = EditorGUI.indentLevel; + using (IndentLevel.Do(indentLevel + 1)) + using (LabelWidth.Do(128.0f)) + { + var driverTransformProperty = property.GetMemberProperty( k => k.driverTransform); + var rotationTypeProperty = property.GetMemberProperty( k => k.rotationType); + var interpolationCurve = property.GetMemberProperty( k => k.interpolationCurve); + + var startRotationLabel = new GUIContent("Start Rotation"); + var endRotationLabel = new GUIContent("End Rotation"); + + EditorGUI.PropertyField(row1, driverTransformProperty); + EditorGUI.PropertyField(row2, rotationTypeProperty); + + row4.Left(row4.width * 0.5f, out var leftRect).Expand(out var rightRect); + leftRect.Right(26.0f, out var leftButtonRect).Left(-10.0f).Expand(out var outLeftRect); + rightRect.Right(26.0f, out var rightButtonRect).Left(-10.0f).Expand(out var outRightRect); + + if (rotationTypeProperty.enumValueIndex == (int)ShapeKeyRotationConfig.RotationType.SingleAxis) + { + var rotationAxisProperty = property.GetMemberProperty( k => k.rotationAxis); + var startRotationProperty = property.GetMemberProperty( k => k.singleAxisStartRotation); + var endRotationProperty = property.GetMemberProperty( k => k.singleAxisEndRotation); + + EditorGUI.PropertyField(row3, rotationAxisProperty); + + EditorGUI.PropertyField(outLeftRect, startRotationProperty, startRotationLabel); + if ( GUI.Button(leftButtonRect, EditorGUIUtility.IconContent("d_Transform Icon"))) {} + + EditorGUI.PropertyField(outRightRect, endRotationProperty, endRotationLabel); + if ( GUI.Button(rightButtonRect, EditorGUIUtility.IconContent("d_Transform Icon"))) {} + } + else + { + var startRotationProperty = property.GetMemberProperty( k => k.multiAxisStartRotation); + var endRotationProperty = property.GetMemberProperty( k => k.multiAxisEndRotation); + + EditorGUI.PropertyField(outLeftRect, startRotationProperty, startRotationLabel); + if ( GUI.Button(leftButtonRect, EditorGUIUtility.IconContent("d_Transform Icon"))) {} + + EditorGUI.PropertyField(outRightRect, endRotationProperty, endRotationLabel); + if ( GUI.Button(rightButtonRect, EditorGUIUtility.IconContent("d_Transform Icon"))) {} + } + + var curveLabel = new GUIContent("Curve"); + + row5.height = EditorHelper.singleLineHeight * 3; + EditorGUI.PropertyField(row5, interpolationCurve, curveLabel); + + } + } + } +} diff --git a/Assets/Editor/Animation/ShapeKeyDriver/ShapeKeyRotationConfigDrawer.cs.meta b/Assets/Editor/Animation/ShapeKeyDriver/ShapeKeyRotationConfigDrawer.cs.meta new file mode 100644 index 0000000..27db18a --- /dev/null +++ b/Assets/Editor/Animation/ShapeKeyDriver/ShapeKeyRotationConfigDrawer.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 1eeee3f5cd3263944a7c0639d5c20181 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Editor/AssetImporter/EventsImporter.cs b/Assets/Editor/AssetImporter/EventsImporter.cs index cc7517a..ba9e259 100644 --- a/Assets/Editor/AssetImporter/EventsImporter.cs +++ b/Assets/Editor/AssetImporter/EventsImporter.cs @@ -18,8 +18,8 @@ private void OnPostprocessAnimation(GameObject root, AnimationClip clip) foreach (var action in eventsData.ActionsMarkers) { - var clipName = clip.name.Split('|'); - if (clipName[1] == action.Name) + var clipName = clip.name.Split('|', 2); + if (clipName.Length == 2 && clipName[1] == action.Name) { List allAnimationEvents = new List(); foreach (var marker in action.Markers) diff --git a/Assets/Editor/Extensions/RectExtensions.cs b/Assets/Editor/Extensions/RectExtensions.cs index d9d8346..93fec1f 100644 --- a/Assets/Editor/Extensions/RectExtensions.cs +++ b/Assets/Editor/Extensions/RectExtensions.cs @@ -1,5 +1,6 @@ using UnityEditor; using UnityEngine; +using UnityEngine.UIElements; namespace BitStrap { @@ -20,8 +21,8 @@ public static class RectExtensions /// public static Rect Row( this Rect position, int rowNumber ) { - position.y += EditorGUIUtility.singleLineHeight * rowNumber; - position.height = EditorGUIUtility.singleLineHeight; + position.y += EditorHelper.singleLineHeight * rowNumber; + position.height = EditorHelper.singleLineHeight; return position; } @@ -149,5 +150,6 @@ public static void Expand( this Rect position, out Rect target ) { target = position; } + } } \ No newline at end of file diff --git a/Assets/Editor/Helpers/EditorHelper.cs b/Assets/Editor/Helpers/EditorHelper.cs index fb6792f..c78df67 100644 --- a/Assets/Editor/Helpers/EditorHelper.cs +++ b/Assets/Editor/Helpers/EditorHelper.cs @@ -81,6 +81,8 @@ public static GUIStyle Warning } } + public static float singleLineHeight = EditorGUIUtility.singleLineHeight + 2.0f; + private static string searchField = ""; private static Vector2 scroll = Vector2.zero; private static Texture[] unityIcons = null; @@ -203,7 +205,7 @@ public static Rect Rect( float height ) { return GUILayoutUtility.GetRect( 0.0f, height, GUILayout.ExpandWidth( true ) ); } - + /// /// Returns a GUIContent containing a label and the tooltip defined in GUI.tooltip. /// diff --git a/Assets/Editor/Presets/FBXImporter_Generic.preset b/Assets/Editor/Presets/FBXImporter_Generic.preset new file mode 100644 index 0000000..1f01e6c --- /dev/null +++ b/Assets/Editor/Presets/FBXImporter_Generic.preset @@ -0,0 +1,387 @@ +%YAML 1.1 +%TAG !u! tag:unity3d.com,2011: +--- !u!181963792 &2655988077585873504 +Preset: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_Name: FBXImporter_Generic + m_TargetType: + m_NativeTypeID: 1041 + m_ManagedTypePPtr: {fileID: 0} + m_ManagedTypeFallback: + m_Properties: + - target: {fileID: 0} + propertyPath: m_ExternalObjects.Array.size + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 0} + propertyPath: m_MaterialImportMode + value: 1 + objectReference: {fileID: 0} + - target: {fileID: 0} + propertyPath: m_MaterialName + value: 1 + objectReference: {fileID: 0} + - target: {fileID: 0} + propertyPath: m_MaterialSearch + value: 1 + objectReference: {fileID: 0} + - target: {fileID: 0} + propertyPath: m_MaterialLocation + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 0} + propertyPath: m_LegacyGenerateAnimations + value: 4 + objectReference: {fileID: 0} + - target: {fileID: 0} + propertyPath: m_BakeSimulation + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 0} + propertyPath: m_ResampleCurves + value: 1 + objectReference: {fileID: 0} + - target: {fileID: 0} + propertyPath: m_OptimizeGameObjects + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 0} + propertyPath: m_RemoveConstantScaleCurves + value: 1 + objectReference: {fileID: 0} + - target: {fileID: 0} + propertyPath: m_MotionNodeName + value: + objectReference: {fileID: 0} + - target: {fileID: 0} + propertyPath: m_RigImportErrors + value: + objectReference: {fileID: 0} + - target: {fileID: 0} + propertyPath: m_RigImportWarnings + value: + objectReference: {fileID: 0} + - target: {fileID: 0} + propertyPath: m_AnimationImportErrors + value: + objectReference: {fileID: 0} + - target: {fileID: 0} + propertyPath: m_AnimationImportWarnings + value: + objectReference: {fileID: 0} + - target: {fileID: 0} + propertyPath: m_AnimationRetargetingWarnings + value: + objectReference: {fileID: 0} + - target: {fileID: 0} + propertyPath: m_AnimationDoRetargetingWarnings + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 0} + propertyPath: m_ImportAnimatedCustomProperties + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 0} + propertyPath: m_ImportConstraints + value: 1 + objectReference: {fileID: 0} + - target: {fileID: 0} + propertyPath: m_AnimationCompression + value: 3 + objectReference: {fileID: 0} + - target: {fileID: 0} + propertyPath: m_AnimationRotationError + value: 0.5 + objectReference: {fileID: 0} + - target: {fileID: 0} + propertyPath: m_AnimationPositionError + value: 0.5 + objectReference: {fileID: 0} + - target: {fileID: 0} + propertyPath: m_AnimationScaleError + value: 0.5 + objectReference: {fileID: 0} + - target: {fileID: 0} + propertyPath: m_AnimationWrapMode + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 0} + propertyPath: m_ExtraExposedTransformPaths.Array.size + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 0} + propertyPath: m_ExtraUserProperties.Array.size + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 0} + propertyPath: m_ClipAnimations.Array.size + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 0} + propertyPath: m_IsReadable + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 0} + propertyPath: m_LODScreenPercentages.Array.size + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 0} + propertyPath: m_GlobalScale + value: 1 + objectReference: {fileID: 0} + - target: {fileID: 0} + propertyPath: m_MeshCompression + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 0} + propertyPath: m_AddColliders + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 0} + propertyPath: m_UseSRGBMaterialColor + value: 1 + objectReference: {fileID: 0} + - target: {fileID: 0} + propertyPath: m_SortHierarchyByName + value: 1 + objectReference: {fileID: 0} + - target: {fileID: 0} + propertyPath: m_ImportVisibility + value: 1 + objectReference: {fileID: 0} + - target: {fileID: 0} + propertyPath: m_ImportBlendShapes + value: 1 + objectReference: {fileID: 0} + - target: {fileID: 0} + propertyPath: m_ImportCameras + value: 1 + objectReference: {fileID: 0} + - target: {fileID: 0} + propertyPath: m_ImportLights + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 0} + propertyPath: m_NodeNameCollisionStrategy + value: 1 + objectReference: {fileID: 0} + - target: {fileID: 0} + propertyPath: swapUVChannels + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 0} + propertyPath: generateSecondaryUV + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 0} + propertyPath: m_UseFileUnits + value: 1 + objectReference: {fileID: 0} + - target: {fileID: 0} + propertyPath: keepQuads + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 0} + propertyPath: weldVertices + value: 1 + objectReference: {fileID: 0} + - target: {fileID: 0} + propertyPath: bakeAxisConversion + value: 1 + objectReference: {fileID: 0} + - target: {fileID: 0} + propertyPath: m_PreserveHierarchy + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 0} + propertyPath: skinWeightsMode + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 0} + propertyPath: maxBonesPerVertex + value: 4 + objectReference: {fileID: 0} + - target: {fileID: 0} + propertyPath: minBoneWeight + value: 0.001 + objectReference: {fileID: 0} + - target: {fileID: 0} + propertyPath: optimizeBones + value: 1 + objectReference: {fileID: 0} + - target: {fileID: 0} + propertyPath: meshOptimizationFlags + value: -1 + objectReference: {fileID: 0} + - target: {fileID: 0} + propertyPath: indexFormat + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 0} + propertyPath: secondaryUVAngleDistortion + value: 8 + objectReference: {fileID: 0} + - target: {fileID: 0} + propertyPath: secondaryUVAreaDistortion + value: 15.000001 + objectReference: {fileID: 0} + - target: {fileID: 0} + propertyPath: secondaryUVHardAngle + value: 88 + objectReference: {fileID: 0} + - target: {fileID: 0} + propertyPath: secondaryUVMarginMethod + value: 1 + objectReference: {fileID: 0} + - target: {fileID: 0} + propertyPath: secondaryUVMinLightmapResolution + value: 40 + objectReference: {fileID: 0} + - target: {fileID: 0} + propertyPath: secondaryUVMinObjectScale + value: 1 + objectReference: {fileID: 0} + - target: {fileID: 0} + propertyPath: secondaryUVPackMargin + value: 4 + objectReference: {fileID: 0} + - target: {fileID: 0} + propertyPath: m_UseFileScale + value: 1 + objectReference: {fileID: 0} + - target: {fileID: 0} + propertyPath: normalSmoothAngle + value: 60 + objectReference: {fileID: 0} + - target: {fileID: 0} + propertyPath: normalImportMode + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 0} + propertyPath: tangentImportMode + value: 3 + objectReference: {fileID: 0} + - target: {fileID: 0} + propertyPath: normalCalculationMode + value: 4 + objectReference: {fileID: 0} + - target: {fileID: 0} + propertyPath: legacyComputeAllNormalsFromSmoothingGroupsWhenMeshHasBlendShapes + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 0} + propertyPath: blendShapeNormalImportMode + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 0} + propertyPath: normalSmoothingSource + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 0} + propertyPath: m_ReferencedClips.Array.size + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 0} + propertyPath: m_ImportAnimation + value: 1 + objectReference: {fileID: 0} + - target: {fileID: 0} + propertyPath: m_HumanDescription.m_Human.Array.size + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 0} + propertyPath: m_HumanDescription.m_Skeleton.Array.size + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 0} + propertyPath: m_HumanDescription.m_ArmTwist + value: 0.5 + objectReference: {fileID: 0} + - target: {fileID: 0} + propertyPath: m_HumanDescription.m_ForeArmTwist + value: 0.5 + objectReference: {fileID: 0} + - target: {fileID: 0} + propertyPath: m_HumanDescription.m_UpperLegTwist + value: 0.5 + objectReference: {fileID: 0} + - target: {fileID: 0} + propertyPath: m_HumanDescription.m_LegTwist + value: 0.5 + objectReference: {fileID: 0} + - target: {fileID: 0} + propertyPath: m_HumanDescription.m_ArmStretch + value: 0.05 + objectReference: {fileID: 0} + - target: {fileID: 0} + propertyPath: m_HumanDescription.m_LegStretch + value: 0.05 + objectReference: {fileID: 0} + - target: {fileID: 0} + propertyPath: m_HumanDescription.m_FeetSpacing + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 0} + propertyPath: m_HumanDescription.m_GlobalScale + value: 1 + objectReference: {fileID: 0} + - target: {fileID: 0} + propertyPath: m_HumanDescription.m_RootMotionBoneName + value: + objectReference: {fileID: 0} + - target: {fileID: 0} + propertyPath: m_HumanDescription.m_HasTranslationDoF + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 0} + propertyPath: m_HumanDescription.m_HasExtraRoot + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 0} + propertyPath: m_HumanDescription.m_SkeletonHasParents + value: 1 + objectReference: {fileID: 0} + - target: {fileID: 0} + propertyPath: m_LastHumanDescriptionAvatarSource + value: + objectReference: {fileID: 0} + - target: {fileID: 0} + propertyPath: m_AutoGenerateAvatarMappingIfUnspecified + value: 1 + objectReference: {fileID: 0} + - target: {fileID: 0} + propertyPath: m_AnimationType + value: 2 + objectReference: {fileID: 0} + - target: {fileID: 0} + propertyPath: m_HumanoidOversampling + value: 1 + objectReference: {fileID: 0} + - target: {fileID: 0} + propertyPath: m_AvatarSetup + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 0} + propertyPath: m_AddHumanoidExtraRootOnlyWhenUsingAvatar + value: 1 + objectReference: {fileID: 0} + - target: {fileID: 0} + propertyPath: m_AdditionalBone + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 0} + propertyPath: m_UserData + value: + objectReference: {fileID: 0} + - target: {fileID: 0} + propertyPath: m_AssetBundleName + value: + objectReference: {fileID: 0} + - target: {fileID: 0} + propertyPath: m_AssetBundleVariant + value: + objectReference: {fileID: 0} + m_ExcludedProperties: [] diff --git a/Assets/Editor/Presets/FBXImporter_Generic.preset.meta b/Assets/Editor/Presets/FBXImporter_Generic.preset.meta new file mode 100644 index 0000000..6420c7d --- /dev/null +++ b/Assets/Editor/Presets/FBXImporter_Generic.preset.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 5610357c12c40f44b87b8e29d22acb1d +NativeFormatImporter: + externalObjects: {} + mainObjectFileID: 2655988077585873504 + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Runtime/Animation/ShapeKeyDriver.meta b/Assets/Runtime/Animation/ShapeKeyDriver.meta new file mode 100644 index 0000000..b349341 --- /dev/null +++ b/Assets/Runtime/Animation/ShapeKeyDriver.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: a11a852b1d3098e4d904cc4dc0e67cd2 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Runtime/Animation/ShapeKeyDriver/ShapeKeyDefinition.cs b/Assets/Runtime/Animation/ShapeKeyDriver/ShapeKeyDefinition.cs new file mode 100644 index 0000000..2b947fc --- /dev/null +++ b/Assets/Runtime/Animation/ShapeKeyDriver/ShapeKeyDefinition.cs @@ -0,0 +1,51 @@ +using UnityEngine; + +[System.Serializable] +public struct ShapeKeyDefinition +{ + public string name; + public int index; +} + +[System.Serializable] +public struct ShapeKeyRotationConfig +{ + public enum RotationType + { + SingleAxis, + MultiAxis + } + + public enum RotationAxis + { + X, + Y, + Z + } + + public Transform driverTransform; + public RotationType rotationType; + public RotationAxis rotationAxis; + public float singleAxisStartRotation; + public float singleAxisEndRotation; + public Vector3 multiAxisStartRotation; + public Vector3 multiAxisEndRotation; + public AnimationCurve interpolationCurve; +} + +[System.Serializable] +public struct ShapeKeyRecipe +{ + public ShapeKeyDefinition shapeKeyDefinition; + + public enum DriverType + { + Rotation, + Position, + MecanimParameter, + Callback, + } + + public DriverType driverType; + public ShapeKeyRotationConfig shapeKeyRotationConfig; +} \ No newline at end of file diff --git a/Assets/Runtime/Animation/ShapeKeyDriver/ShapeKeyDefinition.cs.meta b/Assets/Runtime/Animation/ShapeKeyDriver/ShapeKeyDefinition.cs.meta new file mode 100644 index 0000000..1bc2918 --- /dev/null +++ b/Assets/Runtime/Animation/ShapeKeyDriver/ShapeKeyDefinition.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 4d788e8a0bbca5f47ad2ef80e76527f5 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Runtime/Animation/ShapeKeyDriver/ShapeKeyDriver.cs b/Assets/Runtime/Animation/ShapeKeyDriver/ShapeKeyDriver.cs new file mode 100644 index 0000000..3ab4df1 --- /dev/null +++ b/Assets/Runtime/Animation/ShapeKeyDriver/ShapeKeyDriver.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + + +[ExecuteInEditMode] +public class ShapeKeyDriver : MonoBehaviour +{ + [SerializeField] private SkinnedMeshRenderer skinnedMeshRenderer; + [SerializeField] private ShapeKeyRecipe shapeKeyRecipe; + + // Update is called once per frame + void Update() + { + if (shapeKeyRecipe.shapeKeyRotationConfig.driverTransform == null) + return; + + var currentRotation = shapeKeyRecipe.shapeKeyRotationConfig.driverTransform.localRotation.eulerAngles.y; + Debug.Log(currentRotation); + var percentage = Mathf.InverseLerp(shapeKeyRecipe.shapeKeyRotationConfig.singleAxisStartRotation, shapeKeyRecipe.shapeKeyRotationConfig.singleAxisEndRotation, currentRotation); + var interpolation = Mathf.Lerp(0, 100, shapeKeyRecipe.shapeKeyRotationConfig.interpolationCurve.Evaluate(percentage)); + Debug.Log(interpolation); + skinnedMeshRenderer.SetBlendShapeWeight(shapeKeyRecipe.shapeKeyDefinition.index, interpolation); + } +} diff --git a/Assets/Runtime/Animation/ShapeKeyDriver/ShapeKeyDriver.cs.meta b/Assets/Runtime/Animation/ShapeKeyDriver/ShapeKeyDriver.cs.meta new file mode 100644 index 0000000..7b0cbcc --- /dev/null +++ b/Assets/Runtime/Animation/ShapeKeyDriver/ShapeKeyDriver.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: ea86ae99dbd067b4db1f44da1c93a5c7 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Packages/manifest.json b/Packages/manifest.json index 1d14866..4ff376b 100644 --- a/Packages/manifest.json +++ b/Packages/manifest.json @@ -2,7 +2,7 @@ "dependencies": { "com.unity.2d.sprite": "1.0.0", "com.unity.2d.tilemap": "1.0.0", - "com.unity.ide.rider": "3.0.7", + "com.unity.ide.rider": "3.0.9", "com.unity.ide.visualstudio": "2.0.12", "com.unity.ide.vscode": "1.2.4", "com.unity.test-framework": "1.1.29", diff --git a/Packages/packages-lock.json b/Packages/packages-lock.json index 7acfca2..05d1ce3 100644 --- a/Packages/packages-lock.json +++ b/Packages/packages-lock.json @@ -20,7 +20,7 @@ "url": "https://packages.unity.com" }, "com.unity.ide.rider": { - "version": "3.0.7", + "version": "3.0.9", "depth": 0, "source": "registry", "dependencies": { diff --git a/ProjectSettings/PresetManager.asset b/ProjectSettings/PresetManager.asset index 23fddb1..bb8acaf 100644 --- a/ProjectSettings/PresetManager.asset +++ b/ProjectSettings/PresetManager.asset @@ -42,3 +42,7 @@ PresetManager: type: 2} m_Filter: glob:"SK_*.fbx" m_Disabled: 0 + - m_Preset: {fileID: 2655988077585873504, guid: 5610357c12c40f44b87b8e29d22acb1d, + type: 2} + m_Filter: + m_Disabled: 0 diff --git a/ProjectSettings/ProjectVersion.txt b/ProjectSettings/ProjectVersion.txt index 86fa6b5..bbae793 100644 --- a/ProjectSettings/ProjectVersion.txt +++ b/ProjectSettings/ProjectVersion.txt @@ -1,2 +1,2 @@ -m_EditorVersion: 2021.2.6f1 -m_EditorVersionWithRevision: 2021.2.6f1 (8c4e826ba445) +m_EditorVersion: 2021.2.7f1 +m_EditorVersionWithRevision: 2021.2.7f1 (6bd9e232123f) From 86f5ad91d0a08e97c57762e029320096cab0a5b2 Mon Sep 17 00:00:00 2001 From: elamhut Date: Tue, 18 Jan 2022 14:00:46 -0300 Subject: [PATCH 2/7] Shape Key Position Drawer --- .../ShapeKeyPositionConfigDrawer.cs | 146 ++++++++++++++++++ .../ShapeKeyPositionConfigDrawer.cs.meta | 11 ++ .../ShapeKeyDriver/ShapeKeyRecipeDrawer.cs | 57 +++++++ .../ShapeKeyRecipeDrawer.cs.meta | 11 ++ .../ShapeKeyRotationConfigDrawer.cs | 59 ++++++- .../ShapeKeyDriver/ShapeKeyDefinition.cs | 95 ++++++++++++ .../ShapeKeyDriver/ShapeKeyDriver.cs | 81 +++++++++- 7 files changed, 445 insertions(+), 15 deletions(-) create mode 100644 Assets/Editor/Animation/ShapeKeyDriver/ShapeKeyPositionConfigDrawer.cs create mode 100644 Assets/Editor/Animation/ShapeKeyDriver/ShapeKeyPositionConfigDrawer.cs.meta create mode 100644 Assets/Editor/Animation/ShapeKeyDriver/ShapeKeyRecipeDrawer.cs create mode 100644 Assets/Editor/Animation/ShapeKeyDriver/ShapeKeyRecipeDrawer.cs.meta diff --git a/Assets/Editor/Animation/ShapeKeyDriver/ShapeKeyPositionConfigDrawer.cs b/Assets/Editor/Animation/ShapeKeyDriver/ShapeKeyPositionConfigDrawer.cs new file mode 100644 index 0000000..8e47be8 --- /dev/null +++ b/Assets/Editor/Animation/ShapeKeyDriver/ShapeKeyPositionConfigDrawer.cs @@ -0,0 +1,146 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using UnityEditor; +using UnityEngine; + +namespace BitStrap +{ + [CustomPropertyDrawer(typeof(ShapeKeyPositionConfig))] + public class ShapeKeyPositionConfigDrawer : PropertyDrawer + { + public override float GetPropertyHeight( SerializedProperty property, GUIContent label ) + { + if (property.isExpanded) + return EditorHelper.singleLineHeight * 8; + + return EditorHelper.singleLineHeight; + } + public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) + { + PropertyDrawerHelper.LoadAttributeTooltip( this, label ); + var row0 = position.Row(0); + var row1 = position.Row(1); + var row2 = position.Row(2); + var row3 = position.Row(3); + var row4 = position.Row(4); + var row5 = position.Row(5); + + property.isExpanded = EditorGUI.Foldout(row0, property.isExpanded, label); + + + if (!property.isExpanded) + return; + + var indentLevel = EditorGUI.indentLevel; + using (IndentLevel.Do(indentLevel + 1)) + using (LabelWidth.Do(128.0f)) + { + var driverTransformProperty = property.GetMemberProperty( k => k.driverTransform); + EditorGUI.PropertyField(row1, driverTransformProperty); + + var positionTypeProperty = property.GetMemberProperty( k => k.positionType); + EditorGUI.PropertyField(row2, positionTypeProperty); + + var startPropertyLabel = new GUIContent("Start Position"); + var endPropertyLabel = new GUIContent("End Position"); + + switch ((ShapeKeyPositionConfig.PositionType)positionTypeProperty.enumValueIndex) + { + case ShapeKeyPositionConfig.PositionType.OneAxis: + { + var axisProperty = property.GetMemberProperty( k => k.positionOneAxis); + var positionAxisLabel = new GUIContent("Axis"); + EditorGUI.PropertyField(row3, axisProperty, positionAxisLabel); + + row4.Left(row4.width * 0.5f, out var leftRect).Expand(out var rightRect); + leftRect.Right(26.0f, out var leftButtonRect).Left(-10.0f).Expand(out var outLeftRect); + rightRect.Right(26.0f, out var rightButtonRect).Left(-10.0f).Expand(out var outRightRect); + + var startValueProperty = property.GetMemberProperty( k => k.oneAxisStartPosition); + EditorGUI.PropertyField(outLeftRect, startValueProperty, startPropertyLabel); + + if (GUI.Button(leftButtonRect, EditorGUIUtility.IconContent("d_Transform Icon"))) + { + SetOneAxis(axisProperty, startValueProperty, driverTransformProperty); + } + + var endValueProperty = property.GetMemberProperty( k => k.oneAxisEndPosition); + EditorGUI.PropertyField(outRightRect, endValueProperty, endPropertyLabel); + + if (GUI.Button(rightButtonRect, EditorGUIUtility.IconContent("d_Transform Icon"))) + { + SetOneAxis(axisProperty, endValueProperty, driverTransformProperty); + } + + break; + } + case ShapeKeyPositionConfig.PositionType.TwoAxis: + { + var axisProperty = property.GetMemberProperty(k => k.positionTwoAxis); + var positionAxisLabel = new GUIContent("Axis"); + EditorGUI.PropertyField(row3, axisProperty, positionAxisLabel); + + row4.Left(row4.width * 0.5f, out var leftRect).Expand(out var rightRect); + + var startValueProperty = property.GetMemberProperty( k => k.twoAxisStartPosition); + EditorGUI.PropertyField(leftRect, startValueProperty, startPropertyLabel); + + var endValueProperty = property.GetMemberProperty( k => k.twoAxisEndPosition); + EditorGUI.PropertyField(rightRect, endValueProperty, endPropertyLabel); + + break; + } + case ShapeKeyPositionConfig.PositionType.ThreeAxis: + { + row4.Left(row4.width * 0.5f, out var leftRect).Expand(out var rightRect); + + var startValueProperty = property.GetMemberProperty(k => k.threeAxisStartPosition); + EditorGUI.PropertyField(leftRect, startValueProperty, startPropertyLabel); + + var endValueProperty = property.GetMemberProperty(k => k.threeAxisEndPosition); + EditorGUI.PropertyField(rightRect, endValueProperty, endPropertyLabel); + + break; + } + case ShapeKeyPositionConfig.PositionType.DistanceFromTarget: + var targetTransformProperty = property.GetMemberProperty(k => k.targetTransform); + EditorGUI.PropertyField(row4, targetTransformProperty); + + break; + } + var interpolationCurve = property.GetMemberProperty( k => k.interpolationCurve); + var curveLabel = new GUIContent("Curve"); + + row5.height = EditorHelper.singleLineHeight * 3; + EditorGUI.PropertyField(row5, interpolationCurve, curveLabel); + } + } + + private void SetOneAxis(SerializedProperty axisProperty, SerializedProperty propertyToSet, + SerializedProperty driverTransformProperty) + { + switch ((ShapeKeyPositionConfig.PositionOneAxis)axisProperty.enumValueIndex) + { + case ShapeKeyPositionConfig.PositionOneAxis.X: + propertyToSet.floatValue = + GetDriverPosition(driverTransformProperty.objectReferenceValue as Transform).x; + break; + case ShapeKeyPositionConfig.PositionOneAxis.Y: + propertyToSet.floatValue = + GetDriverPosition(driverTransformProperty.objectReferenceValue as Transform).y; + break; + case ShapeKeyPositionConfig.PositionOneAxis.Z: + propertyToSet.floatValue = + GetDriverPosition(driverTransformProperty.objectReferenceValue as Transform).z; + break; + } + } + + private Vector3 GetDriverPosition(Transform transform) + { + return transform.position; + } + } +} diff --git a/Assets/Editor/Animation/ShapeKeyDriver/ShapeKeyPositionConfigDrawer.cs.meta b/Assets/Editor/Animation/ShapeKeyDriver/ShapeKeyPositionConfigDrawer.cs.meta new file mode 100644 index 0000000..9af2891 --- /dev/null +++ b/Assets/Editor/Animation/ShapeKeyDriver/ShapeKeyPositionConfigDrawer.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: d4f9b79689c72ba4699217d48e10e974 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Editor/Animation/ShapeKeyDriver/ShapeKeyRecipeDrawer.cs b/Assets/Editor/Animation/ShapeKeyDriver/ShapeKeyRecipeDrawer.cs new file mode 100644 index 0000000..7a9b96a --- /dev/null +++ b/Assets/Editor/Animation/ShapeKeyDriver/ShapeKeyRecipeDrawer.cs @@ -0,0 +1,57 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using UnityEditor; +using UnityEngine; + +namespace BitStrap +{ + [CustomPropertyDrawer(typeof(ShapeKeyRecipe))] + + public class ShapeKeyRecipeDrawer : PropertyDrawer + { + public override float GetPropertyHeight( SerializedProperty property, GUIContent label ) + { + if (property.isExpanded) + return EditorHelper.singleLineHeight * 10; + + return EditorHelper.singleLineHeight; + } + + public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) + { + var row0 = position.Row(0); + var row1 = position.Row(1); + var row2 = position.Row(2); + var row3 = position.Row(3); + var row4 = position.Row(4); + var row5 = position.Row(5); + + var shapeKeyProperty = property.GetMemberProperty( k => k.shapeKeyDefinition); + EditorGUI.PropertyField(row0, shapeKeyProperty); + + var driverTypeProperty = property.GetMemberProperty( k => k.driverType); + EditorGUI.PropertyField(row1, driverTypeProperty); + + switch ((ShapeKeyRecipe.DriverType)driverTypeProperty.enumValueIndex) + { + case ShapeKeyRecipe.DriverType.Rotation: + var shapeKeyRotationProperty = property.GetMemberProperty( k => k.shapeKeyRotationConfig); + EditorGUI.PropertyField(row2, shapeKeyRotationProperty); + break; + case ShapeKeyRecipe.DriverType.Position: + var shapeKeyPositionProperty = property.GetMemberProperty( k => k.shapeKeyPositionConfig); + EditorGUI.PropertyField(row2, shapeKeyPositionProperty); + break; + case ShapeKeyRecipe.DriverType.MecanimParameter: + break; + case ShapeKeyRecipe.DriverType.Callback: + break; + default: + throw new ArgumentOutOfRangeException(); + } + + } + } +} + diff --git a/Assets/Editor/Animation/ShapeKeyDriver/ShapeKeyRecipeDrawer.cs.meta b/Assets/Editor/Animation/ShapeKeyDriver/ShapeKeyRecipeDrawer.cs.meta new file mode 100644 index 0000000..b71cfb4 --- /dev/null +++ b/Assets/Editor/Animation/ShapeKeyDriver/ShapeKeyRecipeDrawer.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 3d2d849eba043cf4385ce1b9219e762b +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Editor/Animation/ShapeKeyDriver/ShapeKeyRotationConfigDrawer.cs b/Assets/Editor/Animation/ShapeKeyDriver/ShapeKeyRotationConfigDrawer.cs index 07d286c..1255213 100644 --- a/Assets/Editor/Animation/ShapeKeyDriver/ShapeKeyRotationConfigDrawer.cs +++ b/Assets/Editor/Animation/ShapeKeyDriver/ShapeKeyRotationConfigDrawer.cs @@ -32,6 +32,7 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten if (!property.isExpanded) return; + // Draw Rotation Config var indentLevel = EditorGUI.indentLevel; using (IndentLevel.Do(indentLevel + 1)) using (LabelWidth.Do(128.0f)) @@ -53,16 +54,47 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten if (rotationTypeProperty.enumValueIndex == (int)ShapeKeyRotationConfig.RotationType.SingleAxis) { var rotationAxisProperty = property.GetMemberProperty( k => k.rotationAxis); - var startRotationProperty = property.GetMemberProperty( k => k.singleAxisStartRotation); - var endRotationProperty = property.GetMemberProperty( k => k.singleAxisEndRotation); + var startValueProperty = property.GetMemberProperty( k => k.singleAxisStartRotation); + var endValueProperty = property.GetMemberProperty( k => k.singleAxisEndRotation); + // Draw Property EditorGUI.PropertyField(row3, rotationAxisProperty); - EditorGUI.PropertyField(outLeftRect, startRotationProperty, startRotationLabel); - if ( GUI.Button(leftButtonRect, EditorGUIUtility.IconContent("d_Transform Icon"))) {} + EditorGUI.PropertyField(outLeftRect, startValueProperty, startRotationLabel); + if (GUI.Button(leftButtonRect, EditorGUIUtility.IconContent("d_Transform Icon"))) + { + switch ((ShapeKeyRotationConfig.RotationAxis)rotationAxisProperty.enumValueIndex) + { + case ShapeKeyRotationConfig.RotationAxis.X: + startValueProperty.floatValue = GetDriverRotation(driverTransformProperty.objectReferenceValue as Transform).x; + break; + case ShapeKeyRotationConfig.RotationAxis.Y: + startValueProperty.floatValue = GetDriverRotation(driverTransformProperty.objectReferenceValue as Transform).y; + break; + case ShapeKeyRotationConfig.RotationAxis.Z: + startValueProperty.floatValue = GetDriverRotation(driverTransformProperty.objectReferenceValue as Transform).z; + break; + default: + throw new ArgumentOutOfRangeException(); + } + } - EditorGUI.PropertyField(outRightRect, endRotationProperty, endRotationLabel); - if ( GUI.Button(rightButtonRect, EditorGUIUtility.IconContent("d_Transform Icon"))) {} + EditorGUI.PropertyField(outRightRect, endValueProperty, endRotationLabel); + if (GUI.Button(rightButtonRect, EditorGUIUtility.IconContent("d_Transform Icon"))) + { + switch (rotationAxisProperty.enumValueIndex) + { + case 0: + endValueProperty.floatValue = GetDriverRotation(driverTransformProperty.objectReferenceValue as Transform).x; + break; + case 1: + endValueProperty.floatValue = GetDriverRotation(driverTransformProperty.objectReferenceValue as Transform).y; + break; + case 2: + endValueProperty.floatValue = GetDriverRotation(driverTransformProperty.objectReferenceValue as Transform).z; + break; + } + } } else { @@ -70,10 +102,16 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten var endRotationProperty = property.GetMemberProperty( k => k.multiAxisEndRotation); EditorGUI.PropertyField(outLeftRect, startRotationProperty, startRotationLabel); - if ( GUI.Button(leftButtonRect, EditorGUIUtility.IconContent("d_Transform Icon"))) {} + if (GUI.Button(leftButtonRect, EditorGUIUtility.IconContent("d_Transform Icon"))) + { + startRotationProperty.vector3Value = GetDriverRotation(driverTransformProperty.objectReferenceValue as Transform); + } EditorGUI.PropertyField(outRightRect, endRotationProperty, endRotationLabel); - if ( GUI.Button(rightButtonRect, EditorGUIUtility.IconContent("d_Transform Icon"))) {} + if (GUI.Button(rightButtonRect, EditorGUIUtility.IconContent("d_Transform Icon"))) + { + endRotationProperty.vector3Value = GetDriverRotation(driverTransformProperty.objectReferenceValue as Transform); + } } var curveLabel = new GUIContent("Curve"); @@ -83,5 +121,10 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten } } + + private Vector3 GetDriverRotation(Transform transform) + { + return transform.localEulerAngles; + } } } diff --git a/Assets/Runtime/Animation/ShapeKeyDriver/ShapeKeyDefinition.cs b/Assets/Runtime/Animation/ShapeKeyDriver/ShapeKeyDefinition.cs index 2b947fc..24a2b95 100644 --- a/Assets/Runtime/Animation/ShapeKeyDriver/ShapeKeyDefinition.cs +++ b/Assets/Runtime/Animation/ShapeKeyDriver/ShapeKeyDefinition.cs @@ -1,3 +1,4 @@ +using System; using UnityEngine; [System.Serializable] @@ -31,6 +32,99 @@ public enum RotationAxis public Vector3 multiAxisStartRotation; public Vector3 multiAxisEndRotation; public AnimationCurve interpolationCurve; + + public float GetTargetSingleRotation() + { + switch (rotationAxis) + { + case RotationAxis.X: + return driverTransform.localEulerAngles.x; + case RotationAxis.Y: + return driverTransform.localEulerAngles.y; + case RotationAxis.Z: + return driverTransform.localEulerAngles.z; + } + + return -1.0f; + } +} + +[System.Serializable] +public struct ShapeKeyPositionConfig +{ + public enum PositionType + { + OneAxis, + TwoAxis, + ThreeAxis, + DistanceFromTarget + } + + public enum PositionOneAxis + { + X, + Y, + Z + } + + public enum PositionTwoAxis + { + XY, + XZ, + YX, + YZ, + ZY, + ZX + } + + public Transform driverTransform; + public Transform targetTransform; + public PositionType positionType; + public PositionOneAxis positionOneAxis; + public PositionTwoAxis positionTwoAxis; + public float oneAxisStartPosition; + public float oneAxisEndPosition; + public Vector2 twoAxisStartPosition; + public Vector2 twoAxisEndPosition; + public Vector3 threeAxisStartPosition; + public Vector3 threeAxisEndPosition; + public AnimationCurve interpolationCurve; + + public float GetTargetOneAxisPosition() + { + switch (positionOneAxis) + { + case PositionOneAxis.X: + return driverTransform.position.x; + case PositionOneAxis.Y: + return driverTransform.position.y; + case PositionOneAxis.Z: + return driverTransform.position.z; + } + return -1.0f; + } + + public Vector2 GetTargetTwoAxisPosition() + { + var position = driverTransform.position; + + switch (positionTwoAxis) + { + case PositionTwoAxis.XY: + return new Vector2(position.x, position.y); + case PositionTwoAxis.XZ: + return new Vector2(position.x, position.z); + case PositionTwoAxis.YX: + return new Vector2(position.y, position.x); + case PositionTwoAxis.YZ: + return new Vector2(position.y, position.z); + case PositionTwoAxis.ZY: + return new Vector2(position.z, position.y); + case PositionTwoAxis.ZX: + return new Vector2(position.z, position.x); + } + return Vector2.zero; + } } [System.Serializable] @@ -48,4 +142,5 @@ public enum DriverType public DriverType driverType; public ShapeKeyRotationConfig shapeKeyRotationConfig; + public ShapeKeyPositionConfig shapeKeyPositionConfig; } \ No newline at end of file diff --git a/Assets/Runtime/Animation/ShapeKeyDriver/ShapeKeyDriver.cs b/Assets/Runtime/Animation/ShapeKeyDriver/ShapeKeyDriver.cs index 3ab4df1..66a092b 100644 --- a/Assets/Runtime/Animation/ShapeKeyDriver/ShapeKeyDriver.cs +++ b/Assets/Runtime/Animation/ShapeKeyDriver/ShapeKeyDriver.cs @@ -1,7 +1,10 @@ using System; using System.Collections; using System.Collections.Generic; +using System.Diagnostics; +using BitStrap; using UnityEngine; +using Debug = UnityEngine.Debug; [ExecuteInEditMode] @@ -15,12 +18,76 @@ void Update() { if (shapeKeyRecipe.shapeKeyRotationConfig.driverTransform == null) return; - - var currentRotation = shapeKeyRecipe.shapeKeyRotationConfig.driverTransform.localRotation.eulerAngles.y; - Debug.Log(currentRotation); - var percentage = Mathf.InverseLerp(shapeKeyRecipe.shapeKeyRotationConfig.singleAxisStartRotation, shapeKeyRecipe.shapeKeyRotationConfig.singleAxisEndRotation, currentRotation); - var interpolation = Mathf.Lerp(0, 100, shapeKeyRecipe.shapeKeyRotationConfig.interpolationCurve.Evaluate(percentage)); - Debug.Log(interpolation); - skinnedMeshRenderer.SetBlendShapeWeight(shapeKeyRecipe.shapeKeyDefinition.index, interpolation); + + switch (shapeKeyRecipe.shapeKeyRotationConfig.rotationType) + { + case ShapeKeyRotationConfig.RotationType.SingleAxis: + { + var currentRotation = shapeKeyRecipe.shapeKeyRotationConfig.GetTargetSingleRotation(); + var percentage = Mathf.InverseLerp(shapeKeyRecipe.shapeKeyRotationConfig.singleAxisStartRotation, + shapeKeyRecipe.shapeKeyRotationConfig.singleAxisEndRotation, currentRotation); + var interpolation = Mathf.Lerp(0, 100, + shapeKeyRecipe.shapeKeyRotationConfig.interpolationCurve.Evaluate(percentage)); + skinnedMeshRenderer.SetBlendShapeWeight(shapeKeyRecipe.shapeKeyDefinition.index, interpolation); + break; + } + case ShapeKeyRotationConfig.RotationType.MultiAxis: + { + var start = Quaternion.Euler(shapeKeyRecipe.shapeKeyRotationConfig.multiAxisStartRotation); + var end = Quaternion.Euler(shapeKeyRecipe.shapeKeyRotationConfig.multiAxisEndRotation); + var mid = Quaternion.SlerpUnclamped(start, end, 0.5f); + // var projected = Vector3.ProjectOnPlane(shapeKeyRecipe.shapeKeyRotationConfig.driverTransform.forward, normal); + var maxAngle = Quaternion.Angle(start, end); + + if (maxAngle < 0) + maxAngle += 360.0f; + + var angleFromStart = + Quaternion.Angle(start, shapeKeyRecipe.shapeKeyRotationConfig.driverTransform.localRotation); + + if (angleFromStart < 0) + angleFromStart += 360.0f; + + var angleToEnd = + Quaternion.Angle(shapeKeyRecipe.shapeKeyRotationConfig.driverTransform.localRotation, end); + + if (angleToEnd < 0) + angleToEnd += 360.0f; + + var angleToMid = Quaternion.Angle(shapeKeyRecipe.shapeKeyRotationConfig.driverTransform.localRotation, + mid); + + if (angleToMid < 0) + angleToMid += 360.0f; + + var percentage = 0.0f; + if (angleFromStart < angleToEnd) + percentage = Mathf.InverseLerp(maxAngle / 2.0f, 0.0f, angleToMid) / 2.0f; + else + percentage = Mathf.InverseLerp(0.0f, maxAngle, angleToMid) + 0.5f; + + // var percentage = Mathf.InverseLerp(0, maxAngle, angle); + + Debug.Log($"Max: {maxAngle}, Angle From Start: {angleFromStart}, Angle to End: {angleToEnd}, Angle to Mid: {angleToMid}"); + + // var cross = Vector3.Cross(start, projected); + // var max = normal.magnitude; + // var t = cross.magnitude; + // var percentage = t / max; + + var interpolation = shapeKeyRecipe.shapeKeyRotationConfig.interpolationCurve.Evaluate(percentage) * 100; + + skinnedMeshRenderer.SetBlendShapeWeight(shapeKeyRecipe.shapeKeyDefinition.index, interpolation); + break; + } + } + + } + + public static float InverseLerp(Vector3 a, Vector3 b, Vector3 value) + { + Vector3 AB = b - a; + Vector3 AV = value - a; + return Vector3.Dot(AV, AB) / Vector3.Dot(AB, AB); } } From a79ec3b8a17e76db9323bbfef837833cbdf777e5 Mon Sep 17 00:00:00 2001 From: elamhut Date: Sat, 19 Feb 2022 20:53:02 -0300 Subject: [PATCH 3/7] ShapeKey Position Config working! --- .../ShapeKeyPositionConfigDrawer.cs | 84 ++++++++++++++--- .../ShapeKeyDriver/ShapeKeyRecipeDrawer.cs | 10 +- .../ShapeKeyDriver/ShapeKeyDefinition.cs | 5 +- .../ShapeKeyDriver/ShapeKeyDriver.cs | 91 +++++++++++++++++-- Packages/manifest.json | 2 +- Packages/packages-lock.json | 2 +- ProjectSettings/ProjectVersion.txt | 4 +- 7 files changed, 160 insertions(+), 38 deletions(-) diff --git a/Assets/Editor/Animation/ShapeKeyDriver/ShapeKeyPositionConfigDrawer.cs b/Assets/Editor/Animation/ShapeKeyDriver/ShapeKeyPositionConfigDrawer.cs index 8e47be8..1d32474 100644 --- a/Assets/Editor/Animation/ShapeKeyDriver/ShapeKeyPositionConfigDrawer.cs +++ b/Assets/Editor/Animation/ShapeKeyDriver/ShapeKeyPositionConfigDrawer.cs @@ -83,32 +83,51 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten EditorGUI.PropertyField(row3, axisProperty, positionAxisLabel); row4.Left(row4.width * 0.5f, out var leftRect).Expand(out var rightRect); + leftRect.Right(26.0f, out var leftButtonRect).Left(-10.0f).Expand(out var outLeftRect); + rightRect.Right(26.0f, out var rightButtonRect).Left(-10.0f).Expand(out var outRightRect); var startValueProperty = property.GetMemberProperty( k => k.twoAxisStartPosition); - EditorGUI.PropertyField(leftRect, startValueProperty, startPropertyLabel); + EditorGUI.PropertyField(outLeftRect, startValueProperty, startPropertyLabel); + + if (GUI.Button(leftButtonRect, EditorGUIUtility.IconContent("d_Transform Icon"))) + { + SetTwoAxis(axisProperty, startValueProperty, driverTransformProperty); + } var endValueProperty = property.GetMemberProperty( k => k.twoAxisEndPosition); - EditorGUI.PropertyField(rightRect, endValueProperty, endPropertyLabel); + EditorGUI.PropertyField(outRightRect, endValueProperty, endPropertyLabel); + + if (GUI.Button(rightButtonRect, EditorGUIUtility.IconContent("d_Transform Icon"))) + { + SetTwoAxis(axisProperty, endValueProperty, driverTransformProperty); + } break; } case ShapeKeyPositionConfig.PositionType.ThreeAxis: { row4.Left(row4.width * 0.5f, out var leftRect).Expand(out var rightRect); + leftRect.Right(26.0f, out var leftButtonRect).Left(-10.0f).Expand(out var outLeftRect); + rightRect.Right(26.0f, out var rightButtonRect).Left(-10.0f).Expand(out var outRightRect); var startValueProperty = property.GetMemberProperty(k => k.threeAxisStartPosition); - EditorGUI.PropertyField(leftRect, startValueProperty, startPropertyLabel); + EditorGUI.PropertyField(outLeftRect, startValueProperty, startPropertyLabel); + + if (GUI.Button(leftButtonRect, EditorGUIUtility.IconContent("d_Transform Icon"))) + { + SetThreeAxis(startValueProperty, driverTransformProperty); + } var endValueProperty = property.GetMemberProperty(k => k.threeAxisEndPosition); - EditorGUI.PropertyField(rightRect, endValueProperty, endPropertyLabel); + EditorGUI.PropertyField(outRightRect, endValueProperty, endPropertyLabel); + + if (GUI.Button(rightButtonRect, EditorGUIUtility.IconContent("d_Transform Icon"))) + { + SetThreeAxis(endValueProperty, driverTransformProperty); + } break; } - case ShapeKeyPositionConfig.PositionType.DistanceFromTarget: - var targetTransformProperty = property.GetMemberProperty(k => k.targetTransform); - EditorGUI.PropertyField(row4, targetTransformProperty); - - break; } var interpolationCurve = property.GetMemberProperty( k => k.interpolationCurve); var curveLabel = new GUIContent("Curve"); @@ -121,22 +140,57 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten private void SetOneAxis(SerializedProperty axisProperty, SerializedProperty propertyToSet, SerializedProperty driverTransformProperty) { + var driverPos = GetDriverPosition(driverTransformProperty.objectReferenceValue as Transform); + switch ((ShapeKeyPositionConfig.PositionOneAxis)axisProperty.enumValueIndex) { case ShapeKeyPositionConfig.PositionOneAxis.X: - propertyToSet.floatValue = - GetDriverPosition(driverTransformProperty.objectReferenceValue as Transform).x; + propertyToSet.floatValue = driverPos.x; break; case ShapeKeyPositionConfig.PositionOneAxis.Y: - propertyToSet.floatValue = - GetDriverPosition(driverTransformProperty.objectReferenceValue as Transform).y; + propertyToSet.floatValue = driverPos.y; break; case ShapeKeyPositionConfig.PositionOneAxis.Z: - propertyToSet.floatValue = - GetDriverPosition(driverTransformProperty.objectReferenceValue as Transform).z; + propertyToSet.floatValue = driverPos.z; + break; + } + } + + private void SetTwoAxis(SerializedProperty axisProperty, SerializedProperty propertyToSet, + SerializedProperty driverTransformProperty) + { + var driverPos = GetDriverPosition(driverTransformProperty.objectReferenceValue as Transform); + + switch ((ShapeKeyPositionConfig.PositionTwoAxis)axisProperty.enumValueIndex) + { + case ShapeKeyPositionConfig.PositionTwoAxis.XY: + propertyToSet.vector2Value = new Vector2(driverPos.x, driverPos.y); + break; + case ShapeKeyPositionConfig.PositionTwoAxis.XZ: + propertyToSet.vector2Value = new Vector2(driverPos.x, driverPos.z); + break; + case ShapeKeyPositionConfig.PositionTwoAxis.YX: + propertyToSet.vector2Value = new Vector2(driverPos.y, driverPos.x); + break; + case ShapeKeyPositionConfig.PositionTwoAxis.YZ: + propertyToSet.vector2Value = new Vector2(driverPos.y, driverPos.z); + break; + case ShapeKeyPositionConfig.PositionTwoAxis.ZY: + propertyToSet.vector2Value = new Vector2(driverPos.z, driverPos.y); + break; + case ShapeKeyPositionConfig.PositionTwoAxis.ZX: + propertyToSet.vector2Value = new Vector2(driverPos.z, driverPos.x); break; } } + + private void SetThreeAxis(SerializedProperty propertyToSet, SerializedProperty driverTransformProperty) + { + var driverPos = GetDriverPosition(driverTransformProperty.objectReferenceValue as Transform); + + propertyToSet.vector3Value = driverPos; + } + private Vector3 GetDriverPosition(Transform transform) { diff --git a/Assets/Editor/Animation/ShapeKeyDriver/ShapeKeyRecipeDrawer.cs b/Assets/Editor/Animation/ShapeKeyDriver/ShapeKeyRecipeDrawer.cs index 7a9b96a..15d6370 100644 --- a/Assets/Editor/Animation/ShapeKeyDriver/ShapeKeyRecipeDrawer.cs +++ b/Assets/Editor/Animation/ShapeKeyDriver/ShapeKeyRecipeDrawer.cs @@ -43,12 +43,10 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten var shapeKeyPositionProperty = property.GetMemberProperty( k => k.shapeKeyPositionConfig); EditorGUI.PropertyField(row2, shapeKeyPositionProperty); break; - case ShapeKeyRecipe.DriverType.MecanimParameter: - break; - case ShapeKeyRecipe.DriverType.Callback: - break; - default: - throw new ArgumentOutOfRangeException(); + // case ShapeKeyRecipe.DriverType.MecanimParameter: + // break; + // case ShapeKeyRecipe.DriverType.Callback: + // break; } } diff --git a/Assets/Runtime/Animation/ShapeKeyDriver/ShapeKeyDefinition.cs b/Assets/Runtime/Animation/ShapeKeyDriver/ShapeKeyDefinition.cs index 24a2b95..279b866 100644 --- a/Assets/Runtime/Animation/ShapeKeyDriver/ShapeKeyDefinition.cs +++ b/Assets/Runtime/Animation/ShapeKeyDriver/ShapeKeyDefinition.cs @@ -57,7 +57,6 @@ public enum PositionType OneAxis, TwoAxis, ThreeAxis, - DistanceFromTarget } public enum PositionOneAxis @@ -136,8 +135,8 @@ public enum DriverType { Rotation, Position, - MecanimParameter, - Callback, + // MecanimParameter, + // Callback, } public DriverType driverType; diff --git a/Assets/Runtime/Animation/ShapeKeyDriver/ShapeKeyDriver.cs b/Assets/Runtime/Animation/ShapeKeyDriver/ShapeKeyDriver.cs index 66a092b..d9041c5 100644 --- a/Assets/Runtime/Animation/ShapeKeyDriver/ShapeKeyDriver.cs +++ b/Assets/Runtime/Animation/ShapeKeyDriver/ShapeKeyDriver.cs @@ -2,6 +2,7 @@ using System.Collections; using System.Collections.Generic; using System.Diagnostics; +using System.Runtime.CompilerServices; using BitStrap; using UnityEngine; using Debug = UnityEngine.Debug; @@ -16,9 +17,23 @@ public class ShapeKeyDriver : MonoBehaviour // Update is called once per frame void Update() { - if (shapeKeyRecipe.shapeKeyRotationConfig.driverTransform == null) + if (shapeKeyRecipe.shapeKeyRotationConfig.driverTransform == null && + shapeKeyRecipe.shapeKeyPositionConfig.driverTransform == null) return; + + switch(shapeKeyRecipe.driverType) + { + case ShapeKeyRecipe.DriverType.Rotation: + SwitchRotationConfig(); + break; + case ShapeKeyRecipe.DriverType.Position: + SwitchPositionConfig(); + break; + } + } + private void SwitchRotationConfig() + { switch (shapeKeyRecipe.shapeKeyRotationConfig.rotationType) { case ShapeKeyRotationConfig.RotationType.SingleAxis: @@ -38,7 +53,7 @@ void Update() var mid = Quaternion.SlerpUnclamped(start, end, 0.5f); // var projected = Vector3.ProjectOnPlane(shapeKeyRecipe.shapeKeyRotationConfig.driverTransform.forward, normal); var maxAngle = Quaternion.Angle(start, end); - + if (maxAngle < 0) maxAngle += 360.0f; @@ -47,7 +62,7 @@ void Update() if (angleFromStart < 0) angleFromStart += 360.0f; - + var angleToEnd = Quaternion.Angle(shapeKeyRecipe.shapeKeyRotationConfig.driverTransform.localRotation, end); @@ -56,7 +71,7 @@ void Update() var angleToMid = Quaternion.Angle(shapeKeyRecipe.shapeKeyRotationConfig.driverTransform.localRotation, mid); - + if (angleToMid < 0) angleToMid += 360.0f; @@ -65,25 +80,81 @@ void Update() percentage = Mathf.InverseLerp(maxAngle / 2.0f, 0.0f, angleToMid) / 2.0f; else percentage = Mathf.InverseLerp(0.0f, maxAngle, angleToMid) + 0.5f; - + // var percentage = Mathf.InverseLerp(0, maxAngle, angle); - Debug.Log($"Max: {maxAngle}, Angle From Start: {angleFromStart}, Angle to End: {angleToEnd}, Angle to Mid: {angleToMid}"); - + // Debug.Log($"Max: {maxAngle}, Angle From Start: {angleFromStart}, Angle to End: {angleToEnd}, Angle to Mid: {angleToMid}"); + // var cross = Vector3.Cross(start, projected); // var max = normal.magnitude; // var t = cross.magnitude; // var percentage = t / max; - + var interpolation = shapeKeyRecipe.shapeKeyRotationConfig.interpolationCurve.Evaluate(percentage) * 100; - + skinnedMeshRenderer.SetBlendShapeWeight(shapeKeyRecipe.shapeKeyDefinition.index, interpolation); break; } } + } + private void SwitchPositionConfig() + { + var posConf = shapeKeyRecipe.shapeKeyPositionConfig; + + switch (posConf.positionType) + { + case ShapeKeyPositionConfig.PositionType.OneAxis: + { + var currentPosition = posConf.GetTargetOneAxisPosition(); + var percentage = Mathf.InverseLerp(posConf.oneAxisStartPosition, + posConf.oneAxisEndPosition, currentPosition); + var interpolation = Mathf.Lerp(0, 100, + posConf.interpolationCurve.Evaluate(percentage)); + skinnedMeshRenderer.SetBlendShapeWeight(shapeKeyRecipe.shapeKeyDefinition.index, interpolation); + break; + } + case ShapeKeyPositionConfig.PositionType.TwoAxis: + { + var currentPosition = posConf.GetTargetTwoAxisPosition(); + + var percentageX = Mathf.InverseLerp(posConf.twoAxisStartPosition.x, + posConf.twoAxisEndPosition.x, currentPosition.x); + + var percentageY = Mathf.InverseLerp(posConf.twoAxisStartPosition.y, + posConf.twoAxisEndPosition.y, currentPosition.y); + + var percentageMedian = (percentageX + percentageY) / 2; + + var interpolation = Mathf.Lerp(0, 100, posConf.interpolationCurve.Evaluate(percentageMedian)); + skinnedMeshRenderer.SetBlendShapeWeight(shapeKeyRecipe.shapeKeyDefinition.index, interpolation); + break; + } + + case ShapeKeyPositionConfig.PositionType.ThreeAxis: + { + var currentPosition = posConf.driverTransform.position; + + var percentageX = Mathf.InverseLerp(posConf.threeAxisStartPosition.x, + posConf.threeAxisEndPosition.x, currentPosition.x); + + var percentageY = Mathf.InverseLerp(posConf.threeAxisStartPosition.y, + posConf.threeAxisEndPosition.y, currentPosition.y); + + var percentageZ = Mathf.InverseLerp(posConf.threeAxisStartPosition.z, + posConf.threeAxisEndPosition.z, currentPosition.z); + + var percentageMedian = (percentageX + percentageY + percentageZ) / 3; + + var interpolation = Mathf.Lerp(0, 100, posConf.interpolationCurve.Evaluate(percentageMedian)); + skinnedMeshRenderer.SetBlendShapeWeight(shapeKeyRecipe.shapeKeyDefinition.index, interpolation); + + break; + } + } } - + + public static float InverseLerp(Vector3 a, Vector3 b, Vector3 value) { Vector3 AB = b - a; diff --git a/Packages/manifest.json b/Packages/manifest.json index 4ff376b..844d8f5 100644 --- a/Packages/manifest.json +++ b/Packages/manifest.json @@ -3,7 +3,7 @@ "com.unity.2d.sprite": "1.0.0", "com.unity.2d.tilemap": "1.0.0", "com.unity.ide.rider": "3.0.9", - "com.unity.ide.visualstudio": "2.0.12", + "com.unity.ide.visualstudio": "2.0.14", "com.unity.ide.vscode": "1.2.4", "com.unity.test-framework": "1.1.29", "com.unity.textmeshpro": "3.0.6", diff --git a/Packages/packages-lock.json b/Packages/packages-lock.json index 05d1ce3..6dd6cc0 100644 --- a/Packages/packages-lock.json +++ b/Packages/packages-lock.json @@ -29,7 +29,7 @@ "url": "https://packages.unity.com" }, "com.unity.ide.visualstudio": { - "version": "2.0.12", + "version": "2.0.14", "depth": 0, "source": "registry", "dependencies": { diff --git a/ProjectSettings/ProjectVersion.txt b/ProjectSettings/ProjectVersion.txt index bbae793..1f6fff2 100644 --- a/ProjectSettings/ProjectVersion.txt +++ b/ProjectSettings/ProjectVersion.txt @@ -1,2 +1,2 @@ -m_EditorVersion: 2021.2.7f1 -m_EditorVersionWithRevision: 2021.2.7f1 (6bd9e232123f) +m_EditorVersion: 2021.2.11f1 +m_EditorVersionWithRevision: 2021.2.11f1 (e50cafbb4399) From 7642652b21b5975e2902cd053e173e24b0f684a0 Mon Sep 17 00:00:00 2001 From: elamhut Date: Sun, 20 Feb 2022 22:16:54 -0300 Subject: [PATCH 4/7] Start Pull Request improvements --- .../ShapeKeyDriver/ShapeKeyDrawer.cs | 18 ++++--- .../ShapeKeyPositionConfigDrawer.cs | 48 +++++++++---------- 2 files changed, 34 insertions(+), 32 deletions(-) diff --git a/Assets/Editor/Animation/ShapeKeyDriver/ShapeKeyDrawer.cs b/Assets/Editor/Animation/ShapeKeyDriver/ShapeKeyDrawer.cs index bb6b7bc..154486d 100644 --- a/Assets/Editor/Animation/ShapeKeyDriver/ShapeKeyDrawer.cs +++ b/Assets/Editor/Animation/ShapeKeyDriver/ShapeKeyDrawer.cs @@ -10,6 +10,8 @@ namespace BitStrap [CustomPropertyDrawer(typeof(ShapeKeyDefinition))] public class ShapeKeyDrawer : PropertyDrawer { + string[] blendShapeNames = null; + public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { PropertyDrawerHelper.LoadAttributeTooltip( this, label ); @@ -26,18 +28,20 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten if (skinnedMeshRenderer != null) { - var blendShapeCount = skinnedMeshRenderer.sharedMesh.blendShapeCount; - var blendShapeNames = new List(); - for (int i = 0; i < blendShapeCount; i++) + if (blendShapeNames == null) { - blendShapeNames.Add(skinnedMeshRenderer.sharedMesh.GetBlendShapeName(i)); + var blendShapeCount = skinnedMeshRenderer.sharedMesh.blendShapeCount; + blendShapeNames = new string[blendShapeCount]; + for (int i = 0; i < blendShapeCount; i++) + { + blendShapeNames[i] = skinnedMeshRenderer.sharedMesh.GetBlendShapeName(i); + } } - var popupOptions = blendShapeNames.Select( x => new GUIContent( x ) ).ToArray(); - int currentIndex = blendShapeNames.IndexOf(nameProperty.stringValue); + int currentIndex = Array.IndexOf(blendShapeNames, nameProperty.stringValue); EditorGUI.BeginChangeCheck(); - currentIndex = EditorGUI.Popup( position, label, currentIndex, popupOptions ); + currentIndex = EditorGUI.Popup( position, label.text, currentIndex, blendShapeNames ); if( EditorGUI.EndChangeCheck() ) { diff --git a/Assets/Editor/Animation/ShapeKeyDriver/ShapeKeyPositionConfigDrawer.cs b/Assets/Editor/Animation/ShapeKeyDriver/ShapeKeyPositionConfigDrawer.cs index 1d32474..7d87ed4 100644 --- a/Assets/Editor/Animation/ShapeKeyDriver/ShapeKeyPositionConfigDrawer.cs +++ b/Assets/Editor/Animation/ShapeKeyDriver/ShapeKeyPositionConfigDrawer.cs @@ -20,28 +20,26 @@ public override float GetPropertyHeight( SerializedProperty property, GUIContent public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { PropertyDrawerHelper.LoadAttributeTooltip( this, label ); - var row0 = position.Row(0); - var row1 = position.Row(1); - var row2 = position.Row(2); - var row3 = position.Row(3); - var row4 = position.Row(4); - var row5 = position.Row(5); + var rowFoldout = position.Row(0); + var rowDriverTransform = position.Row(1); + var rowPositionType = position.Row(2); + var rowAxis = position.Row(3); + var rowValues = position.Row(4); + var rowCurve = position.Row(5); - property.isExpanded = EditorGUI.Foldout(row0, property.isExpanded, label); - + property.isExpanded = EditorGUI.Foldout(rowFoldout, property.isExpanded, label); if (!property.isExpanded) return; - var indentLevel = EditorGUI.indentLevel; - using (IndentLevel.Do(indentLevel + 1)) + using (IndentLevel.Do(EditorGUI.indentLevel + 1)) using (LabelWidth.Do(128.0f)) { var driverTransformProperty = property.GetMemberProperty( k => k.driverTransform); - EditorGUI.PropertyField(row1, driverTransformProperty); + EditorGUI.PropertyField(rowDriverTransform, driverTransformProperty); var positionTypeProperty = property.GetMemberProperty( k => k.positionType); - EditorGUI.PropertyField(row2, positionTypeProperty); + EditorGUI.PropertyField(rowPositionType, positionTypeProperty); var startPropertyLabel = new GUIContent("Start Position"); var endPropertyLabel = new GUIContent("End Position"); @@ -52,11 +50,11 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten { var axisProperty = property.GetMemberProperty( k => k.positionOneAxis); var positionAxisLabel = new GUIContent("Axis"); - EditorGUI.PropertyField(row3, axisProperty, positionAxisLabel); + EditorGUI.PropertyField(rowAxis, axisProperty, positionAxisLabel); - row4.Left(row4.width * 0.5f, out var leftRect).Expand(out var rightRect); - leftRect.Right(26.0f, out var leftButtonRect).Left(-10.0f).Expand(out var outLeftRect); - rightRect.Right(26.0f, out var rightButtonRect).Left(-10.0f).Expand(out var outRightRect); + rowValues.Left(rowValues.width * 0.5f, out var leftRect).Expand(out var rightRect); + leftRect.Right(26.0f, out var leftButtonRect).Left(-2.0f).Expand(out var outLeftRect); + rightRect.Right(26.0f, out var rightButtonRect).Left(-2.0f).Expand(out var outRightRect); var startValueProperty = property.GetMemberProperty( k => k.oneAxisStartPosition); EditorGUI.PropertyField(outLeftRect, startValueProperty, startPropertyLabel); @@ -80,11 +78,11 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten { var axisProperty = property.GetMemberProperty(k => k.positionTwoAxis); var positionAxisLabel = new GUIContent("Axis"); - EditorGUI.PropertyField(row3, axisProperty, positionAxisLabel); + EditorGUI.PropertyField(rowAxis, axisProperty, positionAxisLabel); - row4.Left(row4.width * 0.5f, out var leftRect).Expand(out var rightRect); - leftRect.Right(26.0f, out var leftButtonRect).Left(-10.0f).Expand(out var outLeftRect); - rightRect.Right(26.0f, out var rightButtonRect).Left(-10.0f).Expand(out var outRightRect); + rowValues.Left(rowValues.width * 0.5f, out var leftRect).Expand(out var rightRect); + leftRect.Right(26.0f, out var leftButtonRect).Left(-2.0f).Expand(out var outLeftRect); + rightRect.Right(26.0f, out var rightButtonRect).Left(-2.0f).Expand(out var outRightRect); var startValueProperty = property.GetMemberProperty( k => k.twoAxisStartPosition); EditorGUI.PropertyField(outLeftRect, startValueProperty, startPropertyLabel); @@ -106,9 +104,9 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten } case ShapeKeyPositionConfig.PositionType.ThreeAxis: { - row4.Left(row4.width * 0.5f, out var leftRect).Expand(out var rightRect); - leftRect.Right(26.0f, out var leftButtonRect).Left(-10.0f).Expand(out var outLeftRect); - rightRect.Right(26.0f, out var rightButtonRect).Left(-10.0f).Expand(out var outRightRect); + rowValues.Left(rowValues.width * 0.5f, out var leftRect).Expand(out var rightRect); + leftRect.Right(26.0f, out var leftButtonRect).Left(-2.0f).Expand(out var outLeftRect); + rightRect.Right(26.0f, out var rightButtonRect).Left(-2.0f).Expand(out var outRightRect); var startValueProperty = property.GetMemberProperty(k => k.threeAxisStartPosition); EditorGUI.PropertyField(outLeftRect, startValueProperty, startPropertyLabel); @@ -132,8 +130,8 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten var interpolationCurve = property.GetMemberProperty( k => k.interpolationCurve); var curveLabel = new GUIContent("Curve"); - row5.height = EditorHelper.singleLineHeight * 3; - EditorGUI.PropertyField(row5, interpolationCurve, curveLabel); + rowCurve.height = EditorHelper.singleLineHeight * 3; + EditorGUI.PropertyField(rowCurve, interpolationCurve, curveLabel); } } From 5808b7ac38551dbb1d7bccef15bcfa9b209a5ebc Mon Sep 17 00:00:00 2001 From: elamhut Date: Sun, 20 Feb 2022 23:38:42 -0300 Subject: [PATCH 5/7] All Pull Request Fixes. Need to look at ShakeKeyDriver now :) --- .../ShapeKeyDriver/ShapeKeyDrawer.cs | 6 +- .../ShapeKeyPositionConfigDrawer.cs | 32 +++----- .../ShapeKeyDriver/ShapeKeyRecipeDrawer.cs | 36 ++++----- .../ShapeKeyRotationConfigDrawer.cs | 74 +++++++++---------- Assets/Editor/Extensions/RectExtensions.cs | 4 +- Assets/Editor/Helpers/EditorHelper.cs | 2 +- .../ShapeKeyDriver/ShapeKeyDefinition.cs | 3 +- .../ShapeKeyDriver/ShapeKeyDriver.cs | 16 +--- 8 files changed, 74 insertions(+), 99 deletions(-) diff --git a/Assets/Editor/Animation/ShapeKeyDriver/ShapeKeyDrawer.cs b/Assets/Editor/Animation/ShapeKeyDriver/ShapeKeyDrawer.cs index 154486d..81a02f9 100644 --- a/Assets/Editor/Animation/ShapeKeyDriver/ShapeKeyDrawer.cs +++ b/Assets/Editor/Animation/ShapeKeyDriver/ShapeKeyDrawer.cs @@ -1,7 +1,3 @@ -using System; -using System.Collections; -using System.Collections.Generic; -using System.Linq; using UnityEditor; using UnityEngine; @@ -38,7 +34,7 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten } } - int currentIndex = Array.IndexOf(blendShapeNames, nameProperty.stringValue); + int currentIndex = System.Array.IndexOf(blendShapeNames, nameProperty.stringValue); EditorGUI.BeginChangeCheck(); currentIndex = EditorGUI.Popup( position, label.text, currentIndex, blendShapeNames ); diff --git a/Assets/Editor/Animation/ShapeKeyDriver/ShapeKeyPositionConfigDrawer.cs b/Assets/Editor/Animation/ShapeKeyDriver/ShapeKeyPositionConfigDrawer.cs index 7d87ed4..f119301 100644 --- a/Assets/Editor/Animation/ShapeKeyDriver/ShapeKeyPositionConfigDrawer.cs +++ b/Assets/Editor/Animation/ShapeKeyDriver/ShapeKeyPositionConfigDrawer.cs @@ -1,7 +1,3 @@ -using System; -using System.Collections; -using System.Collections.Generic; -using System.Linq; using UnityEditor; using UnityEngine; @@ -13,9 +9,9 @@ public class ShapeKeyPositionConfigDrawer : PropertyDrawer public override float GetPropertyHeight( SerializedProperty property, GUIContent label ) { if (property.isExpanded) - return EditorHelper.singleLineHeight * 8; + return EditorHelper.SingleLineHeight * 8; - return EditorHelper.singleLineHeight; + return EditorHelper.SingleLineHeight; } public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { @@ -130,7 +126,7 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten var interpolationCurve = property.GetMemberProperty( k => k.interpolationCurve); var curveLabel = new GUIContent("Curve"); - rowCurve.height = EditorHelper.singleLineHeight * 3; + rowCurve.height = EditorHelper.SingleLineHeight * 3; EditorGUI.PropertyField(rowCurve, interpolationCurve, curveLabel); } } @@ -138,18 +134,18 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten private void SetOneAxis(SerializedProperty axisProperty, SerializedProperty propertyToSet, SerializedProperty driverTransformProperty) { - var driverPos = GetDriverPosition(driverTransformProperty.objectReferenceValue as Transform); + var driverTransformPosition = ((Transform)driverTransformProperty.objectReferenceValue).position; switch ((ShapeKeyPositionConfig.PositionOneAxis)axisProperty.enumValueIndex) { case ShapeKeyPositionConfig.PositionOneAxis.X: - propertyToSet.floatValue = driverPos.x; + propertyToSet.floatValue = driverTransformPosition.x; break; case ShapeKeyPositionConfig.PositionOneAxis.Y: - propertyToSet.floatValue = driverPos.y; + propertyToSet.floatValue = driverTransformPosition.y; break; case ShapeKeyPositionConfig.PositionOneAxis.Z: - propertyToSet.floatValue = driverPos.z; + propertyToSet.floatValue = driverTransformPosition.z; break; } } @@ -157,7 +153,9 @@ private void SetOneAxis(SerializedProperty axisProperty, SerializedProperty prop private void SetTwoAxis(SerializedProperty axisProperty, SerializedProperty propertyToSet, SerializedProperty driverTransformProperty) { - var driverPos = GetDriverPosition(driverTransformProperty.objectReferenceValue as Transform); + var driverTransformPosition = ((Transform)driverTransformProperty.objectReferenceValue).position; + + var driverPos = driverTransformPosition; switch ((ShapeKeyPositionConfig.PositionTwoAxis)axisProperty.enumValueIndex) { @@ -184,15 +182,9 @@ private void SetTwoAxis(SerializedProperty axisProperty, SerializedProperty prop private void SetThreeAxis(SerializedProperty propertyToSet, SerializedProperty driverTransformProperty) { - var driverPos = GetDriverPosition(driverTransformProperty.objectReferenceValue as Transform); - - propertyToSet.vector3Value = driverPos; - } - + var driverTransformPosition = ((Transform)driverTransformProperty.objectReferenceValue).position; - private Vector3 GetDriverPosition(Transform transform) - { - return transform.position; + propertyToSet.vector3Value = driverTransformPosition; } } } diff --git a/Assets/Editor/Animation/ShapeKeyDriver/ShapeKeyRecipeDrawer.cs b/Assets/Editor/Animation/ShapeKeyDriver/ShapeKeyRecipeDrawer.cs index 15d6370..94443dc 100644 --- a/Assets/Editor/Animation/ShapeKeyDriver/ShapeKeyRecipeDrawer.cs +++ b/Assets/Editor/Animation/ShapeKeyDriver/ShapeKeyRecipeDrawer.cs @@ -1,48 +1,48 @@ -using System; -using System.Collections; -using System.Collections.Generic; using UnityEditor; using UnityEngine; namespace BitStrap { [CustomPropertyDrawer(typeof(ShapeKeyRecipe))] - public class ShapeKeyRecipeDrawer : PropertyDrawer { public override float GetPropertyHeight( SerializedProperty property, GUIContent label ) { if (property.isExpanded) - return EditorHelper.singleLineHeight * 10; + return EditorHelper.SingleLineHeight * 10; - return EditorHelper.singleLineHeight; + return EditorHelper.SingleLineHeight; } public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { - var row0 = position.Row(0); - var row1 = position.Row(1); - var row2 = position.Row(2); - var row3 = position.Row(3); - var row4 = position.Row(4); - var row5 = position.Row(5); + var rowShapeKeyDefinition = position.Row(0); + var rowDriverType = position.Row(1); + var rowShapeKeyConfigs = position.Row(2); var shapeKeyProperty = property.GetMemberProperty( k => k.shapeKeyDefinition); - EditorGUI.PropertyField(row0, shapeKeyProperty); + EditorGUI.PropertyField(rowShapeKeyDefinition, shapeKeyProperty); var driverTypeProperty = property.GetMemberProperty( k => k.driverType); - EditorGUI.PropertyField(row1, driverTypeProperty); + EditorGUI.PropertyField(rowDriverType, driverTypeProperty); switch ((ShapeKeyRecipe.DriverType)driverTypeProperty.enumValueIndex) { case ShapeKeyRecipe.DriverType.Rotation: - var shapeKeyRotationProperty = property.GetMemberProperty( k => k.shapeKeyRotationConfig); - EditorGUI.PropertyField(row2, shapeKeyRotationProperty); + { + var shapeKeyRotationProperty = + property.GetMemberProperty(k => k.shapeKeyRotationConfig); + EditorGUI.PropertyField(rowShapeKeyConfigs, shapeKeyRotationProperty); break; + } case ShapeKeyRecipe.DriverType.Position: - var shapeKeyPositionProperty = property.GetMemberProperty( k => k.shapeKeyPositionConfig); - EditorGUI.PropertyField(row2, shapeKeyPositionProperty); + { + var shapeKeyPositionProperty = + property.GetMemberProperty(k => k.shapeKeyPositionConfig); + EditorGUI.PropertyField(rowShapeKeyConfigs, shapeKeyPositionProperty); break; + } + // TODO: implement // case ShapeKeyRecipe.DriverType.MecanimParameter: // break; // case ShapeKeyRecipe.DriverType.Callback: diff --git a/Assets/Editor/Animation/ShapeKeyDriver/ShapeKeyRotationConfigDrawer.cs b/Assets/Editor/Animation/ShapeKeyDriver/ShapeKeyRotationConfigDrawer.cs index 1255213..b03d997 100644 --- a/Assets/Editor/Animation/ShapeKeyDriver/ShapeKeyRotationConfigDrawer.cs +++ b/Assets/Editor/Animation/ShapeKeyDriver/ShapeKeyRotationConfigDrawer.cs @@ -1,7 +1,3 @@ -using System; -using System.Collections; -using System.Collections.Generic; -using System.Linq; using UnityEditor; using UnityEngine; @@ -10,31 +6,32 @@ namespace BitStrap [CustomPropertyDrawer(typeof(ShapeKeyRotationConfig))] public class ShapeKeyRotationConfigDrawer : PropertyDrawer { + static readonly GUIContent CurveLabel = new GUIContent("Curve"); + public override float GetPropertyHeight( SerializedProperty property, GUIContent label ) { if (property.isExpanded) - return EditorHelper.singleLineHeight * 8; + return EditorHelper.SingleLineHeight * 8; - return EditorHelper.singleLineHeight; + return EditorHelper.SingleLineHeight; } public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { PropertyDrawerHelper.LoadAttributeTooltip( this, label ); - var row0 = position.Row(0); - var row1 = position.Row(1); - var row2 = position.Row(2); - var row3 = position.Row(3); - var row4 = position.Row(4); - var row5 = position.Row(5); - - property.isExpanded = EditorGUI.Foldout(row0, property.isExpanded, label); + var rowFoldout = position.Row(0); + var rowDriverTransform = position.Row(1); + var rowRotationType = position.Row(2); + var rowAxis = position.Row(3); + var rowValues = position.Row(4); + var rowCurve = position.Row(5); + + property.isExpanded = EditorGUI.Foldout(rowFoldout, property.isExpanded, label); if (!property.isExpanded) return; // Draw Rotation Config - var indentLevel = EditorGUI.indentLevel; - using (IndentLevel.Do(indentLevel + 1)) + using (IndentLevel.Do(EditorGUI.indentLevel + 1)) using (LabelWidth.Do(128.0f)) { var driverTransformProperty = property.GetMemberProperty( k => k.driverTransform); @@ -44,12 +41,17 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten var startRotationLabel = new GUIContent("Start Rotation"); var endRotationLabel = new GUIContent("End Rotation"); - EditorGUI.PropertyField(row1, driverTransformProperty); - EditorGUI.PropertyField(row2, rotationTypeProperty); + EditorGUI.PropertyField(rowDriverTransform, driverTransformProperty); + EditorGUI.PropertyField(rowRotationType, rotationTypeProperty); + + rowValues.Left(rowValues.width * 0.5f, out var leftRect).Expand(out var rightRect); + leftRect.Right(26.0f, out var leftButtonRect).Left(-2.0f).Expand(out var outLeftRect); + rightRect.Right(26.0f, out var rightButtonRect).Left(-2.0f).Expand(out var outRightRect); + + if (driverTransformProperty.objectReferenceValue == null) + return; - row4.Left(row4.width * 0.5f, out var leftRect).Expand(out var rightRect); - leftRect.Right(26.0f, out var leftButtonRect).Left(-10.0f).Expand(out var outLeftRect); - rightRect.Right(26.0f, out var rightButtonRect).Left(-10.0f).Expand(out var outRightRect); + var driverTransformEulerAngles = ((Transform)driverTransformProperty.objectReferenceValue).localEulerAngles; if (rotationTypeProperty.enumValueIndex == (int)ShapeKeyRotationConfig.RotationType.SingleAxis) { @@ -58,7 +60,7 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten var endValueProperty = property.GetMemberProperty( k => k.singleAxisEndRotation); // Draw Property - EditorGUI.PropertyField(row3, rotationAxisProperty); + EditorGUI.PropertyField(rowAxis, rotationAxisProperty); EditorGUI.PropertyField(outLeftRect, startValueProperty, startRotationLabel); if (GUI.Button(leftButtonRect, EditorGUIUtility.IconContent("d_Transform Icon"))) @@ -66,16 +68,14 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten switch ((ShapeKeyRotationConfig.RotationAxis)rotationAxisProperty.enumValueIndex) { case ShapeKeyRotationConfig.RotationAxis.X: - startValueProperty.floatValue = GetDriverRotation(driverTransformProperty.objectReferenceValue as Transform).x; + startValueProperty.floatValue = driverTransformEulerAngles.x; break; case ShapeKeyRotationConfig.RotationAxis.Y: - startValueProperty.floatValue = GetDriverRotation(driverTransformProperty.objectReferenceValue as Transform).y; + startValueProperty.floatValue = driverTransformEulerAngles.y; break; case ShapeKeyRotationConfig.RotationAxis.Z: - startValueProperty.floatValue = GetDriverRotation(driverTransformProperty.objectReferenceValue as Transform).z; + startValueProperty.floatValue = driverTransformEulerAngles.z; break; - default: - throw new ArgumentOutOfRangeException(); } } @@ -85,13 +85,13 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten switch (rotationAxisProperty.enumValueIndex) { case 0: - endValueProperty.floatValue = GetDriverRotation(driverTransformProperty.objectReferenceValue as Transform).x; + endValueProperty.floatValue = driverTransformEulerAngles.x; break; case 1: - endValueProperty.floatValue = GetDriverRotation(driverTransformProperty.objectReferenceValue as Transform).y; + endValueProperty.floatValue = driverTransformEulerAngles.y; break; case 2: - endValueProperty.floatValue = GetDriverRotation(driverTransformProperty.objectReferenceValue as Transform).z; + endValueProperty.floatValue = driverTransformEulerAngles.z; break; } } @@ -104,27 +104,21 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten EditorGUI.PropertyField(outLeftRect, startRotationProperty, startRotationLabel); if (GUI.Button(leftButtonRect, EditorGUIUtility.IconContent("d_Transform Icon"))) { - startRotationProperty.vector3Value = GetDriverRotation(driverTransformProperty.objectReferenceValue as Transform); + startRotationProperty.vector3Value = driverTransformEulerAngles; } EditorGUI.PropertyField(outRightRect, endRotationProperty, endRotationLabel); if (GUI.Button(rightButtonRect, EditorGUIUtility.IconContent("d_Transform Icon"))) { - endRotationProperty.vector3Value = GetDriverRotation(driverTransformProperty.objectReferenceValue as Transform); + endRotationProperty.vector3Value = driverTransformEulerAngles; } } - var curveLabel = new GUIContent("Curve"); - row5.height = EditorHelper.singleLineHeight * 3; - EditorGUI.PropertyField(row5, interpolationCurve, curveLabel); + rowCurve.height = EditorHelper.SingleLineHeight * 3; + EditorGUI.PropertyField(rowCurve, interpolationCurve, CurveLabel); } } - - private Vector3 GetDriverRotation(Transform transform) - { - return transform.localEulerAngles; - } } } diff --git a/Assets/Editor/Extensions/RectExtensions.cs b/Assets/Editor/Extensions/RectExtensions.cs index 93fec1f..a732c62 100644 --- a/Assets/Editor/Extensions/RectExtensions.cs +++ b/Assets/Editor/Extensions/RectExtensions.cs @@ -21,8 +21,8 @@ public static class RectExtensions /// public static Rect Row( this Rect position, int rowNumber ) { - position.y += EditorHelper.singleLineHeight * rowNumber; - position.height = EditorHelper.singleLineHeight; + position.y += EditorHelper.SingleLineHeight * rowNumber; + position.height = EditorHelper.SingleLineHeight; return position; } diff --git a/Assets/Editor/Helpers/EditorHelper.cs b/Assets/Editor/Helpers/EditorHelper.cs index c78df67..20931f8 100644 --- a/Assets/Editor/Helpers/EditorHelper.cs +++ b/Assets/Editor/Helpers/EditorHelper.cs @@ -81,7 +81,7 @@ public static GUIStyle Warning } } - public static float singleLineHeight = EditorGUIUtility.singleLineHeight + 2.0f; + public static float SingleLineHeight = EditorGUIUtility.singleLineHeight + 2.0f; private static string searchField = ""; private static Vector2 scroll = Vector2.zero; diff --git a/Assets/Runtime/Animation/ShapeKeyDriver/ShapeKeyDefinition.cs b/Assets/Runtime/Animation/ShapeKeyDriver/ShapeKeyDefinition.cs index 279b866..279eb13 100644 --- a/Assets/Runtime/Animation/ShapeKeyDriver/ShapeKeyDefinition.cs +++ b/Assets/Runtime/Animation/ShapeKeyDriver/ShapeKeyDefinition.cs @@ -1,4 +1,3 @@ -using System; using UnityEngine; [System.Serializable] @@ -135,6 +134,8 @@ public enum DriverType { Rotation, Position, + + // TODO: implement // MecanimParameter, // Callback, } diff --git a/Assets/Runtime/Animation/ShapeKeyDriver/ShapeKeyDriver.cs b/Assets/Runtime/Animation/ShapeKeyDriver/ShapeKeyDriver.cs index d9041c5..ee01bf8 100644 --- a/Assets/Runtime/Animation/ShapeKeyDriver/ShapeKeyDriver.cs +++ b/Assets/Runtime/Animation/ShapeKeyDriver/ShapeKeyDriver.cs @@ -1,21 +1,13 @@ -using System; -using System.Collections; -using System.Collections.Generic; -using System.Diagnostics; -using System.Runtime.CompilerServices; -using BitStrap; using UnityEngine; -using Debug = UnityEngine.Debug; [ExecuteInEditMode] -public class ShapeKeyDriver : MonoBehaviour +public sealed class ShapeKeyDriver : MonoBehaviour { [SerializeField] private SkinnedMeshRenderer skinnedMeshRenderer; [SerializeField] private ShapeKeyRecipe shapeKeyRecipe; - // Update is called once per frame - void Update() + private void Update() { if (shapeKeyRecipe.shapeKeyRotationConfig.driverTransform == null && shapeKeyRecipe.shapeKeyPositionConfig.driverTransform == null) @@ -144,9 +136,9 @@ private void SwitchPositionConfig() var percentageZ = Mathf.InverseLerp(posConf.threeAxisStartPosition.z, posConf.threeAxisEndPosition.z, currentPosition.z); - var percentageMedian = (percentageX + percentageY + percentageZ) / 3; + var percentageMean = (percentageX + percentageY + percentageZ) / 3; - var interpolation = Mathf.Lerp(0, 100, posConf.interpolationCurve.Evaluate(percentageMedian)); + var interpolation = Mathf.Lerp(0, 100, posConf.interpolationCurve.Evaluate(percentageMean)); skinnedMeshRenderer.SetBlendShapeWeight(shapeKeyRecipe.shapeKeyDefinition.index, interpolation); break; From d9bc2464726f010d3c91461e1d8a90f61d26759e Mon Sep 17 00:00:00 2001 From: elamhut Date: Mon, 21 Feb 2022 12:20:59 -0300 Subject: [PATCH 6/7] Some improvements Lessa helped me do. --- .../ShapeKeyDriver/ShapeKeyDrawer.cs | 44 ++++++++++++++----- .../ShapeKeyDriver/ShapeKeyRecipeDrawer.cs | 5 +-- .../ShapeKeyRotationConfigDrawer.cs | 9 ++-- .../ShapeKeyDriver/ShapeKeyDefinition.cs | 14 ++++++ .../ShapeKeyDriver/ShapeKeyDriver.cs | 10 +++-- 5 files changed, 60 insertions(+), 22 deletions(-) diff --git a/Assets/Editor/Animation/ShapeKeyDriver/ShapeKeyDrawer.cs b/Assets/Editor/Animation/ShapeKeyDriver/ShapeKeyDrawer.cs index 81a02f9..e9a6255 100644 --- a/Assets/Editor/Animation/ShapeKeyDriver/ShapeKeyDrawer.cs +++ b/Assets/Editor/Animation/ShapeKeyDriver/ShapeKeyDrawer.cs @@ -7,21 +7,21 @@ namespace BitStrap public class ShapeKeyDrawer : PropertyDrawer { string[] blendShapeNames = null; - + + public override float GetPropertyHeight(SerializedProperty property, GUIContent label) + { + if (FindSkinnedMeshRenderer(property) == null) + return 0.0f; + return base.GetPropertyHeight(property, label); + } + public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { PropertyDrawerHelper.LoadAttributeTooltip( this, label ); - - var behaviour = property.serializedObject.targetObject as MonoBehaviour; - - SkinnedMeshRenderer skinnedMeshRenderer = null; + var nameProperty = property.GetMemberProperty( p => p.name ); - if( behaviour != null ) - { - skinnedMeshRenderer = behaviour.GetComponent(); - } - + var skinnedMeshRenderer = FindSkinnedMeshRenderer(property); if (skinnedMeshRenderer != null) { if (blendShapeNames == null) @@ -46,5 +46,29 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten } } } + + private SkinnedMeshRenderer FindSkinnedMeshRenderer(SerializedProperty property) + { + var behaviour = property.serializedObject.targetObject as MonoBehaviour; + if (behaviour != null) + { + if (fieldInfo.GetAttribute(false) + .TryGet(out var skinnedMeshRendererFieldAttribute)) + { + var skinnedMeshRendererProperty = + property.serializedObject.FindProperty(skinnedMeshRendererFieldAttribute.skinnedMeshRendererFieldName); + if (skinnedMeshRendererProperty != null) + return skinnedMeshRendererProperty.objectReferenceValue as SkinnedMeshRenderer; + } + else + { + var skinnedMeshRendererProperty = property.serializedObject.FindProperty("skinnedMeshRenderer"); + if (skinnedMeshRendererProperty != null) + return skinnedMeshRendererProperty.objectReferenceValue as SkinnedMeshRenderer; + } + } + + return null; + } } } diff --git a/Assets/Editor/Animation/ShapeKeyDriver/ShapeKeyRecipeDrawer.cs b/Assets/Editor/Animation/ShapeKeyDriver/ShapeKeyRecipeDrawer.cs index 94443dc..d894d14 100644 --- a/Assets/Editor/Animation/ShapeKeyDriver/ShapeKeyRecipeDrawer.cs +++ b/Assets/Editor/Animation/ShapeKeyDriver/ShapeKeyRecipeDrawer.cs @@ -8,10 +8,7 @@ public class ShapeKeyRecipeDrawer : PropertyDrawer { public override float GetPropertyHeight( SerializedProperty property, GUIContent label ) { - if (property.isExpanded) - return EditorHelper.SingleLineHeight * 10; - - return EditorHelper.SingleLineHeight; + return EditorHelper.SingleLineHeight * 10; } public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) diff --git a/Assets/Editor/Animation/ShapeKeyDriver/ShapeKeyRotationConfigDrawer.cs b/Assets/Editor/Animation/ShapeKeyDriver/ShapeKeyRotationConfigDrawer.cs index b03d997..ed2446a 100644 --- a/Assets/Editor/Animation/ShapeKeyDriver/ShapeKeyRotationConfigDrawer.cs +++ b/Assets/Editor/Animation/ShapeKeyDriver/ShapeKeyRotationConfigDrawer.cs @@ -7,6 +7,7 @@ namespace BitStrap public class ShapeKeyRotationConfigDrawer : PropertyDrawer { static readonly GUIContent CurveLabel = new GUIContent("Curve"); + private Vector3 driverTransformEulerAngles; public override float GetPropertyHeight( SerializedProperty property, GUIContent label ) { @@ -48,11 +49,9 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten leftRect.Right(26.0f, out var leftButtonRect).Left(-2.0f).Expand(out var outLeftRect); rightRect.Right(26.0f, out var rightButtonRect).Left(-2.0f).Expand(out var outRightRect); - if (driverTransformProperty.objectReferenceValue == null) - return; - - var driverTransformEulerAngles = ((Transform)driverTransformProperty.objectReferenceValue).localEulerAngles; - + if (driverTransformProperty.objectReferenceValue != null) + driverTransformEulerAngles = ((Transform)driverTransformProperty.objectReferenceValue).localEulerAngles; + if (rotationTypeProperty.enumValueIndex == (int)ShapeKeyRotationConfig.RotationType.SingleAxis) { var rotationAxisProperty = property.GetMemberProperty( k => k.rotationAxis); diff --git a/Assets/Runtime/Animation/ShapeKeyDriver/ShapeKeyDefinition.cs b/Assets/Runtime/Animation/ShapeKeyDriver/ShapeKeyDefinition.cs index 279eb13..2e61947 100644 --- a/Assets/Runtime/Animation/ShapeKeyDriver/ShapeKeyDefinition.cs +++ b/Assets/Runtime/Animation/ShapeKeyDriver/ShapeKeyDefinition.cs @@ -7,6 +7,20 @@ public struct ShapeKeyDefinition public int index; } +/// +/// Lets you use ShapeKeyDefinition even when its MonoBehaviour does not have a sibling SkinnedMeshRenderer component. +/// +[System.AttributeUsage( System.AttributeTargets.Field, AllowMultiple = false, Inherited = true )] +public sealed class SkinnedMeshRendererFieldAttribute : System.Attribute +{ + public string skinnedMeshRendererFieldName; + + public SkinnedMeshRendererFieldAttribute( string skinnedMeshRendererFieldName ) + { + this.skinnedMeshRendererFieldName = skinnedMeshRendererFieldName; + } +} + [System.Serializable] public struct ShapeKeyRotationConfig { diff --git a/Assets/Runtime/Animation/ShapeKeyDriver/ShapeKeyDriver.cs b/Assets/Runtime/Animation/ShapeKeyDriver/ShapeKeyDriver.cs index ee01bf8..339f868 100644 --- a/Assets/Runtime/Animation/ShapeKeyDriver/ShapeKeyDriver.cs +++ b/Assets/Runtime/Animation/ShapeKeyDriver/ShapeKeyDriver.cs @@ -4,21 +4,25 @@ [ExecuteInEditMode] public sealed class ShapeKeyDriver : MonoBehaviour { - [SerializeField] private SkinnedMeshRenderer skinnedMeshRenderer; + [SerializeField] public SkinnedMeshRenderer skinnedMeshRenderer; [SerializeField] private ShapeKeyRecipe shapeKeyRecipe; private void Update() { - if (shapeKeyRecipe.shapeKeyRotationConfig.driverTransform == null && - shapeKeyRecipe.shapeKeyPositionConfig.driverTransform == null) + if (skinnedMeshRenderer == null) return; switch(shapeKeyRecipe.driverType) { case ShapeKeyRecipe.DriverType.Rotation: + Debug.Log("Cheguei"); + if (shapeKeyRecipe.shapeKeyRotationConfig.driverTransform == null) + return; SwitchRotationConfig(); break; case ShapeKeyRecipe.DriverType.Position: + if (shapeKeyRecipe.shapeKeyPositionConfig.driverTransform == null) + return; SwitchPositionConfig(); break; } From 3dad5a6cbe20e0179cdc936aff09c95fdb6ca009 Mon Sep 17 00:00:00 2001 From: elamhut Date: Tue, 22 Feb 2022 09:45:03 -0300 Subject: [PATCH 7/7] [ExecuteInEditMode] -> [ExecuteAlways] --- Assets/Runtime/Animation/ShapeKeyDriver/ShapeKeyDriver.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Assets/Runtime/Animation/ShapeKeyDriver/ShapeKeyDriver.cs b/Assets/Runtime/Animation/ShapeKeyDriver/ShapeKeyDriver.cs index 339f868..18dc181 100644 --- a/Assets/Runtime/Animation/ShapeKeyDriver/ShapeKeyDriver.cs +++ b/Assets/Runtime/Animation/ShapeKeyDriver/ShapeKeyDriver.cs @@ -1,7 +1,7 @@ using UnityEngine; -[ExecuteInEditMode] +[ExecuteAlways] public sealed class ShapeKeyDriver : MonoBehaviour { [SerializeField] public SkinnedMeshRenderer skinnedMeshRenderer;