PhysicsDemo: Add support of alpha testing to depth-prepass

This commit is contained in:
Jérôme Leclercq 2021-08-02 11:14:21 +02:00
parent ac08afe9f1
commit e98e46164c
3 changed files with 99 additions and 54 deletions

View File

@ -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<f32>
}
[layout(std140)]
struct InstanceData
{
worldMatrix: mat4<f32>,
invWorldMatrix: mat4<f32>
}
[layout(std140)]
struct ViewerData
{
projectionMatrix: mat4<f32>,
invProjectionMatrix: mat4<f32>,
viewMatrix: mat4<f32>,
invViewMatrix: mat4<f32>,
viewProjMatrix: mat4<f32>,
invViewProjMatrix: mat4<f32>,
renderTargetSize: vec2<f32>,
invRenderTargetSize: vec2<f32>,
eyePosition: vec3<f32>
}
external
{
[set(0), binding(0)] viewerData: uniform<ViewerData>,
[set(1), binding(0)] instanceData: uniform<InstanceData>,
[set(2), binding(0)] settings: uniform<BasicSettings>,
[set(2), binding(2)] MaterialAlphaMap: sampler2D<f32>,
}
// Fragment stage
struct FragIn
{
[location(0), cond(HasUV)] uv: vec2<f32>
}
[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<f32>,
[location(1), cond(HasUV)] uv: vec2<f32>
}
struct VertOut
{
[location(0), cond(HasUV)] uv: vec2<f32>,
[builtin(position)] position: vec4<f32>
}
[entry(vert)]
fn main(input: VertIn) -> VertOut
{
let output: VertOut;
output.position = viewerData.viewProjMatrix * instanceData.worldMatrix * vec4<f32>(input.pos, 1.0);
const if (HasUV)
output.uv = input.uv;
return output;
}

View File

@ -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<f32>,
invWorldMatrix: mat4<f32>
}
[layout(std140)]
struct ViewerData
{
projectionMatrix: mat4<f32>,
invProjectionMatrix: mat4<f32>,
viewMatrix: mat4<f32>,
invViewMatrix: mat4<f32>,
viewProjMatrix: mat4<f32>,
invViewProjMatrix: mat4<f32>,
renderTargetSize: vec2<f32>,
invRenderTargetSize: vec2<f32>,
eyePosition: vec3<f32>
}
external
{
[set(0), binding(0)] viewerData: uniform<ViewerData>,
[set(1), binding(0)] instanceData: uniform<InstanceData>
}
// Vertex stage
struct VertIn
{
[location(0)] pos: vec3<f32>,
[location(1), cond(HasUV)] uv: vec2<f32>
}
struct VertOut
{
[builtin(position)] position: vec4<f32>
}
[entry(vert)]
fn main(input: VertIn) -> VertOut
{
let output: VertOut;
output.position = viewerData.viewProjMatrix * instanceData.worldMatrix * vec4<f32>(input.pos, 1.0);
return output;
}

View File

@ -79,7 +79,7 @@ int main()
auto customSettings = Nz::BasicMaterial::GetSettings()->GetBuilderData(); auto customSettings = Nz::BasicMaterial::GetSettings()->GetBuilderData();
customSettings.shaders.clear(); customSettings.shaders.clear();
customSettings.shaders.emplace_back(std::make_shared<Nz::UberShader>(Nz::ShaderStageType::Vertex, Nz::ShaderLang::Parse(resourceDir / "depth_vert.nzsl"))); customSettings.shaders.emplace_back(std::make_shared<Nz::UberShader>(Nz::ShaderStageType::Fragment | Nz::ShaderStageType::Vertex, Nz::ShaderLang::Parse(resourceDir / "depth_pass.nzsl")));
auto depthSettings = std::make_shared<Nz::MaterialSettings>(std::move(customSettings)); auto depthSettings = std::make_shared<Nz::MaterialSettings>(std::move(customSettings));
@ -104,6 +104,9 @@ int main()
basicMat.SetDiffuseMap(Nz::Texture::LoadFromFile(resourceDir / "Spaceship/Texture/diffuse.png", texParams)); basicMat.SetDiffuseMap(Nz::Texture::LoadFromFile(resourceDir / "Spaceship/Texture/diffuse.png", texParams));
basicMat.SetDiffuseSampler(samplerInfo); basicMat.SetDiffuseSampler(samplerInfo);
Nz::BasicMaterial basicMatDepth(*depthPass);
basicMatDepth.SetAlphaMap(Nz::Texture::LoadFromFile(resourceDir / "alphatile.png", texParams));
std::shared_ptr<Nz::Model> model = std::make_shared<Nz::Model>(std::move(gfxMesh)); std::shared_ptr<Nz::Model> model = std::make_shared<Nz::Model>(std::move(gfxMesh));
for (std::size_t i = 0; i < model->GetSubMeshCount(); ++i) for (std::size_t i = 0; i < model->GetSubMeshCount(); ++i)
model->SetMaterial(i, material); model->SetMaterial(i, material);
@ -226,7 +229,10 @@ int main()
case Nz::WindowEventType::KeyPressed: case Nz::WindowEventType::KeyPressed:
if (event.key.virtualKey == Nz::Keyboard::VKey::A) if (event.key.virtualKey == Nz::Keyboard::VKey::A)
{
basicMat.EnableAlphaTest(!basicMat.IsAlphaTestEnabled()); basicMat.EnableAlphaTest(!basicMat.IsAlphaTestEnabled());
basicMatDepth.EnableAlphaTest(!basicMatDepth.IsAlphaTestEnabled());
}
else if (event.key.virtualKey == Nz::Keyboard::VKey::B) else if (event.key.virtualKey == Nz::Keyboard::VKey::B)
{ {
showColliders = !showColliders; showColliders = !showColliders;