Skip to content

Commit

Permalink
Add compatibility with Shabby and Deferred (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
LGhassen authored Jun 23, 2024
1 parent 79ce0fd commit f4062d9
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 56 deletions.
15 changes: 9 additions & 6 deletions Unity/Shaders/Diffuse_Multiply_Random_Normal.shader
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,21 @@ Shader "KK/Diffuse_Multiply_Random"
LOD 200
ZWrite On
ZTest LEqual
Blend SrcAlpha OneMinusSrcAlpha

Stencil
{
Ref 4
Comp Always
Pass Replace
}

CGPROGRAM
// Upgrade NOTE: excluded shader from OpenGL ES 2.0 because it uses non-square matrices
#pragma exclude_renderers gles
// Physically based Standard lighting model, and enable shadows on all light types

// Physically based Standard lighting model, and enable shadows on all light types
#pragma surface surf BlinnPhong fullforwardshadows nofog

// Use shader model 3.0 target, to get nicer looking lighting
#pragma target 3.0

#include "KSP-include.cginc"
Expand Down Expand Up @@ -112,9 +118,6 @@ Shader "KK/Diffuse_Multiply_Random"
//o.Emission = glow.rgb * glow.aaa * _EmissiveColor.rgb * _EmissiveColor.aaa + stockEmit(IN.viewDir, normal, _RimColor, _RimFalloff, _TemperatureColor) * _Opacity;
o.Emission = stockEmit(IN.viewDir, normal, _RimColor, _RimFalloff, _TemperatureColor) * _Opacity;

//controlled directly by shader property
o.Alpha = _Opacity;


//apply the standard shader param multipliers to the sampled/computed values.
fixed4 fog = UnderwaterFog(IN.worldPos, outputColor);
Expand Down
11 changes: 7 additions & 4 deletions Unity/Shaders/Ground_Multi_NoUV_Normal.shader
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,13 @@ Shader "KK/Ground_Multi_NoUV"
LOD 200
ZWrite On
ZTest LEqual
Blend SrcAlpha OneMinusSrcAlpha

Stencil
{
Ref 4
Comp Always
Pass Replace
}

CGPROGRAM
// Upgrade NOTE: excluded shader from OpenGL ES 2.0 because it uses non-square matrices
Expand Down Expand Up @@ -293,9 +299,6 @@ Shader "KK/Ground_Multi_NoUV"
//o.Emission = glow.rgb * glow.aaa * _EmissiveColor.rgb * _EmissiveColor.aaa + stockEmit(IN.viewDir, normal, _RimColor, _RimFalloff, _TemperatureColor) * _Opacity;
o.Emission = stockEmit(IN.viewDir, normal, _RimColor, _RimFalloff, _TemperatureColor) * _Opacity;

//controlled directly by shader property
o.Alpha = _Opacity;


//apply the standard shader param multipliers to the sampled/computed values.
fixed4 fog = UnderwaterFog(IN.worldPos, outputColor);
Expand Down
7 changes: 7 additions & 0 deletions Unity/Shaders/NormalFromTexture.shader
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@
Tags { "RenderType"="Opaque" }
LOD 100

Stencil
{
Ref 4
Comp Always
Pass Replace
}

Pass
{
CGPROGRAM
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,13 +226,14 @@ private void ReplaceMaterial(MeshRenderer renderer, string materialName)

internal void ReplaceShader(MeshRenderer renderer, string newShaderName)
{
if (!KKGraphics.HasShader(newShaderName))
Shader newShader = KKGraphics.GetShader(newShaderName);

if (newShader == null)
{
Log.UserError("No Shader like this found: " + newShaderName);
return;
}

Shader newShader = KKGraphics.GetShader(newShaderName);
renderer.material.shader = newShader;
//Log.Normal("Applied Shader: " + newShader.name);

Expand Down
59 changes: 15 additions & 44 deletions src/Utilities/KKGraphics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ namespace KerbalKonstructs
{
class KKGraphics
{
private static Dictionary<string, Shader> allShaders = new Dictionary<string, Shader>();
private static bool loadedShaders = false;
private static Dictionary<string, Shader> assetBundleShaders = new Dictionary<string, Shader>();
private static bool loadedMaterials = false;

private static Dictionary<string, Texture2D> cachedTextures = new Dictionary<string, Texture2D>();
Expand All @@ -23,65 +22,37 @@ class KKGraphics

private static Dictionary<string, Texture2D> normalMaps = new Dictionary<string, Texture2D>();


/// <summary>
/// Load all shaders into the system and fill our shader database.
/// </summary>
internal static void LoadShaders()
{
foreach (var shader in Resources.FindObjectsOfTypeAll<Shader>())
{
if (!allShaders.ContainsKey(shader.name))
{
allShaders.Add(shader.name, shader);
// Debug Code
//Log.Normal("Loaded shader: " + shader.name);
}
}
loadedShaders = true;
}

internal static bool HasShader(string name)
{
if (!loadedShaders)
{
LoadShaders();
}
return allShaders.ContainsKey(name);
}


/// <summary>
/// Replacement for Shader.Find() function, as we return also shaders, that are through KSP asset bundles (with autoload on)
/// Return shaders loaded from KSP asset bundles (with autoload on), then fallback to Shader.Find
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
internal static Shader GetShader(string name)
{
if (!loadedShaders)
{
LoadShaders();
}
Shader shader = null;

if (allShaders.ContainsKey(name))
if (assetBundleShaders.ContainsKey(name))
{
return allShaders[name];
shader = assetBundleShaders[name];
}
else
{
shader = Shader.Find(name);
}

if (shader == null)
{
Log.UserError("AdvTexture: Shader not found: " + name);
Log.Trace();
// return the error Shader, if we have one
if (allShaders.ContainsKey("Hidden/InternalErrorShader"))
if (assetBundleShaders.ContainsKey("Hidden/InternalErrorShader"))
{
//Log.UserWarning("Cannot load shader: " + name);
return allShaders["Hidden/InternalErrorShader"];
}
else
{
return null;
return assetBundleShaders["Hidden/InternalErrorShader"];
}
}

return shader;
}


Expand Down Expand Up @@ -143,7 +114,7 @@ private static void LoadAndRegisterShader(AssetBundle bundle , string shaderName
else
{
GameDatabase.Instance.databaseShaders.AddUnique(newShader);
allShaders.Add(newShader.name, newShader);
assetBundleShaders.Add(newShader.name, newShader);
Log.Normal("Loaded Shader: " + newShader.name + " from file: " + shaderName);
}

Expand Down

0 comments on commit f4062d9

Please sign in to comment.