Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Godot;
- using Godot.NativeInterop;
- using System;
- using System.Collections.Generic;
- public partial class Chunk : MeshInstance3D
- {
- ArrayMesh mesh;
- int[][][] voxels;
- public override void _Ready()
- {
- base._Ready();
- voxels = GenerateVoxels();
- GenerateMesh(voxels);
- }
- static int[][][] GenerateVoxels()
- {
- int[][][] array = new int[16][][];
- for (int x = 0; x < 16; x++)
- {
- array[x] = new int[16][];
- for (int y = 0; y < 16; y++)
- {
- array[x][y] = new int[16];
- for (int z = 0; z < 16; z++) array[x][y][z] = 1;
- }
- }
- return array;
- }
- public void GenerateMesh(int[][][] vxls)
- {
- Vector2[] tempUVs = [
- new(0,0),
- new(1,0),
- new(1,1),
- new(0,1)
- ];
- var faces = new List<Dictionary<string, Godot.Collections.Array>>();
- for (int x = 0; x < 16; x++)
- {
- for (int y = 0; y < 16; y++)
- {
- for (int z = 0; z < 16; z++)
- {
- if (vxls[x][y][z] == 1)
- {
- Vector3 pos = new(x, y, z);
- faces.Add(CreateFace(Vector3.Left, pos, tempUVs));
- faces.Add(CreateFace(Vector3.Right, pos, tempUVs));
- faces.Add(CreateFace(Vector3.Up, pos, tempUVs));
- faces.Add(CreateFace(Vector3.Down, pos, tempUVs));
- faces.Add(CreateFace(Vector3.Forward, pos, tempUVs));
- faces.Add(CreateFace(Vector3.Back, pos, tempUVs));
- }
- }
- }
- }
- var verts = new Godot.Collections.Array();
- var norms = new Godot.Collections.Array();
- var uvs = new Godot.Collections.Array();
- foreach (var face in faces)
- {
- verts.Add(face["verts"]);
- verts.Add(face["verts"]);
- uvs.Add(face["uvs"]);
- }
- var arrays = new Godot.Collections.Array();
- arrays.Resize((int)Mesh.ArrayType.Max);
- arrays[(int)Mesh.ArrayType.Vertex] = verts;
- arrays[(int)Mesh.ArrayType.Normal] = norms;
- arrays[(int)Mesh.ArrayType.TexUV] = uvs;
- mesh = new ArrayMesh();
- mesh.AddSurfaceFromArrays(Mesh.PrimitiveType.Triangles, arrays);
- Mesh = mesh;
- }
- public Dictionary<string, Godot.Collections.Array> CreateFace(Vector3 dir, Vector3 pos, Vector2[] uvs)
- {
- Vector3[] verts = new Vector3[4];
- Vector3[] norms = [dir, dir, dir, dir];
- if (dir == Vector3.Up)
- verts = [
- pos + new Vector3(-.5f, .5f, -.5f),
- pos + new Vector3(.5f, .5f, -.5f),
- pos + new Vector3(.5f, .5f, .5f),
- pos + new Vector3(-.5f, .5f, .5f)
- ];
- else if (dir == Vector3.Down)
- verts = [
- pos + new Vector3(-.5f, -.5f, .5f),
- pos + new Vector3(.5f, -.5f, .5f),
- pos + new Vector3(.5f, -.5f, -.5f),
- pos + new Vector3(-.5f, -.5f, -.5f)
- ];
- else if (dir == Vector3.Left)
- verts = [
- pos + new Vector3(-.5f, -.5f, -.5f),
- pos + new Vector3(-.5f, .5f, -.5f),
- pos + new Vector3(-.5f, .5f, .5f),
- pos + new Vector3(-.5f, -.5f, .5f)
- ];
- else if (dir == Vector3.Right)
- verts = [
- pos + new Vector3(.5f, -.5f, .5f),
- pos + new Vector3(.5f, .5f, .5f),
- pos + new Vector3(.5f, .5f, -.5f),
- pos + new Vector3(.5f, -.5f, -.5f)
- ];
- else if (dir == Vector3.Forward)
- verts = [
- pos + new Vector3(-.5f, -.5f, -.5f),
- pos + new Vector3(.5f, -.5f, -.5f),
- pos + new Vector3(.5f, .5f, -.5f),
- pos + new Vector3(-.5f, .5f, -.5f)
- ];
- else if (dir == Vector3.Back)
- verts = [
- pos + new Vector3(-.5f, .5f, .5f),
- pos + new Vector3(.5f, .5f, .5f),
- pos + new Vector3(.5f, -.5f, .5f),
- pos + new Vector3(-.5f, -.5f, .5f)
- ];
- return new Dictionary<string, Godot.Collections.Array>
- {
- ["verts"] = [
- verts[0], verts[1], verts[2],
- verts[0], verts[2], verts[3]
- ],
- ["norms"] = [
- norms[0], norms[1], norms[2],
- norms[0], norms[2], norms[3]
- ],
- ["uvs"] = [
- uvs[0], uvs[1], uvs[2],
- uvs[0], uvs[2], uvs[3]
- ]
- };
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment