Docs / Rendering
Decals
A decal is a mark a surface keeps after something happened to it: a bullet hole, a scorch, a footprint, a splash. DecalSet holds the marks and ages them on the fixed step, with no GPU, so it runs under swift test. DecalRenderer draws whatever the set holds as small quads lying flat on their surfaces, each showing one cell of a texture atlas. It's the same split as particles: the simulation in one type, the drawing in another.
The types
| Type | What it is |
|---|---|
Decal | One mark: position, normal, age, size, uvRect and tint. |
DecalAtlas | A sheet of marks cut into equal cells, columns across and rows down. |
DecalSet | The marks a world is keeping, with a capacity, a hold and a fade. |
DecalRenderer | Draws a DecalSet in one instanced draw. |
DecalRendererError | Why a renderer could not be made: .pipelineUnavailable or .couldNotCreate, each carrying a reason as text. |
The atlas
Put every mark a game uses on one texture and cut it into a grid. DecalAtlas(columns:rows:) describes the grid, and rect(column:row:) gives the cell to sample as (u0, v0, du, dv). That's the value a mark carries in uvRect.
let atlas = DecalAtlas(columns: 3, rows: 4) atlas.rect(column: 1, row: 3) // (1/3, 0.75, 1/3, 0.25)
Row 0 is the top of the image. Out-of-range indices are clamped onto the sheet, so a mark never samples past the edge, and zero or negative columns or rows count as one. DecalAtlas.whole is the whole texture as a single cell.
ParticleRenderer takes a DecalAtlas too, for its sprite sheet. It's the same grid type, not a shared texture.
Leaving marks
public init(capacity: Int = 256, holdSeconds: Float = .infinity, fadeSeconds: Float = 0, surfaceOffset: Float = 0.03)
capacityis how many marks are kept at once.holdSecondsis how long a mark stays at full strength. By default that's forever: marks stay untilclear()or until the set fills.fadeSecondsis how long a held mark takes to fade out. Zero removes it the moment its hold ends.surfaceOffsetis how far, in meters, each mark is lifted along its normal.
The two times are a design decision. If your marks carry information, like where a shot landed, hold them long. If they're just garnish, fade them fast.
mark(at:normal:size:uvRect:tint:) leaves a mark. Pass the point on the surface; the set does the lifting. The normal is normalized for you, and a zero normal counts as straight up. size is meters across. tint multiplies the art: rgb tints it and a is its opacity. White with an alpha of 1 draws the art exactly as it was made.
decals.mark(at: point, normal: normal, size: 0.12,
uvRect: atlas.rect(column: 2, row: 0)) // your game
step(deltaTime:) ages every mark and removes the ones that have faded. Call it from your fixed step, next to the particle system. opacity(of:) returns a mark's strength right now, from 1 down to 0. decals and count are for reading the state. A Decal's fields can be changed, but decals is read-only from outside, so the set owns the list.
When the set is full, the oldest mark goes, never the newest. The newest one is the mark that was just made, and it's the one the player is looking at. "Oldest" is decided by age, not by slot, so it stays right after marks have faded out of the middle of the list.
clear() removes every mark. Use it for a new level, a new round or a reloaded save.
Finding the surface
A mark needs a point and the normal there. Ask the hit test for the surface, not just the distance. Each of the engine's hit tests has a surface form that returns a SurfaceHit: what was struck (index), how far (distance), where (point) and the struck face's unit normal, facing the side the shot came from.
| Hit test | Surface form | The normal is |
|---|---|---|
Volume.firstHit(segmentFrom:to:against:) | Volume.firstSurfaceHit(segmentFrom:to:against:) | the face of the volume's worldBounds the shot entered |
StaticVolumes.firstHit(segmentFrom:to:) | StaticVolumes.firstSurfaceHit(segmentFrom:to:) | the same, for scenery that never moves |
CollisionMesh.firstHit(from:direction:limit:) | CollisionMesh.firstSurfaceHit(from:direction:limit:) | the struck triangle's, turned to face the shooter whichever way it was wound |
The ray forms (origin:direction:) are there too. Distances and the index are exactly what the distance-only forms give, so a game can switch without anything else changing.
// your game if let hit = Volume.firstSurfaceHit(segmentFrom: start, to: end, against: volumes) { decals.mark(at: hit.point, normal: hit.normal, size: 0.12, uvRect: atlas.rect(column: 0, row: 0)) }
A volume is faced as a box. Its distance comes from its worldBounds, the axis-aligned box around it, so its normal is that box's face: a crate turned on a deck is marked on the face of the box around it. For an exact face on something turned, build a CollisionMesh of it. A segment of no length enters nothing and returns nil, and a shot starting inside a box returns distance 0 with a normal facing back along the shot.
On ground with a height function, such as WorldGround.height(x:z:) or WorldRenderer.groundHeight(x:z:holes:), take the slope from the heights on either side:
// your game let e: Float = 0.25 let dx = ground.height(x: x + e, z: z) - ground.height(x: x - e, z: z) let dz = ground.height(x: x, z: z + e) - ground.height(x: x, z: z - e) let normal = simd_normalize(SIMD3(-dx, 2 * e, -dz))
Drawing
public init(device: MTLDevice, texture: MTLTexture, capacity: Int, colorPixelFormat: MTLPixelFormat = FrameRenderer.sceneColorPixelFormat, sampleCount: Int = 1) throws
texture is the atlas. Mipmap it, or marks seen at a distance shimmer. If you load it with TextureStore.texture(_:isColor:origin:bundle:), pass origin: .topLeft, because the renderer reads row 0 as the top of the image. capacity should be the set's capacity: marks beyond it aren't drawn. sampleCount must match the pass, which for the engine's scene is FrameRenderer.effectiveSampleCount. Make the renderer again when that changes. Add allocations (both buffers and the atlas) to the renderer's residency set.
Draw in encode, after the opaque scene and before particles, so dust thrown up by a strike hangs in front of the mark it left:
decalRenderer.draw(decals, into: encoder, frameSlot: frameSlot, viewProjection: viewProjection)
particleRenderer.draw(particles, into: encoder, frameSlot: frameSlot, viewProjection: viewProjection,
cameraRight: view.right, cameraUp: view.up)
- Depth is tested but never written. A mark behind a rock is hidden by it, and two overlapping marks don't punch holes in each other.
- Premultiplied "over" blending. The texel's alpha times the mark's opacity decides how much of the surface it covers.
- Clamped, never repeated. A repeating sampler would bleed the neighboring cell in along every edge.
- Both faces are drawn. A mark is visible from behind its surface too, where the surface itself usually hides it.
lightmultiplies every mark. It defaults to white, which draws them as made. Pass the light falling on the surfaces so marks darken with the scene.
draw skips marks that have fully faded and returns how many it drew. It leaves its pipeline, depth state and argument table bound, so anything drawn after it binds its own.
What it does not do
- No lighting per mark.
lightapplies to the whole draw. A mark in shadow is as bright as one in sun unless you darken itstintwhen you make it. - No projection onto geometry. A mark is a flat quad. Across a corner, or on ground that curves within its width, it hangs off the edge or sinks in. Keep marks small next to the shapes they land on.
- Marks don't follow moving things. Positions are in world space, and the set can't move a mark after it's made.
- No sorting. Overlapping translucent marks are drawn in the set's own order, not by distance from the camera.