Advertisement
noradninja

Glass distortion with grabpass using normalmaps

Aug 12th, 2023
965
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.66 KB | None | 0 0
  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.  
  5. Shader "FX/Glass/Stained BumpDistort/Perspective" {
  6. Properties {
  7.     _BumpAmt  ("Distortion", range (0,128)) = 10
  8.     _TintColor ("Tint Color", Color) = (.5, .5, .5, .5)
  9.     _BumpMap ("Normalmap", 2D) = "bump" {}
  10. }
  11.  
  12. Category {
  13.  
  14.     // We must be transparent, so other objects are drawn before this one.
  15.     Tags { "Queue"="Transparent" "RenderType"="Opaque" }
  16.  
  17.  
  18.     SubShader {
  19.  
  20.         // This pass grabs the screen behind the object into a texture.
  21.         // We can access the result in the next pass as _GrabTexture
  22.         GrabPass {
  23.             Name "BASE"
  24.             Tags { "LightMode" = "Always" }
  25.         }
  26.        
  27.         // Main pass: Take the texture grabbed above and use the bumpmap to perturb it
  28.         // on to the screen
  29.         Pass {
  30.             Name "BASE"
  31.             Tags { "LightMode" = "Always" }
  32.            
  33. CGPROGRAM
  34. #pragma vertex vert
  35. #pragma fragment frag
  36. #pragma multi_compile_fog
  37. #include "UnityCG.cginc"
  38.  
  39. struct appdata_t {
  40.     half4 vertex : POSITION;
  41.     half2 texcoord: TEXCOORD0;
  42. };
  43.  
  44. struct v2f {
  45.     half4 vertex : SV_POSITION;
  46.     half4 uvgrab : TEXCOORD0;
  47.     half2 uvbump : TEXCOORD1;
  48.     half2 uvmain : TEXCOORD2;
  49.     UNITY_FOG_COORDS(3)
  50. };
  51.  
  52. half _BumpAmt;
  53. half4 _BumpMap_ST;
  54. half4 _TintColor;
  55.  
  56. v2f vert (appdata_t v)
  57. {
  58.     v2f o;
  59.     o.vertex = UnityObjectToClipPos(v.vertex);
  60.     o.uvgrab = ComputeGrabScreenPos(o.vertex);
  61.     o.uvbump = TRANSFORM_TEX( v.texcoord, _BumpMap );
  62.     UNITY_TRANSFER_FOG(o,o.vertex);
  63.     return o;
  64. }
  65.  
  66. sampler2D _GrabTexture;
  67. half4 _GrabTexture_TexelSize;
  68. sampler2D _BumpMap;
  69.  
  70.  
  71. half4 frag (v2f i) : SV_Target
  72. {
  73.     #if UNITY_SINGLE_PASS_STEREO
  74.     i.uvgrab.xy = TransformStereoScreenSpaceTex(i.uvgrab.xy, i.uvgrab.w);
  75.     #endif
  76.  
  77.     // calculate perturbed coordinates
  78.     const half2 bump = UnpackNormal(tex2D( _BumpMap, i.uvbump )).rg; // we could optimize this by just reading the x & y without reconstructing the Z
  79.     half2 offset = bump * _BumpAmt * _GrabTexture_TexelSize.xy;
  80.     #ifdef UNITY_Z_0_FAR_FROM_CLIPSPACE //to handle recent standard asset package on older version of unity (before 5.5)
  81.         i.uvgrab.xy = offset * UNITY_Z_0_FAR_FROM_CLIPSPACE(i.uvgrab.z) + i.uvgrab.xy;
  82.     #else
  83.         i.uvgrab.xy = offset * i.uvgrab.z + i.uvgrab.xy;
  84.     #endif
  85.  
  86.     half4 col = tex2Dproj( _GrabTexture, UNITY_PROJ_COORD(i.uvgrab));
  87.     half4 tint = _TintColor;
  88.     col *= tint*1.9h;
  89.     col.a = tint.a;
  90.     UNITY_APPLY_FOG(i.fogCoord, col);
  91.     return col;
  92. }
  93. ENDCG
  94.         }
  95.     }
  96.  
  97.     // ------------------------------------------------------------------
  98.     // Fallback for older cards and Unity non-Pro
  99.  
  100.     SubShader {
  101.         Blend DstColor Zero
  102.         Pass {
  103.             Name "BASE"
  104.             SetTexture [_MainTex] { combine texture }
  105.         }
  106.     }
  107. }
  108.  
  109. }
  110.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement