ProjectorCaustics.shader 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. Shader "Projector/Caustics" {
  2. Properties {
  3. _Color ("Main Color", Color) = (1,1,1,1)
  4. _CausticTex ("Cookie", 2D) = "" {}
  5. [Toggle]
  6. _Scroll("Enable Scrolling",float) = 0
  7. _Speed("Caustic Speed",float) = 1
  8. _Blending("Blending",Range(1,2)) = 2
  9. _FalloffTex ("FallOff", 2D) = "" {}
  10. [Header(Distortion)]
  11. _DistortionTex("Distortion Texture",2D) = "black" {}
  12. _Distortion ("Distortion",Range(0,1)) = 0.5
  13. _DistortionSpeed("Distortion Speed",float) = 0.5
  14. }
  15. Subshader {
  16. Tags {"Queue"="Transparent"}
  17. Pass {
  18. ZWrite Off
  19. ColorMask RGB
  20. Blend SrcAlpha One
  21. Offset -1, -1
  22. CGPROGRAM
  23. #pragma vertex vert
  24. #pragma fragment frag
  25. #pragma multi_compile_fog
  26. #pragma multi_compile _ _SCROLL_ON
  27. #include "UnityCG.cginc"
  28. struct v2f {
  29. float4 pos : SV_POSITION;
  30. float4 uvFalloff : TEXCOORD0;
  31. float4 worldPos : TEXCOORD1;
  32. float3 worldNormal : TEXCOORD2;
  33. float4 xUV : TEXCOORD3;
  34. float4 yUV : TEXCOORD4;
  35. float4 zUV : TEXCOORD5;
  36. };
  37. float4x4 unity_Projector;
  38. float4x4 unity_ProjectorClip;
  39. float _Speed;
  40. float _Blending;
  41. float _Distortion;
  42. float _DistortionSpeed;
  43. half4 _CausticTex_ST;
  44. half4 _DistortionTex_ST;
  45. v2f vert (float4 vertex : POSITION, float3 normal : NORMAL)
  46. {
  47. v2f o;
  48. o.pos = UnityObjectToClipPos (vertex);
  49. o.uvFalloff = mul (unity_ProjectorClip, vertex);
  50. o.worldPos = mul (unity_ObjectToWorld,vertex);
  51. o.worldNormal = UnityObjectToWorldNormal(normal);
  52. #if _SCROLL_ON
  53. float time1 = frac(_Time.x * _Speed);
  54. float time2 = frac(_Time.x * _DistortionSpeed);
  55. #else
  56. float time1 = 0;
  57. float time2 = 0;
  58. #endif
  59. // Anim UV
  60. o.xUV.xy = o.worldPos.zy * _CausticTex_ST.xy + time1;
  61. o.xUV.zw = o.worldPos.zy * _DistortionTex_ST.xy - time2;
  62. o.yUV.xy = o.worldPos.xz * _CausticTex_ST.xy + time1;
  63. o.yUV.zw = o.worldPos.xz * _DistortionTex_ST.xy - time2;
  64. o.zUV.xy = o.worldPos.xy * _CausticTex_ST.xy + time1;
  65. o.zUV.zw = o.worldPos.xy * _DistortionTex_ST.xy - time2;
  66. return o;
  67. }
  68. fixed4 _Color;
  69. sampler2D _CausticTex;
  70. sampler2D _FalloffTex;
  71. sampler2D _DistortionTex;
  72. fixed4 frag (v2f i) : SV_Target
  73. {
  74. fixed texF = tex2Dproj (_FalloffTex, UNITY_PROJ_COORD(i.uvFalloff)).a;
  75. half3 blendWeights = smoothstep(0,_Blending,abs(i.worldNormal));
  76. float2 offset = tex2D (_DistortionTex, i.xUV.zw).rg * blendWeights.x * _Distortion;
  77. float2 offset2 = tex2D (_DistortionTex, i.yUV.zw).rg * blendWeights.y * _Distortion;
  78. float2 offset3 = tex2D (_DistortionTex, i.zUV.zw).rg * blendWeights.z * _Distortion;
  79. fixed tex = tex2D (_CausticTex, i.xUV.xy + offset * 0.5 + 0.5) * blendWeights.x;
  80. fixed tex2 = tex2D (_CausticTex, i.yUV.xy + offset2 * 0.5 + 0.5)* blendWeights.y;
  81. fixed tex3 = tex2D (_CausticTex, i.zUV.xy + offset3 * 0.5 + 0.5) * blendWeights.z;
  82. // fixed tex = max(tex2D (_CausticTex, i.xUV.xy), tex2D (_CausticTex, i.xUV.zw)) * blendWeights.x;
  83. // fixed tex2 = max(tex2D (_CausticTex, i.yUV.xy), tex2D (_CausticTex, i.yUV.zw)) * blendWeights.y;
  84. // fixed tex3 = max(tex2D (_CausticTex, i.zUV.xy), tex2D (_CausticTex, i.zUV.zw)) * blendWeights.z;
  85. fixed res = tex + tex2 + tex3;
  86. //return fixed4(blendWeights,1);
  87. return 2.0 * fixed4(_Color.rgb,saturate(res) * _Color.a * texF);
  88. }
  89. ENDCG
  90. }
  91. }
  92. }