Class PropColliderStreamer
Puts a scene's baked prop collision into physics only where something can touch it: within ServerRadius of every character on the server, and within ClientRadius of the camera on the client. A chunk (CellMetres square) at a time, as one mesh: every collidable prop in it merged and cooked off the main thread, then handed to one static collider.
public static class PropColliderStreamer
- Inheritance
-
PropColliderStreamer
- Inherited Members
Remarks
Why stream. A generated scene has tens of thousands of trees and rocks. Baked into chunk meshes on disk their collision is every prop's geometry over again, tens of megabytes per scene, all of it in physics all the time, on a server that hosts many scenes mostly far from any player. Streamed, a prop kind's collision mesh is shared, a prop is an instance record, and physics holds only the chunks near someone.
One collider per chunk. A chunk's props become a single mesh (one per collision layer present), so physics holds one static actor per chunk rather than one per rock: the merge (PropColliderStreamer.MergeJob) and the cooking (PropColliderStreamer.BakeJob, BakeMesh(EntityId, bool)) both run as jobs, and the main thread only applies the merged data and assigns the cooked mesh.
Radii. The client's covers its view distance (100 m) and a margin, so effects, projectiles and the camera meet every rock in sight. The server's covers each character's own reach, players and NPCs alike, so an NPC in view never walks through a rock the client draws. A character that appears has the chunks round it built at once (AddFocus(Transform, float)), before its first step; farther chunks follow, nearest first, at most ChunksPerTick started per tick.
Held by cell, counted. Which chunks are wanted is kept by PropChunkLedger: each focus holds the chunks round the cell it stands in, recomputed only when it crosses into another cell, and a chunk stays while any focus holds it. Holding by cell is a superset of the chunks round the focus's exact position, and a chunk is let go only ReleaseMarginMetres beyond the radius, so collision is never in fewer places than a per-tick recompute round every exact position would put it.
Each collider in its own scene. Colliders are created in the prop's scene, so a server running each world scene in its own physics scene finds them in the right one.
Fields
CellMetres
Chunk side, metres: collision comes and goes a chunk at a time.
public const float CellMetres = 32
Field Value
ChunksPerTick
The most chunks started per tick once the chunks right round every focus are built.
public const int ChunksPerTick = 24
Field Value
ClientRadius
How far round the camera the client keeps prop collision, metres: the 100 m view distance and a margin.
public static float ClientRadius
Field Value
ImmediateRadius
How far round a character appearing its chunks are built at once, metres.
public static float ImmediateRadius
Field Value
ReleaseMarginMetres
How far beyond its radius a focus keeps a chunk it already holds, metres: one chunk, so pacing over a cell edge does not empty and rebuild the far rim. Only ever keeps more collision.
public static float ReleaseMarginMetres
Field Value
ServerRadius
How far round each character the server keeps prop collision, metres.
public static float ServerRadius
Field Value
TickSeconds
Seconds between updates of which chunks are wanted.
public const float TickSeconds = 0.1
Field Value
Properties
ActiveChunks
Chunk colliders in physics, across every scene.
public static int ActiveChunks { get; }
Property Value
ActiveCount
Props whose collision is in physics, across every scene.
public static int ActiveCount { get; }
Property Value
Methods
Add(ScenePropColliders)
A scene's prop collision came into play (its ScenePropColliders was enabled).
public static void Add(ScenePropColliders owner)
Parameters
ownerScenePropColliders
AddFocus(Transform, float)
Keeps prop collision within radius of transform; the chunks right round it are built now.
public static void AddFocus(Transform transform, float radius)
Parameters
transformTransformradiusfloat
DescribeFoci()
Every focus being kept, with where it stands, its radius, scene and how many chunks it holds: for a test or a probe asking why collision is still somewhere (another character, or the camera, holding it).
public static string DescribeFoci()
Returns
Remove(ScenePropColliders)
A scene's prop collision left play: every job is finished, every chunk and pool of it destroyed.
public static void Remove(ScenePropColliders owner)
Parameters
ownerScenePropColliders
RemoveFocus(Transform)
Stops keeping collision for transform.
public static void RemoveFocus(Transform transform)
Parameters
transformTransform
TryPropAt(Collider, int, out ScenePropCollisionSet, out Mesh)
The prop a ray struck, when what it struck is a streamed chunk's collider: the set it came from (its Source says what kind of thing: Scatter, Cliffs, Boulders, POI…) and the prototype's collision mesh, whose name says which prop. False for any other collider.
public static bool TryPropAt(Collider collider, int triangleIndex, out ScenePropCollisionSet set, out Mesh prototype)
Parameters
colliderCollidertriangleIndexintThe hit's UnityEngine.RaycastHit.triangleIndex.
setScenePropCollisionSetprototypeMesh