ImmersalCube.shader 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. Shader "Immersal/Samples/URP/ImmersalCube"
  2. {
  3. Properties
  4. {
  5. _MainTex("Texture", 2D) = "white" {}
  6. _EmissionTex("Emission", 2D) = "black" {}
  7. _EmissionStrength("Emission Strength", Range(0, 5)) = 0
  8. _DirectSpecular("Direct Specular Color", Color) = (1.0, 1.0, 1.0, 1.0)
  9. _DirectGlossiness("Direct Specular Glossiness", Range(1, 50)) = 10
  10. _IndirectSpecular("indirect Specular Color", Color) = (1.0, 1.0, 1.0, 1.0)
  11. _IndirectGlossiness("Indirect Specular Glossiness", Range(0, 1)) = 0.5
  12. _FresnelExponent("Fresnel Exponent", Range(0, 10)) = 1.0
  13. _GlossinessTex("Glossiness Texture", 2D) = "white" {}
  14. [NoScaleOffset]
  15. _IBLTexCube("IBL Cubemap", Cube) = "black" {}
  16. }
  17. SubShader
  18. {
  19. Tags
  20. {
  21. "LightMode" = "UniversalForward"
  22. }
  23. LOD 100
  24. Pass
  25. {
  26. CGPROGRAM
  27. #pragma vertex vert
  28. #pragma fragment frag
  29. // make fog work
  30. #pragma multi_compile_fog
  31. #include "UnityCG.cginc"
  32. struct appdata
  33. {
  34. float4 P : POSITION;
  35. half3 N : NORMAL;
  36. half2 uv0 : TEXCOORD0;
  37. };
  38. struct v2f
  39. {
  40. float4 P : SV_POSITION;
  41. half3 N : NORMAL;
  42. half2 uv0 : TEXCOORD0;
  43. fixed3 Cd : COLOR;
  44. fixed3 I : TEXCOORD1;
  45. fixed3 R : TEXCOORD2;
  46. fixed3 L : TEXCOORD3;
  47. fixed falloff : TEXCOORD4;
  48. UNITY_FOG_COORDS(5)
  49. };
  50. sampler2D _MainTex;
  51. sampler2D _EmissionTex;
  52. sampler2D _GlossinessTex;
  53. float4 _MainTex_ST;
  54. samplerCUBE _IBLTexCube;
  55. fixed _EmissionStrength;
  56. fixed3 _IndirectSpecular;
  57. fixed _IndirectGlossiness;
  58. fixed3 _DirectSpecular;
  59. fixed _DirectGlossiness;
  60. half _FresnelExponent;
  61. #define GLOSSY_MIP_COUNT 6
  62. fixed3 SampleTexCube(samplerCUBE cube, half3 normal, half mip)
  63. {
  64. return texCUBElod(cube, half4(normal, mip));
  65. }
  66. v2f vert(appdata v)
  67. {
  68. v2f o;
  69. UNITY_INITIALIZE_OUTPUT(v2f, o);
  70. o.P = UnityObjectToClipPos(v.P);
  71. o.N = UnityObjectToWorldNormal(v.N);
  72. o.uv0 = TRANSFORM_TEX(v.uv0, _MainTex);
  73. float3 worldPos = mul(unity_ObjectToWorld, v.P).xyz;
  74. o.I = normalize(worldPos - _WorldSpaceCameraPos);
  75. o.R = reflect(o.I, o.N);
  76. o.L = reflect(_WorldSpaceLightPos0, o.N);
  77. half3 H = normalize((o.N + o.R) * 0.5);
  78. fixed mix = pow(1 - 0 - max(0.0, dot(H, -o.I)), _FresnelExponent);
  79. o.falloff = lerp(0.0, 1.0, mix);
  80. UNITY_TRANSFER_FOG(o, o.P);
  81. return o;
  82. }
  83. fixed4 frag(v2f i) : SV_Target
  84. {
  85. fixed4 tex = tex2D(_MainTex, i.uv0);
  86. fixed4 emission = tex2D(_EmissionTex, i.uv0) * _EmissionStrength;
  87. fixed glossiness = 1.0 - (tex2D(_GlossinessTex, i.uv0).a * _IndirectGlossiness);
  88. fixed3 directSpecular = pow(max(0.0, dot(i.L, i.I)), _DirectGlossiness) * _DirectSpecular * i.falloff;
  89. fixed3 indirectSpecular = SampleTexCube(_IBLTexCube, i.R, glossiness * GLOSSY_MIP_COUNT) * _IndirectSpecular * i.falloff;
  90. fixed edges = pow(1.0 - max(dot(-i.I, i.N), 0.0), 3.2);
  91. fixed4 color = tex * i.falloff + emission;
  92. color.rgb = color + indirectSpecular + directSpecular + (_DirectSpecular * edges);
  93. // apply fog
  94. UNITY_APPLY_FOG(i.fogCoord, color);
  95. return color;
  96. }
  97. ENDCG
  98. }
  99. }
  100. FallBack "Diffuse"
  101. }