protected override void OnUpdate() { var commandBuffer = new EntityCommandBuffer(Allocator.TempJob); var chunks = hybridQuery.CreateArchetypeChunkArray(Allocator.TempJob); var entityType = GetArchetypeChunkEntityType(); var translationType = GetArchetypeChunkComponentType <Translation>(true); var matrixType = GetArchetypeChunkComponentType <CellSystem.MatrixComponent>(true); var localToWorldType = GetArchetypeChunkComponentType <LocalToWorld>(true); var meshType = GetArchetypeChunkSharedComponentType <RenderMesh>(); for (int c = 0; c < chunks.Length; c++) { var chunk = chunks[c]; var entities = chunk.GetNativeArray(entityType); var translations = chunk.GetNativeArray(translationType); var matrixComponents = chunk.GetNativeArray(matrixType); var localToWorldComponents = chunk.GetNativeArray(localToWorldType); for (int e = 0; e < entities.Length; e++) { Entity entity = entities[e]; float3 position = translations[e].Value; CellSystem.MatrixComponent matrix = matrixComponents[e]; float4x4 localToWorld = localToWorldComponents[e].Value; Mesh mesh = chunk.GetSharedComponentData <RenderMesh>(meshType, entityManager).mesh; GameObject sectorGameObject = GameObject.Instantiate(sectorPrefab, position, Quaternion.identity); sectorGameObject.GetComponent <MeshCollider>().sharedMesh = mesh; NavMeshSurface navMeshComponent = sectorGameObject.GetComponent <NavMeshSurface>(); NavMeshBuildSettings settings = NavMesh.GetSettingsByID(0); List <NavMeshBuildSource> sources = CreateNavMeshSource(matrix.width, navMeshComponent, mesh, localToWorld); Bounds bounds = CalculateWorldBounds(sources, position); NavMeshData data = NavMeshBuilder.BuildNavMeshData(settings, sources, bounds, position, Quaternion.identity); if (data != null) { data.name = sectorGameObject.name; navMeshComponent.RemoveData(); navMeshComponent.navMeshData = data; if (navMeshComponent.isActiveAndEnabled) { navMeshComponent.AddData(); } } commandBuffer.AddComponent <Tags.HybridGameObjectCreated>(entity, new Tags.HybridGameObjectCreated()); commandBuffer.AddSharedComponent <GameObjectComponent>(entity, new GameObjectComponent { gameObject = sectorGameObject }); } } commandBuffer.Playback(entityManager); commandBuffer.Dispose(); chunks.Dispose(); }
float3 AddCellMatrixComponent() { CellSystem.MatrixComponent cellMatrix = new CellSystem.MatrixComponent { root = pointMatrix.rootPosition, width = pointMatrix.width }; commandBuffer.AddComponent <CellSystem.MatrixComponent>(sectorEntity, cellMatrix); return(cellMatrix.root); }
void ScheduleMeshDataJobs() { var commandBuffer = new EntityCommandBuffer(Allocator.Temp); var chunks = meshDataGroup.CreateArchetypeChunkArray(Allocator.TempJob); var entityType = GetArchetypeChunkEntityType(); var matrixType = GetArchetypeChunkComponentType <CellSystem.MatrixComponent>(true); var sectorMasterCellType = GetArchetypeChunkComponentType <SectorSystem.MasterCell>(true); var worleyType = GetArchetypeChunkBufferType <WorleyNoise.PointData>(true); for (int c = 0; c < chunks.Length; c++) { var chunk = chunks[c]; var entities = chunk.GetNativeArray(entityType); var matrices = chunk.GetNativeArray(matrixType); var sectorMasterCells = chunk.GetNativeArray(sectorMasterCellType); var worleyArrays = chunk.GetBufferAccessor(worleyType); for (int e = 0; e < entities.Length; e++) { CellSystem.MatrixComponent matrix = matrices[e]; WorleyNoise.CellData masterCell = sectorMasterCells[e].Value; var worley = new NativeArray <WorleyNoise.PointData>(worleyArrays[e].AsNativeArray(), Allocator.Persistent); WaterMeshDataJob waterJob = new WaterMeshDataJob { commandBuffer = jobManager.commandBuffer, waterEntityArchetype = waterArchetype, matrix = matrix, masterCell = masterCell, worley = worley, topologyUtil = topologyUtil }; jobManager.ScheduleNewJob(waterJob); commandBuffer.RemoveComponent <Tags.CreateWaterEntity>(entities[e]); } } commandBuffer.Playback(entityManager); commandBuffer.Dispose(); chunks.Dispose(); }