Last active
May 2, 2022 11:25
-
-
Save PiMaker/71b06fc34bcc425dfa29ba732813df9f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Available under the terms of the CC0 license | |
// No copyright intended, this file is in the public domain | |
// Originally written by: _pi_ | |
// | |
// Creates inventory enable/disable animation files for use with Silent's Cel Shading Shader: | |
// https://gitlab.com/s-ilent/SCSS | |
#if UNITY_EDITOR | |
using UnityEngine; | |
using UnityEditor; | |
using UnityEditor.Animations; | |
using System.Collections.Generic; | |
namespace Pi { | |
public class Create : MonoBehaviour | |
{ | |
public AnimatorController FX; | |
public VRC.SDK3.Avatars.ScriptableObjects.VRCExpressionParameters Parameters; | |
private static readonly string[] names = {"off", "on"}; | |
public void CreateAnims() { | |
Debug.Log("Creating SCSS animations for: " + this.name); | |
var newAnimList = new List<AnimationClip>(); | |
for (int i = 1; i <= 16; i++) { | |
for (int j = 0; j < 2; j++) { | |
var clip = new AnimationClip(); | |
clip.frameRate = 90; | |
var curve = new AnimationCurve(new[] { | |
new Keyframe(0.0f, j), | |
new Keyframe(1.0f/90.0f, j) | |
}); | |
clip.SetCurve(this.name, typeof(SkinnedMeshRenderer), $"material._InventoryItem{i:00}Animated", curve); | |
newAnimList.Add(clip); | |
AssetDatabase.CreateAsset(clip, $"Assets\\_SCSS_Inventory_Anim\\{this.name}_slot{i}_{names[j]}.anim"); | |
} | |
} | |
if (FX != null && Parameters != null) { | |
var layername = $"scss_inv_{this.name}_slot"; | |
for (int i = 0; i < FX.layers.Length; i++) { | |
var layer = FX.layers[i]; | |
if (layer.name.StartsWith(layername)) { | |
FX.RemoveLayer(i); | |
i--; | |
} | |
} | |
for (int i = 0; i < FX.parameters.Length; i++) { | |
var parm = FX.parameters[i]; | |
if (parm.name.StartsWith(layername)) { | |
FX.RemoveParameter(i); | |
i--; | |
} | |
} | |
for (int i = 1; i <= 16; i++) { | |
var parm = new AnimatorControllerParameter() { | |
name = $"{layername}{i}", | |
type = AnimatorControllerParameterType.Bool, | |
defaultBool = false | |
}; | |
FX.AddParameter(parm); | |
if (Parameters.FindParameter(parm.name) == null) { | |
var ptmp = new List<VRC.SDK3.Avatars.ScriptableObjects.VRCExpressionParameters.Parameter>(Parameters.parameters); | |
ptmp.Add(new VRC.SDK3.Avatars.ScriptableObjects.VRCExpressionParameters.Parameter() { | |
name = parm.name, | |
valueType = VRC.SDK3.Avatars.ScriptableObjects.VRCExpressionParameters.ValueType.Bool, | |
saved = true, | |
defaultValue = 1.0f | |
}); | |
Parameters.parameters = ptmp.ToArray(); | |
} | |
var layer = new AnimatorControllerLayer() { | |
defaultWeight = 1.0f, | |
name = $"{layername}{i}" | |
}; | |
var offState = new AnimatorState() { | |
motion = (Motion) newAnimList[(i-1)*2], | |
name = "Off", | |
writeDefaultValues = false | |
}; | |
var onState = new AnimatorState() { | |
motion = (Motion) newAnimList[(i-1)*2+1], | |
name = "On", | |
writeDefaultValues = false | |
}; | |
var transOn = new AnimatorStateTransition() { | |
hasExitTime = false, | |
canTransitionToSelf = false, | |
destinationState = onState, | |
duration = 0.01f, | |
hasFixedDuration = true, | |
name = "turn_on", | |
conditions = new[] { | |
new AnimatorCondition() { | |
mode = AnimatorConditionMode.If, | |
parameter = parm.name, | |
threshold = 1.0f | |
} | |
} | |
}; | |
var transOff = new AnimatorStateTransition() { | |
hasExitTime = false, | |
canTransitionToSelf = false, | |
destinationState = offState, | |
duration = 0.01f, | |
hasFixedDuration = true, | |
name = "turn_off", | |
conditions = new[] { | |
new AnimatorCondition() { | |
mode = AnimatorConditionMode.IfNot, | |
parameter = parm.name, | |
} | |
} | |
}; | |
onState.AddTransition(transOff); | |
offState.AddTransition(transOn); | |
layer.stateMachine = new AnimatorStateMachine(); | |
layer.stateMachine.AddState(onState, new Vector3(240, 60)); | |
layer.stateMachine.AddState(offState, new Vector3(240, 140)); | |
layer.stateMachine.defaultState = onState; | |
FX.AddLayer(layer); | |
} | |
} | |
AssetDatabase.SaveAssets(); | |
AssetDatabase.Refresh(); | |
} | |
} | |
[CustomEditor(typeof(Create))] | |
public class RunButton : Editor | |
{ | |
public override void OnInspectorGUI() | |
{ | |
DrawDefaultInspector(); | |
if (GUILayout.Button("Create Animations")) | |
{ | |
((Create)target).CreateAnims(); | |
} | |
} | |
} | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment