public static GenerateNoiseMap ( int mapWidth, int mapHeight, float scale ) : ].float[ | ||
mapWidth | int | |
mapHeight | int | |
scale | float | |
리턴 | ].float[ |
public void GenerateMap() { float[,] noiseMap = Noise.GenerateNoiseMap(mapWidth, mapHeight, seed, noiseScale, octaves, persistance, lacunarity, offset); Color[] colorMap = new Color[mapWidth * mapHeight]; for (int y = 0; y < mapHeight; y++) { for (int x = 0; x < mapWidth; x++) { float currentHeight = noiseMap[x, y]; for (int i = 0; i < regions.Length; i++) { if (currentHeight <= regions[i].height) { colorMap[y * mapWidth + x] = regions[i].color; break; } } } } MapDisplayScr display = GetComponent <MapDisplayScr>(); if (dMode == DrawMode.NoiseMap) { display.DrawTexture(TextureGenerator.TextureFromHeightMap(noiseMap)); } else if (dMode == DrawMode.ColorMap) { display.DrawTexture(TextureGenerator.TextureFromColorMap(colorMap, mapWidth, mapHeight)); } else if (dMode == DrawMode.Mesh) { display.DrawMesh(MeshGenerator.GenerateTerrainMesh(noiseMap, meshHeightMultiplier, animationCurve), TextureGenerator.TextureFromColorMap(colorMap, mapWidth, mapHeight)); } }
void AddHeights() { Random.seed = System.DateTime.Now.Millisecond; int seed = Random.Range(0, 50); persistance = Random.Range(0.2f, 0.7f); lacunarity = Random.Range(1f, 1.5f); scale = (lacunarity < 1.3f) ? Random.Range(7f, 10f): Random.Range(10f, 15f); Vector3 offset = new Vector3(Random.Range(-500f, 500f), 0f, Random.Range(-500f, 500f)); float[,] noise = Noise.GenerateNoiseMap(xResolution, yResolution, seed, scale, 4, persistance, lacunarity, offset, 0); peakHeight = 0; for (int y = 0; y < yResolution; y++) { for (int x = 0; x < xResolution; x++) { float yDist = Mathf.PerlinNoise(rawVertices[x, y].x * 0.3f, rawVertices[x, y].z * 0.3f) * 1f; rawVertices[x, y] += Vector3.up * noise[x, y] * 35f * gradient[x, y] + Vector3.up * yDist; if (rawVertices[x, y].y > peakHeight) { peakHeight = rawVertices[x, y].y; } } } Debug.Log(peakHeight); //stats.text = "Scale: " + scale + "\nPersistance:" + persistance + "\nLacunarity: " + lacunarity + "\nPeak Height:" + peakHeight; //Debug.Log(rawVertices[xResolution - 1,yResolution -1]); }
MapData GenerateMapData(Vector2 centre) { float[,] noiseMap = Noise.GenerateNoiseMap(mapChunkSize + 2, mapChunkSize + 2, noiseData.seed, noiseData.noiseScale, noiseData.octaves, noiseData.persistance, noiseData.lacunarity, centre + noiseData.offset, noiseData.normalizeMode); if (terrainData.useFalloffMap) { if (falloffMap == null) { falloffMap = FalloffGenerator.GenerateFalloffMap(mapChunkSize + 2); } for (int y = 0; y < mapChunkSize + 2; y++) { for (int x = 0; x < mapChunkSize + 2; x++) { if (terrainData.useFalloffMap) { noiseMap[x, y] = Mathf.Clamp01(noiseMap[x, y] - falloffMap[x, y]); } } } } return(new MapData(noiseMap)); }
MapData generateMapData(Vector2 centre) { float[,] noiseMap = Noise.GenerateNoiseMap(mapChunkSize + 2, mapChunkSize + 2, noiseData.seed, noiseData.noiseScale, noiseData.octave, noiseData.persistent, noiseData.lacunarity, centre + noiseData.offSet, noiseData.normalizeMode); if (terrainData.useFallOff) { if (fallOffMap == null) { fallOffMap = FallOffGenerator.fallOffMap(mapChunkSize + 2); } for (int y = 0; y < mapChunkSize + 2; y++) { for (int x = 0; x < mapChunkSize + 2; x++) { if (terrainData.useFallOff) { noiseMap[x, y] = Mathf.Clamp(noiseMap[x, y] - fallOffMap[x, y], 0, 1); } float currentHeight = noiseMap[x, y]; } } } return(new MapData(noiseMap)); }
public override void Apply(TilemapStructure tilemap) { // Make sure TileType ordered from small to high height tileTypes = tileTypes.OrderBy(a => a.Height).ToArray(); var noiseMap = Noise.GenerateNoiseMap(tilemap.Width, tilemap.Height, tilemap.Seed, NoiseScale, Octaves, Persistance, Lacunarity, Offset); for (int x = 0; x < tilemap.Width; x++) { for (int y = 0; y < tilemap.Height; y++) { var height = noiseMap[y * tilemap.Width + x]; for (int i = 0; i < tileTypes.Length; i++) { if (height <= tileTypes[i].Height) { tilemap.SetTile(x, y, (int)tileTypes[i].tileTypeEnum); break; } } } } }
MapData GenerateMapData(Vector2 center) { float[,] noiseMap = Noise.GenerateNoiseMap(mapChunkSize + 2, mapChunkSize + 2, noiseData.noiseScale, noiseData.octaves, noiseData.persistance, noiseData.lacunarity, noiseData.seed, center + noiseData.offset, noiseData.normalizeMode); if (terrainData.useFalloff) { if (falloffMap == null) { falloffMap = FalloffGenerator.GenerateFalloffMap(mapChunkSize + 2); } for (int y = 0; y < mapChunkSize + 2; y++) { for (int x = 0; x < mapChunkSize + 2; x++) { noiseMap [x, y] = Mathf.Clamp01(noiseMap [x, y] - falloffMap [x, y]); } } } textureData.UpdateMeshHeights(terrainMaterial, terrainData.minHeight, terrainData.maxHeight); return(new MapData(noiseMap)); }
private MapData GenerateMapData() { float[,] noiseMap = Noise.GenerateNoiseMap(mapChunkSize, mapChunkSize, seed, noiseScale, octaves, persistance, lacurarity, offset); Color[] colourMap = 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) { colourMap[y * mapChunkSize + x] = regions[i].colour; break; } } } } return(new MapData(noiseMap, colourMap)); }
public static HeightMap GenerateHeightMap(int width, int height, HeightMapSettings settings, Vector2 sampleCenter) { float[,] values = Noise.GenerateNoiseMap(width, height, settings.noiseSettings, sampleCenter); // Create a new height Curve because curve evaluation will cause problems when accessed from different threads // Alternatively, lock the thread but it is slower AnimationCurve heightCurve = new AnimationCurve(settings.heightCurve.keys); float minValue = float.MaxValue; float maxValue = float.MinValue; float[,] fallOffValues = FalloffGenerator.GenerateFallOfMap(width, settings.falloffCurve); for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { values[i, j] *= (heightCurve.Evaluate(values[i, j]) * settings.heightMultiplier); if (values[i, j] > maxValue) { maxValue = values[i, j]; } if (values[i, j] < minValue) { minValue = values[i, j]; } if (settings.useFalloff) { values[i, j] *= fallOffValues[i, j]; } } } return new HeightMap(values, minValue, maxValue); }
public static HeightMap GenerateHeightMap(int width, int height, HeightMapSettings settings, Vector2 sampleCentre) { float[,] values = Noise.GenerateNoiseMap(width, height, settings.noiseSettings, sampleCentre); AnimationCurve heightCurve_threadsafe = new AnimationCurve(settings.heightCurve.keys); float minValue = float.MaxValue; float maxValue = float.MinValue; if (settings.useFalloff) { if (falloffMap == null) { falloffMap = FalloffGenerator.GenerateFalloffMap(width); } } for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { //values [i, j] *= heightCurve_threadsafe.Evaluate (values [i, j]) * settings.heightMultiplier; values[i, j] *= heightCurve_threadsafe.Evaluate(values[i, j] - (settings.useFalloff ? falloffMap[i, j] : 0)) * settings.heightMultiplier; if (values [i, j] > maxValue) { maxValue = values [i, j]; } if (values [i, j] < minValue) { minValue = values [i, j]; } } } return(new HeightMap(values, minValue, maxValue)); }
// Use this for initialization void Awake() { instance = this; // tiles2d = new MeshTile[height, width]; heightNoiseValues = Noise.GenerateNoiseMap(width, height, seed, noiseScale, octaves, persistance, lacunarity, offset); moistNoiseValues = Noise.GenerateNoiseMap(width, height, moistSeed, moistScale, moistOctaves, moistPersistance, moistLacunarity, offset); tempNoiseValues = Noise.GenerateNoiseMap(width, height, tempSeed, tempScale, tempOctaves, tempPersistance, tempLacunarity, offset); // tileCollectionArray = new MeshTileCollection[3, 3]; // CreateTiles(heightNoiseValues); if (!File.Exists(streamPath)) { // heightNoiseValues = Noise.GenerateNoiseMap(width, height, seed, noiseScale, octaves, persistance, lacunarity, offset); // CreateTiles(heightNoiseValues); } if (File.Exists(streamPath)) { // SpawnTiles(); // WorldTrees.instance.SpawnTrees(); } }
public void DrawInEditor() { if (!isRand) { noiseRender.gameObject.SetActive(true); grassRender.gameObject.SetActive(false); //Get dimensions of plane float[,] values = Noise.GenerateNoiseMap((int)Mathf.Round(foliageSettings.drawSize.x), (int)Mathf.Round(foliageSettings.drawSize.y), foliageSettings.noiseSettings, Vector2.zero); DrawTexture(TextureGenerator.TextureFromNoise(values, values.GetLength(0), values.GetLength(1))); SpawnObjectNoise(values); } else { noiseRender.gameObject.SetActive(false); grassRender.gameObject.SetActive(true); for (int i = 0; i < foliageSettings.objects.Length; i++) { SpawnObjectRand(foliageSettings.objects[i].preFab, foliageSettings.objects[i].sizeMultiplier); } } }
public void GenerateMapPerlinNoise() { float[,] noiseMap = Noise.GenerateNoiseMap(mapWidth, mapheight, seed, noiseScale, octaves, persistance, lacunarity, offset); float[,] noiseMapOre = Noise.GenerateNoiseMap(mapWidth, mapheight, seed + 1, noiseScale, octaves, persistance, lacunarity, offset); TileBase[] customTileMap = new TileBase[mapWidth * mapheight]; for (int y = 0; y < mapWidth; y++) { for (int x = 0; x < mapWidth; x++) { float value = noiseMap[x, y]; if (value > regions[1].value) { float valueOre = noiseMapOre[x, y]; customTileMap[y * mapWidth + x] = FindTileFromOre(valueOre); } else { customTileMap[y * mapWidth + x] = FindTileFromRegion(value); } } } SetTileMap(customTileMap); }
MapData GenerateMapData(Vector2 center) { // Generate the noise map with the necessary parameters float[,] noiseMap = Noise.GenerateNoiseMap(mapChunkSize, mapChunkSize, seed, noiseScale, octaves, persistence, lacunarity, center + offset, normalizeMode); // Create an array to hold all the colors on the map Color[] colorMap = new Color[mapChunkSize * mapChunkSize]; // Iterate through each coordinate on the noiseMap for (int y = 0; y < mapChunkSize; y++) { for (int x = 0; x < mapChunkSize; x++) { // Get the height value at the coordinates float currentHeight = noiseMap[x, y]; // Iterate through the regions to find which color corresponds to the height for (int i = 0; i < regions.Length; i++) { if (currentHeight >= regions[i].height) { // Converting the index of two-dimensional array to one-dimensional // y * width will give us the row, and adding x will give us the column // Assign the region color associated with the height colorMap[y * mapChunkSize + x] = regions[i].color; } else { break; } } } } return(new MapData(noiseMap, colorMap)); }
MapData GenerateMapData() { float[,] noiseMap = Noise.GenerateNoiseMap(mapChunkSize, mapChunkSize, seed, noiseScale, octaves, persistance, lacunarity, offset); //Apply color depending of the map's height value and value height's value assign to each regions Color[] colourMap = new Color[math.mul(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) { colourMap[math.mad(y, mapChunkSize, x)] = regions[i].colour; break; } } } } return(new MapData(noiseMap, colourMap)); }
///<summary> ///Generates the map texture via a noiseMap or a colourMap. Acts as user-interface in the Unity Inspector panel. ///</summary> ///<remarks> ///Whether a noiseMap or colourMap is generated is determined by the DrawMode setting set in the Unity Inspector. ///</remarks> public void GenerateMap() { float[,] noiseMap = Noise.GenerateNoiseMap (mapChunkSize,mapChunkSize, seed, noiseScale, octaves, persistance, lacunarity, offset); //Detecting height and assigning colour. Color[] colourMap = new Color[mapChunkSize*mapChunkSize]; for (int y = 0; y < mapChunkSize; y++) //loop through y coordinates of map. { for (int x = 0; x < mapChunkSize; x++) //loop through x coordinates of map. { float currentHeight = noiseMap[x, y]; for (int i = 0; i < regions.Length; i++) //Judging color by comparing height value to each region's bounds. { if (currentHeight <= regions[i].height) { colourMap[y * mapChunkSize + x] = regions[i].colour; //[y * mapwidth + x] is a 2D array to 1D array mapping. break; } } } } MapDisplay display = FindObjectOfType<MapDisplay>(); if (drawMode == DrawMode.NoiseMap) //Depending on Draw Mode setting in inspector, generate a BW texture from heightMap, color texture by colourMap, or Mesh from the Map data. { display.DrawTexture(TextureGenerator.TextureFromHeightMap(noiseMap)); } else if (drawMode == DrawMode.ColourMap) { display.DrawTexture(TextureGenerator.TextureFromColourMap(colourMap, mapChunkSize, mapChunkSize)); } else if (drawMode == DrawMode.Mesh) { display.DrawMesh(MeshGenerator.GenerateTerrainMesh(noiseMap, meshHeightMultiplier, meshHeightCurve, levelOfDetail), TextureGenerator.TextureFromColourMap(colourMap, mapChunkSize, mapChunkSize)); } }
public void GenerateMap() { float[,] noiseMap = Noise.GenerateNoiseMap(mapWidth, mapHeight, seed, noiseScale, octaves, persistance, lacunarity, offset); Color[] ColorMap = new Color[mapWidth * mapHeight]; for (int y = 0; y < mapHeight; y++) { for (int x = 0; x < mapHeight; x++) { float currentHeight = noiseMap[x, y]; for (int i = 0; i < regions.Length; i++) { if (currentHeight <= regions[i].height) { ColorMap[y * mapWidth + x] = regions[i].color; break; } } } } MapDisplay display = FindObjectOfType <MapDisplay>(); if (drawMode == DrawMode.NoiseMap) { display.DrawTexture(TextureGenerator.TextureFromHeightMap(noiseMap)); } else if (drawMode == DrawMode.ColorMap) { display.DrawTexture(TextureGenerator.TextureFromColorMap(ColorMap, mapWidth, mapHeight)); } else if (drawMode == DrawMode.Mesh) { display.DrawMesh(MeshGenerator.GenerateTerrainMesh(noiseMap), TextureGenerator.TextureFromColorMap(ColorMap, mapWidth, mapHeight)); } }
void GenerateBaseMap() { if (!mapDisplay) { mapDisplay = FindObjectOfType <MapDisplay>(); } float[,] noiseMap = Noise.GenerateNoiseMap(mapWidth, mapHeight, seed, noiseScale, octaves, persistance, lacunarity); map = new bool[mapWidth, mapHeight]; if (prng == null) { prng = new System.Random(seed); } for (int y = 0; y < mapHeight; y++) { for (int x = 0; x < mapWidth; x++) { map[x, y] = noiseMap[x, y] < fillAmount; for (int i = 0; i < edgeDistSmoothing; i++) { if (y == i || x == i || y == mapHeight - i - 1 || x == mapWidth - i - 1) { if (prng.Next(0, edgeDistSmoothing) >= i) { map[x, y] = false; } } } } } }
public void DisplayMap() { tileMap.ClearAllTiles(); terrainNoiseMap = Noise.GenerateNoiseMap(mapWidth, mapHeight, seed, noiseScale, octaves, persistance, lacunarity, offset); for (int x = 0; x < mapWidth; x++) { for (int y = 0; y < mapHeight; y++) { float currentHeight = terrainNoiseMap[x, y]; for (int i = 0; i < tileTypes.Length; i++) { //set a specific tile if (currentHeight <= tileTypes[i].height) { Tile tile = tileTypes[i].tile; Vector3Int pos = new Vector3Int(x, y, 0); // sets the tile on the groundmap tileMap.SetTile(pos, tile); // sets the watertile on the watermap if (tile.name == "water_plain_tile") { waterMap.SetTile(pos, waterTile); } break; } } } } }
public void GenerateMap() { float[,] noiseMap = Noise.GenerateNoiseMap(mapChunkSize, mapChunkSize, seed, noiseScale, octaves, persistance, lacunarity, offset); Color[] colourMap = 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) { colourMap[y * mapChunkSize + x] = regions[i].colour; break; } } } } MapDisplay display = FindObjectOfType <MapDisplay>(); if (drawMode == DrawMode.NoiseMap) { display.DrawTexture(TextureGenerator.TextureFromHeightMap(noiseMap)); } else if (drawMode == DrawMode.ColourMap) { display.DrawTexture(TextureGenerator.TextureFromColourMap(colourMap, mapChunkSize, mapChunkSize)); } else if (drawMode == DrawMode.Mesh) { display.DrawMesh(MeshGenerator.GenerateTerrainMesh(noiseMap, meshHeightMultiplier, meshHeightCurve, levelOfDetail), TextureGenerator.TextureFromColourMap(colourMap, mapChunkSize, mapChunkSize)); } }
public void SetNoiseMap() { Renderer r = GetComponent<Renderer>(); if (r == null) return; // get noise map float[,] noiseMap = Noise.GenerateNoiseMap(width, height, scale, octaves, persistance, lacunarity); Texture2D noiseTexture = new Texture2D(width, height); Color[] colorMap = new Color[width * height]; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { colorMap[y * width + x] = Color.Lerp(Color.black, Color.white, noiseMap[x, y]); } } noiseTexture.SetPixels(colorMap); noiseTexture.Apply(); r.sharedMaterial.mainTexture = noiseTexture; }
MapData GenerateMapData(Vector2 centre) { float[,] noiseMap = Noise.GenerateNoiseMap(mapChunkSize + 2, mapChunkSize + 2, seed, noiseScale, octaves, persistance, lacunarity, centre + offset, normalizeMode); Color[] colors = new Color[mapChunkSize * mapChunkSize]; for (int x = 0; x < mapChunkSize; ++x) { for (int y = 0; y < mapChunkSize; ++y) { if (useFalloffMap) { noiseMap[x, y] = Mathf.Clamp01(noiseMap[x, y] - falloffMap[x, y]); } if (useFillterMap) { noiseMap[x, y] = Mathf.Clamp01(noiseMap[x, y] - fillterMap[x, y]); } colors[y * mapChunkSize + x] = regions.Evaluate(Mathf.InverseLerp(0, 1, noiseMap[x, y])); } } return(new MapData(noiseMap, colors)); }
MapData GenerateMapData(Vector2 centre) { float[,] noiseMap = Noise.GenerateNoiseMap(mapChunkSize + 2, mapChunkSize + 2, seed, noiseScale, octaves, persistance, lacunarity, centre + offset, normalizeMode); Color[] colourMap = 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) { colourMap[y * mapChunkSize + x] = regions[i].colour; break; } } } } return(new MapData(noiseMap, colourMap)); }
public void GenerateMap() { float[,] noiseMap = Noise.GenerateNoiseMap(mapWidth, mapHeight, seed, noiseScale, octaves, persistance, lacunarity, offSet); // looping over noise map and finding region that matches and setting its colour Color[] colourMap = new Color[mapWidth * mapHeight]; for (int y = 0; y < mapHeight; y++) { for (int x = 0; x < mapWidth; x++) { float currentHeight = noiseMap[x, y]; for (int i = 0; i < regions.Length; i++) { if (currentHeight <= regions[i].height) { colourMap[y * mapWidth + x] = regions[i].colour; break; } } } } MapDisplay display = FindObjectOfType <MapDisplay>(); if (drawMode == DrawMode.NoiseMap) { display.DrawTexture(TextureGenerator.TextureFromHeightMap(noiseMap)); } else if (drawMode == DrawMode.ColourMap) { display.DrawTexture(TextureGenerator.TextureFromColourMap(colourMap, mapWidth, mapHeight)); } else if (drawMode == DrawMode.Mesh) { display.DrawMesh(MeshGenerator.GenerateTerrainMesh(noiseMap, meshHeightMultiplier, meshHeightCurve), TextureGenerator.TextureFromColourMap(colourMap, mapWidth, mapHeight)); } }
private void Start() { noiseMapX = Noise.GenerateNoiseMap(mapWidth, mapWidth, seed, noiseScale, octaves, persistance, lacunarity, offset); noiseMapY = Noise.GenerateNoiseMap(mapWidth, mapWidth, seed, noiseScale, octaves, persistance, lacunarity, offset); timeLastUpdate = Time.time; }
private float[,] GenerateHeightMap(int chunkOffsetX, int chunkOffsetY) { Vector2 chunkOffset = new Vector2(chunkOffsetX, chunkOffsetY); return(Noise.GenerateNoiseMap(NoiseSettings, _chunkSize + 1, chunkOffset)); }
public static HeightMap GenerateHeightMap(int width, int height, HeightMapSettings settings, MapRulesSettings mapRules, Vector2 sampleCenter, ChunkBorderInfo borderInfo) { float[,] values = Noise.GenerateNoiseMap(width, height, settings.noiseSettings, sampleCenter); AnimationCurve heightCurveThreadSafe = new AnimationCurve(settings.heightCurve.keys); float minValue = float.MaxValue; float maxValue = float.MinValue; if (mapRules.useFalloff) { float[,] falloffMap; if (mapRules.useFalloff) { if (mapRules.maxMapSizeInChunks.x <= 1 && mapRules.maxMapSizeInChunks.y <= 1) { falloffMap = FalloffGenerator.GenerateFalloffMap(width, mapRules); } else if (borderInfo.isCorner) { falloffMap = FalloffGenerator.GenerateCornerFalloffMap(width, mapRules, borderInfo.corner); } else if (borderInfo.isEdge) { falloffMap = FalloffGenerator.GenerateEdgeFalloffMap(width, mapRules, borderInfo.edge); } else { falloffMap = new float[width, height]; } } else { falloffMap = new float[width, height]; } for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { values[i, j] = Mathf.Clamp01(values[i, j] - falloffMap[i, j]); values[i, j] *= heightCurveThreadSafe.Evaluate(values[i, j]) * settings.heightMultiplier; if (values[i, j] > maxValue) { maxValue = values[i, j]; } if (values[i, j] < minValue) { minValue = values[i, j]; } } } } else { for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { values[i, j] = Mathf.Clamp01(values[i, j]); values[i, j] *= heightCurveThreadSafe.Evaluate(values[i, j]) * settings.heightMultiplier; if (values[i, j] > maxValue) { maxValue = values[i, j]; } if (values[i, j] < minValue) { minValue = values[i, j]; } } } } return(new HeightMap(values, minValue, maxValue)); }
public GameObject GenerateChunkAt(Vector2 center, bool removeExisting = false) { hueMap = Noise.GenerateHueMap(chunkSize, seed - 2, noiseScale, hueFrequency, center); ditherMap = Noise.GenerateDitherMap(chunkSize, seed - 1, center, ditherStrength); noiseMap = Noise.GenerateNoiseMap(chunkSize, seed, noiseScale, octaves, persistence, lacunarity, center); biomeHueMap = Noise.GenerateNoiseMap(chunkSize, seed + 1, biomeNoiseScale, biomeOctaves, biomePersistence, biomeLacunarity, center); biomeValueMap = Noise.GenerateNoiseMap(chunkSize, seed + 2, biomeNoiseScale, biomeOctaves, biomePersistence, biomeLacunarity, center); biomeSaturationMap = Noise.GenerateNoiseMap(chunkSize, seed + 3, biomeNoiseScale, biomeOctaves, biomePersistence, biomeLacunarity, center); // generate the noise map with the given variables Color[] colorMap = new Color[chunkSize * chunkSize]; // make a new colormap to apply colors to float H, S, V; for (int y = 0; y < chunkSize; y++) { for (int x = 0; x < chunkSize; x++) { // for every coordinate // if apply falloffmap, alter (x,y) as such float currentHeight = noiseMap[x, y]; // get the current height at (x, y) for (int i = 0; i < regions.Length; i++) { // for every region if (currentHeight <= regions[i].height) { // found the region that the point belongs to Color newColor = regions[i].color; if (currentHeight <= 0.2 && hueMap[x, y] > 0) { Color.RGBToHSV(newColor, out H, out S, out V); // get the HSV variables from the color newColor = Color.HSVToRGB(H - hueMap[x, y] / hueStrength, S, V); // use the hsv variables to create a new color, but with modified hue (make it more green or blue) } else if (currentHeight > 0.2) { Color.RGBToHSV(newColor, out H, out S, out V); newColor = Color.HSVToRGB( H + ((biomeHueMap[x, y] + 1) / 2) / biomeHueStrength, S + (biomeSaturationMap[x, y] - biomeSaturationOffset) / biomeSaturationStrength, V + (biomeValueMap[x, y] - biomeValueOffset) / biomeValueStrength ); } Color.RGBToHSV(newColor, out H, out S, out V); // get the HSV variables from the color newColor = Color.HSVToRGB(H, S + ditherMap[x, y] / 2, V + ditherMap[x, y]); // use the hsv variables to create a new color, but with modified saturation and value colorMap[y * chunkSize + x] = newColor; // assign the color at given point to the colormap break; // no need to check other regions, so break out } } } } Texture2D texture = new Texture2D(chunkSize, chunkSize); // create a new texture texture.filterMode = FilterMode.Point; // point (as opposed to bilinear) filtering texture.wrapMode = TextureWrapMode.Clamp; // clamp (as opposed to wrap) wrap mode texture.SetPixels(colorMap); // set the pixels of the texture texture.Apply(); // apply it if (removeExisting) { foreach (GameObject chunk in chunks) { DestroyImmediate(chunk); } chunks.Clear(); } // remove any existing chunks if desired (used for editing in scene mode) GameObject newChunk = Instantiate(mapPrefab, center, Quaternion.identity); // instantiate a new chunk gameobject newChunk.GetComponent <Chunk>().noiseMap = noiseMap; // assign the noisemap variable of the chunk newChunk.GetComponent <SpriteRenderer>().sprite = Sprite.Create(texture, new Rect(0, 0, chunkSize, chunkSize), origin, 1f, 0u, SpriteMeshType.FullRect); // create a sprite from the chunk chunks.Add(newChunk); chunkPositions.Add(center); // add it to the list newChunk.transform.parent = chunkParent.transform; // child the chunk to a gameobject return(newChunk); // return it }
public Material MakeMaterial(int size, Vector2 pos, MeshRenderer renderer) { Material _material = new Material(_shader); Texture2D noiseTex = new Texture2D(size, size, TextureFormat.RGBA32, false); noiseTex.wrapMode = TextureWrapMode.Clamp; var colour = noiseTex.GetRawTextureData(); Color[] colorMap = new Color[1]; Task.Run(() => { try { colorMap = new Color[size * size]; float[][,] noises = new float[LAYER_COUNT][, ]; for (int i = 0; i < LAYER_COUNT; i++) { if (_layers[i].GetNoise != null) { noises[i] = Noise.GenerateNoiseMap(size, size, _noiseDetailLevel, _layers[i].GetNoise.NoiseSettingsDataMerge, pos); noises[i] = Noise.Clamp(noises[i], _layers[i].GetNoise); } else { noises[i] = new float[size, size]; } } float f = 0; int a = 0, b = 0; int aa = 0; int bb = 0; //byte[] noiseByte = new byte[size * size]; for (int y = 0; y < size; y++) { for (int x = 0; x < size; x++) { float large = Math.Max(noises[0][x, y], noises[1][x, y]); large = Math.Max(large, noises[2][x, y]); if (large > f) { f = large; aa = x; bb = y; } //ThreadedDataRequester.AddToCallbackQueue(() => { Debug.Log(noises[1][x, y]); }); // colour[y * size + x] = (byte)(255 * noise[x, y]); colorMap[y * size + x].r = noises[0][x, y]; colorMap[y * size + x].g = noises[1][x, y]; colorMap[y * size + x].b = noises[2][x, y]; colorMap[y * size + x].a = noises[3][x, y]; } } //colour = noiseByte; //Debug.Log(noiseByte[1]); ThreadedDataRequester.AddToCallbackQueue(() => { //noiseTex.LoadRawTextureData(colour); noiseTex.SetPixels(colorMap); noiseTex.Apply(); //MaterialPropertyBlock prop = new MaterialPropertyBlock(); //prop.SetTexture("_NoiseTextures", noiseTex); //renderer.SetPropertyBlock(prop); _material.SetTexture("_NoiseTexture", noiseTex); }); } catch (Exception e) { Debug.LogError(e); } }); _material.SetTexture("_AltTex0", _layers[0].GetTexture); _material.SetTexture("_AltTex1", _layers[1].GetTexture); _material.SetTexture("_AltTex2", _layers[2].GetTexture); _material.SetTexture("_AltTex3", _layers[3].GetTexture); _material.SetTexture("_MainTex", _mainTex); _material.SetTexture("_Emission", _emission); _material.SetFloat("baseTextureScale", _mainTexScale); _material.SetFloatArray("startStep", _layers.Select(x => x.GetStepValues.startStep).ToArray()); _material.SetFloatArray("endStep", _layers.Select(x => x.GetStepValues.endStep).ToArray()); _material.SetFloatArray("altTextureScale", _layers.Select(x => x.GetTextureScale).ToArray()); _material.SetColorArray("altTextureColour", _layers.Select(x => x.GetColour).ToArray()); _material.SetFloat("mainTexStop", _mainTexStop); _material.SetFloat("mainTexStart", _mainTexStart); return(_material); }
private void FixedUpdate() { // ~50 times per second, no matter the machine Vector2 center = new Vector2(Mathf.RoundToInt(player.transform.position.x * (mapScale / chunkGenerator.noiseScale)), Mathf.Round(player.transform.position.y * (mapScale / chunkGenerator.noiseScale))); // get the position of the player from the actual map relative to the minimap float[,] noiseMap = Noise.GenerateNoiseMap(mapSize, seed, mapScale, octaves, persistence, lacunarity, center); float[,] biomeHueMap = Noise.GenerateNoiseMap(mapSize, seed + 1, 5 * mapScale, 3, 0.5f, 1.5f, center); float[,] biomeValueMap = Noise.GenerateNoiseMap(mapSize, seed + 2, 5 * mapScale, 3, 0.5f, 1.5f, center); float[,] biomeSaturationMap = Noise.GenerateNoiseMap(mapSize, seed + 3, 5 * mapScale, 3, 0.5f, 1.5f, center); float[,] hueMap = Noise.GenerateHueMap(mapSize, seed - 2, mapScale, 0.25f, center); // create the maps Color[] colorMap = new Color[mapSize * mapSize]; // create an array to place colors into for (int y = 0; y < mapSize; y++) { for (int x = 0; x < mapSize; x++) { // for every point (x,y) for (int i = 0; i < chunkGenerator.regions.Length; i++) { // for every region float currentHeight = noiseMap[x, y]; float H, S, V; if (currentHeight <= chunkGenerator.regions[i].height) { // found the region that the point belongs to Color newColor = chunkGenerator.regions[i].color; if (currentHeight <= 0.2 && hueMap[x, y] > 0) { Color.RGBToHSV(newColor, out H, out S, out V); // get the HSV variables from the color newColor = Color.HSVToRGB(H - hueMap[x, y] / 0.25f, S, V); // use the hsv variables to create a new color, but with modified hue (make it more green or blue) } else if (currentHeight > 0.2) { Color.RGBToHSV(newColor, out H, out S, out V); newColor = Color.HSVToRGB( H + ((biomeHueMap[x, y] + 1) / 2) / 2.333333f, S + (biomeSaturationMap[x, y] - 0.2f) / 1f, V + (biomeValueMap[x, y] - 0.4f) / 4f ); } colorMap[y * mapSize + x] = newColor; // assign the color at given point to the colormap break; // no need to check other regions, so break out } } } } Texture2D texture = new Texture2D(mapSize, mapSize); // create a new texture texture.filterMode = FilterMode.Point; // point (as opposed to bilinear) filtering texture.wrapMode = TextureWrapMode.Clamp; // clamp (as opposed to wrap) wrap mode texture.SetPixels(colorMap); // set the pixels of the texture texture.Apply(); // apply it // GetComponent<SpriteRenderer>().sprite = Sprite.Create(texture, mapRect, origin, 1f, 0u, SpriteMeshType.FullRect); GetComponent <SpriteRenderer>().sprite = Sprite.Create(texture, new Rect(0, 0, mapSize, mapSize), origin, 1f, 0u, SpriteMeshType.FullRect); // create a sprite from the chunk }
void GenerateMap(int updateType = 0) { switch (updateType) //皆空值為重新計算地圖 { case 0: Debug.Log("CreateNewMap"); map = new int[width, height]; noiseOffsetMap = new float[width, height]; //隨機填入0(water),1(land)產生地圖 RandomFillMap(); //檢查周圍 for (int i = 0; i < smoothTime; i++) { SmoothMap(); } //ProcessMap(); // //產生falloffMap falloffMap = FalloffGenerator.GenerateFalloffMap(map); //產生noiseMap noiseMap = Noise.GenerateNoiseMap(width, height, seed.GetHashCode(), noiseScale, octaves, persistance, lacunarity, offset, normalizeMode, (falloffMap)); colorMap = ColorMapGenerator(noiseMap, map, width, height, noiseOffsetMap); textureMap = TextureGenerator.TextureFromColourMap(colorMap, width, height); CreateMapMesh(textureMap, meshHeightCurve, meshHeightMultiplier, (noiseMap), (falloffMap), noiseOffsetMap); /* //創建mesh與計算outline * survivingLandRegions = new List<Region>(); * survivingLandRegions = GetAllRegion((int)TileType.Land); * CreateRegionMesh(survivingLandRegions, textureMap, meshHeightCurve, meshHeightMultiplier, (noiseMap), (falloffMap)); * survivingWaterRegions = new List<Region>(); * survivingWaterRegions = GetAllRegion((int)TileType.Water); * Color waterColor; * UnityEngine.ColorUtility.TryParseHtmlString("#05336000",out waterColor); * CreateRegionMesh(survivingWaterRegions, waterColor, meshHeightCurve, meshHeightMultiplier, (noiseMap), (falloffMap)); * ShowRegionOutline(survivingLandRegions.ToArray(), Color.green);*/ ShowRegionOutline(survivingLandRegions.ToArray(), Color.green); //ShowRegionOutline(survivingWaterRegions.ToArray(), Color.red); break; case 1: //map空為建造山 Debug.Log("CreateMountain"); //產生falloffMap falloffMap = FalloffGenerator.GenerateFalloffMap(map); //產生noiseMap noiseMap = Noise.GenerateNoiseMap(width, height, seed.GetHashCode(), noiseScale, octaves, persistance, lacunarity, offset, normalizeMode, (falloffMap)); colorMap = ColorMapGenerator(noiseMap, map, width, height, noiseOffsetMap); textureMap = TextureGenerator.TextureFromColourMap(colorMap, width, height); CreateMapMesh(textureMap, meshHeightCurve, meshHeightMultiplier, (noiseMap), (falloffMap), noiseOffsetMap); break; case 2: //noiseOffsetMap空為建造水 Debug.Log("CreateWater"); // //產生falloffMap falloffMap = FalloffGenerator.GenerateFalloffMap(map); //產生noiseMap noiseMap = Noise.GenerateNoiseMap(width, height, seed.GetHashCode(), noiseScale, octaves, persistance, lacunarity, offset, normalizeMode, (falloffMap)); colorMap = ColorMapGenerator(noiseMap, map, width, height, noiseOffsetMap); textureMap = TextureGenerator.TextureFromColourMap(colorMap, width, height); CreateMapMesh(textureMap, meshHeightCurve, meshHeightMultiplier, (noiseMap), (falloffMap), noiseOffsetMap); break; } }