DistanceFade-Add.shader 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // Unity built-in shader source. Copyright (c) 2016 Unity Technologies. MIT license (see license.txt)
  2. Shader "Mirza Beig/Particles/Distance Fade/Additive"
  3. {
  4. Properties
  5. {
  6. _TintColor("Tint Color", Color) = (0.5,0.5,0.5,0.5)
  7. _MainTex("Particle Texture", 2D) = "white" {}
  8. _InvFade("Soft Particles Factor", Range(0.01,3.0)) = 1.0
  9. _FadeMin("Fade Min", Range(0.0, 10.0)) = 1.0
  10. _FadeMax("Fade Max", Range(0.0, 10.0)) = 2.0
  11. }
  12. Category {
  13. Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" "PreviewType"="Plane" }
  14. Blend SrcAlpha One
  15. ColorMask RGB
  16. Cull Off Lighting Off ZWrite Off
  17. SubShader {
  18. Pass {
  19. CGPROGRAM
  20. #pragma vertex vert
  21. #pragma fragment frag
  22. #pragma target 2.0
  23. #pragma multi_compile_particles
  24. #pragma multi_compile_fog
  25. #include "UnityCG.cginc"
  26. sampler2D _MainTex;
  27. fixed4 _TintColor;
  28. struct appdata_t {
  29. float4 vertex : POSITION;
  30. fixed4 color : COLOR;
  31. float2 texcoord : TEXCOORD0;
  32. UNITY_VERTEX_INPUT_INSTANCE_ID
  33. };
  34. struct v2f {
  35. float4 vertex : SV_POSITION;
  36. fixed4 color : COLOR;
  37. float2 texcoord : TEXCOORD0;
  38. UNITY_FOG_COORDS(1)
  39. #ifdef SOFTPARTICLES_ON
  40. float4 projPos : TEXCOORD2;
  41. #endif
  42. UNITY_VERTEX_OUTPUT_STEREO
  43. };
  44. float4 _MainTex_ST;
  45. float _FadeMin;
  46. float _FadeMax;
  47. v2f vert (appdata_t v)
  48. {
  49. v2f o;
  50. UNITY_SETUP_INSTANCE_ID(v);
  51. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
  52. o.vertex = UnityObjectToClipPos(v.vertex);
  53. #ifdef SOFTPARTICLES_ON
  54. o.projPos = ComputeScreenPos (o.vertex);
  55. COMPUTE_EYEDEPTH(o.projPos.z);
  56. #endif
  57. o.color = v.color;
  58. float4 viewPos = mul(UNITY_MATRIX_MV, v.vertex);
  59. float distance = -viewPos.z - _ProjectionParams.y;
  60. float alpha = (distance - _FadeMin) / _FadeMax;
  61. alpha = min(alpha, 1.0);
  62. o.color.a *= alpha;
  63. o.texcoord = TRANSFORM_TEX(v.texcoord,_MainTex);
  64. UNITY_TRANSFER_FOG(o,o.vertex);
  65. return o;
  66. }
  67. UNITY_DECLARE_DEPTH_TEXTURE(_CameraDepthTexture);
  68. float _InvFade;
  69. fixed4 frag (v2f i) : SV_Target
  70. {
  71. #ifdef SOFTPARTICLES_ON
  72. float sceneZ = LinearEyeDepth (SAMPLE_DEPTH_TEXTURE_PROJ(_CameraDepthTexture, UNITY_PROJ_COORD(i.projPos)));
  73. float partZ = i.projPos.z;
  74. float fade = saturate (_InvFade * (sceneZ-partZ));
  75. i.color.a *= fade;
  76. #endif
  77. fixed4 col = 2.0f * i.color * _TintColor * tex2D(_MainTex, i.texcoord);
  78. UNITY_APPLY_FOG_COLOR(i.fogCoord, col, fixed4(0,0,0,0)); // fog towards black due to our blend mode
  79. return col;
  80. }
  81. ENDCG
  82. }
  83. }
  84. }
  85. }