BFX_Blood.shader 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. Shader "KriptoFX/BFX/BFX_Blood"
  2. {
  3. Properties
  4. {
  5. _Color("Color", Color) = (1,1,1,1)
  6. _boundingMax("Bounding Max", Float) = 1.0
  7. _boundingMin("Bounding Min", Float) = 1.0
  8. _numOfFrames("Number Of Frames", int) = 240
  9. _speed("Speed", Float) = 0.33
  10. _HeightOffset("_Height Offset", Vector) = (0, 0, 0)
  11. //[MaterialToggle] _pack_normal("Pack Normal", Float) = 0
  12. _posTex("Position Map (RGB)", 2D) = "white" {}
  13. _nTex("Normal Map (RGB)", 2D) = "grey" {}
  14. _SunPos("Sun Pos", Vector) = (1, 0.5, 1, 0)
  15. }
  16. SubShader
  17. {
  18. Tags{ "Queue" = "AlphaTest+1"}
  19. Blend SrcAlpha OneMinusSrcAlpha
  20. Cull Back
  21. ZWrite On
  22. Pass
  23. {
  24. CGPROGRAM
  25. #pragma vertex vert
  26. #pragma fragment frag
  27. #pragma multi_compile_instancing
  28. #pragma multi_compile_fog
  29. #include "UnityCG.cginc"
  30. struct appdata
  31. {
  32. float4 vertex : POSITION;
  33. float2 uv : TEXCOORD0;
  34. float4 tangent : TEXCOORD2;
  35. UNITY_VERTEX_INPUT_INSTANCE_ID
  36. };
  37. struct v2f
  38. {
  39. float2 uv : TEXCOORD0;
  40. float4 pos : SV_POSITION;
  41. float3 worldNormal : TEXCOORD2;
  42. float4 screenPos : TEXCOORD4;
  43. float3 viewDir : TEXCOORD5;
  44. float height : TEXCOORD6;
  45. UNITY_VERTEX_INPUT_INSTANCE_ID
  46. UNITY_VERTEX_OUTPUT_STEREO
  47. };
  48. sampler2D _GrabTexture;
  49. sampler2D _posTex;
  50. sampler2D _nTex;
  51. uniform float _boundingMax;
  52. uniform float _boundingMin;
  53. uniform float _speed;
  54. uniform int _numOfFrames;
  55. half4 _Color;
  56. float4 _HeightOffset;
  57. float _HDRFix;
  58. float4 _SunPos;
  59. UNITY_INSTANCING_BUFFER_START(Props)
  60. UNITY_DEFINE_INSTANCED_PROP(float, _UseCustomTime)
  61. UNITY_DEFINE_INSTANCED_PROP(float, _TimeInFrames)
  62. UNITY_DEFINE_INSTANCED_PROP(float, _LightIntencity)
  63. UNITY_INSTANCING_BUFFER_END(Props)
  64. v2f vert (appdata v)
  65. {
  66. v2f o;
  67. UNITY_INITIALIZE_OUTPUT(v2f, o);
  68. UNITY_SETUP_INSTANCE_ID(v);
  69. UNITY_TRANSFER_INSTANCE_ID(v, o);
  70. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
  71. float timeInFrames;
  72. float currentSpeed = 1.0f / (_numOfFrames / _speed);
  73. timeInFrames = UNITY_ACCESS_INSTANCED_PROP(Props, _UseCustomTime) > 0.5 ? UNITY_ACCESS_INSTANCED_PROP(Props, _TimeInFrames) : 1;
  74. float4 texturePos = tex2Dlod(_posTex, float4(v.uv.x, (timeInFrames + v.uv.y), 0, 0));
  75. float3 textureN = tex2Dlod(_nTex, float4(v.uv.x, (timeInFrames + v.uv.y), 0, 0));
  76. #if !UNITY_COLORSPACE_GAMMA
  77. texturePos.xyz = LinearToGammaSpace(texturePos.xyz);
  78. textureN = LinearToGammaSpace(textureN);
  79. #endif
  80. float expand = _boundingMax - _boundingMin;
  81. texturePos.xyz *= expand;
  82. texturePos.xyz += _boundingMin;
  83. texturePos.x *= -1;
  84. v.vertex.xyz = texturePos.xzy;
  85. v.vertex.xyz += _HeightOffset.xyz;
  86. o.worldNormal = textureN.xzy * 2 - 1;
  87. o.worldNormal.x *= -1;
  88. o.viewDir = ObjSpaceViewDir(v.vertex);
  89. o.pos = UnityObjectToClipPos(v.vertex);
  90. o.screenPos = ComputeGrabScreenPos(o.pos);
  91. return o;
  92. }
  93. half4 frag(v2f i) : SV_Target
  94. {
  95. UNITY_SETUP_INSTANCE_ID(i);
  96. i.worldNormal = normalize(i.worldNormal);
  97. i.viewDir = normalize(i.viewDir);
  98. half fresnel = saturate(1 - dot(i.worldNormal, i.viewDir));
  99. half intencity = UNITY_ACCESS_INSTANCED_PROP(Props, _LightIntencity);
  100. half3 grabColor = intencity * 0.75;
  101. half light = max(0.001, dot(normalize(i.worldNormal), normalize(_SunPos.xyz)));
  102. light = pow(light, 50) * 10;
  103. #if !UNITY_COLORSPACE_GAMMA
  104. _Color.rgb = _Color.rgb * .65;
  105. fresnel = fresnel * fresnel;
  106. #endif
  107. grabColor *= _Color.rgb;
  108. grabColor = lerp(grabColor * 0.15, grabColor, fresnel);
  109. grabColor = min(grabColor, _Color.rgb * 0.55);
  110. half3 color = grabColor.xyz + saturate(light) * intencity;
  111. return half4(color, _Color.a);
  112. }
  113. ENDCG
  114. }
  115. //you can optimize it by removing shadow rendering and depth writing
  116. //start remove line
  117. Pass
  118. {
  119. Tags {"LightMode" = "ShadowCaster"}
  120. CGPROGRAM
  121. #pragma vertex vert
  122. #pragma fragment frag
  123. #pragma multi_compile_shadowcaster
  124. #pragma multi_compile_instancing
  125. #include "UnityCG.cginc"
  126. sampler2D _GrabTexture;
  127. sampler2D _posTex;
  128. sampler2D _nTex;
  129. uniform float _boundingMax;
  130. uniform float _boundingMin;
  131. uniform float _speed;
  132. uniform int _numOfFrames;
  133. half4 _Color;
  134. float4 _HeightOffset;
  135. UNITY_INSTANCING_BUFFER_START(Props)
  136. UNITY_DEFINE_INSTANCED_PROP(float, _TimeInFrames)
  137. UNITY_INSTANCING_BUFFER_END(Props)
  138. struct appdata
  139. {
  140. float2 uv : TEXCOORD0;
  141. float4 vertex : POSITION;
  142. float3 normal : NORMAL;
  143. UNITY_VERTEX_INPUT_INSTANCE_ID
  144. };
  145. struct v2f {
  146. V2F_SHADOW_CASTER;
  147. UNITY_VERTEX_INPUT_INSTANCE_ID
  148. UNITY_VERTEX_OUTPUT_STEREO
  149. };
  150. v2f vert(appdata v)
  151. {
  152. v2f o;
  153. UNITY_INITIALIZE_OUTPUT(v2f, o);
  154. UNITY_SETUP_INSTANCE_ID(v);
  155. UNITY_TRANSFER_INSTANCE_ID(v, o);
  156. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
  157. float timeInFrames;
  158. float currentSpeed = 1.0f / (_numOfFrames / _speed);
  159. timeInFrames = UNITY_ACCESS_INSTANCED_PROP(Props, _TimeInFrames);
  160. float4 texturePos = tex2Dlod(_posTex, float4(v.uv.x, (timeInFrames + v.uv.y), 0, 0));
  161. #if !UNITY_COLORSPACE_GAMMA
  162. texturePos.xyz = LinearToGammaSpace(texturePos.xyz);
  163. #endif
  164. float expand = _boundingMax - _boundingMin;
  165. texturePos.xyz *= expand;
  166. texturePos.xyz += _boundingMin;
  167. texturePos.x *= -1;
  168. v.vertex.xyz = texturePos.xzy;
  169. v.vertex.xyz += _HeightOffset.xyz;
  170. TRANSFER_SHADOW_CASTER_NORMALOFFSET(o)
  171. return o;
  172. }
  173. float4 frag(v2f i) : SV_Target
  174. {
  175. UNITY_SETUP_INSTANCE_ID(i);
  176. SHADOW_CASTER_FRAGMENT(i)
  177. }
  178. ENDCG
  179. }
  180. //end remove light
  181. }
  182. }