Help rendering image with depth info #23778
|
Hello, I've made a few small projects with bevy before, but I'm still quite new. I want to render an image retrieved from a compute shader while preserving the depth of pixels in the image (the depth data is an array of f32s) so that rasterized objects is the scene will be properly occluded by certain parts of the image. I'm unaware of any way to do this in bevy, so any and all suggestions would be great. |
Replies: 1 comment
|
Short answer: yes, this is possible — you need to write your compute-shader output to both a color attachment and a depth attachment, and then make sure your main 3D pass uses that depth texture as its depth input rather than a freshly-cleared one. There are three ways to do it in Bevy 0.14+ depending on how much of the render pipeline you want to own: 1. Minimal: full-screen triangle pass that writes depthThis is usually what people actually want — the "sprite-ish with depth" look.
The fragment shader is tiny: @group(0) @binding(0) var color_tex: texture_2d<f32>;
@group(0) @binding(1) var depth_tex: texture_2d<f32>;
@group(0) @binding(2) var samp: sampler;
struct Out {
@location(0) color: vec4<f32>,
@builtin(frag_depth) depth: f32,
}
@fragment
fn fs(@builtin(position) pos: vec4<f32>, @location(0) uv: vec2<f32>) -> Out {
let c = textureSample(color_tex, samp, uv);
let d = textureSample(depth_tex, samp, uv).r;
return Out(c, d);
}Two crucial details most people get wrong:
2. Medium: inject a depth texture into the main pass' attachmentInstead of a separate pass, you can preload the main 3D pass's depth attachment with your compute output:
This is a bigger change — you need to override 3. Heavy: full custom render graphIf you need more than depth (e.g., stencil, G-buffer terms, MRT), the full solution is to fork the 3D pipeline graph. See Concrete starterPieces I'd stitch together if I were writing this:
The What to watch out for
If you drop a repro (even pseudocode with the sizes/types), I can give more specific guidance on which of the three paths suits it. |
Short answer: yes, this is possible — you need to write your compute-shader output to both a color attachment and a depth attachment, and then make sure your main 3D pass uses that depth texture as its depth input rather than a freshly-cleared one.
There are three ways to do it in Bevy 0.14+ depending on how much of the render pipeline you want to own:
1. Minimal: full-screen triangle pass that writes depth
This is usually what people actually want — the "sprite-ish with depth" look.
TextureDimension::D2color image (RGBA8)TextureDimension::D2depth image (Depth32Float) built from yourf32arrayRenderGraphnode that runs before your …