Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.13 KB | None | 0 0
  1. Shader "Custom/PhotoGlass"
  2. {
  3. Properties
  4. {
  5. [Slider(0.1)] _F0("F0", Range(0.0, 1.0)) = 0.02
  6. [Slider(0.1)] _ThicknessFactor("Thickness", Range(0.0, 1.0)) = 0.3
  7. [Slider(0.1)] _RefractionIndex("RefractionIndex", Range(1.0, 3.0)) = 1.33
  8. _CubeMap("Cube Map", Cube) = "white" {}
  9. }
  10.  
  11. SubShader
  12. {
  13. Tags{ "Queue" = "Transparent" "RenderType" = "Transparent" }
  14.  
  15. // Forward Base
  16. Pass
  17. {
  18. Tags{"LightMode" = "ForwardBase"}
  19.  
  20. Blend SrcAlpha OneMinusSrcAlpha
  21. ColorMask RGB
  22. Cull Back
  23. // ZWrite Off
  24.  
  25. CGPROGRAM
  26. #pragma vertex vert
  27. #pragma fragment frag
  28. #pragma multi_compile_fwdbase
  29.  
  30. #include "UnityCG.cginc"
  31. #include "AutoLight.cginc"
  32.  
  33. struct appdata
  34. {
  35. float4 vertex : POSITION;
  36. half3 normal : NORMAL;
  37. };
  38.  
  39. struct v2f
  40. {
  41. float4 vertex : SV_POSITION;
  42. half3 viewDir : TEXCOORD1;
  43. half3 normal : TEXCOORD2;
  44. half3 lightDir : TEXCOORD3;
  45. float4 worldPos : TEXCOORD4;
  46. LIGHTING_COORDS(5, 6)
  47. };
  48.  
  49. UNITY_DECLARE_TEXCUBE(_CubeMap);
  50. float _F0;
  51. float _ThicknessFactor;
  52. float _RefractionIndex;
  53. uniform fixed4 _LightColor0;
  54.  
  55. v2f vert(appdata v)
  56. {
  57. v2f o;
  58. o.vertex = UnityObjectToClipPos(v.vertex);
  59. o.viewDir = normalize(WorldSpaceViewDir(v.vertex));
  60. o.normal = normalize(UnityObjectToWorldNormal(v.normal));
  61. o.lightDir = normalize(WorldSpaceLightDir(v.vertex));
  62. o.worldPos = mul(unity_ObjectToWorld, o.vertex);
  63.  
  64. TRANSFER_VERTEX_TO_FRAGMENT(o);
  65.  
  66. return o;
  67. }
  68.  
  69. fixed4 frag(v2f i) : SV_Target
  70. {
  71. half atten = LIGHT_ATTENUATION(i);
  72. half3 ambient = UNITY_LIGHTMODEL_AMBIENT;
  73. half4 lightColor = _LightColor0;
  74.  
  75. float fresnel = _F0 + (1.0h - _F0) * pow(1.0h - dot(i.viewDir, i.normal) + 0.3, 5);
  76. fresnel = clamp(fresnel, 0, 1);
  77. half3 reflDir = reflect(-i.viewDir, i.normal);
  78. half3 refrDir = refract(-i.viewDir, i.normal, 1.0 / _RefractionIndex);
  79. half3 dir = lerp(reflDir, refrDir, lerp(0, 1 - fresnel, _ThicknessFactor));
  80.  
  81. fixed4 color = UNITY_SAMPLE_TEXCUBE_LOD(_CubeMap, dir, 0);
  82. color.rgb = DecodeHDR(color, unity_SpecCube0_HDR);
  83. color.rgb = (color * lightColor * atten) + ambient;
  84. color.a = lerp(fresnel, 1, _ThicknessFactor);
  85. return color;
  86. }
  87. ENDCG
  88. }
  89.  
  90. // Forward Add
  91. Pass
  92. {
  93. Tags{"LightMode" = "ForwardAdd"}
  94.  
  95. Blend SrcAlpha One
  96. ColorMask RGB
  97. Cull Back
  98. // ZWrite Off
  99.  
  100. CGPROGRAM
  101. #pragma vertex vert
  102. #pragma fragment frag
  103. #pragma multi_compile_fwdadd
  104.  
  105. #include "UnityCG.cginc"
  106. #include "AutoLight.cginc"
  107.  
  108. struct appdata
  109. {
  110. float4 vertex : POSITION;
  111. half3 normal : NORMAL;
  112. };
  113.  
  114. struct v2f
  115. {
  116. float4 vertex : SV_POSITION;
  117. half3 viewDir : TEXCOORD1;
  118. half3 normal : TEXCOORD2;
  119. half3 worldPos: TEXCOORD3;
  120. LIGHTING_COORDS(4, 5)
  121. };
  122.  
  123. UNITY_DECLARE_TEXCUBE(_CubeMap);
  124. float _F0;
  125. float _ThicknessFactor;
  126. float _RefractionIndex;
  127. uniform fixed4 _LightColor0;
  128.  
  129. v2f vert(appdata v)
  130. {
  131. v2f o;
  132. o.vertex = UnityObjectToClipPos(v.vertex);
  133. o.viewDir = normalize(WorldSpaceViewDir(v.vertex));
  134. o.normal = normalize(UnityObjectToWorldNormal(v.normal));
  135. o.worldPos = mul(unity_ObjectToWorld, v.vertex);
  136.  
  137. TRANSFER_VERTEX_TO_FRAGMENT(o);
  138.  
  139. return o;
  140. }
  141.  
  142. fixed4 frag(v2f i) : SV_Target
  143. {
  144. half atten = LIGHT_ATTENUATION(i);
  145. half4 lightColor = _LightColor0;
  146.  
  147. half3 lightDir;
  148. if (_WorldSpaceLightPos0.w > 0) {
  149. lightDir = _WorldSpaceLightPos0.xyz - i.worldPos.xyz;
  150. }
  151. else {
  152. lightDir = _WorldSpaceLightPos0.xyz;
  153. }
  154. lightDir = normalize(lightDir);
  155.  
  156. float fresnel = _F0 + (1.0h - _F0) * pow(1.0h - dot(i.viewDir, i.normal) + 0.3, 5);
  157. fresnel = clamp(fresnel, 0, 1);
  158. half3 reflDir = reflect(-i.viewDir, i.normal);
  159. half3 refrDir = refract(-i.viewDir, i.normal, 1.0 / _RefractionIndex);
  160. half3 dir = lerp(reflDir, refrDir, lerp(0, 1 - fresnel, _ThicknessFactor));
  161.  
  162. fixed4 color = UNITY_SAMPLE_TEXCUBE_LOD(_CubeMap, reflDir, (1 - fresnel));
  163. color.rgb = DecodeHDR(color, unity_SpecCube0_HDR);
  164. color.rgb *= dot(i.normal, lightDir);
  165. color.rgb = color * lightColor * atten;
  166. color.a = lerp(fresnel, 1, _ThicknessFactor);
  167. return color;
  168. }
  169. ENDCG
  170. }
  171. }
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement