Game.Tools.ClearAreaHelpers
Assembly:
Namespace: Game.Tools
Type: static class
Base: System.Object
Summary:
Utility helpers for building and testing "clear areas" used by placement/upgrade tools. This class collects triangular clear areas from installed upgrades, prefab sub-areas, or node buffers, transforms/initilizes them for tests, and provides a set of ShouldClear checks that determine whether a point, curve, or other geometry intersects an existing clear area. The helpers use Unity.Entities DynamicBuffer/ComponentLookup/BufferLookup and Unity.Collections Native containers. Note: several methods create a NativeList
Fields
- (none)
This static helper type declares no instance or static fields.
Properties
- (none)
No properties are declared on this type.
Constructors
- (none)
This is a static class and does not declare constructors.
Methods
-
public static void FillClearAreas(DynamicBuffer<InstalledUpgrade> installedUpgrades, Entity ignoreUpgradeOrArea, ComponentLookup<Transform> transformData, ComponentLookup<Clear> clearAreaData, ComponentLookup<PrefabRef> prefabRefData, ComponentLookup<ObjectGeometryData> prefabObjectGeometryData, BufferLookup<Game.Areas.SubArea> subAreaBuffers, BufferLookup<Node> nodeBuffers, BufferLookup<Triangle> triangleBuffers, ref NativeList<ClearAreaData> clearAreas)
Collects clear areas from a list of installed upgrades. For each installed upgrade (except ignoreUpgradeOrArea), it resolves the prefab and transform and delegates to the SubArea-based FillClearAreas overload. If clearAreas is not created the method will instantiate it (Allocator.Temp). Caller must manage disposal. -
public static void FillClearAreas(DynamicBuffer<Game.Areas.SubArea> subAreas, Transform transform, ObjectGeometryData objectGeometryData, Entity ignoreArea, ref ComponentLookup<Clear> clearAreaData, ref BufferLookup<Node> nodeBuffers, ref BufferLookup<Triangle> triangleBuffers, ref NativeList<ClearAreaData> clearAreas)
Iterates through runtime sub-areas (areas already instantiated as entities). For each sub-area entity that has a Clear component (and is not ignoreArea) it converts area triangles into ClearAreaData entries. Top Y is computed from the owner's transform + prefab object bounds max.y + 1f. Allocates clearAreas if not created. Uses AreaUtils.GetTriangle3 to get world triangles from nodes and triangles buffers. -
public static void FillClearAreas(Entity ownerPrefab, Transform ownerTransform, ComponentLookup<ObjectGeometryData> prefabObjectGeometryData, ComponentLookup<AreaGeometryData> prefabAreaGeometryData, BufferLookup<Game.Prefabs.SubArea> prefabSubAreas, BufferLookup<SubAreaNode> prefabSubAreaNodes, ref NativeList<ClearAreaData> clearAreas)
Collects clear areas from prefab-sub-area definitions (the prefab author-time geometry). Triangulates the sub-area polygon (expands nodes by -0.1) using GeometrySystem.Triangulate and EqualizeTriangles, converts triangle indices into world-space triangle vertices (using ObjectUtils.LocalToWorld) and records ClearAreaData entries with computed topY. Allocates clearAreas if required. Temporary NativeArray/NativeList memory created in the method is disposed inside the method. -
public static void FillClearAreas(Entity ownerPrefab, Transform ownerTransform, DynamicBuffer<Node> nodes, bool isComplete, ComponentLookup<ObjectGeometryData> prefabObjectGeometryData, ref NativeList<ClearAreaData> clearAreas)
Creates clear areas from a Runtime node buffer (DynamicBuffer) for an owner entity. Handles optionally-closed polygons (last node equal to first). Expands nodes by -0.1f, triangulates, equalizes triangles, and adds ClearAreaData entries. Allocates clearAreas if not created. Temporary native memory is disposed in the method. -
public static void TransformClearAreas(NativeList<ClearAreaData> clearAreas, Transform oldTransform, Transform newTransform)
Transforms an existing NativeList of ClearAreaData from one parent transform to another. Each triangle vertex is transformed from old parent space to new parent space by computing the inverse of oldTransform and applying ObjectUtils.WorldToLocal/ObjectUtils.LocalToWorld. Also updates m_TopY by the Y difference between transforms. Does nothing if clearAreas is not created. -
public static void InitClearAreas(NativeList<ClearAreaData> clearAreas, Transform topLevelTransform)
Initializes clear areas for runtime tests: sets the m_OnGround flag if any triangle Y equals (within +/-1f) the topLevelTransform Y, and decreases the triangle Y plane by 1f (value is subtracted from the triangle.y). Does nothing if clearAreas is not created. Typically called before performing ShouldClear checks. -
public static bool ShouldClear(NativeList<ClearAreaData> clearAreas, float3 position, bool onGround)
Checks whether a point position should be cleared based on the collected clearAreas. For each clear area that contains the XZ projection of the position (via MathUtils.Intersect), returns true if both are on-ground or if the position's Y lies between the triangle surface Y and a computed top bound (max(topY, triangleY + 2f)). Uses barycentric intersection math to compute triangle Y at the intersection point. Returns false if clearAreas is not created or no match found. -
public static bool ShouldClear(NativeList<ClearAreaData> clearAreas, Bezier4x3 curve, bool onGround)
Checks whether any segment along a Bezier curve intersects clear areas. Samples the curve into 16 segments, checks XZ intersections and computes sample positions and triangle heights similar to the point overload. Returns true when an intersection requires clearing. Returns false if clearAreas not created. -
public static bool ShouldClear(NativeList<ClearAreaData> clearAreas, DynamicBuffer<Node> nodes, DynamicBuffer<Triangle> triangles, Transform ownerTransform)
Checks whether any triangle from a runtime node/triangle buffer (converted to Triangle3 via AreaUtils.GetTriangle3) should be cleared using the private triangle-based ShouldClear helper. Returns false if clearAreas not created. -
public static bool ShouldClear(NativeList<ClearAreaData> clearAreas, DynamicBuffer<SubAreaNode> subAreaNodes, int2 nodeRange, Transform ownerTransform)
Takes a sub-range of SubAreaNode entries (prefab data), triangulates them (expands nodes by -0.1f), converts triangle indices into world-space Triangle3 using ObjectUtils.LocalToWorld, and checks each resulting triangle with the private triangle-based ShouldClear. Temporary arrays are disposed in-method. Returns false if clearAreas not created. -
private static bool ShouldClear(NativeList<ClearAreaData> clearAreas, Triangle3 triangle, Transform ownerTransform)
Internal triangle-vs-clear-area test. Computes bounds and a flag for on-ground (triangle.y near ownerTransform.y). For each clear area, if XZ bounds intersect and triangle XZ intersects clear area XZ, it returns true when on-ground rules match or when the triangle vertical bounds overlap the clear area's vertical band (from clear area min Y to max(clearArea.m_TopY, clearAreaMinY + 2f)). Used by overloads that operate on triangle sets.
Notes and important behavior: - Many methods allocate clearAreas if not created by using Allocator.Temp. Allocator.Temp memory must be short-lived and should be disposed by the owner/caller. The methods that allocate do not dispose clearAreas — the caller must call clearAreas.Dispose() or reuse it appropriately. - Several temporary NativeArray/NativeList objects are allocated and disposed inside methods (those are cleaned up within the methods that allocate them). - These helpers rely on many other game/geometry utilities: AreaUtils, GeometrySystem, MathUtils, ObjectUtils and Unity.Math types. Ensure those systems are available in your environment when calling these methods. - Most geometry checks use XZ projections first for quick culling then evaluate heights.
Usage Example
// Example: collect clear areas from a prefab and test a position
NativeList<ClearAreaData> clearAreas = default;
try
{
// Collect clear areas for a prefab (prefab entity, transform, and lookups assumed available)
ClearAreaHelpers.FillClearAreas(ownerPrefab, ownerTransform, prefabObjectGeometryData, prefabAreaGeometryData, prefabSubAreas, prefabSubAreaNodes, ref clearAreas);
// Prepare clear areas for tests (topLevelTransform is the relevant world transform)
ClearAreaHelpers.InitClearAreas(clearAreas, topLevelTransform);
// Test a point
float3 testPos = new float3(10f, 5f, 20f);
bool shouldClear = ClearAreaHelpers.ShouldClear(clearAreas, testPos, onGround: false);
// Use result...
}
finally
{
if (clearAreas.IsCreated)
{
clearAreas.Dispose();
}
}