Skip to content
This repository was archived by the owner on Aug 3, 2023. It is now read-only.

Commit 62ef62d

Browse files
committed
Multi texture blocks
+Logs render properly +Textures were shifted before, which was now fixed
1 parent 43216b8 commit 62ef62d

File tree

4 files changed

+87
-84
lines changed

4 files changed

+87
-84
lines changed

Program.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ static void Main(string[] args)
9191
texturesPath += '/';
9292
World.textureBasePath = texturesPath;
9393
#if DEBUG
94-
//World.targetFilePath = @"C:\Users\Tomas\Desktop\Project Earth\Api\data\buildplates\7cd6d53b-1715-4b22-9a99-d6d43edd61df.json";
95-
World.targetFilePath = @"C:\Users\Tomas\Desktop\Project Earth\Api\data\buildplates\2cb8bda2-c49b-4ead-887f-593d37bc2784.json";
94+
World.targetFilePath = @"C:\Users\Tomas\Desktop\Project Earth\Api\data\buildplates\70c9c5eb-580d-41d2-8201-2577fec29dd6.json";
95+
//World.targetFilePath = @"C:\Users\Tomas\Desktop\Project Earth\Api\data\buildplates\2cb8bda2-c49b-4ead-887f-593d37bc2784.json";
9696
#else
9797
Console.Write("Build plate to edit (.json): ");
9898
// json - api, plate - data only, plate64 - base64 encoded data

SkyBox.cs

Lines changed: 0 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -117,77 +117,4 @@ public static void Render(Shader s)
117117
GL.DepthMask(true);
118118
}
119119
}
120-
121-
/*public static class SkyBox
122-
{
123-
private static Vertex2[] verts = new Vertex2[0];
124-
private static uint[] tris = new uint[0];
125-
126-
public static Vector3 pos;
127-
128-
private static int vao;
129-
private static int vbo;
130-
private static int ebo;
131-
132-
public static int texId { get; private set; }
133-
134-
public static void Init(string texturePath, Vector3 cameraPos, float size)
135-
{
136-
if (File.Exists(texturePath)) {
137-
Texture tex = new Texture(texturePath);
138-
texId = tex.id;
139-
}
140-
else
141-
texId = -1;
142-
pos = cameraPos;
143-
//Util.SkyboxTex(Vector3.One * size, ref verts, ref tris);
144-
SphereMesh.Create(5, size, ref tris, ref verts);
145-
InitMesh();
146-
}
147-
148-
static void InitMesh()
149-
{
150-
GL.CreateVertexArrays(1, out vao);
151-
GL.BindVertexArray(vao);
152-
GL.CreateBuffers(1, out ebo);
153-
GL.CreateBuffers(1, out vbo);
154-
CreateMesh();
155-
}
156-
157-
static void CreateMesh()
158-
{
159-
GL.NamedBufferData(ebo, tris.Length * sizeof(uint), tris, BufferUsageHint.DynamicDraw);
160-
GL.VertexArrayElementBuffer(vao, ebo);
161-
162-
int vertexBindingPoint = 0;
163-
GL.NamedBufferData(vbo, verts.Length * Vertex2.Size, verts, BufferUsageHint.DynamicDraw);
164-
GL.VertexArrayVertexBuffer(vao, vertexBindingPoint, vbo, IntPtr.Zero, Vertex2.Size);
165-
166-
// pos
167-
GL.VertexArrayAttribFormat(vao, 0, 3, VertexAttribType.Float, false, 0);
168-
GL.VertexArrayAttribBinding(vao, 0, vertexBindingPoint);
169-
GL.EnableVertexArrayAttrib(vao, 0);
170-
// uv
171-
GL.VertexArrayAttribFormat(vao, 1, 2, VertexAttribType.Float, false, 3 * sizeof(float));
172-
GL.VertexArrayAttribBinding(vao, 1, vertexBindingPoint);
173-
GL.EnableVertexArrayAttrib(vao, 1);
174-
}
175-
176-
public static void Render(Shader s)
177-
{
178-
if (texId == -1)
179-
return;
180-
181-
Matrix4 transform = Matrix4.CreateTranslation(pos);
182-
183-
s.Bind();
184-
s.UploadMat4("uTransform", ref transform);
185-
GL.ActiveTexture(TextureUnit.Texture0);
186-
GL.BindTexture(TextureTarget.Texture2D, texId);
187-
s.UploadInt("tex", 0);
188-
189-
GL.BindVertexArray(vao);
190-
GL.DrawElements(BeginMode.Triangles, tris.Length, DrawElementsType.UnsignedInt, 0);
191-
}
192-
}*/
193120
}

SubChunk.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,11 @@ public void CreateMeshData()
7070
y = 16;
7171

7272
int renderer = renderers[currentBlock];
73-
if (renderer > -1 && blocks[currentBlock] < palette.Length) // don't render air
74-
World.blockRenderers[renderer](new Vector3(x, y, z), pos * 16, palette[blocks[currentBlock]].textures, palette[blocks[currentBlock]].data,
73+
if (renderer > -1 && blocks[currentBlock] < palette.Length) { // don't render air
74+
Palette pal = palette[blocks[currentBlock]];
75+
World.blockRenderers[renderer](new Vector3(x, y, z), pos * 16, pal.textures, pal.data,
7576
ref vertices, ref triangles);
77+
}
7678

7779
z = origz;
7880
y = origy;

World.cs

Lines changed: 81 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public static class World
3535
{ "jungle_stairs", new []{ "planks_jungle" } },
3636
{ "spruce_stairs", new []{ "planks_spruce" } },
3737
{ "dark_oak_stairs", new []{ "planks_big_oak" } },
38+
{ "birch_stairs", new []{ "planks_birch" } },
3839
{ "stone_brick_stairs", new []{ "stonebrick" } },
3940
{ "polished_andesite_stairs", new []{ "stone_andesite_smooth" } },
4041
{ "polished_diorite_stairs", new []{ "stone_diorite_smooth" } },
@@ -177,15 +178,15 @@ public static class World
177178
int logType = data & 0b_0011;
178179
switch (logType) {
179180
case 0:
180-
return new [] { "log_oak_top" } ;
181+
return new [] { "log_oak", "log_oak_top" } ;
181182
case 1:
182-
return new [] { "log_spruce_top" } ;
183+
return new [] { "log_spruce", "log_spruce_top" } ;
183184
case 2:
184-
return new [] { "log_birch_top" } ;
185+
return new [] { "log_birch", "log_birch_top" } ;
185186
case 3:
186-
return new [] { "log_jungle_top" } ;
187+
return new [] { "log_jungle", "log_jungle_top" } ;
187188
default:
188-
return new [] { "log_oak_top" } ;
189+
return new [] { "log_oak", "log_oak_top" } ;
189190
}
190191
}
191192
},
@@ -695,6 +696,19 @@ public static class World
695696
}
696697
} // oak
697698
},
699+
{ "birch_door", (int data) =>
700+
{
701+
int upper_block_bit = (data & 0b_1000) >> 3;
702+
switch (upper_block_bit) {
703+
case 0:
704+
return new [] { "door_birch_lower" };
705+
case 1:
706+
return new [] { "door_birch_upper" };
707+
default:
708+
return new [] { "door_birch_lower" };
709+
}
710+
} // oak
711+
},
698712

699713

700714
{ "", (int data) =>
@@ -775,12 +789,13 @@ public static class World
775789
{ "snow_layer", 15 },
776790
{ "powered_repeater", 16 },
777791
{ "unpowered_repeater", 16 },
792+
{ "vine", 19 },
778793
};
779794

780795

781796
public delegate void RenderBlock(Vector3 pos, Vector3i cp/*chunk pos, pre multiplied*/, int[] tex, int data, ref List<Vertex> vertices, ref List<uint> triangles);
782797

783-
// TODO stair other corner
798+
// TODO stair other corner, Data meaning from: https://minecraft.fandom.com/wiki/Block_states
784799
public static Dictionary<int, RenderBlock> blockRenderers = new Dictionary<int, RenderBlock>()
785800
{
786801
{ 0, (Vector3 pos, Vector3i cp, int[] tex, int data, ref List<Vertex> vertices, ref List<uint> triangles) => // full
@@ -1360,6 +1375,57 @@ public static class World
13601375
}
13611376
}
13621377
},
1378+
{ 17, (Vector3 pos, Vector3i cp, int[] texA, int data, ref List<Vertex> vertices, ref List<uint> triangles) => // log
1379+
{
1380+
Vector3 offset = new Vector3(-0.5f, -0.5f, -0.5f);
1381+
1382+
uint texSide = (uint)texA[0];
1383+
uint texTopBottom = (uint)texA[1];
1384+
1385+
int dir = (data & 0b_1100) >> 2; // 0 - y, 1 - x, 2 - z
1386+
Matrix3 mat = Matrix3.Identity;
1387+
1388+
if (dir == 1) {
1389+
mat = Matrix3.CreateRotationZ(1.5708f); // 90 degrees
1390+
} else if (dir == 2) {
1391+
mat = Matrix3.CreateRotationX(1.5708f); // 90 degrees
1392+
}
1393+
1394+
for (int p = 0; p < 6; p++) {
1395+
uint firstVertIndex = (uint)vertices.Count;
1396+
if (p == 2 || p == 3) { // top/bottom
1397+
vertices.Add(new Vertex(pos + VoxelData.voxelVerts[VoxelData.voxelTris[p, 0]] + offset, VoxelData.voxelUvs[0], texTopBottom));
1398+
vertices.Add(new Vertex(pos + VoxelData.voxelVerts[VoxelData.voxelTris[p, 1]] + offset, VoxelData.voxelUvs[1], texTopBottom));
1399+
vertices.Add(new Vertex(pos + VoxelData.voxelVerts[VoxelData.voxelTris[p, 2]] + offset, VoxelData.voxelUvs[2], texTopBottom));
1400+
vertices.Add(new Vertex(pos + VoxelData.voxelVerts[VoxelData.voxelTris[p, 3]] + offset, VoxelData.voxelUvs[3], texTopBottom));
1401+
} else {
1402+
vertices.Add(new Vertex(pos + VoxelData.voxelVerts[VoxelData.voxelTris[p, 0]] + offset, VoxelData.voxelUvs[0], texSide));
1403+
vertices.Add(new Vertex(pos + VoxelData.voxelVerts[VoxelData.voxelTris[p, 1]] + offset, VoxelData.voxelUvs[1], texSide));
1404+
vertices.Add(new Vertex(pos + VoxelData.voxelVerts[VoxelData.voxelTris[p, 2]] + offset, VoxelData.voxelUvs[2], texSide));
1405+
vertices.Add(new Vertex(pos + VoxelData.voxelVerts[VoxelData.voxelTris[p, 3]] + offset, VoxelData.voxelUvs[3], texSide));
1406+
}
1407+
triangles.Add(firstVertIndex);
1408+
triangles.Add(firstVertIndex + 1);
1409+
triangles.Add(firstVertIndex + 2);
1410+
triangles.Add(firstVertIndex + 2);
1411+
triangles.Add(firstVertIndex + 1);
1412+
triangles.Add(firstVertIndex + 3);
1413+
}
1414+
}
1415+
},
1416+
{ 18, (Vector3 pos, Vector3i cp, int[] texA, int data, ref List<Vertex> vertices, ref List<uint> triangles) => // log stripped
1417+
{
1418+
blockRenderers[17](pos, cp, texA, data << 2/*here axis is 0b0011, for normal logs it's 0b1100 so we need to shift it*/,
1419+
ref vertices, ref triangles);
1420+
}
1421+
},
1422+
{ 19, (Vector3 pos, Vector3i cp, int[] tex, int data, ref List<Vertex> vertices, ref List<uint> triangles) => // vine
1423+
{
1424+
Vector3 offset = new Vector3(0f, 0f, 0f);
1425+
Vector3 size = new Vector3(1f, 1f, 1f);
1426+
CubeTex(tex[0], pos + offset, size, ref vertices, ref triangles);
1427+
}
1428+
},
13631429
};
13641430

13651431
private static BuildPlate plate;
@@ -1556,6 +1622,10 @@ public static void Init()
15561622
string blockName = blockNames[subchunk][plate.sub_chunks[subchunk].blocks[block]];
15571623
if (blockRenderersLookUp.ContainsKey(blockName))
15581624
renderers[block] = blockRenderersLookUp[blockName];
1625+
else if (blockName.Contains("stripped")) // stripped log
1626+
renderers[block] = 18;
1627+
else if (blockName.Contains("log"))
1628+
renderers[block] = 17;
15591629
else if (blockName.Contains("pressure_plate"))
15601630
renderers[block] = 14;
15611631
else if (blockName.Contains("gate"))
@@ -1580,7 +1650,11 @@ public static void Init()
15801650

15811651
Palette[] palette = new Palette[plate.sub_chunks[subchunk].block_palette.Count - 1];
15821652
for (int i = 0; i < palette.Length; i++) {
1583-
palette[i] = new Palette(plate.sub_chunks[subchunk].block_palette[i + 1].name, plate.sub_chunks[subchunk].block_palette[i].data, textures[i + 1]);
1653+
int[] _tex = textures[i].Cloned();
1654+
for (int j = 0; j < _tex.Length; j++) {
1655+
_tex[j] = _tex[j] + 1;
1656+
}
1657+
palette[i] = new Palette(plate.sub_chunks[subchunk].block_palette[i + 1].name, plate.sub_chunks[subchunk].block_palette[i].data, _tex);
15841658
Console.WriteLine($"[{i}] Block: {palette[i].name}, Data: {palette[i].data}, Texture: {palette[i].textures[0]}");
15851659
}
15861660

0 commit comments

Comments
 (0)