diff options
| author | yum <yum.food.vr@gmail.com> | 2024-04-22 16:02:38 -0700 |
|---|---|---|
| committer | yum <yum.food.vr@gmail.com> | 2024-04-22 16:02:38 -0700 |
| commit | dcb15c0d7425f2b3ede070cd97ac8f1bd8386df1 (patch) | |
| tree | e17e5b620e0784226b6d24acce43c8bbcee22f1f /Editor | |
Initial commit
Code bomb. Put tooner in version control.
Diffstat (limited to 'Editor')
| -rw-r--r-- | Editor/tooner.cs | 745 |
1 files changed, 745 insertions, 0 deletions
diff --git a/Editor/tooner.cs b/Editor/tooner.cs new file mode 100644 index 0000000..44306be --- /dev/null +++ b/Editor/tooner.cs @@ -0,0 +1,745 @@ +using System; +using UnityEngine; +using UnityEngine.Rendering; +using UnityEditor; + +public class ToonerGUI : ShaderGUI { + Material target; + MaterialEditor editor; + MaterialProperty[] properties; + + public override void OnGUI( + MaterialEditor editor, + MaterialProperty[] properties) { + this.target = editor.target as Material; + this.editor = editor; + this.properties = properties; + DoMain(); + } + + static GUIContent staticLabel = new GUIContent(); + + static GUIContent MakeLabel(string prop, string tooltip = null) { + staticLabel.text = prop; + staticLabel.tooltip = tooltip; + return staticLabel; + } + + static GUIContent MakeLabel(MaterialProperty prop, string tooltip = null) { + staticLabel.text = prop.displayName; + staticLabel.tooltip = tooltip; + return staticLabel; + } + + void RecordAction (string label) { + editor.RegisterPropertyChangeUndo(label); + } + + MaterialProperty FindProperty(string label) { + return FindProperty(label, properties); + } + + void SetKeyword(string keyword, bool state) { + if (state) { + target.EnableKeyword(keyword); + } else { + target.DisableKeyword(keyword); + } + } + + void DoBaseColor() { + MaterialProperty bc = FindProperty("_BaseColor"); + MaterialProperty bct = FindProperty("_BaseColorTex"); + editor.TexturePropertySingleLine( + MakeLabel(bct, "Base color (RGBA)"), + bct, + bc); + SetKeyword("_BASECOLOR_MAP", bct.textureValue); + } + + void DoNormal() { + MaterialProperty bct = FindProperty("_NormalTex"); + editor.TexturePropertySingleLine( + MakeLabel(bct, "Normal"), + bct, + FindProperty("_Tex_NormalStr")); + SetKeyword("_NORMAL_MAP", bct.textureValue); + } + + void DoMetallic() { + MaterialProperty bc = FindProperty("_Metallic"); + MaterialProperty bct = FindProperty("_MetallicTex"); + editor.TexturePropertySingleLine( + MakeLabel(bct, "Metallic (RGBA)"), + bct, + bc); + SetKeyword("_METALLIC_MAP", bct.textureValue); + } + + void DoRoughness() { + MaterialProperty bc = FindProperty("_Roughness"); + MaterialProperty bct = FindProperty("_RoughnessTex"); + editor.TexturePropertySingleLine( + MakeLabel(bct, "Roughness (RGBA)"), + bct, + bc); + SetKeyword("_ROUGHNESS_MAP", bct.textureValue); + } + + void DoPBROverlay() { + MaterialProperty bc = FindProperty("_PBR_Overlay_Enable"); + bool enabled = bc.floatValue > 1E-6; + EditorGUI.BeginChangeCheck(); + enabled = EditorGUILayout.Toggle("Enable", enabled); + EditorGUI.EndChangeCheck(); + bc.floatValue = enabled ? 1.0f : 0.0f; + SetKeyword("_PBR_OVERLAY", enabled); + + if (enabled) { + bc = FindProperty("_PBR_Overlay_BaseColor"); + MaterialProperty bct = FindProperty("_PBR_Overlay_BaseColorTex"); + editor.TexturePropertySingleLine( + MakeLabel(bct, "Base color (RGBA)"), + bct, + bc); + SetKeyword("_PBR_OVERLAY_BASECOLOR_MAP", bct.textureValue); + + bct = FindProperty("_PBR_Overlay_NormalTex"); + editor.TexturePropertySingleLine( + MakeLabel(bct, "Normal"), + bct, + FindProperty("_PBR_Overlay_Tex_NormalStr")); + SetKeyword("_PBR_OVERLAY_NORMAL_MAP", bct.textureValue); + + bc = FindProperty("_PBR_Overlay_Metallic"); + bct = FindProperty("_PBR_Overlay_MetallicTex"); + editor.TexturePropertySingleLine( + MakeLabel(bct, "Metallic (RGBA)"), + bct, + bc); + SetKeyword("_PBR_OVERLAY_METALLIC_MAP", bct.textureValue); + + bc = FindProperty("_PBR_Overlay_Roughness"); + bct = FindProperty("_PBR_Overlay_RoughnessTex"); + editor.TexturePropertySingleLine( + MakeLabel(bct, "Roughness (RGBA)"), + bct, + bc); + SetKeyword("_PBR_OVERLAY_ROUGHNESS_MAP", bct.textureValue); + } + } + + void DoCubemap() { + MaterialProperty bc = FindProperty("_Cubemap"); + editor.TexturePropertySingleLine( + MakeLabel(bc, "Cubemap"), + bc); + SetKeyword("_CUBEMAP", bc.textureValue); + } + + void DoBrightness() { + MaterialProperty bc; + + bc = FindProperty("_Min_Brightness"); + editor.RangeProperty( + bc, + "Min brightness"); + + bc = FindProperty("_Max_Brightness"); + editor.RangeProperty( + bc, + "Max brightness"); + } + + void DoEmission() { + MaterialProperty bc = FindProperty("_EmissionTex"); + MaterialProperty bct = FindProperty("_EmissionStrength"); + editor.TexturePropertySingleLine( + MakeLabel(bct, "Emission map"), + bc, + bct); + SetKeyword("_EMISSION", bc.textureValue); + } + + enum MatcapMode { + Add, + Multiply, + Replace, + Subtract, + Min, + Max, + } + + void DoMatcap() { + for (int i = 0; i < 2; i++) { + GUILayout.Label($"Matcap {i}", EditorStyles.boldLabel); + EditorGUI.indentLevel += 1; + + MaterialProperty bc; + + bc = FindProperty($"_Matcap{i}"); + editor.TexturePropertySingleLine( + MakeLabel(bc, $"Matcap {i}"), + bc); + SetKeyword($"_MATCAP{i}", bc.textureValue); + + if (!bc.textureValue) { + EditorGUI.indentLevel -= 1; + continue; + } + + bc = FindProperty($"_Matcap{i}_Mask"); + editor.TexturePropertySingleLine( + MakeLabel(bc, "Mask"), + bc); + SetKeyword($"_MATCAP{i}_MASK", bc.textureValue); + + if (bc.textureValue) { + bc = FindProperty($"_Matcap{i}_Mask_Invert"); + bool enabled = bc.floatValue > 1E-6; + EditorGUI.BeginChangeCheck(); + enabled = EditorGUILayout.Toggle("Invert mask", enabled); + EditorGUI.EndChangeCheck(); + bc.floatValue = enabled ? 1.0f : 0.0f; + } + + EditorGUI.BeginChangeCheck(); + bc = FindProperty($"_Matcap{i}Mode"); + MatcapMode mode = (MatcapMode) Math.Round(bc.floatValue); + mode = (MatcapMode) EditorGUILayout.EnumPopup( + MakeLabel("Matcap mode"), mode); + if (EditorGUI.EndChangeCheck()) { + RecordAction($"Matcap {i}"); + foreach (Material m in editor.targets) { + m.SetFloat($"_Matcap{i}Mode", (int) mode); + } + } + + bc = FindProperty($"_Matcap{i}Str"); + editor.FloatProperty( + bc, + "Matcap strength"); + EditorGUI.indentLevel -= 1; + } + } + + void DoRimLighting() { + GUILayout.Label($"Rim lighting", EditorStyles.boldLabel); + EditorGUI.indentLevel += 1; + + MaterialProperty bc; + + bc = FindProperty("_Rim_Lighting_Enabled"); + bool enabled = bc.floatValue > 1E-6; + EditorGUI.BeginChangeCheck(); + enabled = EditorGUILayout.Toggle("Enable", enabled); + EditorGUI.EndChangeCheck(); + bc.floatValue = enabled ? 1.0f : 0.0f; + SetKeyword($"_RIM_LIGHTING", enabled); + + if (!enabled) { + return; + } + + bc = FindProperty("_Rim_Lighting_Color"); + editor.ColorProperty(bc, "Color (RGB)"); + + bc = FindProperty($"_Rim_Lighting_Mask"); + editor.TexturePropertySingleLine( + MakeLabel(bc, "Mask"), + bc); + SetKeyword($"_RIM_LIGHTING_MASK", bc.textureValue); + + if (bc.textureValue) { + bc = FindProperty($"_Rim_Lighting_Mask_Invert"); + enabled = bc.floatValue > 1E-6; + EditorGUI.BeginChangeCheck(); + enabled = EditorGUILayout.Toggle("Invert mask", enabled); + EditorGUI.EndChangeCheck(); + bc.floatValue = enabled ? 1.0f : 0.0f; + } + + EditorGUI.BeginChangeCheck(); + bc = FindProperty($"_Rim_Lighting_Mode"); + MatcapMode mode = (MatcapMode) Math.Round(bc.floatValue); + mode = (MatcapMode) EditorGUILayout.EnumPopup( + MakeLabel("Rim lighting mode"), mode); + if (EditorGUI.EndChangeCheck()) { + RecordAction("Rim lighting mode"); + foreach (Material m in editor.targets) { + m.SetFloat($"_Rim_Lighting_Mode", (int) mode); + } + } + + bc = FindProperty($"_Rim_Lighting_Center"); + editor.FloatProperty( + bc, + "Center"); + + bc = FindProperty($"_Rim_Lighting_Power"); + editor.FloatProperty( + bc, + "Power"); + + bc = FindProperty($"_Rim_Lighting_Strength"); + editor.FloatProperty( + bc, + "Strength"); + + EditorGUI.indentLevel -= 1; + } + + void DoShadingMode() { + MaterialProperty bc; + + /* + bc = FindProperty("_Shading_Mode"); + editor.RangeProperty( + bc, + "Shading mode (0=realistic,1=flat)"); + SetKeyword("_SHADING_MODE_FLAT", ((int) Math.Round(bc.floatValue, 0)) == 1); + */ + + bc = FindProperty("_Flatten_Mesh_Normals"); + bool enabled = bc.floatValue > 1E-6; + EditorGUI.BeginChangeCheck(); + enabled = EditorGUILayout.Toggle("Flatten normals", enabled); + EditorGUI.EndChangeCheck(); + bc.floatValue = enabled ? 1.0f : 0.0f; + + bc = FindProperty("_Confabulate_Normals"); + enabled = bc.floatValue > 1E-6; + EditorGUI.BeginChangeCheck(); + enabled = EditorGUILayout.Toggle("Confabulate normals", enabled); + EditorGUI.EndChangeCheck(); + bc.floatValue = enabled ? 1.0f : 0.0f; + } + + void DoOKLAB() { + MaterialProperty bc; + + bc = FindProperty("_OKLAB_Enabled"); + bool enabled = bc.floatValue > 1E-6; + EditorGUI.BeginChangeCheck(); + enabled = EditorGUILayout.Toggle("Enable", enabled); + EditorGUI.EndChangeCheck(); + bc.floatValue = enabled ? 1.0f : 0.0f; + + SetKeyword("_OKLAB", enabled); + + if (enabled) { + bc = FindProperty("_OKLAB_Mask"); + editor.TexturePropertySingleLine( + MakeLabel(bc, "Mask"), + bc); + + bc = FindProperty("_OKLAB_Lightness_Shift"); + editor.RangeProperty( + bc, + "Lightness shift"); + bc = FindProperty("_OKLAB_Chroma_Shift"); + editor.RangeProperty( + bc, + "Chroma shift"); + bc = FindProperty("_OKLAB_Hue_Shift"); + editor.RangeProperty( + bc, + "Hue shift"); + } + } + + void DoClones() { + MaterialProperty bc; + + bc = FindProperty("_Clones_Enabled"); + bool enabled = bc.floatValue > 1E-6; + EditorGUI.BeginChangeCheck(); + enabled = EditorGUILayout.Toggle("Enable", enabled); + EditorGUI.EndChangeCheck(); + bc.floatValue = enabled ? 1.0f : 0.0f; + + SetKeyword("_CLONES", enabled); + + if (enabled) { + bc = FindProperty("_Clones_Count"); + editor.RangeProperty( + bc, + "Number of clones"); + bc = FindProperty("_Clones_dx"); + editor.RangeProperty( + bc, + "x offset"); + } + } + + void DoOutlines() { + MaterialProperty bc; + + bc = FindProperty("_Outline_Width"); + editor.RangeProperty( + bc, + "Outline width"); + SetKeyword("_OUTLINES", bc.floatValue > 1E-6); + + if (bc.floatValue > 1E-6) { + bc = FindProperty("_Outline_Color"); + editor.ColorProperty( + bc, + "Outline color (RGBA)"); + + bc = FindProperty("_Outline_Emission_Strength"); + editor.RangeProperty( + bc, + "Outline emission strength"); + + bc = FindProperty("_Outline_Mask"); + editor.TexturePropertySingleLine( + MakeLabel(bc, "Outline mask"), + bc); + + bc = FindProperty("_Outline_Mask_Invert"); + bool inverted = bc.floatValue > 1E-6; + EditorGUI.BeginChangeCheck(); + inverted = EditorGUILayout.Toggle("Invert mask", inverted); + EditorGUI.EndChangeCheck(); + bc.floatValue = inverted ? 1.0f : 0.0f; + } + } + + void DoGlitter() { + MaterialProperty bc = FindProperty("_Glitter_Enabled"); + bool enabled = bc.floatValue > 1E-6; + + EditorGUI.BeginChangeCheck(); + enabled = EditorGUILayout.Toggle("Enable", enabled); + EditorGUI.EndChangeCheck(); + SetKeyword("_GLITTER", enabled); + bc.floatValue = enabled ? 1.0f : 0.0f; + + if (enabled) { + bc = FindProperty("_Glitter_Mask"); + editor.TexturePropertySingleLine( + MakeLabel(bc, "Glitter mask (RGBA)"), + bc); + + bc = FindProperty("_Glitter_Density"); + editor.FloatProperty( + bc, + "Glitter density"); + + bc = FindProperty("_Glitter_Amount"); + editor.FloatProperty( + bc, + "Glitter amount"); + + bc = FindProperty("_Glitter_Speed"); + editor.FloatProperty( + bc, + "Glitter speed"); + + bc = FindProperty("_Glitter_Brightness"); + editor.FloatProperty( + bc, + "Glitter brightness"); + + bc = FindProperty("_Glitter_Angle"); + editor.FloatProperty( + bc, + "Glitter angle"); + } + } + + void DoExplosion() { + MaterialProperty bc = FindProperty("_Explode_Toggle"); + bool enabled = bc.floatValue > 1E-6; + + EditorGUI.BeginChangeCheck(); + enabled = EditorGUILayout.Toggle("Enable", enabled); + EditorGUI.EndChangeCheck(); + SetKeyword("_EXPLODE", enabled); + bc.floatValue = enabled ? 1.0f : 0.0f; + + bc = FindProperty("_Explode_Phase"); + if (enabled) { + editor.RangeProperty( + bc, + "Explosion phase"); + } else { + bc.floatValue = 0.0f; + } + bc = FindProperty("_OutlinesCull"); + bc.floatValue = (float) UnityEngine.Rendering.CullMode.Front; + + /* + if (enabled) { + bc = FindProperty("_Explode_Phase"); + editor.RangeProperty( + bc, + "Explosion phase"); + if (bc.floatValue > 1E-3) { + bc = FindProperty("_Cull"); + bc.floatValue = (float) UnityEngine.Rendering.CullMode.Back; + } else { + bc = FindProperty("_Cull"); + bc.floatValue = (float) UnityEngine.Rendering.CullMode.Front; + } + } else { + bc = FindProperty("_Explode_Phase"); + bc.floatValue = 0.0f; + bc = FindProperty("_Cull"); + bc.floatValue = (float) UnityEngine.Rendering.CullMode.Front; + } + */ + } + + void DoGeoScroll() { + MaterialProperty bc = FindProperty("_Scroll_Toggle"); + bool enabled = bc.floatValue > 1E-6; + + EditorGUI.BeginChangeCheck(); + enabled = EditorGUILayout.Toggle("Enable", enabled); + EditorGUI.EndChangeCheck(); + SetKeyword("_SCROLL", enabled); + bc.floatValue = enabled ? 1.0f : 0.0f; + + if (enabled) { + bc = FindProperty("_Scroll_Top"); + editor.RangeProperty( + bc, + "Scroll top"); + + bc = FindProperty("_Scroll_Bottom"); + editor.RangeProperty( + bc, + "Scroll bottom"); + + bc = FindProperty("_Scroll_Width"); + editor.RangeProperty( + bc, + "Scroll width"); + + bc = FindProperty("_Scroll_Strength"); + editor.RangeProperty( + bc, + "Scroll strength"); + + bc = FindProperty("_Scroll_Speed"); + editor.RangeProperty( + bc, + "Scroll speed"); + } + } + + void DoUVScroll() { + MaterialProperty bc = FindProperty("_UVScroll_Enabled"); + bool enabled = bc.floatValue > 1E-6; + + EditorGUI.BeginChangeCheck(); + enabled = EditorGUILayout.Toggle("Enable", enabled); + EditorGUI.EndChangeCheck(); + SetKeyword("_UVSCROLL", enabled); + bc.floatValue = enabled ? 1.0f : 0.0f; + + if (enabled) { + bc = FindProperty("_UVScroll_Mask"); + editor.TexturePropertySingleLine( + MakeLabel(bc, "Mask"), + bc); + + bc = FindProperty("_UVScroll_U_Speed"); + editor.FloatProperty( + bc, + "U speed"); + + bc = FindProperty("_UVScroll_V_Speed"); + editor.FloatProperty( + bc, + "V speed"); + + bc = FindProperty("_UVScroll_Alpha"); + editor.TexturePropertySingleLine( + MakeLabel(bc, "Alpha"), + bc); + } + } + + void DoChainTessellation() { + MaterialProperty bc = FindProperty("_Enable_Chain_Tessellation"); + bool enabled = bc.floatValue > 1E-6; + + EditorGUI.BeginChangeCheck(); + enabled = EditorGUILayout.Toggle("Enable", enabled); + EditorGUI.EndChangeCheck(); + SetKeyword("_CHAIN_TESSELLATION", enabled); + bc.floatValue = enabled ? 1.0f : 0.0f; + + if (enabled) { + bc = FindProperty("_Chain_Tess_Factor"); + editor.RangeProperty( + bc, + "Tessellation factor"); + } + } + + enum RenderingMode { + Opaque, + Cutout, + Fade + } + + void DoRendering() { + RenderingMode mode = RenderingMode.Opaque; + if (target.IsKeywordEnabled("_RENDERING_CUTOUT")) { + mode = RenderingMode.Cutout; + } else if (target.IsKeywordEnabled("_RENDERING_FADE")) { + mode = RenderingMode.Fade; + } + + EditorGUI.BeginChangeCheck(); + mode = (RenderingMode) EditorGUILayout.EnumPopup( + MakeLabel("Rendering Mode"), mode); + BlendMode src_blend = BlendMode.One; + BlendMode dst_blend = BlendMode.Zero; + bool zwrite = false; + + if (EditorGUI.EndChangeCheck()) { + RecordAction("Rendering Mode"); + SetKeyword("_RENDERING_CUTOUT", mode == RenderingMode.Cutout); + SetKeyword("_RENDERING_FADE", mode == RenderingMode.Fade); + + RenderQueue queue = RenderQueue.Geometry; + string render_type = ""; + switch (mode) { + case RenderingMode.Opaque: + queue = RenderQueue.Geometry; + render_type = ""; + src_blend = BlendMode.One; + dst_blend = BlendMode.Zero; + zwrite = true; + break; + case RenderingMode.Cutout: + queue = RenderQueue.AlphaTest; + render_type = "TransparentCutout"; + src_blend = BlendMode.One; + dst_blend = BlendMode.Zero; + zwrite = true; + break; + case RenderingMode.Fade: + queue = RenderQueue.Transparent; + render_type = "Transparent"; + src_blend = BlendMode.SrcAlpha; + dst_blend = BlendMode.OneMinusSrcAlpha; + zwrite = false; + break; + } + foreach (Material m in editor.targets) { + m.renderQueue = (int) queue; + m.SetOverrideTag("RenderType", render_type); + m.SetInt("_SrcBlend", (int) src_blend); + m.SetInt("_DstBlend", (int) dst_blend); + m.SetInt("_ZWrite", zwrite ? 1 : 0); + } + } + + MaterialProperty bc; + if (mode == RenderingMode.Cutout) { + bc = FindProperty("_Alpha_Cutoff"); + editor.ShaderProperty(bc, MakeLabel(bc)); + } + + bc = FindProperty("_Cull"); + UnityEngine.Rendering.CullMode cull_mode = (UnityEngine.Rendering.CullMode) bc.floatValue; + EditorGUI.BeginChangeCheck(); + cull_mode = (UnityEngine.Rendering.CullMode) EditorGUILayout.EnumPopup( + MakeLabel("Culling mode"), cull_mode); + if (EditorGUI.EndChangeCheck()) { + RecordAction("Culling mode"); + bc.floatValue = (float) cull_mode; + } + } + + void DoMain() { + SetKeyword("VERTEXLIGHT_ON", false); + + GUILayout.Label("PBR", EditorStyles.boldLabel); + EditorGUI.indentLevel += 1; + DoBaseColor(); + DoNormal(); + DoMetallic(); + DoRoughness(); + EditorGUI.indentLevel -= 1; + + GUILayout.Label("PBR overlay", EditorStyles.boldLabel); + EditorGUI.indentLevel += 1; + DoPBROverlay(); + EditorGUI.indentLevel -= 1; + + GUILayout.Label("Lighting", EditorStyles.boldLabel); + EditorGUI.indentLevel += 1; + DoCubemap(); + DoBrightness(); + EditorGUI.indentLevel -= 1; + + GUILayout.Label("Emission", EditorStyles.boldLabel); + EditorGUI.indentLevel += 1; + DoEmission(); + EditorGUI.indentLevel -= 1; + + GUILayout.Label("Shading", EditorStyles.boldLabel); + EditorGUI.indentLevel += 1; + DoShadingMode(); + EditorGUI.indentLevel -= 1; + + DoMatcap(); + DoRimLighting(); + + GUILayout.Label("Outlines", EditorStyles.boldLabel); + EditorGUI.indentLevel += 1; + DoOutlines(); + EditorGUI.indentLevel -= 1; + + GUILayout.Label("Glitter", EditorStyles.boldLabel); + EditorGUI.indentLevel += 1; + DoGlitter(); + EditorGUI.indentLevel -= 1; + + GUILayout.Label("Explosion", EditorStyles.boldLabel); + EditorGUI.indentLevel += 1; + DoExplosion(); + EditorGUI.indentLevel -= 1; + + GUILayout.Label("Geometry scroll", EditorStyles.boldLabel); + EditorGUI.indentLevel += 1; + DoGeoScroll(); + EditorGUI.indentLevel -= 1; + + GUILayout.Label("UV scroll", EditorStyles.boldLabel); + EditorGUI.indentLevel += 1; + DoUVScroll(); + EditorGUI.indentLevel -= 1; + + /* + GUILayout.Label("Chain tessellation", EditorStyles.boldLabel); + EditorGUI.indentLevel += 1; + DoChainTessellation(); + EditorGUI.indentLevel -= 1; + */ + + GUILayout.Label("Hue shift", EditorStyles.boldLabel); + EditorGUI.indentLevel += 1; + DoOKLAB(); + EditorGUI.indentLevel -= 1; + + GUILayout.Label("Clones", EditorStyles.boldLabel); + EditorGUI.indentLevel += 1; + DoClones(); + EditorGUI.indentLevel -= 1; + + GUILayout.Label("Rendering", EditorStyles.boldLabel); + EditorGUI.indentLevel += 1; + DoRendering(); + EditorGUI.indentLevel -= 1; + } +} + + |
