Hi,
I’m working on a little game for fun, so I have a basic map generator but I don’t know how to set a texture on each pixel of the map, I used the first 4 episode of
so the code is (C#) :
MapGenerator.cs
using UnityEngine;
using System.Collections;
public class MapGenerator : MonoBehaviour
{
public enum DrawMode { NoiseMap, ColourMap };
public DrawMode drawMode;
public int mapWidth;
public int mapHeight;
public float noiseScale;
public int octaves;
[Range(0, 1)]
public float persistance;
public float lacunarity;
public int seed;
public Vector2 offset;
public bool autoUpdate;
public TerrainType[] regions;
public void GenerateMap()
{
float[,] noiseMap = Noise.GenerateNoiseMap(mapWidth, mapHeight, seed, noiseScale, octaves, persistance, lacunarity, offset);
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));
}
}
void OnValidate()
{
if (mapWidth < 1)
{
mapWidth = 1;
}
if (mapHeight < 1)
{
mapHeight = 1;
}
if (lacunarity < 1)
{
lacunarity = 1;
}
if (octaves < 0)
{
octaves = 0;
}
}
}
[System.Serializable]
public struct TerrainType
{
public string name;
public float height;
public Color colour;
public Texture texture;
}
Noise.cs
using UnityEngine;
using System.Collections;
public static class Noise
{
public static float[,] GenerateNoiseMap(int mapWidth, int mapHeight, int seed, float scale, int octaves, float persistance, float lacunarity, Vector2 offset)
{
float[,] noiseMap = new float[mapWidth, mapHeight];
System.Random prng = new System.Random(seed);
Vector2[] octaveOffsets = new Vector2[octaves];
for (int i = 0; i < octaves; i++)
{
float offsetX = prng.Next(-100000, 100000) + offset.x;
float offsetY = prng.Next(-100000, 100000) + offset.y;
octaveOffsets[i] = new Vector2(offsetX, offsetY);
}
if (scale <= 0)
{
scale = 0.0001f;
}
float maxNoiseHeight = float.MinValue;
float minNoiseHeight = float.MaxValue;
float halfWidth = mapWidth / 2f;
float halfHeight = mapHeight / 2f;
for (int y = 0; y < mapHeight; y++)
{
for (int x = 0; x < mapWidth; x++)
{
float amplitude = 1;
float frequency = 1;
float noiseHeight = 0;
for (int i = 0; i < octaves; i++)
{
float sampleX = (x - halfWidth) / scale * frequency + octaveOffsets[i].x;
float sampleY = (y - halfHeight) / scale * frequency + octaveOffsets[i].y;
float perlinValue = Mathf.PerlinNoise(sampleX, sampleY) * 2 - 1;
noiseHeight += perlinValue * amplitude;
amplitude *= persistance;
frequency *= lacunarity;
}
if (noiseHeight > maxNoiseHeight)
{
maxNoiseHeight = noiseHeight;
}
else if (noiseHeight < minNoiseHeight)
{
minNoiseHeight = noiseHeight;
}
noiseMap[x, y] = noiseHeight;
}
}
for (int y = 0; y < mapHeight; y++)
{
for (int x = 0; x < mapWidth; x++)
{
noiseMap[x, y] = Mathf.InverseLerp(minNoiseHeight, maxNoiseHeight, noiseMap[x, y]);
}
}
return noiseMap;
}
}
MapDisplay.cs
using UnityEngine;
using System.Collections;
public class MapDisplay : MonoBehaviour {
public Renderer textureRenderer;
public void DrawTexture(Texture2D texture)
{
textureRenderer.sharedMaterial.mainTexture = texture;
textureRenderer.transform.localScale = new Vector3(texture.width, 1, texture.height);
}
}
TextureGenerator.cs
using UnityEngine;
using System.Collections;
public static class TextureGenerator
{
public static Texture2D TextureFromColourMap(Color[] colourMap, int width, int height)
{
Texture2D texture = new Texture2D(width, height);
texture.filterMode = FilterMode.Point;
texture.wrapMode = TextureWrapMode.Clamp;
texture.SetPixels(colourMap);
texture.Apply();
return texture;
}
public static Texture2D TextureFromHeightMap(float[,] heightMap)
{
int width = heightMap.GetLength(0);
int height = heightMap.GetLength(1);
Color[] colourMap = new Color[width * height];
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
colourMap[y * width + x] = Color.Lerp(Color.black, Color.white, heightMap[x, y]);
}
}
return TextureFromColourMap(colourMap, width, height);
}
}
for instance, I have a 32x32 grass texture and I want to put it on grass regions, but I don’t know how to do it …
If anyone can direct me on the good way ^^"
( sorry for my english )