- Home /
How to make infinite chunks out of a single mesh?
i have a mesh with perlin noise that i followed from a brackeys tutorial and i need the mesh to be able to generate seamlessly and infinitly. is there anyway i can do that with a single mesh.
if it is not possible is there another way i can use with perlin noise that will infinitly generate in chunks..
here is my code:
[RequireComponent(typeof(MeshFilter))]
public class MeshGenerator : MonoBehaviour
{
Mesh mesh;
Vector3[] vertices;
int[] triangles;
public int xSize = 20;
public int zSize = 20;
public float noiseScale = 0.3f;
public float noiseHeight = 2f;
void Start()
{
mesh = new Mesh();
GetComponent<MeshFilter>().mesh = mesh;
}
void Update()
{
CreateShape();
UpdateMesh();
}
void CreateShape()
{
vertices = new Vector3[(xSize + 1) * (zSize + 1)];
for (int i = 0, z = 0; z <= zSize; z++)
{
for (int x = 0; x <= xSize; x++)
{
float y = Mathf.PerlinNoise(x * noiseScale, z * noiseScale) * noiseHeight;
vertices[i] = new Vector3(x, y, 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++;
}
}
void UpdateMesh()
{
mesh.Clear();
mesh.vertices = vertices;
mesh.triangles = triangles;
mesh.RecalculateNormals();
}
}
Answer by SunnyChow · Oct 08, 2020 at 03:50 AM
if you generates everything in one mesh. I don't think it's a meaningful chunk. have you play Minecraft with low view distance? you can see if you walk forward, the game generates a chunk of map in front of you so that you can explore infinitely.
so to do the chunk concept, you should generate multiple meshes as grids. when user walking forward, the game remove the grids behind, and generate grids in front. if you really want it to be one mesh, it's possible, you can only generate the area around the player, instead of the whole world. However, it costs more CPU processing than the first method
Your answer
Follow this Question
Related Questions
How to export heightmap of a terrain during runtime. 0 Answers
How to deform a mesh on placing gameobject? 1 Answer
How to Optimize a Voxel-Based Procedural Terrain Generation 1 Answer
Writing a Custom Collider 0 Answers
Terrain trees below surface 0 Answers