Skip to content

Commit 6700bfd

Browse files
authored
Deferred fixes/improvements (#24)
* Potential fix to glowy ground with Deferred * Use the new stencil values matching Deferred * Use the exact same shading/lighting function as Deferred, thanks @cineboxAndrew for putting everything in a single drop-in file to replace KSP lighting functions
1 parent 75d0508 commit 6700bfd

File tree

8 files changed

+186
-37
lines changed

8 files changed

+186
-37
lines changed
Binary file not shown.
-30.5 KB
Binary file not shown.
Binary file not shown.

Unity/Shaders/Diffuse_Multiply_Random_Normal.shader

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Shader "KK/Diffuse_Multiply_Random"
2424

2525
Stencil
2626
{
27-
Ref 4
27+
Ref 3
2828
Comp Always
2929
Pass Replace
3030
}
@@ -34,10 +34,11 @@ Shader "KK/Diffuse_Multiply_Random"
3434
#pragma exclude_renderers gles
3535

3636
// Physically based Standard lighting model, and enable shadows on all light types
37-
#pragma surface surf BlinnPhong fullforwardshadows nofog
37+
#pragma surface surf BlinnPhongKSP fullforwardshadows nofog
3838

3939
#pragma target 3.0
4040

41+
#include "LightingKSP.cginc"
4142
#include "KSP-include.cginc"
4243
#include "TileRandom2-include.cginc"
4344
//#include "TileRandom-include.cginc"
@@ -55,7 +56,6 @@ Shader "KK/Diffuse_Multiply_Random"
5556
//float4 _EmissiveColor;
5657

5758
//standard shader params for adjusting color/etc
58-
float4 _Color;
5959
float _MakeGrayScale;
6060

6161
struct Input
@@ -125,11 +125,17 @@ Shader "KK/Diffuse_Multiply_Random"
125125
o.Emission *= fog.a;
126126
o.Specular = (0);
127127
o.Normal = normal;
128-
// o.SpecularColor = (0,0,0,0);
128+
129+
#if UNITY_PASS_DEFERRED
130+
// In deferred rendering do not use the flat ambient because Deferred adds its own ambient as a composite of flat ambient and probe
131+
unity_SHAr = 0.0.xxxx;
132+
unity_SHAg = 0.0.xxxx;
133+
unity_SHAb = 0.0.xxxx;
134+
#endif
129135
}
130136

131137
ENDCG
132138

133139
}
134140
Fallback "Standard"
135-
}
141+
}

Unity/Shaders/Ground_Multi_NoUV_Normal.shader

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,23 +50,23 @@ Shader "KK/Ground_Multi_NoUV"
5050

5151
Stencil
5252
{
53-
Ref 4
53+
Ref 3
5454
Comp Always
5555
Pass Replace
5656
}
5757

5858
CGPROGRAM
5959
// Upgrade NOTE: excluded shader from OpenGL ES 2.0 because it uses non-square matrices
6060
#pragma exclude_renderers gles
61-
//
62-
#pragma surface surf BlinnPhong fullforwardshadows nofog
63-
61+
62+
#pragma surface surf BlinnPhongKSP fullforwardshadows nofog
6463

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

6867
//#include "TileRandom-include.cginc"
6968
#include "TileRandom2-include.cginc"
69+
#include "LightingKSP.cginc"
7070
#include "KSP-include.cginc"
7171

7272
sampler2D _BlendMaskTexture;
@@ -306,7 +306,14 @@ Shader "KK/Ground_Multi_NoUV"
306306
o.Albedo = fog.rgb;
307307
o.Emission *= fog.a;
308308
o.Specular = (0);
309-
// o.SpecularColor = (0,0,0,0);
309+
310+
311+
#if UNITY_PASS_DEFERRED
312+
// In deferred rendering do not use the flat ambient because Deferred adds its own ambient as a composite of flat ambient and probe
313+
unity_SHAr = 0.0.xxxx;
314+
unity_SHAg = 0.0.xxxx;
315+
unity_SHAb = 0.0.xxxx;
316+
#endif
310317
}
311318

312319
ENDCG

Unity/Shaders/KSP-include.cginc

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,7 @@
1-
float4 _LocalCameraPos;
2-
float4 _LocalCameraDir;
3-
float4 _UnderwaterFogColor;
4-
float _UnderwaterMinAlphaFogDistance;
5-
float _UnderwaterMaxAlbedoFog;
6-
float _UnderwaterMaxAlphaFog;
7-
float _UnderwaterAlbedoDistanceScalar;
8-
float _UnderwaterAlphaDistanceScalar;
9-
float _UnderwaterFogFactor;
10-
11-
121
float _Opacity;
132
float4 _TemperatureColor;
143
float4 _RimColor;
154
float _RimFalloff;
16-
17-
18-
19-
//stock fog function
20-
fixed4 UnderwaterFog(fixed3 worldPos, fixed3 color)
21-
{
22-
fixed3 toPixel = worldPos - _LocalCameraPos.xyz;
23-
fixed toPixelLength = length(toPixel);
24-
25-
fixed underwaterDetection = _UnderwaterFogFactor * _LocalCameraDir.w; ///< sign(1 - sign(_LocalCameraPos.w));
26-
fixed albedoLerpValue = underwaterDetection * (_UnderwaterMaxAlbedoFog * saturate(toPixelLength * _UnderwaterAlbedoDistanceScalar));
27-
fixed alphaFactor = 1 - underwaterDetection * (_UnderwaterMaxAlphaFog * saturate((toPixelLength - _UnderwaterMinAlphaFogDistance) * _UnderwaterAlphaDistanceScalar));
28-
29-
return fixed4(lerp(color, _UnderwaterFogColor.rgb, albedoLerpValue), alphaFactor);
30-
}
315

326
inline half3 stockEmit (float3 viewDir, float3 normal, half4 rimColor, half rimFalloff, half4 tempColor)
337
{

Unity/Shaders/LightingKSP.cginc

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
// WHAT IS THIS FILE?
2+
// this file provides a replacement for the LightingKSP.cginc file that ships with part tools for writing custom shaders.
3+
// This version enables support for the Deferred mod
4+
//
5+
// HOW DO I USE IT?
6+
// Step 1)
7+
// replace LightingKSP.cginc in your shader folder with this file, if present. If you aren't using LightingKSP.cginc
8+
// in your shader, add the following below `CGPROGRAM`:
9+
// `#include "../LightingKSP.cginc"`
10+
//
11+
// Step 2)
12+
// add the following above `CGPROGRAM`, respect the stencil values described in
13+
// https://github.com/LGhassen/Deferred?tab=readme-ov-file#stencil-buffer-usage
14+
// ```
15+
// Stencil
16+
// {
17+
// Ref 1
18+
// Comp Always
19+
// Pass Replace
20+
// }
21+
// ```
22+
//
23+
// Step 3)
24+
// there should be a line in your shader that looks like this:
25+
// `#pragma surface surf BlinnPhongSmooth keepalpha`
26+
// Remove the `keepalpha` if it's there. the part after `surf` is the name of the lighting function your shader uses now.
27+
// If the lighting function is `BlinnPhong` or `BlinnPhongSmooth`, change it to `BlinnPhongKSP`
28+
// If the lighting function is `Standard`, change it to `StandardKSP`
29+
// If the lighting function is `StandardSpecular`, change it to `StandardSpecularKSP`
30+
31+
#ifndef LIGHTING_KSP_INCLUDED
32+
#define LIGHTING_KSP_INCLUDED
33+
34+
#include "UnityPBSLighting.cginc"
35+
36+
#define blinnPhongShininessPower 0.215
37+
38+
// An exact conversion from blinn-phong to PBR is impossible, but the look can be approximated perceptually
39+
// and by observing how blinn-phong looks and feels at various settings, although it can never be perfect
40+
// 1) The specularColor can be used as is in the PBR specular flow, just needs to be divided by PI so it sums up to 1 over the hemisphere
41+
// 2) Blinn-phong shininess doesn't stop feeling shiny unless at very low values, like below 0.04
42+
// while the PBR smoothness feels more linear -> map shininess to smoothness accordingly using a function
43+
// that increases very quickly at first then slows down, I went with something like x^(1/4) or x^(1/6) then made the power configurable
44+
// I tried various mappings from the literature but nothing really worked as well as this
45+
// 3) Finally I noticed that some parts still looked very shiny like the AV-R8 winglet while in stock they looked rough thanks a low
46+
// specularColor but high shininess and specularMap, so I multiplied the smoothness by the sqrt of the specularColor and that caps
47+
// the smoothness when specularColor is low
48+
void GetStandardSpecularPropertiesFromLegacy(float legacyShininess, float specularMap, out float3 specular,
49+
out float smoothness)
50+
{
51+
float3 legacySpecularColor = saturate(_SpecColor);
52+
53+
smoothness = pow(legacyShininess, blinnPhongShininessPower) * specularMap;
54+
smoothness *= sqrt(length(legacySpecularColor));
55+
56+
specular = legacySpecularColor * UNITY_INV_PI;
57+
}
58+
59+
float4 _Color;
60+
61+
// LEGACY BLINN-PHONG LIGHTING FUNCTION FOR KSP WITH PBR CONVERSION FOR DEFERRED
62+
63+
inline float4 LightingBlinnPhongKSP(SurfaceOutput s, half3 viewDir, UnityGI gi)
64+
{
65+
return LightingBlinnPhong(s,viewDir, gi);
66+
}
67+
68+
inline float4 LightingBlinnPhongKSP_Deferred(SurfaceOutput s, float3 worldViewDir, UnityGI gi,
69+
out float4 outDiffuseOcclusion, out float4 outSpecSmoothness,
70+
out float4 outNormal)
71+
{
72+
SurfaceOutputStandardSpecular ss;
73+
ss.Albedo = s.Albedo;
74+
ss.Normal = s.Normal;
75+
ss.Emission = s.Emission;
76+
ss.Occlusion = 1;
77+
ss.Alpha = saturate(s.Alpha);
78+
GetStandardSpecularPropertiesFromLegacy(s.Specular, s.Gloss, ss.Specular, ss.Smoothness);
79+
80+
return LightingStandardSpecular_Deferred(ss, worldViewDir, gi, outDiffuseOcclusion, outSpecSmoothness, outNormal);
81+
}
82+
83+
inline void LightingBlinnPhongKSP_GI(inout SurfaceOutput s, UnityGIInput gi_input, inout UnityGI gi)
84+
{
85+
#ifndef UNITY_PASS_DEFERRED
86+
gi = UnityGlobalIllumination(gi_input, 1.0, s.Normal);
87+
#endif
88+
}
89+
90+
// STANDARD UNITY LIGHTING FUNCTION FOR KSP
91+
92+
inline float4 LightingStandardKSP(SurfaceOutputStandard s, float3 worldViewDir, UnityGI gi)
93+
{
94+
return LightingStandard(s, worldViewDir, gi); // no change
95+
}
96+
97+
inline float4 LightingStandardKSP_Deferred(SurfaceOutputStandard s, float3 worldViewDir, UnityGI gi,
98+
out float4 outDiffuseOcclusion,
99+
out float4 outSpecSmoothness, out float4 outNormal)
100+
{
101+
return LightingStandard_Deferred(s, worldViewDir, gi, outDiffuseOcclusion, outSpecSmoothness, outNormal);
102+
}
103+
104+
inline void LightingStandardKSP_GI(inout SurfaceOutputStandard s, UnityGIInput gi_input, inout UnityGI gi)
105+
{
106+
#ifndef UNITY_PASS_DEFERRED
107+
LightingStandard_GI(s, gi_input, gi);
108+
#endif
109+
}
110+
111+
// STANDARD SPECULAR UNITY LIGHTING FUNCTION FOR KSP
112+
113+
inline float4 LightingStandardSpecularKSP(SurfaceOutputStandardSpecular s, float3 worldViewDir, UnityGI gi)
114+
{
115+
return LightingStandardSpecular(s, worldViewDir, gi); // no change
116+
}
117+
118+
inline float4 LightingStandardSpecularKSP_Deferred(SurfaceOutputStandardSpecular s, float3 worldViewDir, UnityGI gi,
119+
out float4 outDiffuseOcclusion,
120+
out float4 outSpecSmoothness, out float4 outNormal)
121+
{
122+
return LightingStandardSpecular_Deferred(s, worldViewDir, gi, outDiffuseOcclusion, outSpecSmoothness, outNormal);
123+
}
124+
125+
inline void LightingStandardSpecularKSP_GI(inout SurfaceOutputStandardSpecular s, UnityGIInput gi_input,
126+
inout UnityGI gi)
127+
{
128+
#ifndef UNITY_PASS_DEFERRED
129+
LightingStandardSpecular_GI(s, gi_input, gi);
130+
#endif
131+
}
132+
133+
float4 _LocalCameraPos;
134+
float4 _LocalCameraDir;
135+
float4 _UnderwaterFogColor;
136+
float _UnderwaterMinAlphaFogDistance;
137+
float _UnderwaterMaxAlbedoFog;
138+
float _UnderwaterMaxAlphaFog;
139+
float _UnderwaterAlbedoDistanceScalar;
140+
float _UnderwaterAlphaDistanceScalar;
141+
float _UnderwaterFogFactor;
142+
143+
float4 UnderwaterFog(float3 worldPos, float3 color)
144+
{
145+
// skip fog in deferred mode
146+
#ifdef UNITY_PASS_DEFERRED
147+
return float4(color, 1);
148+
#endif
149+
150+
float3 toPixel = worldPos - _LocalCameraPos.xyz;
151+
float toPixelLength = length(toPixel); ///< Comment out the math--looks better without it.
152+
153+
float underwaterDetection = _UnderwaterFogFactor * _LocalCameraDir.w; ///< sign(1 - sign(_LocalCameraPos.w));
154+
float albedoLerpValue = underwaterDetection * (_UnderwaterMaxAlbedoFog * saturate(
155+
toPixelLength * _UnderwaterAlbedoDistanceScalar));
156+
float alphaFactor = 1 - underwaterDetection * (_UnderwaterMaxAlphaFog * saturate(
157+
(toPixelLength - _UnderwaterMinAlphaFogDistance) * _UnderwaterAlphaDistanceScalar));
158+
159+
return float4(lerp(color, _UnderwaterFogColor.rgb, albedoLerpValue), alphaFactor);
160+
}
161+
162+
#endif

Unity/Shaders/NormalFromTexture.shader

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
Stencil
1616
{
17-
Ref 4
17+
Ref 3
1818
Comp Always
1919
Pass Replace
2020
}

0 commit comments

Comments
 (0)