RainShader.shader 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. //
  2. // Rain Maker (c) 2015 Digital Ruby, LLC
  3. // http://www.digitalruby.com
  4. //
  5. Shader "Custom/RainShader"
  6. {
  7. Properties
  8. {
  9. _MainTex ("Color (RGB) Alpha (A)", 2D) = "gray" {}
  10. _TintColor ("Tint Color (RGB)", Color) = (1, 1, 1, 1)
  11. _PointSpotLightMultiplier ("Point/Spot Light Multiplier", Range (0, 10)) = 2
  12. _DirectionalLightMultiplier ("Directional Light Multiplier", Range (0, 10)) = 1
  13. _InvFade ("Soft Particles Factor", Range(0.01, 100.0)) = 1.0
  14. _AmbientLightMultiplier ("Ambient light multiplier", Range(0, 1)) = 0.25
  15. }
  16. SubShader
  17. {
  18. Tags { "QUEUE"="Transparent" "IGNOREPROJECTOR"="true" "RenderType"="Transparent" "LightMode"="Vertex" }
  19. LOD 100
  20. Pass
  21. {
  22. ZWrite Off
  23. Cull Back
  24. Lighting On
  25. AlphaTest Greater 0.01
  26. ColorMask RGB
  27. Blend SrcAlpha OneMinusSrcAlpha
  28. CGPROGRAM
  29. #pragma multi_compile_particles
  30. #pragma vertex vert
  31. #pragma fragment frag
  32. #pragma fragmentoption ARB_precision_hint_fastest
  33. #include "UnityCG.cginc"
  34. #include "Lighting.cginc"
  35. fixed4 _TintColor;
  36. float _DirectionalLightMultiplier;
  37. float _PointSpotLightMultiplier;
  38. float _AmbientLightMultiplier;
  39. #if defined(SOFTPARTICLES_ON)
  40. float _InvFade;
  41. #endif
  42. struct appdata_t
  43. {
  44. float4 vertex : POSITION;
  45. fixed4 color : COLOR;
  46. float2 texcoord : TEXCOORD0;
  47. };
  48. struct v2f
  49. {
  50. half2 uv_MainTex : TEXCOORD0;
  51. fixed4 color : COLOR0;
  52. float4 pos : SV_POSITION;
  53. #if defined(SOFTPARTICLES_ON)
  54. float4 projPos : TEXCOORD1;
  55. #endif
  56. };
  57. float3 ApplyLight(int index, float3 lightColor, float3 viewPos)
  58. {
  59. fixed3 currentLightColor = unity_LightColor[index].rgb;
  60. float4 lightPos = unity_LightPosition[index];
  61. if (lightPos.w == 0)
  62. {
  63. // directional light, the lightPos is actually the direction of the light
  64. // for some weird reason, Unity seems to change the directional light position based on the vertex,
  65. // this hack seems to compensate for that
  66. lightPos = mul(lightPos, UNITY_MATRIX_V);
  67. // depending on how the directional light is pointing, reduce the intensity (which goes to 0 as it goes below the horizon)
  68. fixed multiplier = clamp((lightPos.y * 2) + 1, 0, 1);
  69. return lightColor + (currentLightColor * multiplier * _DirectionalLightMultiplier);
  70. }
  71. else
  72. {
  73. float3 toLight = lightPos.xyz - viewPos;
  74. fixed lengthSq = dot(toLight, toLight);
  75. fixed atten = 1.0 / (1.0 + (lengthSq * unity_LightAtten[index].z));
  76. return lightColor + (currentLightColor * atten * _PointSpotLightMultiplier);
  77. }
  78. }
  79. fixed4 LightForVertex(float4 vertex)
  80. {
  81. float3 viewPos = UnityObjectToViewPos(vertex).xyz;
  82. fixed3 lightColor = UNITY_LIGHTMODEL_AMBIENT.rgb * _AmbientLightMultiplier;
  83. lightColor = ApplyLight(0, lightColor, viewPos);
  84. lightColor = ApplyLight(1, lightColor, viewPos);
  85. lightColor = ApplyLight(2, lightColor, viewPos);
  86. lightColor = ApplyLight(3, lightColor, viewPos);
  87. return fixed4(lightColor, 1);
  88. }
  89. float4 _MainTex_ST;
  90. v2f vert(appdata_t v)
  91. {
  92. v2f o;
  93. o.pos = UnityObjectToClipPos(v.vertex);
  94. o.uv_MainTex = TRANSFORM_TEX(v.texcoord, _MainTex);
  95. o.color = LightForVertex(v.vertex) * v.color * _TintColor;
  96. // o.color = v.color * _TintColor; // temp if you want to disable lighting
  97. // make sure the alpha scales down with the light
  98. o.color *= (min(o.color.rgb, _TintColor.a).r / _TintColor.a);
  99. #if defined(SOFTPARTICLES_ON)
  100. o.projPos = ComputeScreenPos(o.pos);
  101. COMPUTE_EYEDEPTH(o.projPos.z);
  102. #endif
  103. return o;
  104. }
  105. #if defined(SOFTPARTICLES_ON)
  106. sampler2D _CameraDepthTexture;
  107. #endif
  108. sampler2D _MainTex;
  109. fixed4 frag (v2f i) : COLOR
  110. {
  111. #if defined(SOFTPARTICLES_ON)
  112. float sceneZ = LinearEyeDepth(UNITY_SAMPLE_DEPTH(tex2Dproj(_CameraDepthTexture, UNITY_PROJ_COORD(i.projPos))));
  113. float partZ = i.projPos.z;
  114. i.color.a *= saturate(_InvFade * (sceneZ - partZ));
  115. #endif
  116. return tex2D(_MainTex, i.uv_MainTex) * i.color;
  117. }
  118. ENDCG
  119. }
  120. }
  121. Fallback "Particles/Alpha Blended"
  122. }