So iv’e made a noisemap using simplex noise and trying to create a 3d terrain with it. Here’s the code:
public int TextureSize;
public int xSize = 20;
public int zSize =20;
public float NoiseScale, IslandSize;
[Range(1, 20)] public int NoiseOctaves;
// Privates
Color[] col;
Texture2D tex;
int Seed;
Mesh mesh;
Vector3[] vertices;
Vector2[] uvs;
int[] triangles;
float noiseValue;
public Gradient ColorGradient;
private void Start()
{
mesh = new Mesh();
GetComponent<MeshFilter>().mesh = mesh;
Seed = UnityEngine.Random.Range(0, 99999);
tex = new Texture2D(TextureSize, TextureSize);
col = new Color[tex.height * tex.width];
Renderer rend = GetComponent<MeshRenderer>();
rend.sharedMaterial.mainTexture = tex;
Vector2 Org = new Vector2(Mathf.Sqrt(Seed), Mathf.Sqrt(Seed));
for (int x = 0, i = 0; x < TextureSize; x++)
{
for (int y = 0; y < TextureSize; y++, i++)
{
col[i] = ColorGradient.Evaluate(Noisefunction((float)x, (float)y, Org));
}
}
tex.SetPixels(col);
tex.Apply();
tex.wrapMode = TextureWrapMode.Clamp;
}
private void Update()
{
CreateShape();
UpdateMesh();
}
private void UpdateMesh()
{
mesh.Clear();
mesh.vertices = vertices;
mesh.triangles = triangles;
mesh.uv = uvs;
mesh.RecalculateNormals();
}
//creates the mesh
private void CreateShape()
{
vertices = new Vector3[(xSize + 1) * (zSize + 1)];
for (int z = 0, i = 0; z <= zSize; z++)
{
for (int x = 0; x <= xSize; x++)
{
vertices[i] = new Vector3(x, noiseValue, z);
i++;
}
}
triangles = new int[xSize * zSize * 6];
int vert = 0;
int tris = 0;
for (int z = 0; z < zSize; z++)
{
for (int x = 0; x < xSize; x++)
{
triangles[tris + 0] = vert + 0;
triangles[tris + 1] = vert + xSize + 1;
triangles[tris + 2] = vert + 1;
triangles[tris + 3] = vert + 1;
triangles[tris + 4] = vert + xSize + 1;
triangles[tris + 5] = vert + xSize + 2;
vert++;
tris += 6;
}
vert++;
}
uvs = new Vector2[vertices.Length];
for (int z = 0, i = 0; z <= zSize; z++)
{
for (int x = 0; x <= xSize; x++)
{
uvs[i] = new Vector2((float)x / xSize, (float)z / zSize);
i++;
}
}
}
//creates noise
private float Noisefunction(float x, float y, Vector2 Origin)
{
float a = 0, noisesize = NoiseScale, opacity = 1;
for (int octaves = 0; octaves < NoiseOctaves; octaves++)
{
float xVal = (x / (noisesize * TextureSize)) + Origin.x;
float yVal = (y / (noisesize * TextureSize)) - Origin.y;
noiseValue = noise.snoise(new float2(xVal, yVal)); // thought this was the line to get the y value of the mesh
a += Mathf.InverseLerp(0, 1, noiseValue) / opacity;
noisesize /= 2f;
opacity *= 2f;
}
return a -= FallOffMap(x, y, TextureSize, IslandSize);
}
//makes it an island
private float FallOffMap(float x, float y, int size, float islandSize)
{
float gradient = 1;
gradient /= (x * y) / (size * size) * (1 - (x / size)) * (1 - (y / size));
gradient -= 16;
gradient /= islandSize;
return gradient;
}
}
I tried setting the y-value of the vertices to the noisemap, but that didn’t do anything and neither did multiplying the noisevalue by a large number. All vertices are still at the same height. Any help on how I can make it into a 3d terrain would be appreciated!