/// <summary> /// Schedule the job for loading new data from file /// </summary> /// <param name="chunkID"></param> /// <returns></returns> public override ApertureJobHandle getJobFor(Chunk.ID chunkID, FocusAdjustmentType adjustmentType) { IJob job; if (adjustmentType == FocusAdjustmentType.InFocus) { if (LevelDAO.ChunkFileExists(chunkID, level)) { job = new LevelDAO.LoadChunkDataFromFileJob(chunkID, level.name); // if there's no file, we need to generate the chunk data from scratch } else { job = BiomeMap.GetTerrainGenerationJob(chunkID, level); } /// if it's out of focus, we want to save the chunk to file } else if (level.chunks.TryGetValue(chunkID, out Chunk chunkToSave)) { job = new LevelDAO.SaveChunkDataToFileJob(chunkID, level.name, chunkToSave.getVoxels(), chunkToSave.solidVoxelCount); } else { throw new System.MissingMemberException( $"VoxelDataAperture is trying to save chunk data for {chunkID} but could not find the chunk data in the level" ); } return(new ApertureJobHandle(job, this)); }
void setUpTestChunk() { // Set up one chunk to load and mesh Level storage = new Level((1, 1, 1), null); Chunk.ID chunkID = new Chunk.ID(0, 0, 0); World.setActiveLevel(storage); levelManager.initializeFor(World.Current.activeLevel); World.EventSystem.subscribe( levelManager, WorldEventSystem.Channels.ChunkActivationUpdates ); // run the load job syncly BiomeMap.GenerateChunkDataFromSourceJob terrainGenJob = BiomeMap.GetTerrainGenerationJob(chunkID, storage); terrainGenJob.Execute(); // get the data from the load job Chunk newlyLoadedChunk = new Chunk(); if (terrainGenJob.solidVoxelCount[0] > 0) { newlyLoadedChunk.setVoxels( terrainGenJob.outVoxels, terrainGenJob.solidVoxelCount[0] ); } terrainGenJob.outVoxels.Dispose(); // add the loaded chunk to storage storage.chunks.Add(chunkID, newlyLoadedChunk); newlyLoadedChunk.isLoaded = true; // get the mesh gen job and run it syncly MarchingTetsMeshGenerator.MarchingTetsMeshGenJob meshGenJob = MarchingTetsMeshGenerator.GetJob(MarchingTetsMeshGenerator.GetVoxelsToMarchOver(chunkID, storage)); meshGenJob.Execute(); // set up the mesh data bool meshIsEmpty = meshGenJob.outVerticies.Length <= 0; VoxelMeshData chunkMeshData = new VoxelMeshData( chunkID, meshIsEmpty, meshGenJob.outVerticies, meshGenJob.outTriangles, meshGenJob.outColors ); // dispose of the allocated resources meshGenJob.outVerticies.Dispose(); meshGenJob.outTriangles.Dispose(); meshGenJob.outColors.Dispose(); // update the chunk data to say if it's meshed if (storage.chunks.TryGetValue(chunkID, out Chunk updatedChunk)) { updatedChunk.meshIsGenerated = true; updatedChunk.meshIsEmpty = meshIsEmpty; } /// notify the chunk manager World.EventSystem.notifyChannelOf( new MeshGenerationAperture.ChunkMeshLoadingFinishedEvent(chunkMeshData), WorldEventSystem.Channels.ChunkActivationUpdates ); // set the chunk active ActiveChunkObjectAperture.ActivateChunkObjectJob activateChunkJob = new ActiveChunkObjectAperture.ActivateChunkObjectJob(chunkID); activateChunkJob.Execute(); }