Reworked tile generation, removed map, redone colour scheme, v1.2.0
This commit is contained in:
@@ -4,94 +4,193 @@ using UnityEngine;
|
||||
|
||||
public class NoiseGenerator
|
||||
{
|
||||
public void applyTileNoise(GameObject tile)
|
||||
System.Random rand = new System.Random();
|
||||
public void applyNoise(GameObject tile)
|
||||
{
|
||||
//resetMesh(tile);
|
||||
Mesh mesh = tile.GetComponent<MeshFilter>().mesh;
|
||||
Vector3[] vertices = mesh.vertices;
|
||||
float[] samples = calculateSamples(tile, 0.5f);
|
||||
float[] samples;
|
||||
Color32[] colors;
|
||||
Color32 low;
|
||||
Color32 high;
|
||||
int chance = rand.Next(1, 101);
|
||||
if (chance > 85 && chance <= 100)
|
||||
{
|
||||
samples = calculateSamplesForest(tile);
|
||||
low = new Color32(0, 150, 0, 255);
|
||||
high = new Color32(0, 110, 20, 255);
|
||||
}
|
||||
else if (chance > 70 && chance <= 85)
|
||||
{
|
||||
samples = calculateSamplesMountain(tile);
|
||||
low = new Color32(0, 150, 0, 255);
|
||||
high = new Color32(140, 140, 140, 255);
|
||||
}
|
||||
else if (chance > 55 && chance <= 70)
|
||||
{
|
||||
samples = calculateSamplesLake(tile);
|
||||
low = new Color32(30, 110, 190, 255);
|
||||
high = new Color32(0, 150, 0, 255);
|
||||
}
|
||||
else if (chance > 40 && chance <= 55)
|
||||
{
|
||||
samples = calculateSamplesRiver(tile);
|
||||
low = new Color32(30, 160, 190, 255);
|
||||
high = new Color32(0, 150, 0, 255);
|
||||
}
|
||||
else
|
||||
{
|
||||
samples = calculateSamplesPlane(tile);
|
||||
low = new Color32(0, 150, 0, 255);
|
||||
high = new Color32(0, 185, 0, 255);
|
||||
}
|
||||
|
||||
float lowestValue = 10;
|
||||
float highestValue = 0;
|
||||
|
||||
for (int i = 0; i < samples.Length; i++)
|
||||
{
|
||||
vertices[i].y = samples[i] * 5;
|
||||
if (lowestValue > samples[i])
|
||||
{
|
||||
lowestValue = samples[i];
|
||||
}
|
||||
if (highestValue < samples[i])
|
||||
{
|
||||
highestValue = samples[i];
|
||||
}
|
||||
}
|
||||
applyMesh(tile, vertices, mesh);
|
||||
}
|
||||
|
||||
public void applyStoneTileNoise(GameObject tile)
|
||||
{
|
||||
Mesh mesh = tile.GetComponent<MeshFilter>().mesh;
|
||||
Vector3[] vertices = mesh.vertices;
|
||||
float[] samples = calculateSamples(tile, 0.5f);
|
||||
float modifier = highestValue - lowestValue;
|
||||
if (lowestValue < 0)
|
||||
{
|
||||
lowestValue = lowestValue * -1;
|
||||
}
|
||||
|
||||
colors = new Color32[samples.Length];
|
||||
for (int i = 0; i < samples.Length; i++)
|
||||
{
|
||||
vertices[i].y = samples[i] * 10;
|
||||
colors[i] = Color32.Lerp(low, high, (samples[i] * modifier) + lowestValue);
|
||||
}
|
||||
applyMesh(tile, vertices, mesh);
|
||||
}
|
||||
|
||||
public void applyTreeTileNoise(GameObject tile)
|
||||
{
|
||||
Mesh mesh = tile.GetComponent<MeshFilter>().mesh;
|
||||
Vector3[] vertices = mesh.vertices;
|
||||
float[] samples = calculateSamples(tile, 0.5f);
|
||||
for (int i = 0; i < samples.Length; i++)
|
||||
{
|
||||
vertices[i].y = samples[i] * 7.5f;
|
||||
vertices[i].y = samples[i] * 3;
|
||||
}
|
||||
applyMesh(tile, vertices, mesh);
|
||||
applyMesh(tile, vertices, mesh, colors);
|
||||
}
|
||||
|
||||
public void applyRiverTileNoise(GameObject tile)
|
||||
private void resetMesh(GameObject tile)
|
||||
{
|
||||
Mesh mesh = tile.GetComponent<MeshFilter>().mesh;
|
||||
Mesh mesh = tile.GetComponent<MeshCollider>().sharedMesh;
|
||||
Vector3[] vertices = mesh.vertices;
|
||||
float[] samples = calculateSamples(tile, 0.5f);
|
||||
for (int i = 0; i < samples.Length; i++)
|
||||
for (int i = 0; i < vertices.Length; i++)
|
||||
{
|
||||
vertices[i].y = samples[i] * 7.5f;
|
||||
vertices[i].y = 0;
|
||||
}
|
||||
applyMesh(tile, vertices, mesh);
|
||||
mesh.vertices = vertices;
|
||||
mesh.RecalculateBounds();
|
||||
mesh.RecalculateNormals();
|
||||
tile.GetComponent<MeshCollider>().sharedMesh = mesh;
|
||||
}
|
||||
|
||||
public void applyLakeTileNoise(GameObject tile)
|
||||
private float[] calculateBasicSamples(GameObject tile)
|
||||
{
|
||||
Mesh mesh = tile.GetComponent<MeshFilter>().mesh;
|
||||
Vector3[] vertices = mesh.vertices;
|
||||
float[] samples = calculateSamples(tile, 1.5f);
|
||||
for (int i = 0; i < samples.Length; i++)
|
||||
{
|
||||
vertices[i].y = samples[i] * 2;
|
||||
}
|
||||
applyMesh(tile, vertices, mesh);
|
||||
}
|
||||
|
||||
public void applyCityTileNoise(GameObject tile)
|
||||
{
|
||||
//Currently no noise. (Objects floating)
|
||||
}
|
||||
|
||||
private float[] calculateSamples(GameObject tile, float modifier)
|
||||
{
|
||||
Mesh mesh = tile.GetComponent<MeshFilter>().mesh;
|
||||
Vector3[] vertices = mesh.vertices;
|
||||
float[] samples = new float[vertices.Length - 1];
|
||||
float[] samples = new float[vertices.Length];
|
||||
for (int i = 0; i < vertices.Length; i++)
|
||||
{
|
||||
if (vertices[i].x != 5 && vertices[i].z != 5 && vertices[i].x != -5 && vertices[i].z != -5)
|
||||
{
|
||||
float xCord = tile.GetComponent<Tile>().getPosition().x + vertices[i].x / (vertices.Length - 1) * 10;
|
||||
float yCord = tile.GetComponent<Tile>().getPosition().z + vertices[i].z / (vertices.Length - 1) * 10;
|
||||
float sample = Mathf.PerlinNoise(xCord, yCord) - modifier;
|
||||
float sample = Mathf.PerlinNoise(xCord, yCord);
|
||||
samples[i] = sample;
|
||||
}
|
||||
}
|
||||
return samples;
|
||||
}
|
||||
|
||||
private void applyMesh(GameObject tile, Vector3[] vertices, Mesh mesh)
|
||||
private float[] calculateSamplesPlane(GameObject tile)
|
||||
{
|
||||
float[] samples = calculateBasicSamples(tile);
|
||||
Debug.Log("Plane");
|
||||
return samples;
|
||||
}
|
||||
|
||||
private float[] calculateSamplesForest(GameObject tile)
|
||||
{
|
||||
float[] samples = calculateBasicSamples(tile);
|
||||
Debug.Log("Forest");
|
||||
return samples;
|
||||
}
|
||||
|
||||
private float[] calculateSamplesMountain(GameObject tile)
|
||||
{
|
||||
Mesh mesh = tile.GetComponent<MeshFilter>().mesh;
|
||||
Vector3[] vertices = mesh.vertices;
|
||||
float[] samples = calculateBasicSamples(tile);
|
||||
|
||||
int amount = rand.Next(1, 5);
|
||||
int index = 0;
|
||||
for (int i = 0; i < amount; i++)
|
||||
{
|
||||
do
|
||||
{
|
||||
index = rand.Next(0, samples.Length);
|
||||
if (vertices[index].x != 5 && vertices[index].z != 5 && vertices[index].x != -5 && vertices[index].z != -5)
|
||||
{
|
||||
samples[index] = 3;
|
||||
break;
|
||||
}
|
||||
} while (true);
|
||||
}
|
||||
Debug.Log("Mountain");
|
||||
return samples;
|
||||
}
|
||||
|
||||
private float[] calculateSamplesRiver(GameObject tile)
|
||||
{
|
||||
float[] samples = calculateBasicSamples(tile);
|
||||
Debug.Log("River");
|
||||
return samples;
|
||||
}
|
||||
|
||||
private float[] calculateSamplesLake(GameObject tile)
|
||||
{
|
||||
Mesh mesh = tile.GetComponent<MeshFilter>().mesh;
|
||||
Vector3[] vertices = mesh.vertices;
|
||||
float[] samples = calculateBasicSamples(tile);
|
||||
|
||||
int randX = rand.Next(-4, 5);
|
||||
int randZ = rand.Next(-4, 5);
|
||||
|
||||
for (int i = -1; i < 2; i++)
|
||||
{
|
||||
for (int j = -1; j < 2; j++)
|
||||
{
|
||||
for (int k = 0; k < vertices.Length; k++)
|
||||
{
|
||||
if (Mathf.Round(vertices[k].x) == randX+i && Mathf.Round(vertices[k].z) == randZ+j)
|
||||
{
|
||||
samples[k] = samples[k] - rand.Next(1,3) - 0.25f * rand.Next(0,4);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Debug.Log("Lake");
|
||||
return samples;
|
||||
}
|
||||
|
||||
|
||||
private void applyMesh(GameObject tile, Vector3[] vertices, Mesh mesh, Color32[] colors)
|
||||
{
|
||||
mesh.vertices = vertices;
|
||||
mesh.RecalculateBounds();
|
||||
mesh.RecalculateNormals();
|
||||
mesh.colors32 = colors;
|
||||
tile.GetComponent<MeshCollider>().sharedMesh = mesh;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user