From 0393ab02635441701b428613d3bc82017267656f Mon Sep 17 00:00:00 2001 From: drewcassidy Date: Mon, 8 Jun 2020 00:03:26 -0700 Subject: [PATCH] Decal shader changes, try to fix lighting additive pass --- Assets/Shaders/DecalsCommon.cginc | 31 ++++++++++++++----- .../ConformalDecals/Assets/decal-blank.mu | 2 +- .../Resources/conformaldecals.shab | 4 +-- 3 files changed, 27 insertions(+), 10 deletions(-) diff --git a/Assets/Shaders/DecalsCommon.cginc b/Assets/Shaders/DecalsCommon.cginc index 128e67d..1f8994b 100644 --- a/Assets/Shaders/DecalsCommon.cginc +++ b/Assets/Shaders/DecalsCommon.cginc @@ -79,6 +79,22 @@ inline void decalClipAlpha(float alpha) { #endif } +// modifed version of the KSP BlinnPhong because it does some weird things +inline fixed4 LightingBlinnPhongDecal(SurfaceOutput s, fixed3 lightDir, half3 viewDir, fixed atten) +{ + s.Normal = normalize(s.Normal); + half3 h = normalize(lightDir + viewDir); + + fixed diff = max(0, dot(s.Normal, lightDir)); + + float nh = max(0, dot(s.Normal, h)); + float spec = pow(nh, s.Specular*128.0) * s.Gloss; + + fixed4 c = 0; + c.rgb = (s.Albedo * _LightColor0.rgb * diff + _LightColor0.rgb * _SpecColor.rgb * spec) * (atten); + return c; +} + // declare surf function, // this must be defined in any shader using this cginc void surf (DecalSurfaceInput IN, inout SurfaceOutput o); @@ -221,7 +237,9 @@ fixed4 frag_forward(v2f IN) : SV_Target surf(i, o); #ifdef DECAL_PREVIEW - o.Albedo = lerp(_Color.rgb,o.Albedo, o.Alpha); + if (any(IN.uv_decal > 1) || any(IN.uv_decal < 0)) o.Alpha = 0; + + o.Albedo = lerp(_Color.rgb, o.Albedo, o.Alpha); o.Normal = lerp(float3(0,0,1), o.Normal, o.Alpha); o.Gloss = lerp(_Color.a, o.Gloss, o.Alpha); o.Emission = lerp(0, o.Emission, o.Alpha); @@ -239,21 +257,20 @@ fixed4 frag_forward(v2f IN) : SV_Target WorldNormal = normalize(WorldNormal); o.Normal = WorldNormal; - //KSP lighting function - c += LightingBlinnPhongSmooth(o, lightDir, worldViewDir, atten); + //call modified KSP lighting function + c += LightingBlinnPhongDecal(o, lightDir, worldViewDir, atten); // Forward base emission and ambient/vertex lighting #ifdef UNITY_PASS_FORWARDBASE c.rgb += o.Emission; c.rgb += o.Albedo * IN.vlight; - c.a = saturate(c.a); + c.a = o.Alpha; #endif //UNITY_PASS_FORWARDBASE // Forward add multiply by alpha #ifdef UNITY_PASS_FORWARDADD - c.rgb *= c.a; - c.a = 0.0; - #endif + c.rgb *= o.Alpha; + #endif return c; } diff --git a/Distribution/GameData/ConformalDecals/Assets/decal-blank.mu b/Distribution/GameData/ConformalDecals/Assets/decal-blank.mu index ea50654..5a0984f 100644 --- a/Distribution/GameData/ConformalDecals/Assets/decal-blank.mu +++ b/Distribution/GameData/ConformalDecals/Assets/decal-blank.mu @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:41e91c40e21638fcf49f2c5cede4acf1d0b6706fc8b1ac1d7211edc47fb6635a +oid sha256:9d7714407c3b6b0bde5cd830e9789f837d44310db16272c6b42299e9af9f2907 size 3273 diff --git a/Distribution/GameData/ConformalDecals/Resources/conformaldecals.shab b/Distribution/GameData/ConformalDecals/Resources/conformaldecals.shab index 6ed0ecd..634aa4d 100644 --- a/Distribution/GameData/ConformalDecals/Resources/conformaldecals.shab +++ b/Distribution/GameData/ConformalDecals/Resources/conformaldecals.shab @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:680b0c8662bace934a133136cdb52efd484afe1e22806b36d68796a89cdf153a -size 123968 +oid sha256:6f9ec40c787677e390877fe37b5957e06b8ca2caea690c64f96789bfe70ccd2d +size 126198