Skip to content

Commit

Permalink
cleanup. dsp 0.10.28.20779
Browse files Browse the repository at this point in the history
  • Loading branch information
andyoneal committed Dec 15, 2023
1 parent 846b03c commit 072b8af
Show file tree
Hide file tree
Showing 13 changed files with 386 additions and 407 deletions.
23 changes: 11 additions & 12 deletions CustomShaderDesc.cs
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;
}
}
}
175 changes: 79 additions & 96 deletions CustomShaderManager.cs
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);
}
}
}
33 changes: 17 additions & 16 deletions InstDysonNodeFrameRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ public static class InstDysonNodeFrameRenderer
private static int csKernelId;
private static uint csThreads;
private static ComputeBuffer argBuffer;

private static StarData starData;
private static GameData gameData;
private static DysonSphere dysonSphere;
private static DysonSphereSegmentRenderer currentDSSR;

private static readonly int InstBuffer = Shader.PropertyToID("_InstBuffer");
private static readonly int LayerRotations = Shader.PropertyToID("_LayerRotations");
private static readonly int SunColor = Shader.PropertyToID("_SunColor");
Expand All @@ -35,11 +35,10 @@ public static class InstDysonNodeFrameRenderer
private static readonly int LOD1IDBuffer = Shader.PropertyToID("_LOD1_ID_Buffer");
private static readonly int LOD2IDBuffer = Shader.PropertyToID("_LOD2_ID_Buffer");


public static void SetupMeshes()
{
if (lodMeshes != null) return;

lodMeshes = new Mesh[DysonSphereSegmentRenderer.totalProtoCount][];
var meshSimplifier = new MeshSimplifier();
for (int i = 0; i < DysonSphereSegmentRenderer.totalProtoCount; i++)
Expand All @@ -58,7 +57,7 @@ public static void SetupMeshes()
{
options.PreserveBorderEdges = true;
options.PreserveUVFoldoverEdges = true;
options.PreserveUVSeamEdges = false;
options.PreserveUVSeamEdges = false;
}
options.MaxIterationCount = 1000;
meshSimplifier.SimplificationOptions = options;
Expand All @@ -76,7 +75,7 @@ public static void SetupBuffers()
if (lodBatchBuffers != null) return;

var totalProtoCount = DysonSphereSegmentRenderer.totalProtoCount;

lodBatchBuffers = new ComputeBuffer[totalProtoCount][];
for (int i = 0; i < totalProtoCount; i++)
{
Expand Down Expand Up @@ -111,21 +110,20 @@ public static void SetupBuffers()
public static void SetupLODShader()
{
if (frameLODShader != null) return;

frameLODShader = CustomShaderManager.LoadComputeShader("Frame LOD");
csKernelId = frameLODShader.FindKernel("CSMain");
frameLODShader.GetKernelThreadGroupSizes(csKernelId, out csThreads, out var _, out var _);
}

private static void switchDSSR(DysonSphereSegmentRenderer dssr)
{

instBufferChangedSize = true;

starData = dssr.starData;
gameData = dssr.gameData;
dysonSphere = dssr.dysonSphere;

currentDSSR = dssr;
}

Expand All @@ -149,7 +147,7 @@ private static void rebuildInstBuffers()
public static void Render(DysonSphereSegmentRenderer dssr, ERenderPlace place, int editorMask, int gameMask)
{
if (currentDSSR == null || currentDSSR != dssr) switchDSSR(dssr);

if (instBufferChangedSize)
{
rebuildInstBuffers();
Expand All @@ -164,7 +162,7 @@ public static void Render(DysonSphereSegmentRenderer dssr, ERenderPlace place, i
var localPlanet = gameData.localPlanet;
var mainPlayer = gameData.mainPlayer;
sunPos = localPlanet == null
? starData.uPosition - mainPlayer.uPosition
? (Vector3)(starData.uPosition - mainPlayer.uPosition)
: (Vector3)Maths.QInvRotateLF(localPlanet.runtimeRotation,
starData.uPosition - localPlanet.uPosition);
if (place == ERenderPlace.Starmap)
Expand Down Expand Up @@ -197,10 +195,13 @@ public static void Render(DysonSphereSegmentRenderer dssr, ERenderPlace place, i
cam = UIRoot.instance.uiGame.dysonEditor.screenCamera;
layer = 21;
break;
case ERenderPlace.DemoScene:
default:
break;
}
Shader.SetGlobalVector(GlobalDSSunPosition, sunPos);
Shader.SetGlobalVector(GlobalDSSunPositionMap, sunPosMap);

var pos = place == ERenderPlace.Universe ? sunPos : sunPosMap;
var scale = place == ERenderPlace.Starmap || place == ERenderPlace.Dysonmap
? new Vector3(0.00025f, 0.00025f, 0.00025f)
Expand Down Expand Up @@ -254,13 +255,13 @@ public static void Render(DysonSphereSegmentRenderer dssr, ERenderPlace place, i
{
mpb.SetBuffer(InstIndexBuffer, lodBatchBuffers[b][j]);
if (shouldRender)
{
Graphics.DrawMeshInstancedIndirect(lodMeshes[b][j], 0, instMats[b],
new Bounds(Vector3.zero, new Vector3(300000f, 300000f, 300000f)), argBuffer,
(b * 15 + j * 5) * 4, mpb, ShadowCastingMode.Off, false, layer);
}
}
}
}


}
}
Loading

0 comments on commit 072b8af

Please sign in to comment.