Skip to content

Commit

Permalink
reduce size of shell array properties
Browse files Browse the repository at this point in the history
  • Loading branch information
andyoneal committed Mar 10, 2023
1 parent c096f48 commit 4251f7a
Show file tree
Hide file tree
Showing 11 changed files with 2,327 additions and 27 deletions.
10 changes: 4 additions & 6 deletions CustomShaderDesc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,12 @@ public class CustomShaderDesc
public readonly string shortName;
public readonly Shader shader;
public readonly string shaderName;
public readonly string replacementForShader;

public CustomShaderDesc (string shortName, string shaderToReplace, string replacementShader)
public CustomShaderDesc (string shortName, string customShaderName)
{
shader = CustomShaderManager.GetShader(replacementShader);
if (shader == null) SphereOpt.logger.LogError($"Could not find shader for name: {replacementShader}");
shaderName = replacementShader;
replacementForShader = shaderToReplace;
shader = CustomShaderManager.GetShader(customShaderName);
if (shader == null) SphereOpt.logger.LogError($"Could not find shader for name: {customShaderName}");
shaderName = customShaderName;
this.shortName = shortName;
}
}
28 changes: 16 additions & 12 deletions CustomShaderManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public static class CustomShaderManager
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> replacementForShaderMap = new();
private static readonly Dictionary<string, CustomShaderDesc> autoReplaceShaderMap = new();
private static readonly Dictionary<CustomShaderDesc, List<Material>> shaderReplacedOnMaterialsMap = new();

public static void InitWithBundle(string bundleFileName)
Expand Down Expand Up @@ -66,23 +66,17 @@ private static bool LoadShadersFromBundle()
return true;
}

public static void AddCustomShaderDesc(string shortName, string shaderToReplace, string replacementShader)
public static void AddCustomShaderDesc(string shortName, string shaderName, string alwaysReplaceShaderName = null)
{
CustomShaderDesc shaderDesc = new(shortName, shaderToReplace, replacementShader);
CustomShaderDesc shaderDesc = new(shortName, shaderName);
customShaderDescs.Add(shaderDesc);
replacementForShaderMap.Add(shaderDesc.replacementForShader, shaderDesc);
if(alwaysReplaceShaderName != null) autoReplaceShaderMap.Add(alwaysReplaceShaderName, shaderDesc);
shortNameMap.Add(shaderDesc.shortName, shaderDesc);

}

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

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

public static void ApplyCustomShaderToMaterial(Material mat, CustomShaderDesc replacementShader)
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;

Expand Down
17 changes: 17 additions & 0 deletions Patch_DysonShell.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using HarmonyLib;

namespace SphereOpt;

internal class Patch_DysonShell
{
[HarmonyPatch(typeof(DysonShell), "SetProtoId")]
[HarmonyPostfix]
static void DysonShell_SetProtoId(DysonShell __instance)
{
var arraySizeNeeded = __instance.polygon.Count * 3;
if (arraySizeNeeded > 256) CustomShaderManager.ApplyCustomShaderToMaterial(__instance.material, "dysonshell-max");
else if (arraySizeNeeded > 128) CustomShaderManager.ApplyCustomShaderToMaterial(__instance.material, "dysonshell-huge");
else if (arraySizeNeeded > 16) CustomShaderManager.ApplyCustomShaderToMaterial(__instance.material, "dysonshell-large");
else CustomShaderManager.ApplyCustomShaderToMaterial(__instance.material, "dysonshell-small");
}
}
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
# SphereOpt

Replaces the shader used by Dyson Shells.
Currently has no effect to performance or visuals in-game, and might even make both worse.
**SphereOpt v0.5.0 should be considered a "beta" release. The worst you should is experience is errors or graphical glitches, in which case you can uninstall the mod. Please report any issues you encounter.**

Improves rendering of Dyson Shells by drastically reducing the amount of data sent to the gpu per shell. Impact is larger depending on the number of shells in your Dyson Sphere(s).

In a new game with a large and densely packed sphere containing 5,240 shells, framerate increased from 18fps with [DSPOptimizations](https://dsp.thunderstore.io/package/Selsion/DSPOptimizations/) alone to 40fps with SphereOpt.

[DSPOptimizations](https://dsp.thunderstore.io/package/Selsion/DSPOptimizations/) is not required, but I don't know why you wouldn't use it if you're interested in this mod.
23 changes: 18 additions & 5 deletions SphereOpt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,31 @@ private static AssetBundle Bundle
}
private void Awake()
{
// Plugin startup logic
logger = Logger;
logger.LogInfo($"Plugin {PluginInfo.PLUGIN_GUID} is loaded!");

CustomShaderManager.InitWithBundle(Bundle);

CustomShaderManager.AddCustomShaderDesc(
"dysonshell",
"VF Shaders/Dyson Sphere/Dyson Shell Unlit",
"dysonshell-max",
"VF Shaders/Dyson Sphere/Dyson Shell Unlit REPLACE"
);

Harmony.CreateAndPatchAll(typeof(Patch_VFPreload));
CustomShaderManager.AddCustomShaderDesc(
"dysonshell-small",
"VF Shaders/Dyson Sphere/Dyson Shell Unlit REPLACE Small"
);

CustomShaderManager.AddCustomShaderDesc(
"dysonshell-large",
"VF Shaders/Dyson Sphere/Dyson Shell Unlit REPLACE Large"
);

CustomShaderManager.AddCustomShaderDesc(
"dysonshell-huge",
"VF Shaders/Dyson Sphere/Dyson Shell Unlit REPLACE Huge"
);

//Harmony.CreateAndPatchAll(typeof(Patch_VFPreload));
Harmony.CreateAndPatchAll(typeof(Patch_DysonShell));
}
}
5 changes: 3 additions & 2 deletions SphereOpt.csproj
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net472</TargetFramework>
<AssemblyName>SphereOpt</AssemblyName>
<Description>Optimize Dyson Sphere rendering</Description>
<Version>1.0.0</Version>
<Version>0.5.0</Version>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<LangVersion>latest</LangVersion>
<PlatformTarget>x64</PlatformTarget>
</PropertyGroup>

<ItemGroup>
Expand Down
Loading

0 comments on commit 4251f7a

Please sign in to comment.