Vixen
02b45cc4
csharp
public sealed class GlobalDistanceField

Every instance's field composited into a few nested volumes that follow the camera.

No guide page documents this yet — the page shows what the code says about itself.

Remarks

A tracer cannot walk a list of instances — that is the cost the field was built to avoid. It walks one volume, and this is the volume: a clipmap of LevelCount cubes sharing a resolution and doubling in extent, so detail is fine where the camera is and coarse where it is far. Level zero might be sixteen metres across at a quarter of a metre a cell, and level three a hundred and twenty-eight at two metres, for the same memory each.

Every level is snapped to its own cell grid. A level centred exactly on the camera slides its sampling grid under static geometry as the camera moves, and the same fixed wall shimmers between two rows of cells. This is the same defect Vixen.Rendering.ShadowCascades fixes by snapping a cascade to whole texels, it has the same cause, and it has the same fix — which is worth saying because the two look unrelated until both are written down.

A level clamps to what it can know. Nothing outside a level's own cube was consulted when it was built, so a cell that found nothing reports MaxDistanceOf rather than infinity. A tracer reading it may step that far and no further, which is exactly right: the answer is "at least this much", and a step of "at least this much" is always safe.

Each level is a MeshDistanceField. The type is a sampled distance volume with a trilinear read, which is what a clipmap level is — over a whole scene rather than over one mesh. Reusing it means one interpolation, tested once.

Fields and properties (7)

  • public int Resolution

    How many samples along each axis, in every level.

  • public int LevelCount

    How many nested cubes there are.

  • public Vector3 ViewPosition

    Where the clipmap was last centred, before snapping.

  • public bool HasContent

    Whether Update has run.

  • public long Reused

    How many cells the last update kept rather than recomputed.

  • public float FinestExtent

    Half the width of the finest level.

  • public float this[int level, int x, int y, int z]

    The signed distance at one of a level's grid points.

Methods (11)

  • public GlobalDistanceField(int resolution = 32, float finestExtent = 8, int levelCount = 4)

    Builds an empty clipmap.

  • public float ExtentOf(int level)

    Half the width of one level, in world units.

  • public float CellSizeOf(int level)

    How far apart one level's samples are, in world units.

  • public float MaxDistanceOf(int level)

    The furthest a level is willing to claim anything is.

  • public BoundingBox BoundsOf(int level)

    The box one level covers, as it was last snapped.

  • public ReadOnlySpan<float> LevelData(int level)

    One level's samples, in the order a volume texture wants them.

  • public void Update(Vector3 viewPosition, ReadOnlySpan<DistanceFieldInstance> instances, bool parallel = true, bool scroll = false)

    Recomposites every level around a view position.

  • public float Sample(Vector3 position)

    The signed distance at a world-space point, off the finest level that has it.

  • public Vector3 SampleGradient(Vector3 position)

    Which way the distance grows fastest at a point.

  • public bool TryLevelFor(Vector3 world, out int level)

    Which is the finest level covering a point.

  • public static Vector3 Snap(Vector3 position, float cell)

    Moves a point onto a whole multiple of a cell.

Used by (14)

  • ArenaThirdPersonShooter
  • ArenaIlluminationThirdPersonShooter
  • CompositorAssetTestsVixen.Rendering.Tests
  • CompositorBuilderVixen.Rendering
  • DistanceFieldTracerTestsVixen.Rendering.DistanceFields.Tests
  • GlobalDistanceFieldRendererVixen.Rendering
  • GlobalDistanceFieldRendererTestsVixen.Rendering.Tests
  • GlobalDistanceFieldTestsVixen.Rendering.DistanceFields.Tests
  • GlobalDistanceFieldTextureVixen.Rendering
  • GlobalDistanceFieldTextureTestsVixen.Rendering.Tests
  • ReflectionTraceDeviceTestsVixen.Graphics.Golden.Tests
  • RenderedSceneTestsVixen.Rendering.DistanceFields.Tests
  • ScrollTestsVixen.Rendering.DistanceFields.Tests
  • SurfaceCacheRadiosityDeviceTestsVixen.Graphics.Golden.Tests