protected override JobHandle OnUpdate(JobHandle inputDeps) { var colliders = m_AABBGroup.ToComponentDataArray <AABB>(Allocator.TempJob); var aabbCollisionJob = new AABBCollisionJob { Colliders = colliders, }; var collisionJobHandle = aabbCollisionJob.Schedule(colliders.Length, 32); collisionJobHandle.Complete(); colliders.Dispose(); return(collisionJobHandle); }
protected override void OnUpdate() { float3 playerPosition = PlayerTransform.position; playerPosition.y = 0f; float3 playerRight = PlayerTransform.right; float3 playerForward = PlayerTransform.forward; var query = new EntityQueryDesc { All = new ComponentType[] { typeof(GrassComponent), typeof(Translation), typeof(Rotation), typeof(GrassData) }, Options = EntityQueryOptions.IncludeDisabled }; var entityQuery = GetEntityQuery(query); var AABBQuery = GetEntityQuery(typeof(AABB)); var sphereQuery = GetEntityQuery(typeof(Sphere)); var cullingQuery = GetEntityQuery(typeof(StaticGrassCulling)); var translations = entityQuery.ToComponentDataArray <Translation>(Allocator.TempJob); var grassData = entityQuery.ToComponentDataArray <GrassData>(Allocator.TempJob); var aabbColliders = AABBQuery.ToComponentDataArray <AABB>(Allocator.TempJob); var sphereColliders = sphereQuery.ToComponentDataArray <Sphere>(Allocator.TempJob); var cullers = cullingQuery.ToComponentDataArray <StaticGrassCulling>(Allocator.TempJob); var rotations = new NativeArray <Rotation>(translations.Length, Allocator.TempJob); var collisionResults = new NativeArray <bool>(translations.Length, Allocator.TempJob); var nextPositions = new NativeArray <float3>(translations.Length, Allocator.TempJob); var cullingResults = new NativeArray <bool>(cullers.Length, Allocator.TempJob); var distanceCheckJob = new DistanceCheckJob { PlayerPosition = playerPosition, PlayerForward = playerForward, PlayerRight = playerRight, Translations = translations, NextPositions = nextPositions, Rotations = rotations, GrassData = grassData, }; var distanceCheckHandle = distanceCheckJob.Schedule(translations.Length, 32); distanceCheckHandle.Complete(); var collisionCheckJob = new AABBCollisionJob { AABBColliders = aabbColliders, SphereColliders = sphereColliders, NextPositions = nextPositions, CollisionResult = collisionResults, }; var collisionCheckHandle = collisionCheckJob.Schedule(translations.Length, 32); collisionCheckHandle.Complete(); var cullingCheckJob = new CullingCheckJob { PlayerPosition = playerPosition, Results = cullingResults, Cullers = cullers, }; var cullingCheckHandle = cullingCheckJob.Schedule(cullers.Length, 32); cullingCheckHandle.Complete(); var entities = entityQuery.ToEntityArray(Allocator.Temp); for (var i = 0; i < entities.Length; i++) { var translation = new Translation { Value = nextPositions[i] }; EntityManager.SetComponentData(entities[i], rotations[i]); if (!grassData[i].IsDynamic) { for (var j = 0; j < cullingResults.Length; j++) { if (grassData[i].StaticCullingId == cullers[j].Id) { if (cullingResults[j]) { var group = EntityManager.GetBuffer <LinkedEntityGroup>(entities[i]); EntityManager.AddComponent <Disabled>(group[1].Value); EntityManager.AddComponent <Disabled>(entities[i]); } else { var group = EntityManager.GetBuffer <LinkedEntityGroup>(entities[i]); EntityManager.RemoveComponent <Disabled>(group[1].Value); EntityManager.RemoveComponent <Disabled>(entities[i]); } } } continue; } EntityManager.SetComponentData(entities[i], translation); if (!collisionResults[i]) { var group = EntityManager.GetBuffer <LinkedEntityGroup>(entities[i]); EntityManager.RemoveComponent <Disabled>(group[1].Value); EntityManager.RemoveComponent <Disabled>(entities[i]); } else { var group = EntityManager.GetBuffer <LinkedEntityGroup>(entities[i]); EntityManager.AddComponent <Disabled>(group[1].Value); EntityManager.AddComponent <Disabled>(entities[i]); } } entities.Dispose(); translations.Dispose(); aabbColliders.Dispose(); sphereColliders.Dispose(); cullers.Dispose(); grassData.Dispose(); rotations.Dispose(); collisionResults.Dispose(); nextPositions.Dispose(); cullingResults.Dispose(); }