Skip to content

Commit

Permalink
simplify. swap shader only.
Browse files Browse the repository at this point in the history
  • Loading branch information
andyoneal committed Mar 4, 2023
1 parent 0dda1b0 commit ad3e76d
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 87 deletions.
39 changes: 2 additions & 37 deletions CustomShaderDesc.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine;

namespace SphereOpt;

Expand All @@ -10,46 +8,13 @@ public class CustomShaderDesc
public readonly Shader shader;
public readonly string shaderName;
public readonly string replacementForShader;
public readonly Dictionary<string, EShaderPropType> addedProperties;

public CustomShaderDesc (string shortName, string shaderToReplace, string replacementShader, Dictionary<string, EShaderPropType> addedProps)
public CustomShaderDesc (string shortName, string shaderToReplace, string replacementShader)
{
shader = CustomShaderManager.GetShader(replacementShader);
if (shader == null) SphereOpt.logger.LogError($"Could not find shader for name: {replacementShader}");
shaderName = replacementShader;
replacementForShader = shaderToReplace;
if (addedProps == null) addedProps = new Dictionary<string, EShaderPropType>();
addedProperties = addedProps;
this.shortName = shortName;
}

public Type TypeOfAddedProperty(string propName)
{
if (!addedProperties.TryGetValue(propName, out var ePropType))
{
SphereOpt.logger.LogWarning($"{shaderName} has no added property named {propName}.");
return null;
}

return ePropType switch
{
EShaderPropType.Buffer => typeof(ComputeBuffer),
EShaderPropType.Color => typeof(Color),
EShaderPropType.Float => typeof(float),
EShaderPropType.Int => typeof(int),
EShaderPropType.Matrix => typeof(Matrix4x4),
EShaderPropType.Vector => typeof(Vector4),
_ => throw new ArgumentOutOfRangeException()
};
}
}

public enum EShaderPropType
{
Buffer,
Color,
Float,
Int,
Matrix,
Vector
}
52 changes: 5 additions & 47 deletions CustomShaderManager.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using SphereOpt;
using System.Collections.Generic;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using UnityEngine;
Expand Down Expand Up @@ -43,7 +42,6 @@ public static void InitWithBundle(AssetBundle assetBundle)
if (!LoadShadersFromBundle())
{
SphereOpt.logger.LogError("Failed to load custom shaders from bundle.");
return;
}
}

Expand All @@ -68,10 +66,9 @@ private static bool LoadShadersFromBundle()
return true;
}

public static void AddCustomShaderDesc(string shortName, string shaderToReplace, string replacementShader,
Dictionary<string, EShaderPropType> addedProps = null)
public static void AddCustomShaderDesc(string shortName, string shaderToReplace, string replacementShader)
{
CustomShaderDesc shaderDesc = new(shortName, shaderToReplace, replacementShader, addedProps);
CustomShaderDesc shaderDesc = new(shortName, shaderToReplace, replacementShader);
customShaderDescs.Add(shaderDesc);
replacementForShaderMap.Add(shaderDesc.replacementForShader, shaderDesc);
shortNameMap.Add(shaderDesc.shortName, shaderDesc);
Expand All @@ -80,12 +77,12 @@ public static void AddCustomShaderDesc(string shortName, string shaderToReplace,

public static CustomShaderDesc LookupReplacementShaderFor(string originalShaderName)
{
return replacementForShaderMap.TryGetValue(originalShaderName, out CustomShaderDesc customShader) ? customShader : null;
return replacementForShaderMap.TryGetValue(originalShaderName, out var customShader) ? customShader : null;
}

public static bool ReplaceShaderIfAvailable(Material mat)
{
if (replacementForShaderMap.TryGetValue(mat.shader.name, out CustomShaderDesc customShaderDesc))
if (replacementForShaderMap.TryGetValue(mat.shader.name, out var customShaderDesc))
{
SphereOpt.logger.LogInfo($"replacing shader on: {mat.name}");
ApplyCustomShaderToMaterial(mat, customShaderDesc);
Expand All @@ -105,17 +102,6 @@ public static Shader GetShader (string customShaderName)
return null;
}

public static CustomShaderDesc GetCustomShaderDescByShortName(string shortName)
{
if (!shortNameMap.TryGetValue(shortName, out CustomShaderDesc csd))
{
SphereOpt.logger.LogError($"CustomShaderDesc with ShortName: {shortName} not found");
return null;
}

return csd;
}

public static void ApplyCustomShaderToMaterial(Material mat, CustomShaderDesc replacementShader)
{
mat.shader = replacementShader.shader;
Expand All @@ -128,32 +114,4 @@ public static void ApplyCustomShaderToMaterial(Material mat, CustomShaderDesc re

matList.Add(mat);
}

public static void SetPropByShortName(string propName, float propVal, string shortName)
{
CustomShaderDesc csd = GetCustomShaderDescByShortName(shortName);
if (csd == null)
{
SphereOpt.logger.LogError($"No CustomShaderDesc found with shortName: {shortName}");
return;
}

var propType = csd.TypeOfAddedProperty(propName);
if (propType == null)
{
SphereOpt.logger.LogError($"CustomShaderDesc has no AddedProperty named: {propName}");
return;

}

if (propType != typeof(float))
{
SphereOpt.logger.LogError($"Property {propName} is of type {csd.TypeOfAddedProperty(propName)} but value provided is float");
}

foreach (var mat in shaderReplacedOnMaterialsMap[csd])
{
mat.SetFloat(propName, propVal);
}
}
}
3 changes: 1 addition & 2 deletions Patch_VFPreload.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.ComponentModel;
using HarmonyLib;
using HarmonyLib;
using UnityEngine;

namespace SphereOpt;
Expand Down
2 changes: 1 addition & 1 deletion SphereOpt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class SphereOpt : BaseUnityPlugin
private static AssetBundle bundle;
private static readonly string AssemblyPath = Path.GetDirectoryName(Assembly.GetAssembly(typeof(SphereOpt)).Location);

public static AssetBundle Bundle
private static AssetBundle Bundle
{
get
{
Expand Down

0 comments on commit ad3e76d

Please sign in to comment.