GlassStainedBumpDistort.shader 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // Per pixel bumped refraction.
  2. // Uses a normal map to distort the image behind, and
  3. // an additional texture to tint the color.
  4. Shader "FX/Glass/Stained BumpDistort" {
  5. Properties {
  6. _BumpAmt ("Distortion", range (0,128)) = 10
  7. _MainTex ("Tint Color (RGB)", 2D) = "white" {}
  8. _BumpMap ("Normalmap", 2D) = "bump" {}
  9. }
  10. Category {
  11. // We must be transparent, so other objects are drawn before this one.
  12. Tags { "Queue"="Transparent" "RenderType"="Opaque" }
  13. SubShader {
  14. // This pass grabs the screen behind the object into a texture.
  15. // We can access the result in the next pass as _GrabTexture
  16. GrabPass {
  17. Name "BASE"
  18. Tags { "LightMode" = "Always" }
  19. }
  20. // Main pass: Take the texture grabbed above and use the bumpmap to perturb it
  21. // on to the screen
  22. Pass {
  23. Name "BASE"
  24. Tags { "LightMode" = "Always" }
  25. CGPROGRAM
  26. #pragma vertex vert
  27. #pragma fragment frag
  28. #pragma multi_compile_fog
  29. #include "UnityCG.cginc"
  30. struct appdata_t {
  31. float4 vertex : POSITION;
  32. float2 texcoord: TEXCOORD0;
  33. };
  34. struct v2f {
  35. float4 vertex : SV_POSITION;
  36. float4 uvgrab : TEXCOORD0;
  37. float2 uvbump : TEXCOORD1;
  38. float2 uvmain : TEXCOORD2;
  39. UNITY_FOG_COORDS(3)
  40. };
  41. float _BumpAmt;
  42. float4 _BumpMap_ST;
  43. float4 _MainTex_ST;
  44. v2f vert (appdata_t v)
  45. {
  46. v2f o;
  47. o.vertex = UnityObjectToClipPos(v.vertex);
  48. o.uvgrab = ComputeGrabScreenPos(o.vertex);
  49. o.uvbump = TRANSFORM_TEX( v.texcoord, _BumpMap );
  50. o.uvmain = TRANSFORM_TEX( v.texcoord, _MainTex );
  51. UNITY_TRANSFER_FOG(o,o.vertex);
  52. return o;
  53. }
  54. sampler2D _GrabTexture;
  55. float4 _GrabTexture_TexelSize;
  56. sampler2D _BumpMap;
  57. sampler2D _MainTex;
  58. half4 frag (v2f i) : SV_Target
  59. {
  60. #if UNITY_SINGLE_PASS_STEREO
  61. i.uvgrab.xy = TransformStereoScreenSpaceTex(i.uvgrab.xy, i.uvgrab.w);
  62. #endif
  63. // calculate perturbed coordinates
  64. half2 bump = UnpackNormal(tex2D( _BumpMap, i.uvbump )).rg; // we could optimize this by just reading the x & y without reconstructing the Z
  65. float2 offset = bump * _BumpAmt * _GrabTexture_TexelSize.xy;
  66. #ifdef UNITY_Z_0_FAR_FROM_CLIPSPACE //to handle recent standard asset package on older version of unity (before 5.5)
  67. i.uvgrab.xy = offset * UNITY_Z_0_FAR_FROM_CLIPSPACE(i.uvgrab.z) + i.uvgrab.xy;
  68. #else
  69. i.uvgrab.xy = offset * i.uvgrab.z + i.uvgrab.xy;
  70. #endif
  71. half4 col = tex2Dproj( _GrabTexture, UNITY_PROJ_COORD(i.uvgrab));
  72. half4 tint = tex2D(_MainTex, i.uvmain);
  73. col *= tint;
  74. UNITY_APPLY_FOG(i.fogCoord, col);
  75. return col;
  76. }
  77. ENDCG
  78. }
  79. }
  80. // ------------------------------------------------------------------
  81. // Fallback for older cards and Unity non-Pro
  82. SubShader {
  83. Blend DstColor Zero
  84. Pass {
  85. Name "BASE"
  86. SetTexture [_MainTex] { combine texture }
  87. }
  88. }
  89. }
  90. }