-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomShaderManager.cs
More file actions
114 lines (97 loc) · 4.03 KB
/
Copy pathCustomShaderManager.cs
File metadata and controls
114 lines (97 loc) · 4.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
using System.Collections.Generic;
using UnityEngine;
namespace SphereOpt
{
public static class CustomShaderManager
{
private static AssetBundle bundle;
private static readonly List<Shader> bundleShaders = new List<Shader>();
private static readonly Dictionary<string, CustomShaderDesc> shortNameMap =
new Dictionary<string, CustomShaderDesc>();
private static readonly Dictionary<string, CustomShaderDesc> autoReplaceShaderMap =
new Dictionary<string, CustomShaderDesc>();
private static readonly Dictionary<CustomShaderDesc, List<Material>> shaderReplacedOnMaterialsMap =
new Dictionary<CustomShaderDesc, List<Material>>();
public static void InitWithBundle(AssetBundle assetBundle)
{
if (bundleShaders.Count > 0)
{
SphereOpt.logger.LogError($"CustomShaderManager is already initialized with bundle: {bundle.name}");
return;
}
bundle = assetBundle;
if (!LoadShadersFromBundle())
{
SphereOpt.logger.LogError("Failed to load custom shaders from bundle.");
}
}
public static ComputeShader LoadComputeShader(string name)
{
if (bundle != null)
{
return bundle.LoadAsset<ComputeShader>(name);
}
return null;
}
private static bool LoadShadersFromBundle()
{
SphereOpt.logger.LogInfo("Loading custom shaders from bundle.");
if (bundle != null)
{
var shaders = bundle.LoadAllAssets<Shader>();
foreach (var s in shaders)
{
bundleShaders.Add(s);
SphereOpt.logger.LogInfo($"Loaded custom shader: {s.name}");
}
}
else
{
SphereOpt.logger.LogError("Failed to load custom shaders from bundle".Translate());
return false;
}
return true;
}
public static void AddCustomShaderDesc(string shortName, string shaderName, string alwaysReplaceShaderName = null)
{
CustomShaderDesc shaderDesc = new CustomShaderDesc(shortName, shaderName);
if(alwaysReplaceShaderName != null) autoReplaceShaderMap.Add(alwaysReplaceShaderName, shaderDesc);
shortNameMap.Add(shaderDesc.shortName, shaderDesc);
}
public static bool ReplaceShaderIfAvailable(Material mat)
{
if (!autoReplaceShaderMap.TryGetValue(mat.shader.name, out var customShaderDesc)) return false;
SphereOpt.logger.LogInfo($"replacing shader on: {mat.name}");
ApplyCustomShaderToMaterial(mat, customShaderDesc);
return true;
}
public static Shader GetShader (string customShaderName)
{
foreach (var shader in bundleShaders)
{
if (shader.name.Equals(customShaderName)) return shader;
}
SphereOpt.logger.LogWarning($"Couldn't find custom shader with name: {customShaderName}");
return null;
}
public static void ApplyCustomShaderToMaterial(Material mat, string shortName)
{
if (!shortNameMap.TryGetValue(shortName, out var customShaderDesc))
{
SphereOpt.logger.LogWarning($"Couldn't find a CustomShaderDesc with shortname: {shortName}");
return;
}
ApplyCustomShaderToMaterial(mat, customShaderDesc);
}
private static void ApplyCustomShaderToMaterial(Material mat, CustomShaderDesc replacementShader)
{
mat.shader = replacementShader.shader;
if(!shaderReplacedOnMaterialsMap.TryGetValue(replacementShader, out var matList))
{
matList = new List<Material>();
shaderReplacedOnMaterialsMap.Add(replacementShader, matList);
}
matList.Add(mat);
}
}
}