- Home /
how to create chunks in voxel terrain generation
Hello everyone, i am trying to make a multiplayer adventure game and want to have procedural terrain generation. The script I created for this is capable to create a single chunk, the broblem comes when i try to create side by side chunks that dont match. here is the code im using:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Create_Mesh : MonoBehaviour
{
public int width;
public int height;
public int xPos;
public int yPos;
public float scale;
public float multiplier;
public float yOffset;
[HideInInspector]
public float xOffset;
[HideInInspector]
public float zOffset;
public bool roundHeight;
public int chunkAmount;
// Create a plane;
public List <Vector3> verts = new List<Vector3>();
public List <int> tris = new List<int>();
Mesh mesh;
MeshFilter mF;
void Start ()
{
mF = gameObject.AddComponent<MeshFilter>();
gameObject.AddComponent<MeshRenderer>();
mesh = new Mesh();
mF.mesh = mesh;
gameObject.AddComponent<MeshCollider>();
CalculateTerrainAtPos(Vector3.zero);
CalculateTerrainAtPos(Vector3.left * 16);
CalculateTerrainAtPos(Vector3.left * 32);
}
void CalculateTerrainAtPos (Vector3 pos)
{
xOffset = width / 2;
zOffset = height / 2;
for (int y = -height/2; y < height/2; y++)
{
for (int x = -width/2; x < width/2; x++)
{
float xCoord = xPos + x;
float yCoord = yPos + y;
float sample = Mathf.PerlinNoise(pos.x + x / scale, pos.z + y / scale) * multiplier;
GameObject clone = GameObject.CreatePrimitive(PrimitiveType.Cube);
clone.name = "Cube_" + (y * width + x);
clone.transform.position = new Vector3(xCoord - xOffset + pos.x, Mathf.RoundToInt(sample) - yOffset + pos.y, yCoord - zOffset+ pos.z);
}
}
}
}
I know the script has some useless variables but im just trying to make it work before i clean it up a little. I think the solution is prety simple but i just cant find it. So if you have any suggestion or solutions pls tell me, this is driving me nuts. thanks a lot to everyone.
Are you sure the parameters of the perlin function of adjacent terrain chunks are linear and don't have gaps between them? So when chunk 0,1 has values from 0 to 9, chunk 0,2 has to use parameter values from 10 to 19 for example, ins$$anonymous$$d of 100 to 109, if you get what I'm trying to say.
I'm also just gonna levae this handy link here in case you din't know about the thread already:
Answer by maccabbe · Mar 15, 2015 at 03:21 PM
My guess it that
float sample = Mathf.PerlinNoise(pos.x + x / scale, pos.z + y / scale) * multiplier;
should be
float sample = Mathf.PerlinNoise((pos.x + x) / scale, (pos.z + y) / scale) * multiplier;
Your answer
Follow this Question
Related Questions
Is there a way to load premade terrain chunks randomly? 1 Answer
Help with generating Chunks! 0 Answers
How to load stacking chunks on the fly? 1 Answer
Unity Voxel Terrain and structures 0 Answers