Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: add namespaces for features #431

Merged
merged 10 commits into from
Aug 19, 2024
31 changes: 17 additions & 14 deletions features/Cloud Shadows/Shaders/CloudShadows/CloudShadows.hlsli
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,23 @@ TextureCube<float4> cloudShadowsTexture : register(t27);
#define PlanetRadius (6371e3f / 1.428e-2)
#define RcpHPlusR (1.0 / (CloudHeight + PlanetRadius))

float3 GetCloudShadowSampleDir(float3 rel_pos, float3 eye_to_sun)
namespace CloudShadows
{
float r = PlanetRadius;
float3 p = (rel_pos + float3(0, 0, r)) * RcpHPlusR;
float dotprod = dot(p, eye_to_sun);
float lengthsqr = dot(p, p);
float t = -dotprod + sqrt(dotprod * dotprod - dot(p, p) + 1);
float3 v = (p + eye_to_sun * t) * (r + CloudHeight) - float3(0, 0, r);
return v;
}
float3 GetCloudShadowSampleDir(float3 rel_pos, float3 eye_to_sun)
{
float r = PlanetRadius;
float3 p = (rel_pos + float3(0, 0, r)) * RcpHPlusR;
float dotprod = dot(p, eye_to_sun);
float lengthsqr = dot(p, p);
float t = -dotprod + sqrt(dotprod * dotprod - dot(p, p) + 1);
float3 v = (p + eye_to_sun * t) * (r + CloudHeight) - float3(0, 0, r);
return v;
}

float3 GetCloudShadowMult(float3 worldPosition, SamplerState textureSampler)
{
float3 cloudSampleDir = GetCloudShadowSampleDir(worldPosition, DirLightDirectionShared.xyz).xyz;
float cloudCubeSample = cloudShadowsTexture.SampleLevel(textureSampler, cloudSampleDir, 0).w;
return 1.0 - saturate(cloudCubeSample);
float3 GetCloudShadowMult(float3 worldPosition, SamplerState textureSampler)
{
float3 cloudSampleDir = GetCloudShadowSampleDir(worldPosition, DirLightDirectionShared.xyz).xyz;
float cloudCubeSample = cloudShadowsTexture.SampleLevel(textureSampler, cloudSampleDir, 0).w;
return 1.0 - saturate(cloudCubeSample);
}
}
Original file line number Diff line number Diff line change
@@ -1,79 +1,82 @@
TextureCube<float4> specularTexture : register(t64);

// https://www.unrealengine.com/en-US/blog/physically-based-shading-on-mobile
half2 EnvBRDFApprox(half Roughness, half NoV)
namespace DynamicCubemaps
{
const half4 c0 = { -1, -0.0275, -0.572, 0.022 };
const half4 c1 = { 1, 0.0425, 1.04, -0.04 };
half4 r = Roughness * c0 + c1;
half a004 = min(r.x * r.x, exp2(-9.28 * NoV)) * r.x + r.y;
half2 AB = half2(-1.04, 1.04) * a004 + r.zw;
return AB;
}
// https://www.unrealengine.com/en-US/blog/physically-based-shading-on-mobile
half2 EnvBRDFApprox(half Roughness, half NoV)
{
const half4 c0 = { -1, -0.0275, -0.572, 0.022 };
const half4 c1 = { 1, 0.0425, 1.04, -0.04 };
half4 r = Roughness * c0 + c1;
half a004 = min(r.x * r.x, exp2(-9.28 * NoV)) * r.x + r.y;
half2 AB = half2(-1.04, 1.04) * a004 + r.zw;
return AB;
}

#if !defined(WATER)

float3 GetDynamicCubemapSpecularIrradiance(float2 uv, float3 N, float3 VN, float3 V, float roughness, float distance)
{
float3 R = reflect(-V, N);
float level = roughness * 9.0;
float3 GetDynamicCubemapSpecularIrradiance(float2 uv, float3 N, float3 VN, float3 V, float roughness, float distance)
{
float3 R = reflect(-V, N);
float level = roughness * 9.0;

// Horizon specular occlusion
// https://marmosetco.tumblr.com/post/81245981087
float horizon = min(1.0 + dot(R, VN), 1.0);
horizon *= horizon * horizon;
// Horizon specular occlusion
// https://marmosetco.tumblr.com/post/81245981087
float horizon = min(1.0 + dot(R, VN), 1.0);
horizon *= horizon * horizon;

float3 specularIrradiance = specularTexture.SampleLevel(SampColorSampler, R, level).xyz;
specularIrradiance *= horizon;
specularIrradiance = sRGB2Lin(specularIrradiance);
float3 specularIrradiance = specularTexture.SampleLevel(SampColorSampler, R, level).xyz;
specularIrradiance *= horizon;
specularIrradiance = sRGB2Lin(specularIrradiance);

return specularIrradiance;
}
return specularIrradiance;
}

float3 GetDynamicCubemap(float2 uv, float3 N, float3 VN, float3 V, float roughness, float3 F0, float3 diffuseColor, float distance)
{
float3 R = reflect(-V, N);
float NoV = saturate(dot(N, V));
float3 GetDynamicCubemap(float2 uv, float3 N, float3 VN, float3 V, float roughness, float3 F0, float3 diffuseColor, float distance)
{
float3 R = reflect(-V, N);
float NoV = saturate(dot(N, V));

float level = roughness * 7.0;
float level = roughness * 7.0;

float2 specularBRDF = EnvBRDFApprox(roughness, NoV);
float2 specularBRDF = EnvBRDFApprox(roughness, NoV);

// Horizon specular occlusion
// https://marmosetco.tumblr.com/post/81245981087
float horizon = min(1.0 + dot(R, VN), 1.0);
horizon *= horizon * horizon;
// Horizon specular occlusion
// https://marmosetco.tumblr.com/post/81245981087
float horizon = min(1.0 + dot(R, VN), 1.0);
horizon *= horizon * horizon;

// Roughness dependent fresnel
// https://www.jcgt.org/published/0008/01/03/paper.pdf
float3 Fr = max(1.0.xxx - roughness.xxx, F0) - F0;
float3 S = Fr * pow(1.0 - NoV, 5.0);
// Roughness dependent fresnel
// https://www.jcgt.org/published/0008/01/03/paper.pdf
float3 Fr = max(1.0.xxx - roughness.xxx, F0) - F0;
float3 S = Fr * pow(1.0 - NoV, 5.0);

# if defined(DEFERRED)
return horizon * ((F0 + S) * specularBRDF.x + specularBRDF.y);
return horizon * ((F0 + S) * specularBRDF.x + specularBRDF.y);
# else
float3 specularIrradiance = specularTexture.SampleLevel(SampColorSampler, R, level).xyz;
specularIrradiance = sRGB2Lin(specularIrradiance);

return specularIrradiance * ((F0 + S) * specularBRDF.x + specularBRDF.y);
# endif
}

float3 GetDynamicCubemapFresnel(float2 uv, float3 N, float3 VN, float3 V, float roughness, float level, float3 diffuseColor, float distance)
{
float NoV = saturate(dot(N, V));
float2 specularBRDF = EnvBRDFApprox(roughness, NoV);
if (specularBRDF.y > 0.001) {
float3 R = reflect(-V, N);
float3 specularIrradiance = specularTexture.SampleLevel(SampColorSampler, R, level).xyz;
specularIrradiance = sRGB2Lin(specularIrradiance);

// Horizon specular occlusion
// https://marmosetco.tumblr.com/post/81245981087
float horizon = min(1.0 + dot(R, VN), 1.0);
specularIrradiance *= horizon * horizon;
return specularIrradiance * ((F0 + S) * specularBRDF.x + specularBRDF.y);
# endif
}

return specularIrradiance * specularBRDF.y;
float3 GetDynamicCubemapFresnel(float2 uv, float3 N, float3 VN, float3 V, float roughness, float level, float3 diffuseColor, float distance)
{
float NoV = saturate(dot(N, V));
float2 specularBRDF = EnvBRDFApprox(roughness, NoV);
if (specularBRDF.y > 0.001) {
float3 R = reflect(-V, N);
float3 specularIrradiance = specularTexture.SampleLevel(SampColorSampler, R, level).xyz;

// Horizon specular occlusion
// https://marmosetco.tumblr.com/post/81245981087
float horizon = min(1.0 + dot(R, VN), 1.0);
specularIrradiance *= horizon * horizon;

return specularIrradiance * specularBRDF.y;
}
return 0.0;
}
return 0.0;
#endif // !WATER
}
#endif
Loading
Loading