From e98e46164c67db2d755ed32648676803920767c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Leclercq?= Date: Mon, 2 Aug 2021 11:14:21 +0200 Subject: [PATCH] PhysicsDemo: Add support of alpha testing to depth-prepass --- bin/resources/depth_pass.nzsl | 92 +++++++++++++++++++++++++++++++++++ bin/resources/depth_vert.nzsl | 53 -------------------- examples/PhysicsDemo/main.cpp | 8 ++- 3 files changed, 99 insertions(+), 54 deletions(-) create mode 100644 bin/resources/depth_pass.nzsl delete mode 100644 bin/resources/depth_vert.nzsl diff --git a/bin/resources/depth_pass.nzsl b/bin/resources/depth_pass.nzsl new file mode 100644 index 000000000..c15edf127 --- /dev/null +++ b/bin/resources/depth_pass.nzsl @@ -0,0 +1,92 @@ +option HAS_DIFFUSE_TEXTURE: bool; +option HAS_ALPHA_TEXTURE: bool; +option ALPHA_TEST: bool; + +const HasUV = ALPHA_TEST && (HAS_DIFFUSE_TEXTURE || HAS_ALPHA_TEXTURE); + +[layout(std140)] +struct BasicSettings +{ + AlphaThreshold: f32, + DiffuseColor: vec4 +} + +[layout(std140)] +struct InstanceData +{ + worldMatrix: mat4, + invWorldMatrix: mat4 +} + +[layout(std140)] +struct ViewerData +{ + projectionMatrix: mat4, + invProjectionMatrix: mat4, + viewMatrix: mat4, + invViewMatrix: mat4, + viewProjMatrix: mat4, + invViewProjMatrix: mat4, + renderTargetSize: vec2, + invRenderTargetSize: vec2, + eyePosition: vec3 +} + +external +{ + [set(0), binding(0)] viewerData: uniform, + [set(1), binding(0)] instanceData: uniform, + [set(2), binding(0)] settings: uniform, + [set(2), binding(2)] MaterialAlphaMap: sampler2D, +} + +// Fragment stage +struct FragIn +{ + [location(0), cond(HasUV)] uv: vec2 +} + +[entry(frag), cond(ALPHA_TEST)] +fn main(input: FragIn) +{ + let alpha = settings.DiffuseColor.a; + const if (HAS_DIFFUSE_TEXTURE) + // TODO: alpha *= MaterialDiffuseMap.Sample(input.uv).a; + alpha = alpha * MaterialDiffuseMap.Sample(input.uv).a; + + const if (HAS_ALPHA_TEXTURE) + // TODO: alpha *= MaterialAlphaMap.Sample(input.uv).x + alpha = alpha * MaterialAlphaMap.Sample(input.uv).x; + + // TODO: const assert? + if (alpha < settings.AlphaThreshold) + discard; +} + +[entry(frag), cond(!ALPHA_TEST)] +fn main() {} + +// Vertex stage +struct VertIn +{ + [location(0)] pos: vec3, + [location(1), cond(HasUV)] uv: vec2 +} + +struct VertOut +{ + [location(0), cond(HasUV)] uv: vec2, + [builtin(position)] position: vec4 +} + +[entry(vert)] +fn main(input: VertIn) -> VertOut +{ + let output: VertOut; + output.position = viewerData.viewProjMatrix * instanceData.worldMatrix * vec4(input.pos, 1.0); + + const if (HasUV) + output.uv = input.uv; + + return output; +} diff --git a/bin/resources/depth_vert.nzsl b/bin/resources/depth_vert.nzsl deleted file mode 100644 index cece2692d..000000000 --- a/bin/resources/depth_vert.nzsl +++ /dev/null @@ -1,53 +0,0 @@ -option HAS_DIFFUSE_TEXTURE: bool; -option HAS_ALPHA_TEXTURE: bool; -option ALPHA_TEST: bool; - -const HasUV = HAS_DIFFUSE_TEXTURE || HAS_ALPHA_TEXTURE; - -[layout(std140)] -struct InstanceData -{ - worldMatrix: mat4, - invWorldMatrix: mat4 -} - -[layout(std140)] -struct ViewerData -{ - projectionMatrix: mat4, - invProjectionMatrix: mat4, - viewMatrix: mat4, - invViewMatrix: mat4, - viewProjMatrix: mat4, - invViewProjMatrix: mat4, - renderTargetSize: vec2, - invRenderTargetSize: vec2, - eyePosition: vec3 -} - -external -{ - [set(0), binding(0)] viewerData: uniform, - [set(1), binding(0)] instanceData: uniform -} - -// Vertex stage -struct VertIn -{ - [location(0)] pos: vec3, - [location(1), cond(HasUV)] uv: vec2 -} - -struct VertOut -{ - [builtin(position)] position: vec4 -} - -[entry(vert)] -fn main(input: VertIn) -> VertOut -{ - let output: VertOut; - output.position = viewerData.viewProjMatrix * instanceData.worldMatrix * vec4(input.pos, 1.0); - - return output; -} diff --git a/examples/PhysicsDemo/main.cpp b/examples/PhysicsDemo/main.cpp index 1473147d4..8b5ec3efd 100644 --- a/examples/PhysicsDemo/main.cpp +++ b/examples/PhysicsDemo/main.cpp @@ -79,7 +79,7 @@ int main() auto customSettings = Nz::BasicMaterial::GetSettings()->GetBuilderData(); customSettings.shaders.clear(); - customSettings.shaders.emplace_back(std::make_shared(Nz::ShaderStageType::Vertex, Nz::ShaderLang::Parse(resourceDir / "depth_vert.nzsl"))); + customSettings.shaders.emplace_back(std::make_shared(Nz::ShaderStageType::Fragment | Nz::ShaderStageType::Vertex, Nz::ShaderLang::Parse(resourceDir / "depth_pass.nzsl"))); auto depthSettings = std::make_shared(std::move(customSettings)); @@ -104,6 +104,9 @@ int main() basicMat.SetDiffuseMap(Nz::Texture::LoadFromFile(resourceDir / "Spaceship/Texture/diffuse.png", texParams)); basicMat.SetDiffuseSampler(samplerInfo); + Nz::BasicMaterial basicMatDepth(*depthPass); + basicMatDepth.SetAlphaMap(Nz::Texture::LoadFromFile(resourceDir / "alphatile.png", texParams)); + std::shared_ptr model = std::make_shared(std::move(gfxMesh)); for (std::size_t i = 0; i < model->GetSubMeshCount(); ++i) model->SetMaterial(i, material); @@ -226,7 +229,10 @@ int main() case Nz::WindowEventType::KeyPressed: if (event.key.virtualKey == Nz::Keyboard::VKey::A) + { basicMat.EnableAlphaTest(!basicMat.IsAlphaTestEnabled()); + basicMatDepth.EnableAlphaTest(!basicMatDepth.IsAlphaTestEnabled()); + } else if (event.key.virtualKey == Nz::Keyboard::VKey::B) { showColliders = !showColliders;