lightningShader.cginc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #ifndef __LIGHTNING_SHADER_INCLUDE__
  2. #define __LIGHTNING_SHADER_INCLUDE__
  3. #include "UnityCG.cginc"
  4. #define WM_BASE_VERTEX_INPUT UNITY_VERTEX_INPUT_INSTANCE_ID
  5. #define WM_BASE_VERTEX_TO_FRAG UNITY_VERTEX_INPUT_INSTANCE_ID UNITY_VERTEX_OUTPUT_STEREO
  6. #define WM_INSTANCE_VERT(v, type, o) type o; UNITY_SETUP_INSTANCE_ID(v); UNITY_INITIALIZE_OUTPUT(type, o); UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
  7. #define WM_INSTANCE_FRAG(i) UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(i);
  8. uniform fixed4 _TintColor;
  9. uniform float4 _MainTex_ST;
  10. uniform sampler2D _MainTex;
  11. #if defined(SOFTPARTICLES_ON)
  12. uniform float _InvFade;
  13. uniform UNITY_DECLARE_DEPTH_TEXTURE(_CameraDepthTexture);
  14. #endif
  15. uniform float4 _LightningTime;
  16. uniform float _JitterMultiplier;
  17. uniform float _Turbulence;
  18. uniform float4 _TurbulenceVelocity;
  19. uniform float4 _IntensityFlicker;
  20. uniform int _RenderMode;
  21. struct appdata_t
  22. {
  23. float4 vertex : POSITION;
  24. float4 dir : TANGENT;
  25. float4 color : COLOR;
  26. float3 dir2 : NORMAL;
  27. float4 texcoord : TEXCOORD0;
  28. float4 fadeLifetime : TEXCOORD1;
  29. WM_BASE_VERTEX_INPUT
  30. };
  31. struct v2f
  32. {
  33. float2 texcoord : TEXCOORD0;
  34. fixed4 color : COLOR0;
  35. float4 pos : SV_POSITION;
  36. #if defined(SOFTPARTICLES_ON)
  37. float4 projPos : TEXCOORD1;
  38. #endif
  39. WM_BASE_VERTEX_TO_FRAG
  40. };
  41. inline float rand2(float2 pos) { return frac(sin(dot(pos, float2(12.9898, 78.233))) * 43758.5453); }
  42. inline float rand3(float3 pos) { return frac(sin(dot(_LightningTime.xyz * pos, float3(12.9898, 78.233, 45.5432))) * 43758.5453); }
  43. inline float hash(float p) { p = frac(p * 0.011); p *= p + 7.5; p *= p + p; return frac(p); }
  44. inline float noise1d(float x) { float i = floor(x); float f = frac(x); float u = f * f * (3.0 - 2.0 * f); return lerp(hash(i), hash(i + 1.0), u); }
  45. inline float computeFlicker(float offset)
  46. {
  47. //return saturate(rand2(float2(_LightningTime.y * _IntensityFlicker.y, offset)) * _IntensityFlicker.x);
  48. return saturate(noise1d((_LightningTime.y * (_IntensityFlicker.y + offset)))) * _IntensityFlicker.x;
  49. }
  50. // float rand(float n) { return frac(sin(n) * 43758.5453123); }
  51. // float noise(float p) { float fl = floor(p); float fc = frac(p); return lerp(rand(fl), rand(fl + 1.0), fc); }
  52. inline float lerpColor(float4 c)
  53. {
  54. // the vertex will fade in, stay at full color, then fade out
  55. // r = start time
  56. // g = peak start time
  57. // b = peak end time
  58. // a = end time
  59. // debug
  60. // return 1;
  61. float t = _LightningTime.y;
  62. float lerpMultiplier = (t < c.g);
  63. float lerpIn = lerp(0, 1, saturate((t - c.r) / max(0.00001, 0.00001 + c.g - c.r)));
  64. float lerpOut = lerp(1, 0, saturate((t - c.b) / max(0.00001, c.a - c.b)));
  65. float flicker = 1.0;
  66. UNITY_BRANCH
  67. if (_IntensityFlicker.x > 0.0 && _IntensityFlicker.y > 0.0)
  68. {
  69. flicker = computeFlicker(c.a);
  70. }
  71. return saturate(((lerpMultiplier * lerpIn) + ((1.0 - lerpMultiplier) * lerpOut))) * flicker;
  72. /*
  73. float t = _LightningTime.y;
  74. if (t < c.g)
  75. {
  76. return lerp(0.0, 1.0, ((t - c.r) / (c.g - c.r)));
  77. }
  78. return lerp(1.0, 0.0, max(0, ((t - c.b) / (c.a - c.b))));
  79. */
  80. }
  81. inline fixed4 fragMethod(sampler2D tex, v2f i)
  82. {
  83. WM_INSTANCE_FRAG(i);
  84. #if defined(SOFTPARTICLES_ON)
  85. //float sceneZ = LinearEyeDepth(UNITY_SAMPLE_DEPTH(tex2Dproj(_CameraDepthTexture, UNITY_PROJ_COORD(i.projPos))));
  86. float sceneZ = LinearEyeDepth(UNITY_SAMPLE_DEPTH(SAMPLE_DEPTH_TEXTURE_PROJ(_CameraDepthTexture, UNITY_PROJ_COORD(i.projPos))));
  87. float partZ = i.projPos.z;
  88. i.color.a *= saturate(_InvFade * (sceneZ - partZ));
  89. #endif
  90. fixed4 c = tex2D(tex, i.texcoord);
  91. return (c * i.color);
  92. }
  93. fixed4 frag(v2f i) : SV_Target
  94. {
  95. return fragMethod(_MainTex, i);
  96. }
  97. #endif