diff --git a/Assets/Shaders/DecalFeatureBumped.shader b/Assets/Shaders/DecalFeatureBumped.shader index dfa8b6d..f451987 100644 --- a/Assets/Shaders/DecalFeatureBumped.shader +++ b/Assets/Shaders/DecalFeatureBumped.shader @@ -22,8 +22,8 @@ Shader "ConformalDecals/Feature/Bumped" Blend SrcAlpha OneMinusSrcAlpha CGPROGRAM - #pragma vertex vert_forward_base - #pragma fragment frag_forward_base + #pragma vertex vert_forward + #pragma fragment frag_forward #pragma multi_compile_fwdbase nolightmap nodirlightmap nodynlightmap @@ -62,6 +62,53 @@ Shader "ConformalDecals/Feature/Bumped" ENDCG } + Pass + { + Name "FORWARD" + Tags { "LightMode" = "ForwardAdd" } + Blend One One + + CGPROGRAM + #pragma vertex vert_forward + #pragma fragment frag_forward + + #pragma multi_compile_fwdadd nolightmap nodirlightmap nodynlightmap + + #include "UnityCG.cginc" + #include "Lighting.cginc" + #include "AutoLight.cginc" + #include "LightingKSP.cginc" + #include "DecalsCommon.cginc" + + sampler2D _Decal; + sampler2D _DecalBumpMap; + + float _Cutoff; + float _Opacity; + + void surf (DecalSurfaceInput IN, inout SurfaceOutput o) + { + fixed4 projUV = UNITY_PROJ_COORD(IN.uv_decal); + + // since I cant easily affect the clamping mode in KSP, do it here + clip(projUV.xyz); + clip(1-projUV.xyz); + + // clip backsides + clip(dot(_DecalNormal, IN.normal)); + + float4 color = tex2D(_Decal, projUV); + float3 normal = UnpackNormal(tex2D(_DecalBumpMap, projUV)); + //clip(color.a - _Cutoff); + + o.Normal = normal; + o.Albedo = color.rgb; + o.Alpha = color.a * _Opacity; + } + + ENDCG + } + // shadow casting support UsePass "Legacy Shaders/VertexLit/SHADOWCASTER" } diff --git a/Assets/Shaders/DecalsCommon.cginc b/Assets/Shaders/DecalsCommon.cginc index bb65f50..f8e57a1 100644 --- a/Assets/Shaders/DecalsCommon.cginc +++ b/Assets/Shaders/DecalsCommon.cginc @@ -25,14 +25,25 @@ struct v2f UNITY_POSITION(pos); float3 normal : NORMAL; float4 uv_decal : TEXCOORD0; + #ifdef DECAL_BASE_NORMAL float2 uv_base : TEXCOORD1; #endif //DECAL_BASE_NORMAL + float4 tSpace0 : TEXCOORD2; float4 tSpace1 : TEXCOORD3; float4 tSpace2 : TEXCOORD4; - fixed3 vlight : TEXCOORD5; - UNITY_SHADOW_COORDS(6) + + #ifdef UNITY_PASS_FORWARDBASE + fixed3 vlight : TEXCOORD5; + UNITY_SHADOW_COORDS(6) + #endif //UNITY_PASS_FORWARDBASE + + #ifdef UNITY_PASS_FORWARDADD + UNITY_LIGHTING_COORDS(5,6) + #endif + + }; // Projection matrix, normal, and tangent vectors @@ -44,7 +55,7 @@ float3 _DecalTangent; // this must be defined in any shader using this cginc void surf (DecalSurfaceInput IN, inout SurfaceOutput o); -v2f vert_forward_base(appdata_decal v) +v2f vert_forward(appdata_decal v) { v2f o; UNITY_INITIALIZE_OUTPUT(v2f,o); @@ -59,7 +70,6 @@ v2f vert_forward_base(appdata_decal v) float3 worldPos = mul(unity_ObjectToWorld, v.vertex).xyz; float3 worldNormal = UnityObjectToWorldNormal(v.normal); - //fixed3 worldTangent = fixed3(0,0,0);//UnityObjectToWorldDir(v.tangent.xyz); fixed3 worldTangent = UnityObjectToWorldDir(_DecalTangent); fixed3 worldBinormal = cross(worldTangent, worldNormal); worldTangent = cross(worldNormal, worldBinormal); @@ -67,28 +77,32 @@ v2f vert_forward_base(appdata_decal v) o.tSpace1 = float4(worldTangent.y, worldBinormal.y, worldNormal.y, worldPos.y); o.tSpace2 = float4(worldTangent.z, worldBinormal.z, worldNormal.z, worldPos.z); - // SH/ambient light - #if UNITY_SHOULD_SAMPLE_SH - float3 shlight = ShadeSH9 (float4(worldNormal,1.0)); - o.vlight = shlight; - #else - o.vlight = 0.0; - #endif // UNITY_SHOULD_SAMPLE_SH + // forward base pass specific lighting code + #ifdef UNITY_PASS_FORWARDBASE + // SH/ambient light + #if UNITY_SHOULD_SAMPLE_SH + float3 shlight = ShadeSH9 (float4(worldNormal,1.0)); + o.vlight = shlight; + #else + o.vlight = 0.0; + #endif // UNITY_SHOULD_SAMPLE_SH - // vertex light - #ifdef VERTEXLIGHT_ON - o.vlight += Shade4PointLights ( - unity_4LightPosX0, unity_4LightPosY0, unity_4LightPosZ0, - unity_LightColor[0].rgb, unity_LightColor[1].rgb, unity_LightColor[2].rgb, unity_LightColor[3].rgb, - unity_4LightAtten0, worldPos, worldNormal ); - #endif // VERTEXLIGHT_ON + // vertex light + #ifdef VERTEXLIGHT_ON + o.vlight += Shade4PointLights ( + unity_4LightPosX0, unity_4LightPosY0, unity_4LightPosZ0, + unity_LightColor[0].rgb, unity_LightColor[1].rgb, unity_LightColor[2].rgb, unity_LightColor[3].rgb, + unity_4LightAtten0, worldPos, worldNormal ); + #endif // VERTEXLIGHT_ON + #endif // UNITY_PASS_FORWARDBASE - UNITY_TRANSFER_LIGHTING(o, 0.0); // pass shadow and, possibly, light cookie coordinates to pixel shader + // pass shadow and, possibly, light cookie coordinates to pixel shader + UNITY_TRANSFER_LIGHTING(o, 0.0); return o; } -fixed4 frag_forward_base(v2f IN) : SV_Target +fixed4 frag_forward(v2f IN) : SV_Target { DecalSurfaceInput i; SurfaceOutput o; @@ -98,7 +112,7 @@ fixed4 frag_forward_base(v2f IN) : SV_Target float3 worldPos = float3(IN.tSpace0.w, IN.tSpace1.w, IN.tSpace2.w); float3 worldTan = float3(IN.tSpace0.x, IN.tSpace1.x, IN.tSpace2.x); - + #ifndef USING_DIRECTIONAL_LIGHT fixed3 lightDir = normalize(UnityWorldSpaceLightDir(worldPos)); #else @@ -135,11 +149,19 @@ fixed4 frag_forward_base(v2f IN) : SV_Target worldN = normalize(worldN); o.Normal = worldN; - //KSP lighting function c += LightingBlinnPhongSmooth(o, lightDir, viewDir, atten); - c.rgb += o.Emission; - c.rgb += o.Albedo * IN.vlight; + + #ifdef UNITY_PASS_FORWARDBASE + c.rgb += o.Emission; + c.rgb += o.Albedo * IN.vlight; + #endif //UNITY_PASS_FORWARDBASE + + #ifdef UNITY_PASS_FORWARDADD + c.rgb *= c.a; + c.a = 0.0; + #endif + return c; }