Skip to content

Commit

Permalink
[ShaderSwapper] Add support for Vanilla+ shaders with tessellation (#143
Browse files Browse the repository at this point in the history
)
  • Loading branch information
thojmr authored May 6, 2022
1 parent 23400fd commit 9863720
Showing 1 changed file with 49 additions and 2 deletions.
51 changes: 49 additions & 2 deletions src/ShaderSwapper.Core/ShaderSwapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class ShaderSwapper : BaseUnityPlugin
internal static new ManualLogSource Logger;

internal static ConfigEntry<KeyboardShortcut> SwapShadersHotkey { get; private set; }
internal static ConfigEntry<float> TesselationSlider { get; private set; }

private readonly Dictionary<string, string> VanillaPlusShaders = new Dictionary<string, string>
{
Expand All @@ -44,10 +45,36 @@ public class ShaderSwapper : BaseUnityPlugin
{"Koikano/main_clothes_item", "xukmi/MainItemPlus" },
};

private readonly Dictionary<string, string> VanillaPlusTesselationShaders = new Dictionary<string, string>
{
{"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<float>(0f, 1f)
)
);
}

private void Update()
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
}
}

0 comments on commit 9863720

Please sign in to comment.