# Depth, culling, and lighting

A colored triangle needs no depth test. A cube does: its nearest face should cover the farther faces regardless of submission order. Begin with the existing DemoApp cube, then change one pipeline option at a time.

```sh
dotnet run --project Xui/Apps/DemoApp/DemoApp.Desktop.csproj
```

Open **3D Rendering** on desktop or **GPU > 3D** in the phone gallery. Drag the cube to expose another face. This example uses vertex colors; it is not a lit material.

## Separate three causes of missing pixels

`DepthTestEnabled` compares incoming fragments against the depth buffer. `DepthWriteEnabled` controls whether successful fragments update that buffer. For ordinary opaque geometry, enable both. Drawing translucent surfaces needs an explicit ordering/blending strategy; simply disabling writes does not solve all transparency cases.

`CullMode` removes triangles according to their winding. If every second face disappears, inspect the vertex order before changing the camera. Temporarily use `GpuCullMode.None` to isolate a winding problem, then restore the intended culling mode.

Clipping is different again: a triangle beyond the near or far plane is outside the camera volume. Check [projection settings](../cameras/index.md) before changing depth state.

## Add lighting after the unlit scene works

A basic diffuse material needs surface normals, a light direction, and a dot product clamped at zero. Transform normals consistently with the space used for the light. Nonuniform scaling requires a suitable normal transform; reusing the position matrix blindly distorts lighting.

The current basic hardware vertex layout is position plus color. A normal-based material requires an extended vertex layout and matching shader support. Android's current Vulkan path rejects general layouts beyond its cube contract. This guide therefore does not present a portable lit-mesh example or imply that setting a “lighting” property enables one.

Use the unlit cube to establish transforms, depth, and culling on each target backend first. Then add a material only for the capabilities your app checks. Test the final pixels on real targets; an SVG fallback cannot validate GPU depth or lighting.
