BFX_Decal.shader 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. Shader "KriptoFX/BFX/BFX_Decal"
  2. {
  3. Properties
  4. {
  5. [HDR] _TintColor("Tint Color", Color) = (1,1,1,1)
  6. _MainTex("NormalAlpha", 2D) = "white" {}
  7. _LookupFade("Lookup Fade Texture", 2D) = "white" {}
  8. _Cutout("Cutout", Range(0, 1)) = 1
  9. _CutoutTex("CutoutDepth(XZ)", 2D) = "white" {}
  10. [Space]
  11. _SunPos("Sun Pos", Vector) = (1, 0.5, 1, 0)
  12. //[Toggle(CLAMP_SIDE_SURFACE)] _ClampSideSurface("Clamp side surface", Int) = 0
  13. }
  14. SubShader
  15. {
  16. Tags{ "Queue" = "AlphaTest"}
  17. Blend DstColor SrcColor
  18. //Blend SrcAlpha OneMinusSrcAlpha
  19. Cull Front
  20. ZTest Always
  21. ZWrite Off
  22. Pass
  23. {
  24. CGPROGRAM
  25. #pragma vertex vert
  26. #pragma fragment frag
  27. #pragma multi_compile_fog
  28. #pragma multi_compile_instancing
  29. #pragma multi_compile _ USE_CUSTOM_DECAL_LAYERS
  30. #pragma shader_feature CLAMP_SIDE_SURFACE
  31. #pragma multi_compile _ USE_CUSTOM_DECAL_LAYERS_IGNORE_MODE
  32. #include "UnityCG.cginc"
  33. sampler2D _MainTex;
  34. sampler2D _Flowmap;
  35. sampler2D _LookupFade;
  36. sampler2D _CutoutTex;
  37. float4 _MainTex_ST;
  38. float4 _MainTex_NextFrame;
  39. float4 _CutoutTex_ST;
  40. UNITY_INSTANCING_BUFFER_START(Props)
  41. UNITY_DEFINE_INSTANCED_PROP(half4, _TintColor)
  42. UNITY_DEFINE_INSTANCED_PROP(half, _Cutout)
  43. UNITY_DEFINE_INSTANCED_PROP(float, _LightIntencity)
  44. UNITY_INSTANCING_BUFFER_END(Props)
  45. half4 _CutoutColor;
  46. half4 _FresnelColor;
  47. half4 _DistortionSpeedScale;
  48. UNITY_DECLARE_DEPTH_TEXTURE(_CameraDepthTexture);
  49. UNITY_DECLARE_DEPTH_TEXTURE(_LayerDecalDepthTexture);
  50. half InterpolationValue;
  51. half _AlphaPow;
  52. half _DistortSpeed;
  53. half _DistortScale;
  54. float4 _SunPos;
  55. half _DepthMul;
  56. half3 _DecalForwardDir;
  57. struct appdata_t {
  58. float4 vertex : POSITION;
  59. float4 normal : NORMAL;
  60. half4 color : COLOR;
  61. UNITY_VERTEX_INPUT_INSTANCE_ID
  62. };
  63. struct v2f {
  64. float4 vertex : SV_POSITION;
  65. half4 color : COLOR;
  66. float4 screenUV : TEXCOORD0;
  67. float4 ray : TEXCOORD1;
  68. float3 rayCameraOffset : TEXCOORD2;
  69. float3 viewDir : TEXCOORD3;
  70. float4 screenPos : TEXCOORD4;
  71. UNITY_FOG_COORDS(5)
  72. UNITY_VERTEX_INPUT_INSTANCE_ID
  73. UNITY_VERTEX_OUTPUT_STEREO
  74. };
  75. v2f vert(appdata_t v)
  76. {
  77. v2f o;
  78. UNITY_SETUP_INSTANCE_ID(v);
  79. UNITY_TRANSFER_INSTANCE_ID(v, o);
  80. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
  81. o.vertex = UnityObjectToClipPos(v.vertex);
  82. o.color = v.color;
  83. o.screenUV = ComputeScreenPos(o.vertex);
  84. o.viewDir = normalize(ObjSpaceViewDir(v.vertex));
  85. o.screenPos = ComputeGrabScreenPos(o.vertex);
  86. UNITY_TRANSFER_FOG(o,o.vertex);
  87. float3 viewRay = UnityObjectToViewPos(v.vertex).xyz;
  88. o.ray.w = viewRay.z;
  89. viewRay *= -1;
  90. o.ray.xyz = mul((float3x3)mul(unity_WorldToObject, UNITY_MATRIX_I_V), viewRay);
  91. o.rayCameraOffset = mul(mul(unity_WorldToObject, UNITY_MATRIX_I_V), float4(0, 0, 0, 1)).xyz;
  92. return o;
  93. }
  94. half4 frag(v2f i) : SV_Target
  95. {
  96. UNITY_SETUP_INSTANCE_ID(i);
  97. #if USE_CUSTOM_DECAL_LAYERS
  98. float depth = SAMPLE_DEPTH_TEXTURE(_LayerDecalDepthTexture, (i.screenUV.xy / i.screenUV.w));
  99. float depthMask = SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, (i.screenUV.xy / i.screenUV.w));
  100. float fade = depth < depthMask - 0.0005 ? 0 : 1;
  101. #if USE_CUSTOM_DECAL_LAYERS_IGNORE_MODE
  102. fade = 1 - fade;
  103. depth = depthMask;
  104. #endif
  105. #else
  106. float depth = SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, (i.screenUV.xy / i.screenUV.w));
  107. #endif
  108. i.ray /= i.ray.w;
  109. float zEye = LinearEyeDepth(depth);
  110. float3 decalSpaceScenePos = i.rayCameraOffset + i.ray.xyz * zEye;
  111. float3 stepVal = saturate((0.5 - abs(decalSpaceScenePos.xyz)) * 10000);
  112. half lookupHeight = tex2D(_LookupFade, float2(decalSpaceScenePos.y + 0.5, 0));
  113. float projClipFade = stepVal.x * stepVal.y * stepVal.z * lookupHeight;
  114. #if USE_CUSTOM_DECAL_LAYERS
  115. projClipFade *= fade;
  116. #endif
  117. #if CLAMP_SIDE_SURFACE
  118. #ifdef UNITY_UV_STARTS_AT_TOP
  119. half3 n = normalize(cross(ddx(decalSpaceScenePos), ddy(decalSpaceScenePos) * _ProjectionParams.x));
  120. #else
  121. half3 n = normalize(cross(ddx(decalSpaceScenePos), -ddy(decalSpaceScenePos) * _ProjectionParams.x));
  122. #endif
  123. half angle = abs(dot(n, _DecalForwardDir));
  124. angle = angle > 0.1 ? 1 : 0;
  125. projClipFade *= angle;
  126. //return float4(saturate(angle), 0, 0, 1);
  127. #endif
  128. float2 uv = decalSpaceScenePos.xz + 0.5;
  129. float2 uvMain = uv * _MainTex_ST.xy + _MainTex_ST.zw;
  130. float2 uvCutout = (decalSpaceScenePos.xz + 0.5) * _CutoutTex_ST.xy + _CutoutTex_ST.zw;
  131. half4 normAlpha = tex2D(_MainTex, uvMain);
  132. half4 res = 0;
  133. res.a = saturate(normAlpha.w * 2);
  134. if (res.a < 0.1 || projClipFade < 0.1) discard;
  135. normAlpha.xy = normAlpha.xy * 2 - 1;
  136. float3 normal = normalize(float3(normAlpha.x, 1, normAlpha.y));
  137. half3 mask = tex2D(_CutoutTex, uvCutout).xyz;
  138. half cutout = 0.5 + UNITY_ACCESS_INSTANCED_PROP(Props, _Cutout) * i.color.a * 0.5;
  139. half alphaMask = saturate((mask.r - (cutout * 2 - 1)) * 20) * res.a;
  140. half colorMask = saturate((mask.r - (cutout * 2 - 1)) * 5) * res.a;
  141. res.a = alphaMask;
  142. res.a = saturate(res.a * projClipFade);
  143. float intencity = UNITY_ACCESS_INSTANCED_PROP(Props, _LightIntencity);
  144. float light = max(0.001, dot(normal, normalize(_SunPos.xyz)));
  145. light = pow(light, 150) * 3 * intencity;
  146. light *= (1 - mask.z * colorMask);
  147. float4 tintColor = UNITY_ACCESS_INSTANCED_PROP(Props, _TintColor);
  148. #if !UNITY_COLORSPACE_GAMMA
  149. tintColor = tintColor * 1.35;
  150. #endif
  151. res.rgb = lerp(tintColor.rgb, tintColor.rgb * 0.25, mask.z * colorMask) + light;
  152. half fresnel = (1 - dot(normal, normalize(i.viewDir)));
  153. fresnel = pow(fresnel + 0.1, 5);
  154. UNITY_APPLY_FOG_COLOR(i.fogCoord, res, half4(1, 1, 1, 1));
  155. return lerp(0.5, res, res.a * tintColor.a);
  156. return res;
  157. }
  158. ENDCG
  159. }
  160. }
  161. }