void CheckNeighboursAreReady() { NativeArray <ArchetypeChunk> dataChunks = meshReadyGroup.CreateArchetypeChunkArray(Allocator.TempJob); ArchetypeChunkEntityType entityType = GetArchetypeChunkEntityType(); ArchetypeChunkComponentType <AdjacentSectors> adjacentType = GetArchetypeChunkComponentType <AdjacentSectors>(true); for (int c = 0; c < dataChunks.Length; c++) { ArchetypeChunk dataChunk = dataChunks[c]; NativeArray <Entity> entities = dataChunk.GetNativeArray(entityType); NativeArray <AdjacentSectors> adjacents = dataChunk.GetNativeArray(adjacentType); for (int e = 0; e < entities.Length; e++) { Entity entity = entities[e]; AdjacentSectors adjacentSquares = adjacents[e]; NativeArray <Block> current = new NativeArray <Block>(entityManager.GetBuffer <Block>(entity).AsNativeArray(), Allocator.TempJob); NativeArray <Block> northNeighbour = new NativeArray <Block>(entityManager.GetBuffer <Block>(adjacentSquares[0]).AsNativeArray(), Allocator.TempJob); NativeArray <Block> southNeighbour = new NativeArray <Block>(entityManager.GetBuffer <Block>(adjacentSquares[1]).AsNativeArray(), Allocator.TempJob); NativeArray <Block> eastNeighbour = new NativeArray <Block>(entityManager.GetBuffer <Block>(adjacentSquares[2]).AsNativeArray(), Allocator.TempJob); NativeArray <Block> westNeighbour = new NativeArray <Block>(entityManager.GetBuffer <Block>(adjacentSquares[3]).AsNativeArray(), Allocator.TempJob); NativeArray <Block> upNeighbour = new NativeArray <Block>(entityManager.GetBuffer <Block>(adjacentSquares[4]).AsNativeArray(), Allocator.TempJob); NativeArray <Block> downNeighbour = new NativeArray <Block>(entityManager.GetBuffer <Block>(adjacentSquares[5]).AsNativeArray(), Allocator.TempJob); if (northNeighbour.Length == 0 || southNeighbour.Length == 0 || eastNeighbour.Length == 0 || westNeighbour.Length == 0 || upNeighbour.Length == 0 || downNeighbour.Length == 0) { current.Dispose(); northNeighbour.Dispose(); southNeighbour.Dispose(); eastNeighbour.Dispose(); westNeighbour.Dispose(); upNeighbour.Dispose(); downNeighbour.Dispose(); continue; } else { PostUpdateCommands.AddComponent(entity, new NeighboursAreReady()); } current.Dispose(); northNeighbour.Dispose(); southNeighbour.Dispose(); eastNeighbour.Dispose(); westNeighbour.Dispose(); upNeighbour.Dispose(); downNeighbour.Dispose(); } } dataChunks.Dispose(); }
protected override void OnUpdate() { NativeArray <ArchetypeChunk> dataChunks = adjSectorsGroup.CreateArchetypeChunkArray(Allocator.TempJob); if (dataChunks.Length == 0) { dataChunks.Dispose(); return; } ArchetypeChunkEntityType entityType = GetArchetypeChunkEntityType(); ArchetypeChunkComponentType <Translation> positionType = GetArchetypeChunkComponentType <Translation>(true); NativeArray <int3> adjacentPositions = new NativeArray <int3>(6, Allocator.TempJob); for (int i = 0; i < adjacentPositions.Length; i++) { adjacentPositions[i] = cubeDirections[i] * sectorSize; } sectorMatrix = terrainSystem.sectorMatrix; for (int d = 0; d < dataChunks.Length; d++) { ArchetypeChunk dataChunk = dataChunks[d]; NativeArray <Entity> entities = dataChunk.GetNativeArray(entityType); NativeArray <Translation> positions = dataChunk.GetNativeArray(positionType); for (int e = 0; e < entities.Length; e++) { Entity entity = entities[e]; int3 sectorWPos = (int3)positions[e].Value; Entity north = sectorMatrix.GetItem(sectorWPos + adjacentPositions[0]); Entity south = sectorMatrix.GetItem(sectorWPos + adjacentPositions[1]); Entity east = sectorMatrix.GetItem(sectorWPos + adjacentPositions[2]); Entity west = sectorMatrix.GetItem(sectorWPos + adjacentPositions[3]); Entity up = sectorMatrix.GetItem(sectorWPos + adjacentPositions[4]); Entity down = sectorMatrix.GetItem(sectorWPos + adjacentPositions[5]); AdjacentSectors adjSectors = new AdjacentSectors { north = north, south = south, east = east, west = west, up = up, down = down }; if (entityManager.HasComponent <AdjacentSectors>(entity)) { PostUpdateCommands.SetComponent(entity, adjSectors); } else { PostUpdateCommands.AddComponent(entity, adjSectors); } PostUpdateCommands.RemoveComponent <GetAdjacentSectors>(entity); } } adjacentPositions.Dispose(); dataChunks.Dispose(); }
void ScheduleMoreJobs() { NativeArray <ArchetypeChunk> dataChunks = meshGroup.CreateArchetypeChunkArray(Allocator.TempJob); if (dataChunks.Length != 0) { EntityCommandBuffer eCBuffer = new EntityCommandBuffer(Allocator.TempJob); JobHandle allHandles = new JobHandle(); JobHandle previousHandle = new JobHandle(); ArchetypeChunkEntityType entityType = GetArchetypeChunkEntityType(); ArchetypeChunkComponentType <Translation> positionType = GetArchetypeChunkComponentType <Translation>(true); ArchetypeChunkComponentType <AdjacentSectors> adjacentType = GetArchetypeChunkComponentType <AdjacentSectors>(true); for (int c = 0; c < dataChunks.Length; c++) { ArchetypeChunk dataChunk = dataChunks[c]; NativeArray <Entity> entities = dataChunk.GetNativeArray(entityType); NativeArray <Translation> positions = dataChunk.GetNativeArray(positionType); NativeArray <AdjacentSectors> adjacents = dataChunk.GetNativeArray(adjacentType); for (int e = 0; e < entities.Length; e++) { Entity entity = entities[e]; AdjacentSectors adjacentSquares = adjacents[e]; NativeArray <Block> current = new NativeArray <Block>(entityManager.GetBuffer <Block>(entity).AsNativeArray(), Allocator.TempJob); NativeArray <Block> northNeighbour = new NativeArray <Block>(entityManager.GetBuffer <Block>(adjacentSquares[0]).AsNativeArray(), Allocator.TempJob); NativeArray <Block> southNeighbour = new NativeArray <Block>(entityManager.GetBuffer <Block>(adjacentSquares[1]).AsNativeArray(), Allocator.TempJob); NativeArray <Block> eastNeighbour = new NativeArray <Block>(entityManager.GetBuffer <Block>(adjacentSquares[2]).AsNativeArray(), Allocator.TempJob); NativeArray <Block> westNeighbour = new NativeArray <Block>(entityManager.GetBuffer <Block>(adjacentSquares[3]).AsNativeArray(), Allocator.TempJob); NativeArray <Block> upNeighbour = new NativeArray <Block>(entityManager.GetBuffer <Block>(adjacentSquares[4]).AsNativeArray(), Allocator.TempJob); NativeArray <Block> downNeighbour = new NativeArray <Block>(entityManager.GetBuffer <Block>(adjacentSquares[5]).AsNativeArray(), Allocator.TempJob); NativeArray <float3> directions = new NativeArray <float3>(6, Allocator.TempJob); for (int i = 0; i < directions.Length; i++) { directions[i] = cubeDirections[i]; } FacesJob job = new FacesJob() { ECBuffer = eCBuffer, entity = entity, sectorSize = sectorSize, directions = directions, util = util, current = current, northNeighbour = northNeighbour, southNeighbour = southNeighbour, eastNeighbour = eastNeighbour, westNeighbour = westNeighbour, upNeighbour = upNeighbour, downNeighbour = downNeighbour }; JobHandle thisHandle = job.Schedule(previousHandle); allHandles = JobHandle.CombineDependencies(thisHandle, allHandles); previousHandle = thisHandle; } } runningECBuffer = eCBuffer; runningJobHandle = allHandles; } dataChunks.Dispose(); }
protected override JobHandle OnUpdate(JobHandle inputDeps) { NativeArray <ArchetypeChunk> dataChunks = meshGroup.CreateArchetypeChunkArray(Allocator.TempJob); if (dataChunks.Length == 0) { dataChunks.Dispose(); return(inputDeps); } EntityCommandBuffer eCBuffer = new EntityCommandBuffer(Allocator.TempJob); ArchetypeChunkEntityType entityType = GetArchetypeChunkEntityType(); ArchetypeChunkComponentType <Translation> positionType = GetArchetypeChunkComponentType <Translation>(true); ArchetypeChunkComponentType <AdjacentSectors> adjacentType = GetArchetypeChunkComponentType <AdjacentSectors>(true); for (int c = 0; c < dataChunks.Length; c++) { ArchetypeChunk dataChunk = dataChunks[c]; NativeArray <Entity> entities = dataChunk.GetNativeArray(entityType); NativeArray <Translation> positions = dataChunk.GetNativeArray(positionType); NativeArray <AdjacentSectors> adjacents = dataChunk.GetNativeArray(adjacentType); for (int e = 0; e < entities.Length; e++) { Entity entity = entities[e]; AdjacentSectors adjacentSquares = adjacents[e]; NativeArray <Block> current = new NativeArray <Block>(entityManager.GetBuffer <Block>(entity).AsNativeArray(), Allocator.TempJob); NativeArray <Block> northNeighbour = new NativeArray <Block>(entityManager.GetBuffer <Block>(adjacentSquares[0]).AsNativeArray(), Allocator.TempJob); NativeArray <Block> southNeighbour = new NativeArray <Block>(entityManager.GetBuffer <Block>(adjacentSquares[1]).AsNativeArray(), Allocator.TempJob); NativeArray <Block> eastNeighbour = new NativeArray <Block>(entityManager.GetBuffer <Block>(adjacentSquares[2]).AsNativeArray(), Allocator.TempJob); NativeArray <Block> westNeighbour = new NativeArray <Block>(entityManager.GetBuffer <Block>(adjacentSquares[3]).AsNativeArray(), Allocator.TempJob); NativeArray <Block> upNeighbour = new NativeArray <Block>(entityManager.GetBuffer <Block>(adjacentSquares[4]).AsNativeArray(), Allocator.TempJob); NativeArray <Block> downNeighbour = new NativeArray <Block>(entityManager.GetBuffer <Block>(adjacentSquares[5]).AsNativeArray(), Allocator.TempJob); NativeArray <float3> directions = new NativeArray <float3>(6, Allocator.TempJob); for (int i = 0; i < directions.Length; i++) { directions[i] = cubeDirections[i]; } var facesJob = new FacesJob() { ECBuffer = eCBuffer, entity = entity, sectorSize = sectorSize, directions = directions, util = util, current = current, northNeighbour = northNeighbour, southNeighbour = southNeighbour, eastNeighbour = eastNeighbour, westNeighbour = westNeighbour, upNeighbour = upNeighbour, downNeighbour = downNeighbour }.Schedule(inputDeps); facesJob.Complete(); } } eCBuffer.Playback(entityManager); eCBuffer.Dispose(); dataChunks.Dispose(); return(inputDeps); }