Perlin noise chunks not lining up. C#
Before anybody says to look it up I have been for 2 weeks. Have found nothing. And yes I know the code is sloppy I am very new to this.
So my problem is that the chucks are just not seamless. They have the correct x and z offset so I am lost on whats causing it.
I think the picture makes the problem pretty obvious.
Here is the code that is responsible for making the chunks.
Any Ideas? Am I doing this completely wrong? public class ChunkScript : MonoBehaviour {
float waterHigh;
float perlinScale;
float highScale;
Mesh mesh;
public float seedX;
public float seedY;
int chunkSize;
Vector3[] vertices;
int[] trianagles;
public GameObject waterObject;
void Update()
{
//Generate new terrian
if (Input.GetKeyDown(KeyCode.L))
{
Start();
}
}
void Awake()
{
mesh = GetComponent<MeshFilter>().mesh;
}
void Start()
{
gameObject.name = "Chunk: " + transform.position.x + "," + transform.position.z;
perlinScale = transform.parent.GetComponent<ChunkMasterScript>().perlinScale;
highScale = transform.parent.GetComponent<ChunkMasterScript>().highScale;
waterHigh = transform.parent.GetComponent<ChunkMasterScript>().waterHigh;
chunkSize = transform.parent.GetComponent<ChunkMasterScript>().chunkSize;
waterObject.transform.position = new Vector3(waterObject.transform.position.x, waterHigh, waterObject.transform.position.z);
waterObject.transform.localScale = new Vector3(chunkSize / 10, 1, chunkSize / 10);
seedX = transform.parent.GetComponent<ChunkMasterScript>().seedX;
seedY = transform.parent.GetComponent<ChunkMasterScript>().seedY;
BuildChunkMesh();
UpdateChunkMesh();
}
void BuildChunkMesh()
{
float perlinBlockHigh;
int v = 0;
int t = 0;
vertices = new Vector3[chunkSize * chunkSize * 8];
trianagles = new int[chunkSize * chunkSize * 36];
for (int x = 0; x < chunkSize; x++)
{
for (int y = 0; y < chunkSize; y++)
{
float xCoord = seedX + transform.position.x + x / perlinScale;
float yCoord = seedY + transform.position.z + y / perlinScale;
perlinBlockHigh = Mathf.PerlinNoise(xCoord, yCoord) * highScale;
vertices[v+0] = new Vector3(x - chunkSize / 2, 0, y - chunkSize / 2);
vertices[v+1] = new Vector3(x+1 - chunkSize / 2, 0, y - chunkSize / 2);
vertices[v+2] = new Vector3(x - chunkSize / 2, 0, y+1 - chunkSize / 2);
vertices[v+3] = new Vector3(x+1 - chunkSize / 2, 0, y+1 - chunkSize / 2);
vertices[v + 4] = new Vector3(x - chunkSize / 2, perlinBlockHigh, y - chunkSize / 2);
vertices[v + 5] = new Vector3(x + 1 - chunkSize / 2, perlinBlockHigh, y - chunkSize / 2);
vertices[v + 6] = new Vector3(x - chunkSize / 2, perlinBlockHigh, y + 1 - chunkSize / 2);
vertices[v + 7] = new Vector3(x + 1 - chunkSize / 2, perlinBlockHigh, y + 1 - chunkSize / 2);
//bottem
trianagles[t] = v + 1;
trianagles[t + 1] = v + 2;
trianagles[t + 2] = v;
trianagles[t + 3] = v + 3;
trianagles[t + 4] = v + 2;
trianagles[t + 5] = v + 1;
//top
trianagles[t + 6] = v + 4;
trianagles[t + 7] = v + 6;
trianagles[t + 8] = v + 5;
trianagles[t + 9] = v + 5;
trianagles[t + 10] = v + 6;
trianagles[t + 11] = v + 7;
//front
trianagles[t + 12] = v;
trianagles[t + 13] = v + 4;
trianagles[t + 14] = v + 1;
trianagles[t + 15] = v + 1;
trianagles[t + 16] = v + 4;
trianagles[t + 17] = v + 5;
//back
trianagles[t + 18] = v + 3;
trianagles[t + 19] = v + 7;
trianagles[t + 20] = v + 2;
trianagles[t + 21] = v + 2;
trianagles[t + 22] = v + 7;
trianagles[t + 23] = v + 6;
//right
trianagles[t + 24] = v + 1;
trianagles[t + 25] = v + 5;
trianagles[t + 26] = v + 3;
trianagles[t + 27] = v + 3;
trianagles[t + 28] = v + 5;
trianagles[t + 29] = v + 7;
//left
trianagles[t + 30] = v + 2;
trianagles[t + 31] = v + 6;
trianagles[t + 32] = v + 4;
trianagles[t + 33] = v;
trianagles[t + 34] = v + 2;
trianagles[t + 35] = v + 4;
v += 8;
t += 36;
}
}
}
void UpdateChunkMesh()
{
mesh.Clear();
mesh.vertices = vertices;
mesh.triangles = trianagles;
mesh.RecalculateNormals();
GetComponent<MeshCollider>().sharedMesh = mesh;
}
void PlayerEntered()
{
GetComponentInParent<ChunkMasterScript>().LoadChunksAroundPlayer(gameObject);
}
}
Your answer
Follow this Question
Related Questions
Yet another "Mathf.PerlinNoise() returning the same value" problem 0 Answers
Perlinnoise, blocks load far apart 0 Answers
Creating ridged perlin noise 0 Answers
Mesh collider only working on part of procedurally generated mesh 1 Answer
Endless Road Random Generate -Ball Racing, Using Perlin Noise 0 Answers