Guest User

Untitled

a guest
Jul 2nd, 2025
7
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.70 KB | None | 0 0
  1. using Godot;
  2. using Godot.NativeInterop;
  3. using System;
  4. using System.Collections.Generic;
  5.  
  6. public partial class Chunk : MeshInstance3D
  7. {
  8.     ArrayMesh mesh;
  9.     int[][][] voxels;
  10.     public override void _Ready()
  11.     {
  12.         base._Ready();
  13.         voxels = GenerateVoxels();
  14.         GenerateMesh(voxels);
  15.     }
  16.     static int[][][] GenerateVoxels()
  17.     {
  18.         int[][][] array = new int[16][][];
  19.         for (int x = 0; x < 16; x++)
  20.         {
  21.             array[x] = new int[16][];
  22.             for (int y = 0; y < 16; y++)
  23.             {
  24.                 array[x][y] = new int[16];
  25.                 for (int z = 0; z < 16; z++) array[x][y][z] = 1;
  26.             }
  27.         }
  28.         return array;
  29.     }
  30.     public void GenerateMesh(int[][][] vxls)
  31.     {
  32.         Vector2[] tempUVs = [
  33.             new(0,0),
  34.             new(1,0),
  35.             new(1,1),
  36.             new(0,1)
  37.         ];
  38.         var faces = new List<Dictionary<string, Godot.Collections.Array>>();
  39.         for (int x = 0; x < 16; x++)
  40.         {
  41.             for (int y = 0; y < 16; y++)
  42.             {
  43.                 for (int z = 0; z < 16; z++)
  44.                 {
  45.                     if (vxls[x][y][z] == 1)
  46.                     {
  47.                         Vector3 pos = new(x, y, z);
  48.                         faces.Add(CreateFace(Vector3.Left, pos, tempUVs));
  49.                         faces.Add(CreateFace(Vector3.Right, pos, tempUVs));
  50.                         faces.Add(CreateFace(Vector3.Up, pos, tempUVs));
  51.                         faces.Add(CreateFace(Vector3.Down, pos, tempUVs));
  52.                         faces.Add(CreateFace(Vector3.Forward, pos, tempUVs));
  53.                         faces.Add(CreateFace(Vector3.Back, pos, tempUVs));
  54.                     }
  55.                 }
  56.             }
  57.         }
  58.         var verts = new Godot.Collections.Array();
  59.         var norms = new Godot.Collections.Array();
  60.         var uvs = new Godot.Collections.Array();
  61.         foreach (var face in faces)
  62.         {
  63.             verts.Add(face["verts"]);
  64.             verts.Add(face["verts"]);
  65.             uvs.Add(face["uvs"]);
  66.         }
  67.         var arrays = new Godot.Collections.Array();
  68.         arrays.Resize((int)Mesh.ArrayType.Max);
  69.         arrays[(int)Mesh.ArrayType.Vertex] = verts;
  70.         arrays[(int)Mesh.ArrayType.Normal] = norms;
  71.         arrays[(int)Mesh.ArrayType.TexUV] = uvs;
  72.         mesh = new ArrayMesh();
  73.         mesh.AddSurfaceFromArrays(Mesh.PrimitiveType.Triangles, arrays);
  74.         Mesh = mesh;
  75.     }
  76.     public Dictionary<string, Godot.Collections.Array> CreateFace(Vector3 dir, Vector3 pos, Vector2[] uvs)
  77.     {
  78.         Vector3[] verts = new Vector3[4];
  79.         Vector3[] norms = [dir, dir, dir, dir];
  80.         if (dir == Vector3.Up)
  81.             verts = [
  82.                 pos + new Vector3(-.5f, .5f, -.5f),
  83.                 pos + new Vector3(.5f, .5f, -.5f),
  84.                 pos + new Vector3(.5f, .5f, .5f),
  85.                 pos + new Vector3(-.5f, .5f, .5f)
  86.             ];
  87.         else if (dir == Vector3.Down)
  88.             verts = [
  89.                 pos + new Vector3(-.5f, -.5f, .5f),
  90.                 pos + new Vector3(.5f, -.5f, .5f),
  91.                 pos + new Vector3(.5f, -.5f, -.5f),
  92.                 pos + new Vector3(-.5f, -.5f, -.5f)
  93.             ];
  94.         else if (dir == Vector3.Left)
  95.             verts = [
  96.                 pos + new Vector3(-.5f, -.5f, -.5f),
  97.                 pos + new Vector3(-.5f, .5f, -.5f),
  98.                 pos + new Vector3(-.5f, .5f, .5f),
  99.                 pos + new Vector3(-.5f, -.5f, .5f)
  100.             ];
  101.         else if (dir == Vector3.Right)
  102.             verts = [
  103.                 pos + new Vector3(.5f, -.5f, .5f),
  104.                 pos + new Vector3(.5f, .5f, .5f),
  105.                 pos + new Vector3(.5f, .5f, -.5f),
  106.                 pos + new Vector3(.5f, -.5f, -.5f)
  107.             ];
  108.         else if (dir == Vector3.Forward)
  109.             verts = [
  110.                 pos + new Vector3(-.5f, -.5f, -.5f),
  111.                 pos + new Vector3(.5f, -.5f, -.5f),
  112.                 pos + new Vector3(.5f, .5f, -.5f),
  113.                 pos + new Vector3(-.5f, .5f, -.5f)
  114.             ];
  115.         else if (dir == Vector3.Back)
  116.             verts = [
  117.                 pos + new Vector3(-.5f, .5f, .5f),
  118.                 pos + new Vector3(.5f, .5f, .5f),
  119.                 pos + new Vector3(.5f, -.5f, .5f),
  120.                 pos + new Vector3(-.5f, -.5f, .5f)
  121.             ];
  122.         return new Dictionary<string, Godot.Collections.Array>
  123.         {
  124.             ["verts"] = [
  125.                 verts[0], verts[1], verts[2],
  126.                 verts[0], verts[2], verts[3]
  127.             ],
  128.             ["norms"] = [
  129.                 norms[0], norms[1], norms[2],
  130.                 norms[0], norms[2], norms[3]
  131.             ],
  132.             ["uvs"] = [
  133.                 uvs[0], uvs[1], uvs[2],
  134.                 uvs[0], uvs[2], uvs[3]
  135.             ]
  136.         };
  137.     }
  138. }
  139.  
Advertisement
Add Comment
Please, Sign In to add comment