b2dSetDepth

Category: 2D Overlay

Syntax:

b2dSetDepth:Void(z:Double)


Parameters

Returns

Void



Summary

Sets the depth used by subsequent primitive draws, enabling per-pixel occlusion.

Takes z, from 0.0 (nearest, wins) to 1.0 (farthest). Values outside that range are clamped. Every primitive drawn afterwards carries that depth until it is changed.

Returns nothing.

This writes into a real depth buffer, so occlusion is resolved per pixel by the GPU rather than by the order you happened to draw in. Draw your faces in any order, give each one a depth, and they occlude correctly - which is what makes a "3D in 2D" renderer possible. Per-object sorting cannot do the job: adjacent columns of different heights interleave in ways no sort resolves.

Primitives only. b2dDrawRect, b2dDrawQuad, b2dDrawLine and b2dDrawCircle are depth-tested. Images and text never are - they always draw in call order, so a HUD stays on top without any thought.

Existing programs are unaffected. The default depth is 0, the buffer is cleared to 1.0 (far) at the start of every frame, and the test is less-than-or-equal - so anything that never calls this passes the test, and equal depths still resolve by draw order. That is ordinary painter's behaviour, exactly as before.

Two places where the depth test does not apply: primitives drawn into a render target (b2dCreateRenderTarget), because a render target carries no depth buffer, and any drawing done while a screen shader is applied, because the frame is composited through an offscreen target.

The depth is reset to 0 at the start of each frame.

View detailed documentation online



Example
; Faces in arbitrary order - the GPU sorts it out per pixel
b2dSetDepth(dist / FAR)
b2dDrawQuad(x1,y1, x2,y2, x3,y3, x4,y4, 1)

b2dSetDepth(0.0)                   ; HUD in front of everything
b2dDrawText("SCORE", 8, 8, 0, font)

BambooBasic © 2026 Michael Denathorn