1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
| Shader "chp10/GlassRefractionShader" { Properties { _MainTex("Texture", 2D) = "white" {} _BumpTex("Normal map",2D) = "while"{} _Cubemap("Environment Cubemap",Cube) = "_Skybox"{}
_Disortion("Disortion",Range(0,100)) = 10 _RefractionAmount("RefractionAmount",Range(0,1)) = 0.5 } SubShader { Tags {"Queue" = "Transparent" "RenderType" = "Opaque" }
GrabPass{"_RefractionTex"}
Pass { CGPROGRAM #pragma vertex vert #pragma fragment frag
#include "UnityCG.cginc"
sampler2D _MainTex; float4 _MainTex_ST; sampler2D _BumpTex; float4 _BumpTex_ST; samplerCUBE _Cubemap;
float _Disortion; fixed _RefractionAmount;
sampler2D _RefractionTex; float4 _RefractionTex_TexelSize;
struct a2v { float4 vertex : POSITION; float3 normal : NORMAL; float4 tangent : TANGENT; float2 texcoord: TEXCOORD0; };
struct v2f { float4 pos : SV_POSITION; float4 scrPos : TEXCOORD0; float4 uv : TEXCOORD1; float4 TtoW0 : TEXCOORD2; float4 TtoW1 : TEXCOORD3; float4 TtoW2 : TEXCOORD4; };
v2f vert(a2v v) { v2f o; o.pos = UnityObjectToClipPos(v.vertex); o.scrPos = ComputeGrabScreenPos(o.pos); o.uv.xy = TRANSFORM_TEX(v.texcoord, _MainTex); o.uv.zw = TRANSFORM_TEX(v.texcoord, _BumpTex);
float3 worldPos = mul(unity_ObjectToWorld, v.vertex).xyz; fixed3 worldNormal = UnityObjectToWorldNormal(v.normal); fixed3 worldTangent = UnityObjectToWorldDir(v.tangent.xyz); fixed3 worldBinormal = cross(worldNormal,worldTangent) * v.tangent.w;
o.TtoW0 = float4(worldTangent.x,worldBinormal.x,worldNormal.x,worldPos.x); o.TtoW1 = float4(worldTangent.y, worldBinormal.y, worldNormal.y, worldPos.y); o.TtoW2 = float4(worldTangent.z, worldBinormal.z, worldNormal.z, worldPos.z); return o; }
fixed4 frag(v2f i) : SV_Target { float3 worldPos = float3(i.TtoW0.w,i.TtoW1.w,i.TtoW2.w); fixed3 worldViewDir = normalize(UnityWorldSpaceViewDir(worldPos));
fixed3 bump = UnpackNormal(tex2D(_BumpTex, i.uv.zw)); float2 offset = bump.xy * _Disortion * _RefractionTex_TexelSize.xy; i.scrPos.xy = i.scrPos.xy + offset.xy;
fixed3 refrColor = tex2D(_RefractionTex,i.scrPos.xy / i.scrPos.w).rgb;
bump = normalize(half3(dot(i.TtoW0.xyz,bump),dot(i.TtoW1.xyz,bump),dot(i.TtoW2.xyz,bump))); fixed3 reflectDir = reflect(-worldViewDir, bump); fixed4 texColor = tex2D(_MainTex,i.uv.xy); fixed3 reflColor = texCUBE(_Cubemap,reflectDir).rgb * texColor.rgb; fixed3 finalColor = reflColor * (1 - _RefractionAmount) + refrColor * _RefractionAmount; return fixed4(finalColor, 1); } ENDCG } } }
|