Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public struct NativeLODJob : IJob
- {
- public NativeHashSet<AABB> generatedMeshes;
- public NativeList<AABB> meshesToRender;
- public NativeList<AABB> meshesToGenerate;
- public AABB totalBounds;
- public float3 cameraPosition;
- public frustum cameraFrustum;
- public lodParams lodParams;
- public void Execute()
- {
- var subdivisionQueue = new NativeQueue<AABB>(Allocator.Temp);
- subdivisionQueue.Enqueue(totalBounds);
- while (subdivisionQueue.Count > 0)
- {
- var bounds = subdivisionQueue.Dequeue();
- bool hasAllChildMeshes = true;
- AABB newBounds = new MinMaxAABB(bounds.min, bounds.center);
- for (int i = 0; i < 8; i++)
- {
- var offset = Offsets[i];
- var b = newBounds;
- b.center = b.center + bounds.halfExtents * offset;
- if (!generatedMeshes.Contains(b))
- {
- hasAllChildMeshes = false;
- meshesToGenerate.Add(b);
- }
- }
- if (!hasAllChildMeshes)
- {
- meshesToRender.Add(bounds);
- continue;
- }
- for (int i = 0; i < 8; i++)
- {
- var offset = Offsets[i];
- var b = newBounds;
- b.center = b.center + bounds.halfExtents * offset;
- if (ShouldSubdivide(b))
- {
- subdivisionQueue.Enqueue(b);
- }
- else if (cameraFrustum.Contains(b))
- {
- meshesToRender.Add(b);
- }
- }
- }
- }
- private bool ShouldSubdivide(AABB bounds)
- {
- // do something with lodParams
- var cp = CollisionUtils.ClosestPointOnOrInAABB(cameraPosition, bounds);
- var offset = cp - cameraPosition;
- var maxDist = lodParams.maxMultiplier * bounds.size;
- return any(offset < maxDist);
- }
- private static readonly int3[] Offsets =
- {
- new( 0, 0, 0),
- new( 1, 0, 0),
- new( 0, 0, 0),
- new( 1, 0, 0),
- new( 0, 0, 0),
- new( 1, 0, 0),
- new( 0, 0, 0),
- new( 1, 0, 0),
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement