Graphics/PhongMaterial: Add option to disable shadow mapping (and disable it by default on web)

This commit is contained in:
SirLynix 2023-04-10 17:25:18 +02:00
parent 44c42a85c2
commit 2c6191987f
2 changed files with 45 additions and 31 deletions

View File

@ -46,10 +46,17 @@ namespace Nz
settings.AddValueProperty<Color>("AmbientColor", Color::White());
settings.AddValueProperty<Color>("SpecularColor", Color::White());
settings.AddValueProperty<float>("Shininess", 2.f);
#ifndef NAZARA_PLATFORM_WEB
settings.AddValueProperty<bool>("ShadowMapping", true);
#else
// FIXME: Shadowmapping is currently broken on web because WebGL doesn't support non-constant array indexing
settings.AddValueProperty<bool>("ShadowMapping", false);
#endif
settings.AddTextureProperty("EmissiveMap", ImageType::E2D);
settings.AddTextureProperty("HeightMap", ImageType::E2D);
settings.AddTextureProperty("NormalMap", ImageType::E2D);
settings.AddTextureProperty("SpecularMap", ImageType::E2D);
settings.AddPropertyHandler(std::make_unique<OptionValuePropertyHandler>("ShadowMapping", "EnableShadowMapping"));
settings.AddPropertyHandler(std::make_unique<TexturePropertyHandler>("EmissiveMap", "HasEmissiveTexture"));
settings.AddPropertyHandler(std::make_unique<TexturePropertyHandler>("HeightMap", "HasHeightMap"));
settings.AddPropertyHandler(std::make_unique<TexturePropertyHandler>("NormalMap", "HasNormalMap"));

View File

@ -17,6 +17,7 @@ option HasAlphaTexture: bool = false;
option AlphaTest: bool = false;
// Phong material options
option EnableShadowMapping: bool = true;
option HasEmissiveTexture: bool = false;
option HasHeightTexture: bool = false;
option HasNormalTexture: bool = false;
@ -209,36 +210,39 @@ fn main(input: VertToFrag) -> FragOut
specFactor = pow(specFactor, settings.Shininess);
let shadowFactor = 1.0;
if (light.invShadowMapSize.x > 0.0)
const if (EnableShadowMapping)
{
shadowFactor = 0.0;
let sampleDir = vec3[f32](lightToPosNorm.x, lightToPosNorm.y, -lightToPosNorm.z);
const sampleCount = 4;
const offset = 0.005;
const invSampleCount = 1.0 / f32(sampleCount);
const start = vec3[f32](offset * 0.5, offset * 0.5, offset * 0.5);
const shadowContribution = 1.0 / f32(sampleCount * sampleCount * sampleCount);
[unroll]
for x in 0 -> sampleCount
if (light.invShadowMapSize.x > 0.0)
{
shadowFactor = 0.0;
let sampleDir = vec3[f32](lightToPosNorm.x, lightToPosNorm.y, -lightToPosNorm.z);
const sampleCount = 4;
const offset = 0.005;
const invSampleCount = 1.0 / f32(sampleCount);
const start = vec3[f32](offset * 0.5, offset * 0.5, offset * 0.5);
const shadowContribution = 1.0 / f32(sampleCount * sampleCount * sampleCount);
[unroll]
for y in 0 -> sampleCount
for x in 0 -> sampleCount
{
[unroll]
for z in 0 -> sampleCount
for y in 0 -> sampleCount
{
let dirOffset = vec3[f32](f32(x), f32(y), f32(z)) * invSampleCount * offset - start;
let sampleDir = sampleDir + dirOffset;
[unroll]
for z in 0 -> sampleCount
{
let dirOffset = vec3[f32](f32(x), f32(y), f32(z)) * invSampleCount * offset - start;
let sampleDir = sampleDir + dirOffset;
let depth = shadowMapsCube[i].Sample(sampleDir).r;
depth = LinearizeDepth(depth, 0.01, lightRadius);
let depth = shadowMapsCube[i].Sample(sampleDir).r;
depth = LinearizeDepth(depth, 0.01, lightRadius);
if (depth > dist)
shadowFactor += shadowContribution;
if (depth > dist)
shadowFactor += shadowContribution;
}
}
}
}
@ -273,21 +277,24 @@ fn main(input: VertToFrag) -> FragOut
specFactor = pow(specFactor, settings.Shininess);
let shadowFactor = 1.0;
if (light.invShadowMapSize.x > 0.0)
const if (EnableShadowMapping)
{
let shadowCoords = input.lightProjPos[i].xyz / input.lightProjPos[i].w;
shadowFactor = 0.0;
[unroll]
for x in -1 -> 2
if (light.invShadowMapSize.x > 0.0)
{
let shadowCoords = input.lightProjPos[i].xyz / input.lightProjPos[i].w;
shadowFactor = 0.0;
[unroll]
for y in -1 -> 2
for x in -1 -> 2
{
let coords = shadowCoords.xy + vec2[f32](f32(x), f32(y)) * light.invShadowMapSize;
shadowFactor += shadowMaps2D[i].SampleDepthComp(coords, shadowCoords.z).r;
[unroll]
for y in -1 -> 2
{
let coords = shadowCoords.xy + vec2[f32](f32(x), f32(y)) * light.invShadowMapSize;
shadowFactor += shadowMaps2D[i].SampleDepthComp(coords, shadowCoords.z).r;
}
}
shadowFactor /= 9.0;
}
shadowFactor /= 9.0;
}
lightAmbient += attenuationFactor * light.color.rgb * lightAmbientFactor * settings.AmbientColor.rgb;