This repository has been archived by the owner on Aug 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
SubChunk.cs
403 lines (328 loc) · 13.6 KB
/
SubChunk.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
using BuildPlate_Editor.Maths;
using OpenTK;
using OpenTK.Graphics.OpenGL4;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BuildPlate_Editor
{
public class SubChunk
{
List<Vertex> vertices = new List<Vertex>();
List<uint> triangles = new List<uint>();
private ChunkOutline outline;
public readonly uint[] blocks;
public readonly int[] renderers;
public Palette[] palette;
public Vector3i pos;
private int vao;
private int vbo;
private int ebo;
public int texId;
public SubChunk(Vector3i _position, uint[] _blocks, int[] _renderers, Palette[] _palette, int _texId)
{
pos = _position;
blocks = _blocks;
renderers = _renderers;
palette = _palette;
texId = _texId;
outline = new ChunkOutline(pos, VoxelData.ChunkWidth, 4f, Color.Yellow);
}
public static Vector3i[] possitionLookUp;
static SubChunk()
{
possitionLookUp = new Vector3i[VoxelData.ChunkLayerLength * VoxelData.ChunkHeight];
int x = 0;
int y = 0;
int z = 0;
int origx;
int origy;
int origz;
for (int currentBlock = 0; currentBlock < possitionLookUp.Length; currentBlock++) {
z++;
if (z == 16) { z = 0; y += 1; }
if (y == 16) { y = 0; x += 1; }
origz = z;
origy = y;
origx = x;
if (z == 0) {
z = 16;
y -= 1;
}
if (y == -1)
y = 16;
if (Math.Abs(z) % 16 == 0 && y == 16) {
y--;
x--;
}
possitionLookUp[currentBlock] = new Vector3i(x, y, z);
z = origz;
y = origy;
x = origx;
}
}
public void Init()
{
CreateMeshData();
InitMesh();
}
public void CreateMeshData()
{
vertices.Clear();
triangles.Clear();
for (int currentBlock = 0; currentBlock < blocks.Length; currentBlock++) {
int renderer = renderers[currentBlock];
if (renderer > -1 && blocks[currentBlock] < palette.Length) { // don't render air
#if DEBUG
uint blockId = blocks[currentBlock];
Palette pal = palette[blockId];
World.blockRenderers[renderer](possitionLookUp[currentBlock], pos * 16, pal.textures, pal.data,
ref vertices, ref triangles);
#else
try {
Palette pal = palette[blocks[currentBlock]];
World.blockRenderers[renderer](possitionLookUp[currentBlock], pos * 16, pal.textures, pal.data,
ref vertices, ref triangles);
} catch (Exception ex) {
uint b = blocks[currentBlock];
string name = "Couldn't get";
string data = "Couldn't get";
if (b >= 0 && b < palette.Length) {
name = palette[b].name;
data = palette[b].data.ToString();
}
Util.Exit(EXITCODE.World_Render_Block, ex, $"Block ID: {blocks[currentBlock]}, SubChunk pos: {pos}, Renderer ID: {renderer}, " +
$"Block Name: {name}, Block Data: {data}");
}
#endif
}
}
}
public void InitMesh()
{
GL.CreateVertexArrays(1, out vao);
GL.BindVertexArray(vao);
GL.CreateBuffers(1, out ebo);
GL.CreateBuffers(1, out vbo);
CreateMesh();
}
public void CreateMesh()
{
GL.NamedBufferData(ebo, triangles.Count * sizeof(uint), triangles.ToArray(), BufferUsageHint.StaticDraw);
GL.VertexArrayElementBuffer(vao, ebo);
int vertexBindingPoint = 0;
GL.NamedBufferData(vbo, vertices.Count * Vertex.Size, vertices.ToArray(), BufferUsageHint.StaticDraw);
GL.VertexArrayVertexBuffer(vao, vertexBindingPoint, vbo, IntPtr.Zero, Vertex.Size);
// pos
GL.VertexArrayAttribFormat(vao, 0, 3, VertexAttribType.Float, false, 0);
GL.VertexArrayAttribBinding(vao, 0, vertexBindingPoint);
GL.EnableVertexArrayAttrib(vao, 0);
// uv
GL.VertexArrayAttribFormat(vao, 1, 3, VertexAttribType.Float, false, 3 * sizeof(float));
GL.VertexArrayAttribBinding(vao, 1, vertexBindingPoint);
GL.EnableVertexArrayAttrib(vao, 1);
}
public void Update()
{
CreateMeshData();
CreateMesh();
}
public void Render(Shader s, Shader outlineShader)
{
Matrix4 transform = Matrix4.CreateTranslation(new Vector3(pos.X * VoxelData.ChunkWidth, pos.Y * VoxelData.ChunkHeight, pos.Z * VoxelData.ChunkWidth));
s.Bind();
s.UploadMat4("uTransform", ref transform);
GL.ActiveTexture(TextureUnit.Texture1);
GL.BindTexture(TextureTarget.Texture2DArray, texId);
s.UploadInt("textures", 1);
GL.BindVertexArray(vao);
GL.DrawElements(BeginMode.Triangles, triangles.Count, DrawElementsType.UnsignedInt, 0);
if (World.ShowChunkOutlines)
outline.Render(outlineShader);
}
public void SetBlock(int X, int Y, int Z, uint id)
{
if (X < 0 || X >= VoxelData.ChunkWidth || Y < 0 || Y >= VoxelData.ChunkHeight || Z < 0 || Z >= VoxelData.ChunkWidth)
return;
blocks[Z + (X * VoxelData.ChunkWidth) + (Y * VoxelData.ChunkLayerLength)] = id;
}
public void SetBlock(Vector3i pos, uint id)
=> SetBlock(pos.X, pos.Y, pos.Z, id);
public uint GetBlock(int X, int Y, int Z)
{
if (X < 0 || X >= VoxelData.ChunkWidth || Y < 0 || Y >= VoxelData.ChunkHeight || Z < 0 || Z >= VoxelData.ChunkWidth)
return 0;
return blocks[Z + (X * VoxelData.ChunkWidth) + (Y * VoxelData.ChunkLayerLength)];
}
public uint GetBlock(Vector3i pos)
=> GetBlock(pos.X, pos.Y, pos.Z);
public int GetRenderer(int X, int Y, int Z)
{
if (X < 0 || X >= VoxelData.ChunkWidth || Y < 0 || Y >= VoxelData.ChunkHeight || Z < 0 || Z >= VoxelData.ChunkWidth)
return 0;
return renderers[Z + (X * VoxelData.ChunkWidth) + (Y * VoxelData.ChunkLayerLength)];
}
public int GetRenderer(Vector3i pos)
=> GetRenderer(pos.X, pos.Y, pos.Z);
public void GetBlockIndex(int bx, int by, int bz, out int blockIndex)
{
Vector3i offset = new Vector3i(pos.X * VoxelData.ChunkWidth, pos.Y * VoxelData.ChunkHeight, pos.Z * VoxelData.ChunkWidth);
Vector3i block = new Vector3i(bx, by, bz);
block -= offset;
for (int currentBlock = 0; currentBlock < blocks.Length; currentBlock++) {
if (possitionLookUp[currentBlock] == block) {
blockIndex = currentBlock;
return;
}
}
blockIndex = -1;
}
public int GetPaletteIndex(string name, int data, bool compareData)
{
if (!name.Contains(":"))
name = "minecraft:" + name;
for (int i = 0; i < palette.Length; i++)
if (palette[i].name == name && (!compareData || palette[i].data == data))
return i;
return -1;
}
public Palette GetPaletteByName(string name)
{
if (!name.Contains(":"))
name = "minecraft:" + name;
for (int i = 0; i < palette.Length; i++)
if (palette[i].name == name)
return palette[i];
return Palette.NULL;
}
public void SetBlock(int blockIndex, int palletteIndex, int renderer, int data, bool update)
{
if (blockIndex < 0 || blockIndex >= blocks.Length)
return;
blocks[blockIndex] = (uint)palletteIndex;
renderers[blockIndex] = renderer;
Update();
Vector3i p = possitionLookUp[blockIndex];
if (p.X < 2)
World.UpdateChunk(pos - Vector3i.UnitX);
else if (p.X > 13)
World.UpdateChunk(pos + Vector3i.UnitX);
if (p.Y < 2)
World.UpdateChunk(pos - Vector3i.UnitY);
else if (p.Y > 13)
World.UpdateChunk(pos + Vector3i.UnitY);
if (p.Z < 2)
World.UpdateChunk(pos - Vector3i.UnitZ);
else if (p.Z > 13)
World.UpdateChunk(pos + Vector3i.UnitZ);
}
public int AddNewPalette(string name, int data)
{
GL.DeleteTexture(texId);
for (int i = 0; i < World.chunks.Length; i++)
if (World.chunks[i].pos == pos) {
string mcn = name.Contains(':') ? name : "minecraft:" + name;
Array.Resize(ref palette, palette.Length + 1);
palette[palette.Length - 1] = new Palette(mcn, data, -1);
World.ReloadPaletteTextures(i);
return palette.Length - 1;
}
return -1;
}
public int NextPaletteIndex()
{
if (palette.Length > 0 && palette[palette.Length - 1].textures.Length > 0) {
Palette p = palette[palette.Length - 1];
return p.textures[p.textures.Length - 1];
}
else
return -1;
}
public static bool WouldBeBlockInChunk(Vector3i _chunkPos, Vector3i blockPos)
{
Vector3i chunkPos = _chunkPos * 16;
for (int i = 0; i < possitionLookUp.Length; i++)
if (possitionLookUp[i] + chunkPos == blockPos)
return true;
return false;
}
public static Vector3i GetToWhereCreateChunk(Vector3i shouldBeThere, Vector3i blockPos)
{
if (WouldBeBlockInChunk(shouldBeThere, blockPos))
return shouldBeThere;
for (int i = 0; i < VoxelData.faceChecks.Length; i++) {
Vector3i offset = VoxelData.faceChecks[i];
if (WouldBeBlockInChunk(shouldBeThere + offset, blockPos))
return shouldBeThere + offset;
}
return shouldBeThere;
}
class ChunkOutline
{
public Vector3 Position { get; set; }
public ColVertex[] vertices;
public uint[] triangles;
public bool Active { get; set; }
protected int vao;
protected int vbo;
protected int ebo;
public float lineWidth;
public ChunkOutline(Vector3i pos, float size, float _lineWidth, Color _c)
{
Vector4 c = new Vector4(
(float)_c.R / 255f,
(float)_c.G / 255f,
(float)_c.B / 255f,
(float)_c.A / 255f
);
Vector3 half = Vector3.One / 2f;
vertices = new ColVertex[VoxelData.voxelVerts.Length];
for (int i = 0; i < vertices.Length; i++)
vertices[i] = new ColVertex(VoxelData.voxelVerts[i] * size, c);
triangles = VoxelData.voxelLines;
lineWidth = _lineWidth;
Position = pos * size + new Vector3(0.5f, 0.5f, 0.5f);
InitMesh();
Active = true;
}
private void InitMesh()
{
GL.CreateVertexArrays(1, out vao);
GL.BindVertexArray(vao);
GL.CreateBuffers(1, out ebo);
GL.CreateBuffers(1, out vbo);
UploadMesh();
}
public void UploadMesh()
{
GL.NamedBufferData(ebo, triangles.Length * sizeof(uint), triangles, BufferUsageHint.StaticDraw);
GL.VertexArrayElementBuffer(vao, ebo);
int vertexBindingPoint = 0;
GL.NamedBufferData(vbo, vertices.Length * ColVertex.Size, vertices, BufferUsageHint.StaticDraw);
GL.VertexArrayVertexBuffer(vao, vertexBindingPoint, vbo, IntPtr.Zero, ColVertex.Size);
// pos
GL.VertexArrayAttribFormat(vao, 0, 3, VertexAttribType.Float, false, 0);
GL.VertexArrayAttribBinding(vao, 0, vertexBindingPoint);
GL.EnableVertexArrayAttrib(vao, 0);
// color
GL.VertexArrayAttribFormat(vao, 1, 4, VertexAttribType.Float, false, 3 * sizeof(float));
GL.VertexArrayAttribBinding(vao, 1, vertexBindingPoint);
GL.EnableVertexArrayAttrib(vao, 1);
}
public void Render(Shader s)
{
if (!Active)
return;
s.Bind();
Matrix4 transform = Matrix4.CreateTranslation(Position);
s.UploadMat4("uTransform", ref transform);
GL.BindVertexArray(vao);
GL.LineWidth(lineWidth);
GL.DrawElements(BeginMode.Lines, triangles.Length, DrawElementsType.UnsignedInt, 0);
}
}
}
}