Game.Objects.RaycastJobs
Assembly: Game (game assembly containing runtime ECS jobs)
Namespace: Game.Objects
Type: static class
Base: System.Object
Summary:
Container for Burst-compiled raycast jobs used by the game's RaycastSystem. Provides multiple IJob / IJobParallelForDefer implementations to perform high-performance raycasts against static objects, moving objects, net edges, lane objects and to compute source ranges. The jobs read component lookups and buffers (transforms, prefab data, mesh vertex/index/node buffers, culling data, etc.) and accumulate RaycastResult entries into a NativeAccumulator for later consumption by the raycast system. These jobs are performance-critical and designed for use with Unity DOTS (ECS), Burst and native containers.
Fields
- None (the static class contains nested job structs and private helper methods; it does not declare top-level instance fields) {{ This file defines multiple nested job structs (RaycastStaticObjectsJob, GetSourceRangesJob, ExtractLaneObjectsJob, RaycastMovingObjectsJob) and several private static CheckMeshIntersect helper overloads. All state is stored inside the job structs as NativeArray/ComponentLookup/BufferLookup fields so jobs can run in parallel and under Burst. }}
Properties
- None {{ All data used by the jobs are exposed as public fields on the nested job structs (NativeArray, ComponentLookup, BufferLookup, NativeAccumulator, NativeQueue, etc.). There are no top-level properties on the static container. }}
Constructors
- None (static class) {{ Each nested struct uses the default value-type constructor. Instantiate the job struct and assign its fields before scheduling. }}
Methods
- RaycastStaticObjectsJob : IJobParallelForDefer
{{ Summary: Scans static objects referenced by raycast entity lists and performs mesh/raycast intersection tests. Reads RaycastInput array, entity result list, many ComponentLookup/BufferLookup and culling data. Produces RaycastResult entries via a NativeAccumulator
. Key behavior: - Filters by TypeMask and flags (markers, outside connections, placeholders, secondary objects, upgrade handling).
- Uses object prefab geometry (ObjectGeometryData) and per-entity component data to decide whether to perform detailed mesh tests.
- Performs bounding-box checks, lot checks (RaycastLot), and detailed mesh triangle intersection (RaycastMeshes).
- Performs owner resolution (ValidateResult) to map hits from attachment/placeholder to actual owners, optionally finding closest net node when needed.
- Uses many support helpers: HasCachedMesh, RaycastMeshes, RaycastLot, ValidateResult, CheckNetCollisionMask, CheckCompositionCollisionMask, CheckPlaceholder, FindClosestNode, IsNearCamera.
-
Burst-compiled and intended to be executed in parallel using deferred-length scheduling. }}
-
GetSourceRangesJob : IJob {{ Summary: Computes per-raycast source ranges into a native int4 array. The int4 encodes start/end ranges for edge list and static object list for each raycast index. Used to narrow down which entities to examine per raycast when later extracting lane/moving objects. }}
-
ExtractLaneObjectsJob : IJobParallelFor {{ Summary: For each RaycastInput it inspects the precomputed source ranges and discovers lane objects that should be considered for moving-object raycasts. It reads edge lists, static object lists and their composition/prefab data, enumerates connected edges and lanes, and enqueues lane objects into a NativeQueue
. This job is used to collect moving objects that live on lanes for subsequent raycasting (RaycastMovingObjectsJob). Uses a temporary NativeParallelHashSet to avoid duplicate processing. }} -
RaycastMovingObjectsJob : IJobParallelForDefer {{ Summary: Performs raycasts against moving/movable objects (lane objects, vehicles, passengers, sub-objects, etc.). Similar in structure to RaycastStaticObjectsJob but:
- Uses interpolated transforms when available.
- Walks subobjects and passenger buffers to raycast nested objects.
- Reads mesh buffers and applies skeletal/procedural bone transforms when applicable.
- Accumulates RaycastResult entries into a NativeAccumulator
. - Uses helpers: IsNearCamera, HasCachedMesh, RaycastMeshes (moving-object variant).
-
Burst-compiled and intended to be scheduled deferred/parallel. }}
-
private static CheckMeshIntersect(Line3.Segment localLine, DynamicBuffer
vertices, DynamicBuffer indices, int2 elementIndex, ref RaycastHit hit) {{ Summary: Basic triangle-by-triangle intersection over the full index buffer. Iterates indices in groups of 3 and tests each triangle against the provided line segment; updates hit if a closer intersection is found. Used when no node acceleration structure is present. }} -
private unsafe static CheckMeshIntersect overloads (with MeshNode buffer; with ProceduralBone; with ProceduralBone+Bone+Skeleton) {{ Summary: Optimized mesh intersection implementations that use mesh node bounding volumes (spatial hierarchy) and procedural/skeletal transforms:
- Node-accelerated variant: traverses mesh node tree (stack on stackalloc) and only tests triangles in nodes whose bounds intersect the segment.
- Procedural bone variants: transforms the segment into bone-local space using bind poses / bone transforms, then performs node-accelerated traversal.
- These variants are used when meshes have node hierarchies or procedural bones / skeletons to correctly raycast animated/skinned geometry without transforming every vertex on the CPU.
- All overloads update the RaycastHit (direction, normalized distance and cell index) for the closest hit found. }}
General notes about these methods: {{ All jobs are Burst-compiled and rely on Unity.Collections, Unity.Mathematics and Unity.Entities low-level ECS APIs (ComponentLookup, BufferLookup, DynamicBuffer, NativeArray/NativeList, NativeAccumulator, NativeQueue etc.). The code assumes the existence of many game-specific types (RaycastInput, RaycastResult, RaycastHit, ObjectGeometryData, PrefabRef, MeshVertex, MeshIndex, MeshNode, ProceduralBone, Bone, Skeleton, Composition, Edge, Curve, Transform, InterpolatedTransform, CullingInfo and many more) used across the game's ECS. Because these jobs perform low-level mesh intersection tests, ensure buffers (vertices/indices/nodes) and lookups are set up and passed with correct read/write restrictions before scheduling. }}
Usage Example
// Pseudocode example: prepare and schedule the static-objects raycast job.
// This is an outline — fill in the actual data sources from your RaycastSystem.
var staticJob = new RaycastJobs.RaycastStaticObjectsJob
{
m_EditorMode = false,
m_LeftHandTraffic = false,
m_Input = raycastInputsNativeArray, // NativeArray<RaycastInput>
m_Objects = objectEntityListNativeArray, // NativeArray<RaycastSystem.EntityResult>
m_TerrainResults = terrainResultsNativeArray, // NativeArray<RaycastResult>
m_CullingData = preCullingDataList, // NativeList<PreCullingData>
m_TransformData = world.GetComponentLookup<Transform>(true),
m_ElevationData = world.GetComponentLookup<Elevation>(true),
m_PrefabObjectData = world.GetComponentLookup<ObjectGeometryData>(true),
// ... assign other ComponentLookup/BufferLookup fields required ...
m_Results = raycastResultsAccumulator.AsParallelWriter() // NativeAccumulator<RaycastResult>.ParallelWriter
};
// schedule as a deferred parallel-for job (the exact scheduling call depends on the deferred source collection you have)
JobHandle dep = dependencyHandle; // whatever previous dependencies you have
JobHandle jobHandle = staticJob.Schedule(objectEntityListNativeArray, 64, dep); // pseudocode: schedule deferred-parallel job
// After the job completes, read accumulated results from the NativeAccumulator.
jobHandle.Complete();
var resultsList = raycastResultsAccumulator.Collect(); // read out RaycastResult array for further processing
{{ Notes: The code example above is a simplified illustration. In production use you must set up the full set of ComponentLookup/BufferLookup objects with the correct read-only flags, create and pass the appropriate NativeArray/NativeList sources (raycast inputs, entity result lists), and schedule each job with the correct deferred-length parameter or source list (IJobParallelForDefer expects deferred-length scheduling). Also ensure you respect Unity's safety rules (allocators, lifetimes) and dispose native containers when done. }}