IntersectionHighlight-Add.shader 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. Shader "Mirza Beig/Particles/Intersection Highlight/Additive" {
  2. Properties {
  3. _TintColor ("Tint Color", Color) = (0.5, 0.5, 0.5, 0.5)
  4. _highlightColour ("Highlight Colour", Color) = (1.0, 1.0, 1.0, 1.0)
  5. _MainTex ("Particle Texture", 2D) = "white" {}
  6. _InvFade ("Soft Particles Factor", Range(0.01, 32.0)) = 32.0
  7. _highlightEdgeThreshold ("Highlight Edge Threshold", Range(0.0, 32.0)) = 1.0
  8. _highlightColourStrength ("Highlight Colour Strength", Range(0.0, 32.0)) = 1.0
  9. }
  10. Category {
  11. Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" }
  12. Blend SrcAlpha One
  13. AlphaTest Greater .01
  14. ColorMask RGB
  15. Cull Off Lighting Off ZWrite Off
  16. SubShader {
  17. Pass {
  18. CGPROGRAM
  19. #pragma vertex vert
  20. #pragma fragment frag
  21. #pragma multi_compile_particles
  22. #pragma multi_compile_fog
  23. #include "UnityCG.cginc"
  24. sampler2D _MainTex;
  25. fixed4 _TintColor;
  26. struct appdata_t {
  27. float4 vertex : POSITION;
  28. fixed4 color : COLOR;
  29. float2 texcoord : TEXCOORD0;
  30. };
  31. struct v2f {
  32. float4 vertex : SV_POSITION;
  33. fixed4 color : COLOR;
  34. float2 texcoord : TEXCOORD0;
  35. UNITY_FOG_COORDS(1)
  36. float4 uv_depth : TEXCOORD2;
  37. };
  38. float4 _MainTex_ST;
  39. v2f vert (appdata_t v)
  40. {
  41. v2f o;
  42. o.vertex = UnityObjectToClipPos(v.vertex);
  43. o.uv_depth = ComputeScreenPos(o.vertex);
  44. COMPUTE_EYEDEPTH(o.uv_depth.z);
  45. o.color = v.color;
  46. o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
  47. UNITY_TRANSFER_FOG(o, o.vertex);
  48. return o;
  49. }
  50. sampler2D_float _CameraDepthTexture;
  51. float _InvFade;
  52. uniform float4 _highlightColour;
  53. uniform float _highlightColourStrength;
  54. uniform float _highlightEdgeThreshold;
  55. fixed4 frag (v2f i) : SV_Target
  56. {
  57. // Get texture colour.
  58. float4 texColour = tex2D(_MainTex, i.texcoord);
  59. // Tint.
  60. fixed4 colour = _TintColor * texColour;
  61. //
  62. _highlightColour *= texColour;
  63. // Scene depth.
  64. //float sceneZ = LinearEyeDepth(tex2Dproj(_CameraDepthTexture, UNITY_PROJ_COORD(i.uv_depth)).r);
  65. float sceneZ = LinearEyeDepth(SAMPLE_DEPTH_TEXTURE_PROJ(_CameraDepthTexture, UNITY_PROJ_COORD(i.uv_depth)));
  66. // Distance to the camera.
  67. float partZ = i.uv_depth.z;
  68. // Depth and this object's values close together = intersection.
  69. float dist01 = (abs(sceneZ - partZ)) / _highlightEdgeThreshold;
  70. // Attenuate highlight based on difference.
  71. if (dist01 <= 1.0f)
  72. {
  73. colour = lerp(_highlightColour * _highlightColourStrength, colour, dist01);
  74. }
  75. #ifdef SOFTPARTICLES_ON
  76. // Soft particles (soft factor).
  77. // "Comparing depth values of the particle with depth values of world geometry (in view space)." - Special Effects with Depth (Siggraph, 2011).
  78. // float softFactor = saturate((depthEye - zEye) * fade)
  79. // Inverse fade: 0.0f = off.
  80. float fade = saturate(_InvFade * (sceneZ - partZ));
  81. colour.a *= fade;
  82. #endif
  83. // Input colour.
  84. colour *= i.color * 2.0f;
  85. // Fog towards black (additive black = nothing).
  86. UNITY_APPLY_FOG_COLOR(i.fogCoord, colour, fixed4(0.0f, 0.0f, 0.0f, 0.0f));
  87. return colour;
  88. }
  89. ENDCG
  90. }
  91. }
  92. }
  93. }