diff --git a/src/Nazara/Graphics/PredefinedMaterials.cpp b/src/Nazara/Graphics/PredefinedMaterials.cpp index c7e33e6e4..044a473a0 100644 --- a/src/Nazara/Graphics/PredefinedMaterials.cpp +++ b/src/Nazara/Graphics/PredefinedMaterials.cpp @@ -46,10 +46,17 @@ namespace Nz settings.AddValueProperty("AmbientColor", Color::White()); settings.AddValueProperty("SpecularColor", Color::White()); settings.AddValueProperty("Shininess", 2.f); +#ifndef NAZARA_PLATFORM_WEB + settings.AddValueProperty("ShadowMapping", true); +#else + // FIXME: Shadowmapping is currently broken on web because WebGL doesn't support non-constant array indexing + settings.AddValueProperty("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("ShadowMapping", "EnableShadowMapping")); settings.AddPropertyHandler(std::make_unique("EmissiveMap", "HasEmissiveTexture")); settings.AddPropertyHandler(std::make_unique("HeightMap", "HasHeightMap")); settings.AddPropertyHandler(std::make_unique("NormalMap", "HasNormalMap")); diff --git a/src/Nazara/Graphics/Resources/Shaders/PhongMaterial.nzsl b/src/Nazara/Graphics/Resources/Shaders/PhongMaterial.nzsl index 40a0845b2..7d852e503 100644 --- a/src/Nazara/Graphics/Resources/Shaders/PhongMaterial.nzsl +++ b/src/Nazara/Graphics/Resources/Shaders/PhongMaterial.nzsl @@ -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;