Advertisement
Guest User

refraction shader

a guest
Oct 2nd, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Uses geometry normals to distort the image behind, and
  2. // an additional texture to tint the color.
  3.  
  4. Shader "FX/Refraction Distort" {
  5. Properties {
  6.     _BumpAmt  ("Distortion", range (0,128)) = 10.0
  7.     _MainTex ("Tint Color (RGB)", 2D) = "white" {}
  8. }
  9.  
  10. Category {
  11.  
  12.     // We must be transparent, so other objects are drawn before this one.
  13.     Tags { "Queue" = "Transparent"}
  14.    
  15.     // ------------------------------------------------------------------
  16.     //  ARB fragment program
  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 normals to perturb it
  28.          // on to the screen
  29.         Pass {
  30.             Name "BASE"
  31.             Tags { "LightMode" = "Always" }
  32.            
  33. CGPROGRAM
  34. // profiles arbfp1
  35. // vertex vert
  36. // fragment frag
  37. // fragmentoption ARB_precision_hint_fastest
  38. // fragmentoption ARB_fog_exp2
  39.  
  40. #include "UnityCG.cginc"
  41.  
  42. sampler2D _MainTex : register(s1);
  43. samplerRECT _GrabTexture : register(s0);
  44.  
  45. struct v2f {
  46.     V2F_POS_FOG;
  47.     float4 uvrefr    : TEXCOORD0;
  48.     float2 uv         : TEXCOORD1;
  49.     float3 normal    : TEXCOORD2;
  50. };
  51.  
  52. uniform float _BumpAmt;
  53.  
  54. v2f vert (appdata_base v)
  55. {
  56.     v2f o;
  57.     PositionFog( v.vertex, o.pos, o.fog );
  58.     o.uv = TRANSFORM_UV(1);
  59.     o.uvrefr = mul( glstate.matrix.texture[0], v.vertex );
  60.     o.normal = mul( (float3x3)glstate.matrix.mvp, v.normal );
  61.    
  62.     return o;
  63. }
  64.  
  65. half4 frag( v2f i ) : COLOR
  66. {
  67.     i.normal = normalize(i.normal);
  68.    
  69.     // Calculate refracted vector based on the surface normal.
  70.     // This is only an approximation because we don't know the
  71.     // thickness of the object. So just use anything that looks
  72.     // "good enough"
  73.    
  74.     half3 refracted = i.normal * abs(i.normal);
  75.     //half3 refracted = refract( i.normal, half3(0,0,1), 1.333 );
  76.    
  77.     // perturb coordinates of the grabbed image
  78.     i.uvrefr.xy = refracted.xy * (i.uvrefr.w * _BumpAmt) + i.uvrefr.xy;
  79.    
  80.     half4 refr = texRECTproj( _GrabTexture, i.uvrefr );
  81.     half4 col = tex2D( _MainTex, i.uv.xy );
  82.     return col * refr;
  83. }
  84.  
  85. ENDCG
  86.             // Set up the textures for this pass
  87.             SetTexture [_GrabTexture] {}    // Texture we grabbed in the pass above
  88.             SetTexture [_MainTex] {}        // Color tint
  89.         }
  90.     }    
  91.    
  92.     // ------------------------------------------------------------------
  93.     // Fallback for older cards and Unity non-Pro
  94.    
  95.     SubShader {
  96.         Blend DstColor Zero
  97.         Pass {
  98.             Name "BASE"
  99.             SetTexture [_MainTex] {    combine texture }
  100.         }
  101.     }
  102. }
  103.  
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement