Fresnel.shader 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. Shader "Rokid/Fresnel"
  2. {
  3. Properties
  4. {
  5. _Color0 ("Color 0", Color) = (1, 1, 1, 0.5882353)
  6. [Header(Refraction)]
  7. _ChromaticAberration ("Chromatic Aberration", Range(0, 0.3)) = 0.1
  8. _Power ("Power", Float) = 0
  9. _Scale ("Scale", Float) = 0
  10. _Bias ("Bias", Float) = 0.01
  11. [HideInInspector] __dirty ("", Int) = 1
  12. }
  13. SubShader
  14. {
  15. Tags { "RenderType" = "Transparent" "Queue" = "Geometry+0" "IsEmissive" = "true" }
  16. ZWrite Off
  17. Blend SrcColor OneMinusSrcColor
  18. Cull Back
  19. GrabPass { }
  20. CGINCLUDE
  21. #include "UnityPBSLighting.cginc"
  22. #include "Lighting.cginc"
  23. #pragma target 3.0
  24. #pragma multi_compile _ALPHAPREMULTIPLY_ON
  25. #ifdef UNITY_PASS_SHADOWCASTER
  26. #undef INTERNAL_DATA
  27. #undef WorldReflectionVector
  28. #undef WorldNormalVector
  29. #define INTERNAL_DATA half3 internalSurfaceTtoW0; half3 internalSurfaceTtoW1; half3 internalSurfaceTtoW2;
  30. #define WorldReflectionVector(data, normal) reflect(data.worldRefl, half3(dot(data.internalSurfaceTtoW0, normal), dot(data.internalSurfaceTtoW1, normal), dot(data.internalSurfaceTtoW2, normal)))
  31. #define WorldNormalVector(data, normal) half3(dot(data.internalSurfaceTtoW0, normal), dot(data.internalSurfaceTtoW1, normal), dot(data.internalSurfaceTtoW2, normal))
  32. #endif
  33. struct Input
  34. {
  35. float3 worldPos;
  36. float3 worldNormal;
  37. INTERNAL_DATA
  38. float4 screenPos;
  39. };
  40. struct SurfaceOutputStandardCustom
  41. {
  42. half3 Albedo;
  43. half3 Normal;
  44. half3 Emission;
  45. half Metallic;
  46. half Smoothness;
  47. half Occlusion;
  48. half Alpha;
  49. half3 Transmission;
  50. };
  51. uniform float4 _Color0;
  52. uniform float _Bias;
  53. uniform float _Scale;
  54. uniform float _Power;
  55. uniform sampler2D _GrabTexture;
  56. uniform float _ChromaticAberration;
  57. inline half4 LightingStandardCustom(SurfaceOutputStandardCustom s, half3 viewDir, UnityGI gi)
  58. {
  59. half3 transmission = max(0, -dot(s.Normal, gi.light.dir)) * gi.light.color * s.Transmission;
  60. half4 d = half4(s.Albedo * transmission, 0);
  61. SurfaceOutputStandard r;
  62. r.Albedo = s.Albedo;
  63. r.Normal = s.Normal;
  64. r.Emission = s.Emission;
  65. r.Metallic = s.Metallic;
  66. r.Smoothness = s.Smoothness;
  67. r.Occlusion = s.Occlusion;
  68. r.Alpha = s.Alpha;
  69. return LightingStandard(r, viewDir, gi) + d;
  70. }
  71. inline void LightingStandardCustom_GI(SurfaceOutputStandardCustom s, UnityGIInput data, inout UnityGI gi)
  72. {
  73. #if defined(UNITY_PASS_DEFERRED) && UNITY_ENABLE_REFLECTION_BUFFERS
  74. gi = UnityGlobalIllumination(data, s.Occlusion, s.Normal);
  75. #else
  76. UNITY_GLOSSY_ENV_FROM_SURFACE(g, s, data);
  77. gi = UnityGlobalIllumination(data, s.Occlusion, s.Normal, g);
  78. #endif
  79. }
  80. inline float4 Refraction(Input i, SurfaceOutputStandardCustom o, float indexOfRefraction, float chomaticAberration)
  81. {
  82. float3 worldNormal = o.Normal;
  83. float4 screenPos = i.screenPos;
  84. #if UNITY_UV_STARTS_AT_TOP
  85. float scale = -1.0;
  86. #else
  87. float scale = 1.0;
  88. #endif
  89. float halfPosW = screenPos.w * 0.5;
  90. screenPos.y = (screenPos.y - halfPosW) * _ProjectionParams.x * scale + halfPosW;
  91. #if SHADER_API_D3D9 || SHADER_API_D3D11
  92. screenPos.w += 0.00000000001;
  93. #endif
  94. float2 projScreenPos = (screenPos / screenPos.w).xy;
  95. float3 worldViewDir = normalize(UnityWorldSpaceViewDir(i.worldPos));
  96. float3 refractionOffset = (indexOfRefraction - 1.0) * mul(UNITY_MATRIX_V, float4(worldNormal, 0.0)) * (1.0 - dot(worldNormal, worldViewDir));
  97. float2 cameraRefraction = float2(refractionOffset.x, refractionOffset.y);
  98. float4 redAlpha = tex2D(_GrabTexture, (projScreenPos + cameraRefraction));
  99. float green = tex2D(_GrabTexture, (projScreenPos + (cameraRefraction * (1.0 - chomaticAberration)))).g;
  100. float blue = tex2D(_GrabTexture, (projScreenPos + (cameraRefraction * (1.0 + chomaticAberration)))).b;
  101. return float4(redAlpha.r, green, blue, redAlpha.a);
  102. }
  103. void RefractionF(Input i, SurfaceOutputStandardCustom o, inout half4 color)
  104. {
  105. #ifdef UNITY_PASS_FORWARDBASE
  106. float3 ase_worldPos = i.worldPos;
  107. float3 ase_worldViewDir = normalize(UnityWorldSpaceViewDir(ase_worldPos));
  108. float3 ase_worldNormal = WorldNormalVector(i, float3(0, 0, 1));
  109. float fresnelNdotV2 = dot(ase_worldNormal, ase_worldViewDir);
  110. float fresnelNode2 = (_Bias + _Scale * pow(1.0 - fresnelNdotV2, _Power));
  111. float4 temp_output_3_0 = (_Color0 * fresnelNode2);
  112. color.rgb = color.rgb + Refraction(i, o, temp_output_3_0.r, _ChromaticAberration) * (1 - color.a);
  113. color.a = 1;
  114. #endif
  115. }
  116. void surf(Input i, inout SurfaceOutputStandardCustom o)
  117. {
  118. o.Normal = float3(0, 0, 1);
  119. float3 ase_worldPos = i.worldPos;
  120. float3 ase_worldViewDir = normalize(UnityWorldSpaceViewDir(ase_worldPos));
  121. float3 ase_worldNormal = WorldNormalVector(i, float3(0, 0, 1));
  122. float fresnelNdotV2 = dot(ase_worldNormal, ase_worldViewDir);
  123. float fresnelNode2 = (_Bias + _Scale * pow(1.0 - fresnelNdotV2, _Power));
  124. float4 temp_output_3_0 = (_Color0 * fresnelNode2);
  125. o.Albedo = temp_output_3_0.rgb;
  126. o.Emission = temp_output_3_0.rgb;
  127. o.Transmission = temp_output_3_0.rgb;
  128. o.Alpha = 1;
  129. o.Normal = o.Normal + 0.00001 * i.screenPos * i.worldPos;
  130. }
  131. ENDCG
  132. CGPROGRAM
  133. #pragma surface surf StandardCustom keepalpha finalcolor:RefractionF fullforwardshadows exclude_path:deferred
  134. ENDCG
  135. Pass
  136. {
  137. Name "ShadowCaster"
  138. Tags { "LightMode" = "ShadowCaster" }
  139. ZWrite Off
  140. Blend SrcColor OneMinusSrcColor
  141. CGPROGRAM
  142. #pragma vertex vert
  143. #pragma fragment frag
  144. #pragma target 3.0
  145. #pragma multi_compile_shadowcaster
  146. #pragma multi_compile UNITY_PASS_SHADOWCASTER
  147. #pragma skip_variants FOG_LINEAR FOG_EXP FOG_EXP2
  148. #include "HLSLSupport.cginc"
  149. #if (SHADER_API_D3D11 || SHADER_API_GLCORE || SHADER_API_GLES || SHADER_API_GLES3 || SHADER_API_METAL || SHADER_API_VULKAN)
  150. #define CAN_SKIP_VPOS
  151. #endif
  152. #include "UnityCG.cginc"
  153. #include "Lighting.cginc"
  154. #include "UnityPBSLighting.cginc"
  155. struct v2f
  156. {
  157. V2F_SHADOW_CASTER;
  158. float4 screenPos: TEXCOORD1;
  159. float4 tSpace0: TEXCOORD2;
  160. float4 tSpace1: TEXCOORD3;
  161. float4 tSpace2: TEXCOORD4;
  162. UNITY_VERTEX_INPUT_INSTANCE_ID
  163. UNITY_VERTEX_OUTPUT_STEREO
  164. };
  165. v2f vert(appdata_full v)
  166. {
  167. v2f o;
  168. UNITY_SETUP_INSTANCE_ID(v);
  169. UNITY_INITIALIZE_OUTPUT(v2f, o);
  170. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
  171. UNITY_TRANSFER_INSTANCE_ID(v, o);
  172. float3 worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
  173. half3 worldNormal = UnityObjectToWorldNormal(v.normal);
  174. half3 worldTangent = UnityObjectToWorldDir(v.tangent.xyz);
  175. half tangentSign = v.tangent.w * unity_WorldTransformParams.w;
  176. half3 worldBinormal = cross(worldNormal, worldTangent) * tangentSign;
  177. o.tSpace0 = float4(worldTangent.x, worldBinormal.x, worldNormal.x, worldPos.x);
  178. o.tSpace1 = float4(worldTangent.y, worldBinormal.y, worldNormal.y, worldPos.y);
  179. o.tSpace2 = float4(worldTangent.z, worldBinormal.z, worldNormal.z, worldPos.z);
  180. TRANSFER_SHADOW_CASTER_NORMALOFFSET(o)
  181. o.screenPos = ComputeScreenPos(o.pos);
  182. return o;
  183. }
  184. half4 frag(v2f IN): SV_Target
  185. {
  186. UNITY_SETUP_INSTANCE_ID(IN);
  187. Input surfIN;
  188. UNITY_INITIALIZE_OUTPUT(Input, surfIN);
  189. float3 worldPos = float3(IN.tSpace0.w, IN.tSpace1.w, IN.tSpace2.w);
  190. half3 worldViewDir = normalize(UnityWorldSpaceViewDir(worldPos));
  191. surfIN.worldPos = worldPos;
  192. surfIN.worldNormal = float3(IN.tSpace0.z, IN.tSpace1.z, IN.tSpace2.z);
  193. surfIN.internalSurfaceTtoW0 = IN.tSpace0.xyz;
  194. surfIN.internalSurfaceTtoW1 = IN.tSpace1.xyz;
  195. surfIN.internalSurfaceTtoW2 = IN.tSpace2.xyz;
  196. surfIN.screenPos = IN.screenPos;
  197. SurfaceOutputStandardCustom o;
  198. UNITY_INITIALIZE_OUTPUT(SurfaceOutputStandardCustom, o)
  199. surf(surfIN, o);
  200. #if defined(CAN_SKIP_VPOS)
  201. float2 vpos = IN.pos;
  202. #endif
  203. SHADOW_CASTER_FRAGMENT(IN)
  204. }
  205. ENDCG
  206. }
  207. }
  208. Fallback "Diffuse"
  209. }
  210. /*ASEBEGIN
  211. Version=18935
  212. 2445.333;72.66667;691.3333;1326.333;341.6791;1034.048;1.504851;True;False
  213. Node;AmplifyShaderEditor.RangedFloatNode;9;-320.4344,260.7581;Inherit;False;Property;_Power;Power;4;0;Create;True;0;0;0;False;0;False;0;0.75;0;0;0;1;FLOAT;0
  214. Node;AmplifyShaderEditor.RangedFloatNode;10;-338.2071,138.8885;Inherit;False;Property;_Scale;Scale;5;0;Create;True;0;0;0;False;0;False;0;1;0;0;0;1;FLOAT;0
  215. Node;AmplifyShaderEditor.RangedFloatNode;11;-322.9734,36.0611;Inherit;False;Property;_Bias;Bias;6;0;Create;True;0;0;0;False;0;False;0.01;0.01;0;0;0;1;FLOAT;0
  216. Node;AmplifyShaderEditor.ColorNode;1;-133.6667,-244.1667;Inherit;False;Property;_Color0;Color 0;1;0;Create;True;0;0;0;False;0;False;1,1,1,0.5882353;0,0.7403493,1,0.5882353;True;0;5;COLOR;0;FLOAT;1;FLOAT;2;FLOAT;3;FLOAT;4
  217. Node;AmplifyShaderEditor.FresnelNode;2;-122.2464,-1.188284;Inherit;True;Standard;WorldNormal;ViewDir;False;False;5;0;FLOAT3;0,0,1;False;4;FLOAT3;0,0,0;False;1;FLOAT;0.01;False;2;FLOAT;1;False;3;FLOAT;0.75;False;1;FLOAT;0
  218. Node;AmplifyShaderEditor.VoronoiNode;8;-605.8656,-53.57059;Inherit;False;0;0;1;0;1;False;1;False;False;False;4;0;FLOAT2;0,0;False;1;FLOAT;0;False;2;FLOAT;1;False;3;FLOAT;0;False;3;FLOAT;0;FLOAT2;1;FLOAT2;2
  219. Node;AmplifyShaderEditor.TexCoordVertexDataNode;7;-730.8656,-382.5706;Inherit;False;0;2;0;5;FLOAT2;0;FLOAT;1;FLOAT;2;FLOAT;3;FLOAT;4
  220. Node;AmplifyShaderEditor.PowerNode;5;142.6356,-335.817;Inherit;False;False;2;0;FLOAT;0;False;1;FLOAT;1;False;1;FLOAT;0
  221. Node;AmplifyShaderEditor.NoiseGeneratorNode;6;-360.3655,-142.0706;Inherit;False;Simplex2D;False;False;2;0;FLOAT2;0,0;False;1;FLOAT;0.1;False;1;FLOAT;0
  222. Node;AmplifyShaderEditor.WorldPosInputsNode;4;-254.6667,-484.1667;Inherit;False;0;4;FLOAT3;0;FLOAT;1;FLOAT;2;FLOAT;3
  223. Node;AmplifyShaderEditor.SimpleMultiplyOpNode;3;447.2924,-179.9303;Inherit;False;2;2;0;COLOR;0,0,0,0;False;1;FLOAT;0;False;1;COLOR;0
  224. Node;AmplifyShaderEditor.StandardSurfaceOutputNode;0;760.7999,-385.1999;Float;False;True;-1;2;ASEMaterialInspector;0;0;Standard;Fresnel;False;False;False;False;False;False;False;False;False;False;False;False;False;False;False;False;False;False;False;False;False;Back;0;False;-1;0;False;-1;False;0;False;-1;0;False;-1;False;0;Custom;0.5;True;True;0;True;Transparent;;Geometry;ForwardOnly;18;all;True;True;True;True;0;False;-1;False;0;False;-1;255;False;-1;255;False;-1;0;False;-1;0;False;-1;0;False;-1;0;False;-1;0;False;-1;0;False;-1;0;False;-1;0;False;-1;False;2;15;10;25;False;0.5;True;0;0;False;-1;0;False;-1;0;0;False;-1;0;False;-1;0;False;-1;0;False;-1;0;False;0;0,0,0,0;VertexOffset;True;False;Cylindrical;False;True;Relative;0;;0;-1;2;-1;0;False;0;0;False;-1;-1;0;False;-1;0;0;0;False;0.1;False;-1;0;False;-1;False;16;0;FLOAT3;0,0,0;False;1;FLOAT3;0,0,0;False;2;FLOAT3;0,0,0;False;3;FLOAT;0;False;4;FLOAT;0;False;5;FLOAT;0;False;6;FLOAT3;0,0,0;False;7;FLOAT3;0,0,0;False;8;FLOAT;0;False;9;FLOAT;0;False;10;FLOAT;0;False;13;FLOAT3;0,0,0;False;11;FLOAT3;0,0,0;False;12;FLOAT3;0,0,0;False;14;FLOAT4;0,0,0,0;False;15;FLOAT3;0,0,0;False;0
  225. WireConnection;2;1;11;0
  226. WireConnection;2;2;10;0
  227. WireConnection;2;3;9;0
  228. WireConnection;6;0;7;0
  229. WireConnection;3;0;1;0
  230. WireConnection;3;1;2;0
  231. WireConnection;0;0;3;0
  232. WireConnection;0;2;3;0
  233. WireConnection;0;6;3;0
  234. WireConnection;0;8;3;0
  235. ASEEND*/
  236. // CHKSM = A96CCB55C6F9E902D18F9651945017E61DA527F9