Advertisement
plaYer2k

Space Engineers - EnvAmbient.h (darkness shader fix)

Jul 19th, 2016
604
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.74 KB | None | 0 0
  1. #ifndef ENVAMBIENT_H__
  2. #define ENVAMBIENT_H__
  3.  
  4. #include <Lighting/brdf.h>
  5. #include <frame.h>
  6.  
  7. Texture2D<float2> AmbientBRDFTex : register( MERGE(t,AMBIENT_BRDF_LUT_SLOT) );
  8.  
  9. TextureCube<float4> SkyboxTex : register( MERGE(t,SKYBOX_SLOT) );
  10. TextureCube<float4> SkyboxIBLTex : register( MERGE(t,SKYBOX_IBL_SLOT) );
  11.  
  12. TextureCube<float4> Skybox2Tex : register( MERGE(t,SKYBOX2_SLOT) );
  13. TextureCube<float4> Skybox2IBLTex : register( MERGE(t,SKYBOX2_IBL_SLOT) );
  14.  
  15. //#define ENV_IBL_SLOT 20
  16. //TextureCube<float4> ProbeTex : register( MERGE(t,ENV_IBL_SLOT) );
  17.  
  18. static const float IBL_MAX_MIPMAP = 8;
  19. static const float SKYBOX_NIGHT_INTENSITY = 0.25f;
  20.  
  21. #define SKYBOX_BLENDING
  22.  
  23. float3 SkyboxColor(float3 v)
  24. {
  25. float3 sample = SkyboxTex.Sample(TextureSampler, v).xyz;
  26. float3 sample1 = Skybox2Tex.Sample(TextureSampler, v).xyz * SKYBOX_NIGHT_INTENSITY;
  27.  
  28. return lerp(sample, sample1, frame_.skyboxBlend);
  29. }
  30.  
  31. float3 SkyboxColorLod(float3 v, float lod)
  32. {
  33. float3 sample = SkyboxTex.Sample(TextureSampler, v).xyz;
  34. float3 sample1 = Skybox2Tex.SampleLevel(TextureSampler, v, lod).xyz;
  35.  
  36. return lerp(sample, sample1, frame_.skyboxBlend);
  37. }
  38.  
  39. float3 ambient_specular(float3 f0, float gloss, float3 N, float3 V)
  40. {
  41. return 0.000001f;
  42. float nv = saturate(dot(N, V));
  43. float3 R = -reflect(V, N);
  44. R.x = -R.x;
  45.  
  46. float3 sample = SkyboxIBLTex.SampleLevel(TextureSampler, R, (1 - gloss) * IBL_MAX_MIPMAP).xyz;
  47. float3 sample1 = Skybox2IBLTex.SampleLevel(TextureSampler, R, (1 - gloss) * IBL_MAX_MIPMAP).xyz;
  48. float2 env_brdf = AmbientBRDFTex.Sample(DefaultSampler, float2(gloss, nv));
  49.  
  50. return lerp(sample, sample1, smoothstep(0, 1, frame_.skyboxBlend)) * ( f0 * env_brdf.x + env_brdf.y) * frame_.env_mult;
  51. }
  52.  
  53. float3 ambient_diffuse(float3 N, float global_ambient)
  54. {
  55. N.x = -N.x;
  56. float3 sample0 = SkyboxIBLTex.SampleLevel(TextureSampler, N, IBL_MAX_MIPMAP).xyz;
  57. float3 sample1 = Skybox2IBLTex.SampleLevel(TextureSampler, N, IBL_MAX_MIPMAP).xyz;
  58. float3 skybox = lerp(sample0, sample1, smoothstep(0, 1, frame_.skyboxBlend));
  59.  
  60. return (global_ambient + skybox) * frame_.env_mult;
  61. }
  62.  
  63. float3 ambient_diffuse(float3 N)
  64. {
  65. return ambient_diffuse(N, 0.000075f);
  66. }
  67.  
  68. float3 ambient_diffuse(float3 albedo, float3 normal, float depth)
  69. {
  70. return 0.000001f;
  71. float global_ambient = max(min(frame_.skyboxBrightness * lerp(0.000075f, 1.0f, saturate(depth / 100000.0f)), 0.075f), 0.01);
  72. global_ambient += (1 - frame_.skyboxBrightness) * 0.0015f;
  73. return albedo * ambient_diffuse(normal, global_ambient);
  74. }
  75.  
  76. static const uint SamplesNum = 64;
  77. static const float EnvProbeRes = 2048;
  78. static const float EnvProbeMips = 11;
  79.  
  80. float3 ambientSpecularIS(float3 f0, float gloss, float3 N, float3 V) {
  81. float4 acc = 0;
  82.  
  83. const float SamplesRcp = 1 / SamplesNum;
  84. const float NV = saturate(dot(N, V));
  85.  
  86. [unroll]
  87. for(uint i=0; i< SamplesNum; ++i) {
  88. float2 xi = hammersley2d(i, SamplesNum);
  89. float pdf;
  90. float a = remap_gloss(gloss);
  91. float3 H = importance_sample_ggx(xi, a, N, pdf);
  92.  
  93. float3 L = 2 * dot( V, H ) * H - V;
  94. float NL = saturate(dot( N, L ));
  95. float VL = saturate(dot( V, L ));
  96. float NH = saturate(dot( N, H ));
  97. float VH = saturate(dot( V, H ));
  98. [flatten]
  99. if( NL > 0 )
  100. {
  101. float texelSolidAngle = 4 * M_PI / (6 * EnvProbeRes * EnvProbeRes);
  102. float sampleSolidAngle = 1 / ( pdf * SamplesNum );
  103. float lod = gloss == 1 ? 0 : 0.5 * log2((float)(sampleSolidAngle/texelSolidAngle));
  104. float3 sample = SkyboxColorLod(L, lod);
  105. acc.xyz += sample * NL;
  106. acc.w += NL;
  107. }
  108. }
  109.  
  110. float3 env = acc.xyz / acc.w;
  111. float2 env_brdf = AmbientBRDFTex.Sample(DefaultSampler, float2(gloss, NV));
  112. return env * ( f0 * env_brdf.x + env_brdf.y);
  113. }
  114.  
  115.  
  116. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement