Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Shader "Custom/PhotoGlass"
- {
- Properties
- {
- [Slider(0.1)] _F0("F0", Range(0.0, 1.0)) = 0.02
- [Slider(0.1)] _ThicknessFactor("Thickness", Range(0.0, 1.0)) = 0.3
- [Slider(0.1)] _RefractionIndex("RefractionIndex", Range(1.0, 3.0)) = 1.33
- _CubeMap("Cube Map", Cube) = "white" {}
- }
- SubShader
- {
- Tags{ "Queue" = "Transparent" "RenderType" = "Transparent" }
- // Forward Base
- Pass
- {
- Tags{"LightMode" = "ForwardBase"}
- Blend SrcAlpha OneMinusSrcAlpha
- ColorMask RGB
- Cull Back
- // ZWrite Off
- CGPROGRAM
- #pragma vertex vert
- #pragma fragment frag
- #pragma multi_compile_fwdbase
- #include "UnityCG.cginc"
- #include "AutoLight.cginc"
- struct appdata
- {
- float4 vertex : POSITION;
- half3 normal : NORMAL;
- };
- struct v2f
- {
- float4 vertex : SV_POSITION;
- half3 viewDir : TEXCOORD1;
- half3 normal : TEXCOORD2;
- half3 lightDir : TEXCOORD3;
- float4 worldPos : TEXCOORD4;
- LIGHTING_COORDS(5, 6)
- };
- UNITY_DECLARE_TEXCUBE(_CubeMap);
- float _F0;
- float _ThicknessFactor;
- float _RefractionIndex;
- uniform fixed4 _LightColor0;
- v2f vert(appdata v)
- {
- v2f o;
- o.vertex = UnityObjectToClipPos(v.vertex);
- o.viewDir = normalize(WorldSpaceViewDir(v.vertex));
- o.normal = normalize(UnityObjectToWorldNormal(v.normal));
- o.lightDir = normalize(WorldSpaceLightDir(v.vertex));
- o.worldPos = mul(unity_ObjectToWorld, o.vertex);
- TRANSFER_VERTEX_TO_FRAGMENT(o);
- return o;
- }
- fixed4 frag(v2f i) : SV_Target
- {
- half atten = LIGHT_ATTENUATION(i);
- half3 ambient = UNITY_LIGHTMODEL_AMBIENT;
- half4 lightColor = _LightColor0;
- float fresnel = _F0 + (1.0h - _F0) * pow(1.0h - dot(i.viewDir, i.normal) + 0.3, 5);
- fresnel = clamp(fresnel, 0, 1);
- half3 reflDir = reflect(-i.viewDir, i.normal);
- half3 refrDir = refract(-i.viewDir, i.normal, 1.0 / _RefractionIndex);
- half3 dir = lerp(reflDir, refrDir, lerp(0, 1 - fresnel, _ThicknessFactor));
- fixed4 color = UNITY_SAMPLE_TEXCUBE_LOD(_CubeMap, dir, 0);
- color.rgb = DecodeHDR(color, unity_SpecCube0_HDR);
- color.rgb = (color * lightColor * atten) + ambient;
- color.a = lerp(fresnel, 1, _ThicknessFactor);
- return color;
- }
- ENDCG
- }
- // Forward Add
- Pass
- {
- Tags{"LightMode" = "ForwardAdd"}
- Blend SrcAlpha One
- ColorMask RGB
- Cull Back
- // ZWrite Off
- CGPROGRAM
- #pragma vertex vert
- #pragma fragment frag
- #pragma multi_compile_fwdadd
- #include "UnityCG.cginc"
- #include "AutoLight.cginc"
- struct appdata
- {
- float4 vertex : POSITION;
- half3 normal : NORMAL;
- };
- struct v2f
- {
- float4 vertex : SV_POSITION;
- half3 viewDir : TEXCOORD1;
- half3 normal : TEXCOORD2;
- half3 worldPos: TEXCOORD3;
- LIGHTING_COORDS(4, 5)
- };
- UNITY_DECLARE_TEXCUBE(_CubeMap);
- float _F0;
- float _ThicknessFactor;
- float _RefractionIndex;
- uniform fixed4 _LightColor0;
- v2f vert(appdata v)
- {
- v2f o;
- o.vertex = UnityObjectToClipPos(v.vertex);
- o.viewDir = normalize(WorldSpaceViewDir(v.vertex));
- o.normal = normalize(UnityObjectToWorldNormal(v.normal));
- o.worldPos = mul(unity_ObjectToWorld, v.vertex);
- TRANSFER_VERTEX_TO_FRAGMENT(o);
- return o;
- }
- fixed4 frag(v2f i) : SV_Target
- {
- half atten = LIGHT_ATTENUATION(i);
- half4 lightColor = _LightColor0;
- half3 lightDir;
- if (_WorldSpaceLightPos0.w > 0) {
- lightDir = _WorldSpaceLightPos0.xyz - i.worldPos.xyz;
- }
- else {
- lightDir = _WorldSpaceLightPos0.xyz;
- }
- lightDir = normalize(lightDir);
- float fresnel = _F0 + (1.0h - _F0) * pow(1.0h - dot(i.viewDir, i.normal) + 0.3, 5);
- fresnel = clamp(fresnel, 0, 1);
- half3 reflDir = reflect(-i.viewDir, i.normal);
- half3 refrDir = refract(-i.viewDir, i.normal, 1.0 / _RefractionIndex);
- half3 dir = lerp(reflDir, refrDir, lerp(0, 1 - fresnel, _ThicknessFactor));
- fixed4 color = UNITY_SAMPLE_TEXCUBE_LOD(_CubeMap, reflDir, (1 - fresnel));
- color.rgb = DecodeHDR(color, unity_SpecCube0_HDR);
- color.rgb *= dot(i.normal, lightDir);
- color.rgb = color * lightColor * atten;
- color.a = lerp(fresnel, 1, _ThicknessFactor);
- return color;
- }
- ENDCG
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement