From 9863720a4669c2c25dc7b00f3e8e3c70c5f9937d Mon Sep 17 00:00:00 2001 From: thojmr <72571672+thojmr@users.noreply.github.com> Date: Fri, 6 May 2022 13:14:01 -0400 Subject: [PATCH] [ShaderSwapper] Add support for Vanilla+ shaders with tessellation (#143) --- src/ShaderSwapper.Core/ShaderSwapper.cs | 51 ++++++++++++++++++++++++- 1 file changed, 49 insertions(+), 2 deletions(-) diff --git a/src/ShaderSwapper.Core/ShaderSwapper.cs b/src/ShaderSwapper.Core/ShaderSwapper.cs index f7ac331f..6a404064 100644 --- a/src/ShaderSwapper.Core/ShaderSwapper.cs +++ b/src/ShaderSwapper.Core/ShaderSwapper.cs @@ -22,6 +22,7 @@ public class ShaderSwapper : BaseUnityPlugin internal static new ManualLogSource Logger; internal static ConfigEntry SwapShadersHotkey { get; private set; } + internal static ConfigEntry TesselationSlider { get; private set; } private readonly Dictionary VanillaPlusShaders = new Dictionary { @@ -44,10 +45,36 @@ public class ShaderSwapper : BaseUnityPlugin {"Koikano/main_clothes_item", "xukmi/MainItemPlus" }, }; + private readonly Dictionary VanillaPlusTesselationShaders = new Dictionary + { + {"Shader Forge/main_skin", "xukmi/SkinPlusTess" }, + {"Koikano/main_skin", "xukmi/SkinPlusTess" }, + {"Shader Forge/main_hair", "xukmi/HairPlus" }, + {"Koikano/hair_main_sun", "xukmi/HairPlus" }, + {"Shader Forge/main_hair_front", "xukmi/HairFrontPlus" }, + {"Koikano/hair_main_sun_front", "xukmi/HairFrontPlus" }, + {"Shader Forge/toon_eye_lod0", "xukmi/EyePlus" }, + {"Koikano/main_eye", "xukmi/EyePlus" }, + {"Shader Forge/toon_eyew_lod0", "xukmi/EyeWPlus" }, + {"Koikano/main_eyew", "xukmi/EyeWPlus" }, + {"Shader Forge/main_opaque", "xukmi/MainOpaquePlusTess" }, + {"Shader Forge/main_opaque2", "xukmi/MainOpaquePlusTess" }, + {"Koikano/main_clothes_opaque", "xukmi/MainOpaquePlusTess" }, + {"Shader Forge/main_alpha", "xukmi/MainAlphaPlusTess" }, + {"Koikano/main_clothes_alpha", "xukmi/MainAlphaPlusTess" }, + {"Shader Forge/main_item", "xukmi/MainItemPlus" }, + {"Koikano/main_clothes_item", "xukmi/MainItemPlus" }, + }; + private void Awake() { Logger = base.Logger; SwapShadersHotkey = Config.Bind("Keyboard Shortcuts", "Swap Shaders", new KeyboardShortcut(KeyCode.P, KeyCode.RightControl), "Swap all shaders to the equivalent Vanilla+ shader."); + TesselationSlider = Config.Bind("Tesselation", "Tesselation", 0f, + new ConfigDescription("The amount of tesselation to apply. Leave at 0% to use the regular Vanilla+ shaders without tesselation.", + new AcceptableValueRange(0f, 1f) + ) + ); } private void Update() @@ -90,8 +117,19 @@ public static MaterialEditorCharaController GetController(ChaControl chaControl) private void SwapToVanillaPlus(MaterialEditorCharaController controller, int slot, ObjectType objectType, Material mat, GameObject go) { if (controller.GetMaterialShader(slot, ObjectType.Clothing, mat, go) == null) - { - if (VanillaPlusShaders.TryGetValue(mat.shader.name, out var vanillaPlusShaderName)) + { + if (TesselationSlider.Value > 0 && VanillaPlusTesselationShaders.TryGetValue(mat.shader.name, out var vanillaPlusTesShaderName)) + { + int renderQueue = mat.renderQueue; + controller.SetMaterialShader(slot, objectType, mat, vanillaPlusTesShaderName, go); + controller.SetMaterialShaderRenderQueue(slot, objectType, mat, renderQueue, go); + if (mat.shader.name == "xukmi/MainAlphaPlus") + controller.SetMaterialFloatProperty(slot, objectType, mat, "Cutoff", 0.1f, go); + + SetTesselationValue(mat); + } + + if (TesselationSlider.Value == 0 && VanillaPlusShaders.TryGetValue(mat.shader.name, out var vanillaPlusShaderName)) { int renderQueue = mat.renderQueue; controller.SetMaterialShader(slot, objectType, mat, vanillaPlusShaderName, go); @@ -130,5 +168,14 @@ private void SwapToVanillaPlusBody(MaterialEditorCharaController controller) foreach (var material in GetMaterials(controller.ChaControl.gameObject, renderer)) SwapToVanillaPlus(controller, 0, ObjectType.Character, material, controller.ChaControl.gameObject); } + + private void SetTesselationValue(Material mat) + { + if (mat == null || !mat.HasProperty("_TessSmooth")) + return; + + //Adjust the weight of the tesselation + mat.SetFloat("_TessSmooth", TesselationSlider.Value); + } } }