public void GenerateMap() { float[,] noiseMap = Noise.GenNoiseMap(mapChunkSize, mapChunkSize, seed, noiseScale, octaves, persistance, lacunarity, offset); Color[] colormap = new Color[mapChunkSize * mapChunkSize]; for (int y = 0; y < mapChunkSize; y++) { for (int x = 0; x < mapChunkSize; x++) { float currentHeight = noiseMap [x, y]; for (int i = 0; i < regions.Length; i++) { if (currentHeight <= regions [i].height) { colormap [y * mapChunkSize + x] = regions [i].color; break; } } } MapDisplay display = FindObjectOfType <MapDisplay> (); if (drawMode == DrawMode.NoiseMap) { display.DrawTexture(TextureGen.TextureFromHeightMap(noiseMap)); } else if (drawMode == DrawMode.ColorMap) { display.DrawTexture(TextureGen.TextureFromColorMap(colormap, mapChunkSize, mapChunkSize)); } else if (drawMode == DrawMode.Mesh) { display.DrawMesh(MeshGen.GenerateTerrainMesh(noiseMap, meshHeightMultiplier, MeshHeightCurve, levelOfDetail), TextureGen.TextureFromColorMap(colormap, mapChunkSize, mapChunkSize)); } } }
public void GenerateTerrain() { float[,] noiseMap = NoiseGen.GenerateNoiseMap(width, height, seed, scale, octaves, persistance, lacunarity, offset); FoliageGen.removeAllFoliage(); TerrainRendering terrainRendering = FindObjectOfType <TerrainRendering>(); List <ChunkData> chunks = NoiseGen.ChunkNoiseMap(noiseMap); List <MeshData> meshData = new List <MeshData>(); foreach (ChunkData chunkData in chunks) { meshData.Add(MeshGen.GenerateTerrainMesh(chunkData.heightMap, meshHeightMultiplier, meshHeightCurve, chunkData.chunkCoords, regions)); } if (useShader) { terrainRendering.DrawMesh(meshData, meshScale, shaderMaterial); } else { terrainRendering.DrawMesh(meshData, meshScale, null); } UpdateShaderData(); }
private void Update() { // Only update if seed was changed if (randomSeed == _prevSeed) { return; } _prevSeed = randomSeed; Random.InitState(randomSeed); CreateOctaveOffsets(NumOctaves); // Generate height maps var heights = new float[numChunksX * numChunksY][, ]; for (var i = 0; i < numChunksY; i++) { for (var j = 0; j < numChunksX; j++) { heights[i * numChunksY + j] = GenerateHeightMap(chunkLength, chunkWidth, i * (chunkWidth - 1), j * (chunkLength - 1)); } } NormaliseHeightMaps(heights); // Create Meshes and Textures for (var i = 0; i < numChunksX * numChunksY; i++) { var meshData = MeshGen.GenerateTerrainMesh(heights[i], heightScale); _meshTextures[i] = UpdateTextureFromHeightMap(heights[i], _meshTextures[i]); UpdateMeshes(meshData, _meshTextures[i], i); } }
public void Run() { float[] edge = new float[3]; float[] centerSph = new float[3]; GetChunks(); for (int i = 0; i < chunkHolder.Length; i++) { chunkHolder[i].chunk.SetPosition(size); SetBounds(i, ref edge, ref centerSph); MapData mapData = GenerateMapData(chunkHolder[i].position); DisplayMap display = FindObjectOfType <DisplayMap>(); if (mode is Mode.marchingMesh) { //only for first chunk display.DrawMesh(MeshGen.GenerateTerrainMesh(mapData.heightMap, genocideValue, size)); break; } else if (mode is Mode.compute) { display.DrawMesh(MeshGen.GenerateTerrainMesh(mapData.heightMapCompute, genocideValue, size, marchCubes, edge, applySandpaper), chunkHolder[i].chunk, size); } else if (mode is Mode.radius) { display.DrawMesh(MeshGen.GenerateTerrainMesh(mapData.heightMapCompute, genocideValue, size, radius, marchCubes, centerSph, applySandpaper), chunkHolder[i].chunk, size); } } }
void MeshDataThread(MapData mapData, int lod, Action <MeshData> callback) { MeshData meshData = MeshGen.GenerateTerrainMesh(mapData.heightMap, meshHeightMulti, meshHeightCurve, lod); lock (meshDataThreadInfoQuere) { meshDataThreadInfoQuere.Enqueue(new MapThreadInfo <MeshData>(callback, meshData)); } }
void MeshDataThread(MapData mapData, int lod, Action <MeshData> callback) { MeshData meshData = MeshGen.GenerateTerrainMesh(mapData.heightMap, meshHeightMultiplier, meshHeightCurve, lod); //Prevent other threads from executing this code at once lock (meshDataThreadInfoQueue){ meshDataThreadInfoQueue.Enqueue(new MapThreadInfo <MeshData>(callback, meshData)); } }
public void MeshDataThread(MapData mapData, Action <MeshData> callback, int LOD) { MeshData meshData = MeshGen.GenerateTerrainMesh(mapData.heightMap, heightCoefficient, heightExponent, meshHeightCurve, LOD); lock (meshData) { meshDataThreadInfoQueue.Enqueue(new MapThreadInfo <MeshData>(callback, meshData)); } }
public void DrawMapInEditor() { MapData map = GenerateMapData(Vector2.zero); if (drawMode == DrawMode.NoiseMap) { display.DrawTexture(TextureGen.TextureFromNoiseMap(map.heightMap)); } else if (drawMode == DrawMode.ColourMap) { display.DrawTexture(TextureGen.TextureFromColourMap(map.colMap, mapChunkSize, mapChunkSize)); } else if (drawMode == DrawMode.Mesh) { display.DrawMesh(MeshGen.GenerateTerrainMesh(map.heightMap, heightCoefficient, heightExponent, meshHeightCurve, editorLOD), TextureGen.TextureFromColourMap(map.colMap, mapChunkSize, mapChunkSize)); } }
public void DrawMapInEditor() { MapData mapData = GenerateMapData(Vector2.zero); MapDisplay display = FindObjectOfType <MapDisplay>(); if (drawMode == DrawMode.NoiseMap) { display.DrawTexture(TextureGen.TextureFromHeightMap(mapData.heightMap)); } else if (drawMode == DrawMode.ColorMap) { display.DrawTexture(TextureGen.TextureFromColorMap(mapData.colorMap, mapChunkSize, mapChunkSize)); } else if (drawMode == DrawMode.Mesh) { display.DrawMesh(MeshGen.GenerateTerrainMesh(mapData.heightMap, meshHeightMulti, meshHeightCurve, editorPreviewLOD), TextureGen.TextureFromColorMap(mapData.colorMap, mapChunkSize, mapChunkSize)); } }
public TerrainChunk(Vector2 coord, int size, Transform parent, int layer) { position = coord * size; bounds = new Bounds(position, Vector2.one * size); Vector3 positionV3 = new Vector3(position.x, 0, position.y); meshObject = new GameObject("TerrainChunk"); meshObject.layer = layer; MeshRenderer = meshObject.AddComponent <MeshRenderer>(); meshFilter = meshObject.AddComponent <MeshFilter>(); meshObject.transform.position = positionV3; meshObject.transform.SetParent(parent); meshObject.tag = "Ground"; SetVisible(true); MeshMatData data = chunks.mapGen.GetGeneratedData(position, coord); meshFilter.mesh = MeshGen.GenerateTerrainMesh(data.map, 0).CreateMesh(); MeshRenderer.material = data.mat; meshObject.AddComponent <MeshCollider>(); }
private void Start() { _meshMaterial = new Material(Shader.Find("Standard")); _minHeight = float.MaxValue; _maxHeight = float.MinValue; _prevSeed = randomSeed; CreateChunks(); Random.InitState(randomSeed); CreateOctaveOffsets(NumOctaves); // Generate height maps var heights = new float[numChunksX * numChunksY][, ]; for (var i = 0; i < numChunksY; i++) { for (var j = 0; j < numChunksX; j++) { _chunks[i * numChunksY + j].transform.position = new Vector3(j * (chunkWidth - 1), 0, i * (chunkLength - 1)); heights[i * numChunksY + j] = GenerateHeightMap(chunkLength, chunkWidth, i * (chunkWidth - 1), j * (chunkLength - 1)); } } NormaliseHeightMaps(heights); // Create Meshes and Textures _meshTextures = new Texture2D[numChunksX * numChunksY]; for (var i = 0; i < numChunksX * numChunksY; i++) { var meshData = MeshGen.GenerateTerrainMesh(heights[i], heightScale); _meshTextures[i] = TextureFromHeightMap(heights[i]); DrawMeshes(meshData, _meshTextures[i], i); } }
public void GenerateMap() { numOfTrees = 0; numOfStones = 0; parentObject = GameObject.FindGameObjectWithTag("Objects").transform; //SceneManager.LoadScene("LevelGenScrene"); //EditorSceneManager.LoadScene("LevelGenScrene"); GameObject cleanup; while (cleanup = GameObject.FindWithTag("Tree")) { Object.DestroyImmediate(cleanup); } while (cleanup = GameObject.FindWithTag("Stone")) { Object.DestroyImmediate(cleanup); } while (cleanup = GameObject.FindWithTag("Bush")) { Object.DestroyImmediate(cleanup); } while (cleanup = GameObject.FindWithTag("Boulder")) { Object.DestroyImmediate(cleanup); } while (cleanup = GameObject.FindWithTag("Grass")) { Object.DestroyImmediate(cleanup); } float a = Random.Range(5f, 20f); float b = Random.Range(0f, 20f); float c = Random.Range(5f, 20f); float d = Random.Range(0f, 20f); float[,] noiseMap = NoiseMapGen.GenerateNoiseMap(mapWidth, mapHeight, a, b, c, d); MeshData meshData = MeshGen.GenerateTerrainMesh(noiseMap, meshHeightMulitplier, heightCurve); triangles = meshData.triangles; Color[] colorMap = new Color[mapWidth * mapHeight]; for (int y = 0; y < mapHeight; y++) { for (int x = 0; x < mapWidth; x++) { float currentHeight = Mathf.InverseLerp(0, 20, noiseMap[x, y]); for (int i = 0; i < regions.Length; i++) { if (currentHeight <= regions[i].height) { //New code to try and get lerp from one zone to another //Skipping earth into grass transition if (regions[i].name.Equals("Earth")) { float tempMaxHeightOfRegion = regions[i].height; float tempMinHeightOfRegion = regions[i - 1].height; float tempMidHeightOfRegion = regions[i - 1].height + ((regions[i].height - regions[i - 1].height) / 2); if (currentHeight > tempMidHeightOfRegion) { colorMap[y * mapWidth + x] = Color.Lerp(regions[i].color, regions[i + 1].color, Mathf.InverseLerp(tempMidHeightOfRegion, tempMaxHeightOfRegion, currentHeight)); } else { colorMap[y * mapWidth + x] = regions[i].color; } } else if (i > 0 && i < regions.Length - 1) { float tempMaxHeightOfRegion = regions[i].height; float tempMinHeightOfRegion = regions[i - 1].height; float tempMidHeightOfRegion = regions[i - 1].height + ((regions[i].height - regions[i - 1].height) / 2); /*if (currentHeight >= tempMinHeightOfRegion) * colorMap[y * mapWidth + x] = Color.Lerp(regions[i].color, regions[i + 1].color, /*(float) noiseMap[x, y]/10 Mathf.InverseLerp(tempMidHeightOfRegion, tempMaxHeightOfRegion, currentHeight)); * else if (currentHeight < tempMinHeightOfRegion) * { * colorMap[y * mapWidth + x] = Color.Lerp(regions[i].color, regions[i - 1].color, /*(float) noiseMap[x, y]/10 Mathf.InverseLerp(tempMinHeightOfRegion, tempMidHeightOfRegion, currentHeight)); * }*/ //colorMap[y * mapWidth + x] = Color.Lerp(regions[i].color, regions[i + 1].color, /*(float) noiseMap[x, y]/10*/ Mathf.InverseLerp(tempMinHeightOfRegion, tempMaxHeightOfRegion, currentHeight) - 0.1f); if (currentHeight > tempMidHeightOfRegion) { colorMap[y * mapWidth + x] = Color.Lerp(regions[i].color, regions[i + 1].color, Mathf.InverseLerp(tempMidHeightOfRegion, tempMaxHeightOfRegion, currentHeight)); } else { colorMap[y * mapWidth + x] = regions[i].color; } } else if (i == 0) { /*float tempMaxHeightOfRegion = regions[i].height; * float tempMinHeightOfRegion = 0; * float tempMidHeightOfRegion = 0 + ((regions[i].height - 0) / 2); * if(currentHeight > tempMidHeightOfRegion) * colorMap[y * mapWidth + x] = Color.Lerp(regions[i].color, regions[i + 1].color, /*(float) noiseMap[x, y]/10 Mathf.InverseLerp(tempMidHeightOfRegion, tempMaxHeightOfRegion, currentHeight)); * else*/ colorMap[y * mapWidth + x] = regions[i].color; } else { /*float tempMaxHeightOfRegion = regions[i].height; * float tempMinHeightOfRegion = regions[i - 1].height; * float tempMidHeightOfRegion = regions[i - 1].height + ((regions[i].height - regions[i - 1].height) / 2); * colorMap[y * mapWidth + x] = Color.Lerp(regions[i].color, regions[i - 1].color, /*(float) noiseMap[x, y]/10 Mathf.InverseLerp(tempMinHeightOfRegion, tempMaxHeightOfRegion, currentHeight)); */ colorMap[y * mapWidth + x] = regions[i].color; } //OG code for coolour depending on height //colorMap[y * mapWidth + x] = regions[i].color; //place Gameobjects //trees/bushes on grassy area if (regions[i].name.Equals("Grass")) { if (Random.Range(0f, 1f) > 0.9f) { Vector3 treePos = Vector3.Scale(meshData.vertices[y * mapWidth + x], new Vector3(5, 1, 5)); currentInstance = Instantiate(trees[(int)Random.Range(0, trees.Length)], treePos, Quaternion.identity); numOfTrees++; currentInstance.transform.parent = parentObject; } else if (Random.Range(0f, 1f) > 0.8f) { Vector3 bushPos = Vector3.Scale(meshData.vertices[y * mapWidth + x], new Vector3(5, 1, 5)); currentInstance = Instantiate(bushes[(int)Random.Range(0, bushes.Length)], bushPos, Quaternion.identity); currentInstance.transform.parent = parentObject; } if (Random.Range(0f, 1f) > 0.75f) { Vector3 grassPos = Vector3.Scale(meshData.vertices[y * mapWidth + x], new Vector3(5, 1, 5)); currentInstance = Instantiate(grasses[(int)Random.Range(0, grasses.Length)], grassPos, Quaternion.identity); currentInstance.transform.parent = parentObject; } } //Stones on Sand else if (regions[i].name.Equals("Sand")) { if (Random.Range(0f, 1f) > 0.9f) { Vector3 stonePos = Vector3.Scale(meshData.vertices[y * mapWidth + x], new Vector3(5, 1, 5)); currentInstance = Instantiate(stones[(int)Random.Range(0, stones.Length)], stonePos, Quaternion.identity); numOfStones++; currentInstance.transform.parent = parentObject; } } else if (regions[i].name.Equals("Earth")) { if (Random.Range(0f, 1f) > 0.96f) { Vector3 treePos = Vector3.Scale(meshData.vertices[y * mapWidth + x], new Vector3(5, 1, 5)); currentInstance = Instantiate(beachTrees[(int)Random.Range(0, beachTrees.Length)], treePos + new Vector3(0, 3.5f, 0), Quaternion.Euler(-90, 0, 0)); currentInstance.transform.parent = parentObject; } else if (Random.Range(0f, 1f) > 0.9f) { Vector3 stonePos = Vector3.Scale(meshData.vertices[y * mapWidth + x], new Vector3(5, 1, 5)); currentInstance = Instantiate(stones[(int)Random.Range(0, stones.Length)], stonePos, Quaternion.identity); currentInstance.transform.parent = parentObject; } else if (Random.Range(0f, 1f) > 0.90f) { Vector3 grassPos = Vector3.Scale(meshData.vertices[y * mapWidth + x], new Vector3(5, 1, 5)); currentInstance = Instantiate(deadGrass, grassPos + new Vector3(0, 0.3f, 0), Quaternion.identity); currentInstance.transform.parent = parentObject; } } else if (regions[i].name.Equals("Mountain")) { if (Random.Range(0f, 1f) > 0.95f) { Vector3 boulderPos = Vector3.Scale(meshData.vertices[y * mapWidth + x], new Vector3(5, 1, 5)); currentInstance = Instantiate(bouldersMountain[(int)Random.Range(0, bouldersMountain.Length)], boulderPos, Quaternion.identity); currentInstance.transform.localScale = new Vector3(Random.Range(8, 10), Random.Range(8, 10), Random.Range(8, 10)); currentInstance.transform.parent = parentObject; } else if (Random.Range(0f, 1f) > 0.99f) { float temp = Random.Range(5, 10); Vector3 boulderPos = Vector3.Scale(meshData.vertices[y * mapWidth + x], new Vector3(5, 1, 5)); currentInstance = Instantiate(treesMountain[(int)Random.Range(0, treesMountain.Length)], boulderPos, Quaternion.identity); currentInstance.transform.localScale = new Vector3(temp, temp, temp); currentInstance.transform.parent = parentObject; } } else if (regions[i].name.Equals("Snow")) { if (Random.Range(0f, 1f) > 0.9f) { Vector3 boulderPos = Vector3.Scale(meshData.vertices[y * mapWidth + x], new Vector3(5, 1, 5)); currentInstance = Instantiate(boulders[(int)Random.Range(0, boulders.Length)], boulderPos, Quaternion.identity); currentInstance.transform.parent = parentObject; } } break; } } } } //Force conditions if (numOfTrees < minNumOfTree || numOfStones < minNumOfStones) { Debug.Log("Not Enough"); GenerateMap(); } else { Debug.Log("Enough"); MapDisplay display = FindObjectOfType <MapDisplay>(); //display.DrawTexture(TextureGen.TextureFromColorMap(colorMap, mapWidth, mapHeight)); //Select render type just 2d colour or 3d mesh display.DrawMesh(meshData, TextureGen.TextureFromColorMap(colorMap, mapWidth, mapHeight)); //Texture try //setTextureTriangles(meshData); //display.DrawMeshWithSubMesh(meshData/*, water.ToArray(), sand.ToArray(), earth.ToArray(), forest.ToArray(), mountain.ToArray(), snow.ToArray()*/); } }
void RunCubes() { DisplayMap display = FindObjectOfType <DisplayMap>(); display.DrawMesh(MeshGen.GenerateTerrainMesh(sphere)); }