Skip to content

Commit f4062d9

Browse files
authored
Add compatibility with Shabby and Deferred (#18)
1 parent 79ce0fd commit f4062d9

File tree

5 files changed

+41
-56
lines changed

5 files changed

+41
-56
lines changed

Unity/Shaders/Diffuse_Multiply_Random_Normal.shader

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,21 @@ Shader "KK/Diffuse_Multiply_Random"
2121
LOD 200
2222
ZWrite On
2323
ZTest LEqual
24-
Blend SrcAlpha OneMinusSrcAlpha
24+
25+
Stencil
26+
{
27+
Ref 4
28+
Comp Always
29+
Pass Replace
30+
}
2531

2632
CGPROGRAM
2733
// Upgrade NOTE: excluded shader from OpenGL ES 2.0 because it uses non-square matrices
2834
#pragma exclude_renderers gles
29-
// Physically based Standard lighting model, and enable shadows on all light types
35+
36+
// Physically based Standard lighting model, and enable shadows on all light types
3037
#pragma surface surf BlinnPhong fullforwardshadows nofog
3138

32-
// Use shader model 3.0 target, to get nicer looking lighting
3339
#pragma target 3.0
3440

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

115-
//controlled directly by shader property
116-
o.Alpha = _Opacity;
117-
118121

119122
//apply the standard shader param multipliers to the sampled/computed values.
120123
fixed4 fog = UnderwaterFog(IN.worldPos, outputColor);

Unity/Shaders/Ground_Multi_NoUV_Normal.shader

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,13 @@ Shader "KK/Ground_Multi_NoUV"
4747
LOD 200
4848
ZWrite On
4949
ZTest LEqual
50-
Blend SrcAlpha OneMinusSrcAlpha
50+
51+
Stencil
52+
{
53+
Ref 4
54+
Comp Always
55+
Pass Replace
56+
}
5157

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

296-
//controlled directly by shader property
297-
o.Alpha = _Opacity;
298-
299302

300303
//apply the standard shader param multipliers to the sampled/computed values.
301304
fixed4 fog = UnderwaterFog(IN.worldPos, outputColor);

Unity/Shaders/NormalFromTexture.shader

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@
1212
Tags { "RenderType"="Opaque" }
1313
LOD 100
1414

15+
Stencil
16+
{
17+
Ref 4
18+
Comp Always
19+
Pass Replace
20+
}
21+
1522
Pass
1623
{
1724
CGPROGRAM

src/Core/StaticObjects/StaticModules/AdvTextures/AdvTextures.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,13 +226,14 @@ private void ReplaceMaterial(MeshRenderer renderer, string materialName)
226226

227227
internal void ReplaceShader(MeshRenderer renderer, string newShaderName)
228228
{
229-
if (!KKGraphics.HasShader(newShaderName))
229+
Shader newShader = KKGraphics.GetShader(newShaderName);
230+
231+
if (newShader == null)
230232
{
231233
Log.UserError("No Shader like this found: " + newShaderName);
232234
return;
233235
}
234236

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

src/Utilities/KKGraphics.cs

Lines changed: 15 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ namespace KerbalKonstructs
99
{
1010
class KKGraphics
1111
{
12-
private static Dictionary<string, Shader> allShaders = new Dictionary<string, Shader>();
13-
private static bool loadedShaders = false;
12+
private static Dictionary<string, Shader> assetBundleShaders = new Dictionary<string, Shader>();
1413
private static bool loadedMaterials = false;
1514

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

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

26-
27-
/// <summary>
28-
/// Load all shaders into the system and fill our shader database.
29-
/// </summary>
30-
internal static void LoadShaders()
31-
{
32-
foreach (var shader in Resources.FindObjectsOfTypeAll<Shader>())
33-
{
34-
if (!allShaders.ContainsKey(shader.name))
35-
{
36-
allShaders.Add(shader.name, shader);
37-
// Debug Code
38-
//Log.Normal("Loaded shader: " + shader.name);
39-
}
40-
}
41-
loadedShaders = true;
42-
}
43-
44-
internal static bool HasShader(string name)
45-
{
46-
if (!loadedShaders)
47-
{
48-
LoadShaders();
49-
}
50-
return allShaders.ContainsKey(name);
51-
}
52-
53-
5425
/// <summary>
55-
/// Replacement for Shader.Find() function, as we return also shaders, that are through KSP asset bundles (with autoload on)
26+
/// Return shaders loaded from KSP asset bundles (with autoload on), then fallback to Shader.Find
5627
/// </summary>
5728
/// <param name="name"></param>
5829
/// <returns></returns>
5930
internal static Shader GetShader(string name)
6031
{
61-
if (!loadedShaders)
62-
{
63-
LoadShaders();
64-
}
32+
Shader shader = null;
6533

66-
if (allShaders.ContainsKey(name))
34+
if (assetBundleShaders.ContainsKey(name))
6735
{
68-
return allShaders[name];
36+
shader = assetBundleShaders[name];
6937
}
7038
else
39+
{
40+
shader = Shader.Find(name);
41+
}
42+
43+
if (shader == null)
7144
{
7245
Log.UserError("AdvTexture: Shader not found: " + name);
7346
Log.Trace();
7447
// return the error Shader, if we have one
75-
if (allShaders.ContainsKey("Hidden/InternalErrorShader"))
48+
if (assetBundleShaders.ContainsKey("Hidden/InternalErrorShader"))
7649
{
7750
//Log.UserWarning("Cannot load shader: " + name);
78-
return allShaders["Hidden/InternalErrorShader"];
79-
}
80-
else
81-
{
82-
return null;
51+
return assetBundleShaders["Hidden/InternalErrorShader"];
8352
}
8453
}
54+
55+
return shader;
8556
}
8657

8758

@@ -143,7 +114,7 @@ private static void LoadAndRegisterShader(AssetBundle bundle , string shaderName
143114
else
144115
{
145116
GameDatabase.Instance.databaseShaders.AddUnique(newShader);
146-
allShaders.Add(newShader.name, newShader);
117+
assetBundleShaders.Add(newShader.name, newShader);
147118
Log.Normal("Loaded Shader: " + newShader.name + " from file: " + shaderName);
148119
}
149120

0 commit comments

Comments
 (0)