Log inGet started
Package

countingView.module

Newest version

This package holds more than this page lists. See all of its files in its world.

README.md

countingView module

Builds a camera view that counts, per pixel, how many fragments satisfy a depth predicate, and colours the count. overdraw and zfighting are both instances of it; a diagnostic of your own is one more.

local CountingView = require("@builtin::systems.renderDiagnostics.countingView")

return CountingView.new({
    view = "myView",              -- the name `capture pass=myView` selects
    geometry = "@geometry.all",   -- which drawables are counted
    depthCompare = "always",      -- which of their fragments count
    levels = 16,                  -- counts that fit before the sum saturates
    mapShader = asset.resolve("~.shaders.myViewMap", "shader"),
})

The return value is a .renderFeature module — render enqueues the view's passes, teardown releases them — so a feature's whole init.luau is this one call. The view's materials and map effect are built as the feature is created. Register the name separately with renderer.captureView.register so capture pass=<name> resolves to it, and hand the registration ready = function() return CountingView.ready("myView") end: a capture waits on it, so the first capture of the view reads the view rather than the scene the camera draws before the view's pipelines are built.

The knobs that decide the question

depthCompare runs against the scene depth the frame already resolved:

  • "always" counts every fragment the rasterizer produced, hidden ones included — the shading work a pixel costs.
  • "lessEqual" counts only fragments reaching the winning depth — one per pixel, unless surfaces are close enough that the depth buffer cannot tell them apart.

geometry narrows what is counted: "@geometry.all", "@geometry.opaque" or "@geometry.transparent".

depthBias and depthBiasSlope widen a "lessEqual" test from a point to a band toward the camera, stated as a material's depth bias is: a constant in depth-buffer steps, and a multiple of the fragment's own depth gradient across a pixel. zfighting uses the band to count the surfaces close enough for the frame's depth rounding to decide which one shows.

objectDepthBias = true draws each counted surface at the depth bias its own material states, added to the band, so a surface its material lifts toward the camera (a decal, an overlay) is counted at the depth the frame drew it at.

Reading the count

levels is how many counts fit before the accumulated sum saturates at 1.0, so a view's open-ended "this many or more" band belongs at or below that number.

mapShader is a @domain: post-process .shader asset declaring fn zero_count_color(n: i32) -> vec3<f32> — the view's reading of the decoded count, 0 where nothing was counted — over the shared decode body:

// @domain: post-process

fn zero_count_color(n: i32) -> vec3<f32> {
    ...
}

#include "~.shaders.countMap"

Flat, nameable bands read better from a still than a continuous ramp: the question is which band a region falls in, not how bright it is.