/// <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)); }
protected override void LoadContent() { TileMap.SetSpriteBatch(SpriteBatch); TileMap.GenerateTileTypes(); BiomeMap.GenerateBiomeTypes(); GameContent.LoadContent(Content); // Load all textures }
/// <summary> /// Create a new level of the given size that uses the given apetures. /// </summary> /// <param name="chunkBounds"></param> /// <param name="apeturesByPriority"></param> public Level(Coordinate chunkBounds, string name = "") { seed = 1234; this.name = name == "" ? this.name : name; legalFileSaveName = LevelDAO.IllegalCharactersForFileName.Replace(this.name, ""); this.chunkBounds = chunkBounds; biomeMap = new TestPlainIslandMap(this); }
public void GenerateTiles() { //Voronoi biomeNiose = new Voronoi(); //biomeNiose.Seed = Constants.Seed; //RidgedMulti baseNoise = new RidgedMulti(); //baseNoise.Seed = Constants.Seed; //baseNoise.OctaveCount = 1; //Perlin topNoise = new Perlin(); //topNoise.Seed = Constants.Seed; //topNoise.OctaveCount = 10; //topNoise.Persistence = 0.3f; //topNoise.Frequency = 2; //topNoise.Lacunarity = 1; //Add noise = new Add(); //noise.Source0 = baseNoise; //noise.Source1 = topNoise; Perlin tempNoise = new Perlin(); tempNoise.Seed = Constants.Seed; Perlin humidityNoise = new Perlin(); humidityNoise.Seed = Constants.Seed + 1; Normalizer norm = new Normalizer(0.0f, 1.0f); float scale = .01f; // 0.001f for (var y = 0; y < Constants.ChunkDim.Y; y++) { for (var x = 0; x < Constants.ChunkDim.X; x++) { //Vector3 simplexLoc = new Vector3((x + (Position.X * Constants.ChunkDim.X)) * scale, (y + (Position.Y * Constants.ChunkDim.Y)) * scale, 0); //double tileId = baseNoise.GetValue(simplexLoc.X, simplexLoc.Y, simplexLoc.Z); //double biomeId = biomeNiose.GetValue((x + (Position.X * Constants.ChunkDim.X)) * scale, (y + (Position.Y * Constants.ChunkDim.Y)) * scale, 0); //biomeId = ((biomeId + 1) / 2) * BiomeMap.BiomeDict.Count; //tileId = BiomeMap.BiomeDict[(int)biomeId].GetTileAtValue(norm.Normalize((float)(tileId + 1) / 2)).Id; float xValue = (x + (Position.X * Constants.ChunkDim.X)) * scale; float yValue = (y + (Position.Y * Constants.ChunkDim.Y)) * scale; float tempValue = norm.Normalize((float)(tempNoise.GetValue(xValue, yValue, 0) + 1) / 2); float humidityValue = norm.Normalize((float)(humidityNoise.GetValue(xValue, yValue, 0) + 1) / 2); //Logger.Manager.Log(tempValue + " " + humidityValue); //BiomeBase biome = BiomeMap.BiomeDict[BiomeMap.BiomeNames["Swamp"]]; BiomeBase biome = BiomeMap.GetBiome(tempValue, humidityValue); Tiles[y, x] = (int)biome.GetTileAtValue(x, y).Id; } } }
private static void Restore() { if (!HasValidTerrain()) { return; } Debug.Log("Script reload detected. Attempting to restore texture data..."); string path = Path.Combine(Application.dataPath, "splat.bytes"); if (File.Exists(path)) { TerrainMap <byte> byteMap = new TerrainMap <byte>(File.ReadAllBytes(path), 8); Splat = new SplatMap(byteMap); File.Delete(path); Debug.Log("Restored Splat Maps"); } path = Path.Combine(Application.dataPath, "biome.bytes"); if (File.Exists(path)) { TerrainMap <byte> byteMap = new TerrainMap <byte>(File.ReadAllBytes(path), 4); Biome = new BiomeMap(byteMap); File.Delete(path); Debug.Log("Restored Biome Maps"); } path = Path.Combine(Application.dataPath, "alpha.bytes"); if (File.Exists(path)) { TerrainMap <byte> byteMap = new TerrainMap <byte>(File.ReadAllBytes(path), 1); Alpha = new AlphaMap(byteMap); File.Delete(path); Debug.Log("Restored Alpha Maps"); } path = Path.Combine(Application.dataPath, "topology.bytes"); if (File.Exists(path)) { TerrainMap <int> byteMap = new TerrainMap <int>(File.ReadAllBytes(path), 1); Topology = new TopologyMap(byteMap); File.Delete(path); Debug.Log("Restored Topology Maps"); } path = Path.Combine(Application.dataPath, "water.bytes"); if (File.Exists(path)) { TerrainMap <short> byteMap = new TerrainMap <short>(File.ReadAllBytes(path), 1); Water = new WaterMap(byteMap); File.Delete(path); Debug.Log("Restored Water Maps"); } }
public static BiomeMap GenerateBiomeMap(int width, int height, BiomeMapSettings settings, Vector2 sampleCenter, bool randomSeed = false) { NoiseSettings noiseSettings = settings.noiseSettings; if (randomSeed) { noiseSettings.seed = Random.Range(minSeedValue, maxSeedValue + 1); } float[,] noiseMap = NoiseGenerator.GeneratePerlinNoiseMap(width, height, noiseSettings, sampleCenter); BiomeMap biomeMap = new BiomeMap(noiseMap, settings.GetStartValues(), settings.biomes.Length); return(biomeMap); }
public void CreateNewBiomeMaps() { treeBiomeMap = MapGenerator.GenerateBiomeMap(clutterMapWidth, clutterMapWidth, currentTreeBiomeMapSettings, Vector2.zero, true); rockBiomeMap = MapGenerator.GenerateBiomeMap(clutterMapWidth, clutterMapWidth, currentRockBiomeMapSettings, Vector2.zero, true); treeBiomeMapPreviewImage.texture = TextureGenerator.TextureFromFloatArray2D(treeBiomeMap.values, currentTreeBiomeMapSettings.GetStartValues(), currentTreeBiomeMapSettings.GetColors()); rockBiomeMapPreviewImage.texture = TextureGenerator.TextureFromFloatArray2D(rockBiomeMap.values, currentRockBiomeMapSettings.GetStartValues(), currentRockBiomeMapSettings.GetColors()); treeBiomeMapSeed = currentTreeBiomeMapSettings.noiseSettings.seed; rockBiomeMapSeed = currentRockBiomeMapSettings.noiseSettings.seed; treeBiomeMapSeedText.text = treeBiomeMapSeed.ToString(); rockBiomeMapSeedText.text = rockBiomeMapSeed.ToString(); UpdateClutterMaps(); }
private void CreateMaps() { falloffMap = MapGenerator.GenerateFalloffMap(mapWidth, currentFalloffMapSettings); heightMap = MapGenerator.GenerateHeightMap(mapWidth, mapHeight, currentHeightMapSettings, falloffMap, Vector2.zero); treeBiomeMap = MapGenerator.GenerateBiomeMap(clutterMapWidth, clutterMapWidth, currentTreeBiomeMapSettings, Vector2.zero, true); rockBiomeMap = MapGenerator.GenerateBiomeMap(clutterMapWidth, clutterMapWidth, currentRockBiomeMapSettings, Vector2.zero, true); treeClutterMap = MapGenerator.GenerateClutterMap(clutterMapWidth, clutterMapWidth, currentTreeMapSettings, Vector2.zero, treeBiomeMap, true); rockClutterMap = MapGenerator.GenerateClutterMap(clutterMapWidth, clutterMapWidth, currentRockMapSettings, Vector2.zero, rockBiomeMap, true); previewMap = MapGenerator.GeneratePreviewMap(mapWidth, mapHeight, worldMinHeight, worldMaxHeight, previewSettings, heightMap, currentTextureSettings /*, new ClutterMap[] { rockClutterMap, treeClutterMap }*/); //treeBiomeMapSeed = currentTreeBiomeMapSettings.noiseSettings.seed; //rockBiomeMapSeed = currentRockBiomeMapSettings.noiseSettings.seed; //treeMapSeed = currentTreeMapSettings.noiseSettings.seed; //rockMapSeed = currentRockMapSettings.noiseSettings.seed; }
public IEnumerator Load(World.Data world) { ActionProgressBar.UpdateProgress("Loading Terrain", 0f); yield return(null); yield return(null); Splat = new SplatMap(world.splatMap); Alpha = new AlphaMap(world.alphaMap); Biome = new BiomeMap(world.biomeMap); Topology = new TopologyMap(world.topologyMap); Water = new WaterMap(world.waterMap); TerrainData terrainData = new TerrainData(); terrainData.alphamapResolution = Mathf.Clamp(Mathf.NextPowerOfTwo((int)(world.size.x * 0.50f)), 16, 2048); terrainData.baseMapResolution = Mathf.NextPowerOfTwo((int)((float)(world.size.x) * 0.01f)); terrainData.heightmapResolution = Mathf.NextPowerOfTwo((int)((float)(world.size.x) * 0.5f)) + 1; terrainData.size = world.size; terrainData.SetHeights(0, 0, world.landHeightMap); Terrain = Terrain.CreateTerrainGameObject(terrainData).GetComponent <Terrain>(); Terrain.name = Terrain.tag = "Terrain"; Terrain.gameObject.layer = 8; Terrain.transform.position = -0.5f * terrainData.size; Terrain.gameObject.AddComponent <PositionLock>(); SetSplatMaps(PaintType.Splat); CreateWaterPlane(world.size.x); }
public static ClutterMap GenerateClutterMap(int width, int height, ClutterMapSettings settings, Vector2 sampleCenter, BiomeMap biomeMap, bool randomSeed = false) { //NoiseSettings noiseSettings = settings.noiseSettings; //if (randomSeed) //{ // noiseSettings.seed = Random.Range(minSeedValue, maxSeedValue + 1); //} //float[,] noiseMap = NoiseGenerator.GeneratePerlinNoiseMap(width, height, noiseSettings, sampleCenter); float[,] noiseMap = NoiseGenerator.GenerateRandomNoiseMap(width); ClutterMap clutterMap = new ClutterMap(noiseMap, settings.density /*, settings.clutterStartValue, settings.clutterEndValue, settings.amplification*/, biomeMap); return(clutterMap); }
//public readonly float clutterStartValue; //public readonly float clutterEndValue; //public readonly float amplification; public ClutterMap(float[,] values, float density /*, float clutterStartValue, float clutterEndValue, float amplification*/, BiomeMap biomeMap) { this.values = new float[values.GetLength(0), values.GetLength(1)]; //this.clutterStartValue = clutterStartValue; //this.clutterEndValue = clutterEndValue; //this.amplification = amplification; //float amplificationModifier = (amplification - 1) / (biomeMap.numberOfBiomes - 1); this.density = 0.05f; for (int x = 0; x < values.GetLength(0); x++) { for (int y = 0; y < values.GetLength(1); y++) { for (int i = biomeMap.numberOfBiomes - 1; i >= 0; i--) { // Biome 0 always have no clutter if (i == 0) { values[x, y] = 0; } else if (biomeMap.values[x, y] >= biomeMap.biomeStartValues[i]) { if (i == 1) { this.values[x, y] = (values[x, y] <= 1 - density * 0.1f) ? 0 : 1; } else if (i == 2) { this.values[x, y] = (values[x, y] <= 1 - density) ? 0 : 1; } //values[x, y] = Mathf.Clamp01(values[x, y] * (1 + amplificationModifier * i)); break; } } //this.values[x, y] = (values[x, y] <= 1 - density) ? 0 : 1; } } }
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(); }
private void generateBiomeMap() { BiomeMap = new BiomeMap(this); }