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" {
6 Properties {
7 _BumpAmt ("Distortion", range (0,128)) = 10
8 _MainTex ("Tint Color (RGB)", 2D) = "white" {}
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 float4 vertex : POSITION;
41 float2 texcoord: TEXCOORD0;
42 };
43
44 struct v2f {
45 float4 vertex : SV_POSITION;
46 float4 uvgrab : TEXCOORD0;
47 float2 uvbump : TEXCOORD1;
48 float2 uvmain : TEXCOORD2;
49 UNITY_FOG_COORDS(3)
50 };
51
52 float _BumpAmt;
53 float4 _BumpMap_ST;
54 float4 _MainTex_ST;
55
56 v2f vert (appdata_t v)
57 {
58 v2f o;
59 o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
60 #if UNITY_UV_STARTS_AT_TOP
61 float scale = -1.0;
62 #else
63 float scale = 1.0;
64 #endif
65 o.uvgrab.xy = (float2(o.vertex.x, o.vertex.y*scale) + o.vertex.w) * 0.5;
66 o.uvgrab.zw = o.vertex.zw;
67 o.uvbump = TRANSFORM_TEX( v.texcoord, _BumpMap );
68 o.uvmain = TRANSFORM_TEX( v.texcoord, _MainTex );
69 UNITY_TRANSFER_FOG(o,o.vertex);
70 return o;
71 }
72
73 sampler2D _GrabTexture;
74 float4 _GrabTexture_TexelSize;
75 sampler2D _BumpMap;
76 sampler2D _MainTex;
77
78 half4 frag (v2f i) : SV_Target
79 {
80 // calculate perturbed coordinates
81 half2 bump = UnpackNormal(tex2D( _BumpMap, i.uvbump )).rg; // we could optimize this by just reading the x & y without reconstructing the Z
82 float2 offset = bump * _BumpAmt * _GrabTexture_TexelSize.xy;
83 i.uvgrab.xy = offset * i.uvgrab.z + i.uvgrab.xy;
84
85 half4 col = tex2Dproj( _GrabTexture, UNITY_PROJ_COORD(i.uvgrab));
86 half4 tint = tex2D(_MainTex, i.uvmain);
87 col *= tint;
88 UNITY_APPLY_FOG(i.fogCoord, col);
89 return col;
90 }
91 ENDCG
92 }
93 }
94
95 // ------------------------------------------------------------------
96 // Fallback for older cards and Unity non-Pro
97
98 SubShader {
99 Blend DstColor Zero
100 Pass {
101 Name "BASE"
102 SetTexture [_MainTex] { combine texture }
103 }
104 }
105 }
106
107 }