-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
386 additions
and
407 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,17 @@ | ||
using UnityEngine; | ||
|
||
namespace SphereOpt; | ||
|
||
public class CustomShaderDesc | ||
namespace SphereOpt | ||
{ | ||
public readonly string shortName; | ||
public readonly Shader shader; | ||
public readonly string shaderName; | ||
|
||
public CustomShaderDesc (string shortName, string customShaderName) | ||
public class CustomShaderDesc | ||
{ | ||
shader = CustomShaderManager.GetShader(customShaderName); | ||
if (shader == null) SphereOpt.logger.LogError($"Could not find shader for name: {customShaderName}"); | ||
shaderName = customShaderName; | ||
this.shortName = shortName; | ||
public readonly string shortName; | ||
public readonly Shader shader; | ||
|
||
public CustomShaderDesc (string shortName, string customShaderName) | ||
{ | ||
shader = CustomShaderManager.GetShader(customShaderName); | ||
if (shader == null) SphereOpt.logger.LogError($"Could not find shader for name: {customShaderName}"); | ||
this.shortName = shortName; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,131 +1,114 @@ | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Reflection; | ||
using UnityEngine; | ||
|
||
namespace SphereOpt; | ||
|
||
public static class CustomShaderManager | ||
namespace SphereOpt | ||
{ | ||
private static readonly string AssemblyPath = Path.GetDirectoryName(Assembly.GetAssembly(typeof(SphereOpt)).Location); | ||
private static AssetBundle bundle; | ||
private static readonly List<Shader> bundleShaders = new(); | ||
private static readonly List<CustomShaderDesc> customShaderDescs = new(); | ||
private static readonly Dictionary<string, CustomShaderDesc> shortNameMap = new(); | ||
private static readonly Dictionary<string, CustomShaderDesc> autoReplaceShaderMap = new(); | ||
private static readonly Dictionary<CustomShaderDesc, List<Material>> shaderReplacedOnMaterialsMap = new(); | ||
|
||
public static void InitWithBundle(string bundleFileName) | ||
public static class CustomShaderManager | ||
{ | ||
if (bundleShaders.Count > 0) | ||
{ | ||
SphereOpt.logger.LogError($"CustomShaderManager is already initialized with bundle: {bundle.name}"); | ||
return; | ||
} | ||
var path = Path.Combine(AssemblyPath, bundleFileName); | ||
if (File.Exists(path)) | ||
{ | ||
bundle = AssetBundle.LoadFromFile(path); | ||
InitWithBundle(bundle); | ||
} | ||
else SphereOpt.logger.LogError($"Bundle file not found at: {path}"); | ||
} | ||
private static AssetBundle bundle; | ||
private static readonly List<Shader> bundleShaders = new List<Shader>(); | ||
|
||
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()) | ||
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) | ||
{ | ||
SphereOpt.logger.LogError("Failed to load custom shaders from bundle."); | ||
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) | ||
public static ComputeShader LoadComputeShader(string name) | ||
{ | ||
return bundle.LoadAsset<ComputeShader>(name); | ||
} | ||
if (bundle != null) | ||
{ | ||
return bundle.LoadAsset<ComputeShader>(name); | ||
} | ||
|
||
return null; | ||
} | ||
return null; | ||
} | ||
|
||
private static bool LoadShadersFromBundle() | ||
{ | ||
SphereOpt.logger.LogInfo("Loading custom shaders from bundle."); | ||
if (bundle != null) | ||
private static bool LoadShadersFromBundle() | ||
{ | ||
var shaders = bundle.LoadAllAssets<Shader>(); | ||
foreach (var s in shaders) | ||
SphereOpt.logger.LogInfo("Loading custom shaders from bundle."); | ||
if (bundle != null) | ||
{ | ||
bundleShaders.Add(s); | ||
SphereOpt.logger.LogInfo($"Loaded custom shader: {s.name}"); | ||
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; | ||
} | ||
else | ||
|
||
public static void AddCustomShaderDesc(string shortName, string shaderName, string alwaysReplaceShaderName = null) | ||
{ | ||
SphereOpt.logger.LogError("Failed to load custom shaders from bundle".Translate()); | ||
return false; | ||
CustomShaderDesc shaderDesc = new CustomShaderDesc(shortName, shaderName); | ||
if(alwaysReplaceShaderName != null) autoReplaceShaderMap.Add(alwaysReplaceShaderName, shaderDesc); | ||
shortNameMap.Add(shaderDesc.shortName, shaderDesc); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
public static void AddCustomShaderDesc(string shortName, string shaderName, string alwaysReplaceShaderName = null) | ||
{ | ||
CustomShaderDesc shaderDesc = new(shortName, shaderName); | ||
customShaderDescs.Add(shaderDesc); | ||
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)) | ||
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; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
public static Shader GetShader (string customShaderName) | ||
{ | ||
foreach (var shader in bundleShaders) | ||
public static Shader GetShader (string customShaderName) | ||
{ | ||
if (shader.name.Equals(customShaderName)) return shader; | ||
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; | ||
} | ||
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)) | ||
public static void ApplyCustomShaderToMaterial(Material mat, string shortName) | ||
{ | ||
SphereOpt.logger.LogWarning($"Couldn't find a CustomShaderDesc with shortname: {shortName}"); | ||
return; | ||
if (!shortNameMap.TryGetValue(shortName, out var customShaderDesc)) | ||
{ | ||
SphereOpt.logger.LogWarning($"Couldn't find a CustomShaderDesc with shortname: {shortName}"); | ||
return; | ||
} | ||
ApplyCustomShaderToMaterial(mat, customShaderDesc); | ||
} | ||
ApplyCustomShaderToMaterial(mat, customShaderDesc); | ||
} | ||
|
||
private static void ApplyCustomShaderToMaterial(Material mat, CustomShaderDesc replacementShader) | ||
{ | ||
mat.shader = replacementShader.shader; | ||
|
||
if(!shaderReplacedOnMaterialsMap.TryGetValue(replacementShader, out var matList)) | ||
private static void ApplyCustomShaderToMaterial(Material mat, CustomShaderDesc replacementShader) | ||
{ | ||
matList = new List<Material>(); | ||
shaderReplacedOnMaterialsMap.Add(replacementShader, matList); | ||
} | ||
mat.shader = replacementShader.shader; | ||
|
||
if(!shaderReplacedOnMaterialsMap.TryGetValue(replacementShader, out var matList)) | ||
{ | ||
matList = new List<Material>(); | ||
shaderReplacedOnMaterialsMap.Add(replacementShader, matList); | ||
} | ||
|
||
matList.Add(mat); | ||
matList.Add(mat); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.