Created
December 8, 2019 14:08
-
-
Save matyklug18/2a93b82d70e417ab4d4d0d45247d67b7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package opecraft.util; | |
import opecraft.blocks.Block; | |
import opecraft.blocks.BlockAir; | |
import opecraft.blocks.BlockSide; | |
import opecraft.blocks.BlockWater; | |
import opecraft.data.Chunk; | |
import opecraft.data.ChunkMesh; | |
import opecraft.data.Vertex; | |
import org.joml.Vector2f; | |
import org.joml.Vector3f; | |
public class VoxelChunkMeshMaker { | |
static int index = 0; | |
final static int xAmount = 8; | |
final static int yAmount = 1; | |
final static float xStep = 1.0f / ((float) xAmount); | |
final static float yStep = 1.0f / ((float) yAmount); | |
static Block[][][] blockss; | |
public static ChunkMesh gen(Chunk chunk) { | |
Block[][][] blocks = chunk.blocks; | |
blockss = blocks; | |
index = 0; | |
ChunkMesh ChunkMesh = new ChunkMesh(new Vertex[] {}, new int[] {}); | |
for(int x = 0; x < blocks.length; x++) { | |
for(int y = 0; y < blocks[x].length; y++) { | |
for(int z = 0; z < blocks[x][y].length; z++) { | |
if(!(blocks[x][y][z] instanceof BlockAir)) { | |
if (x < blocks.length - 1) { | |
if (blocks[x+1][y][z].isTransparent() && !(blocks[x+1][y][z].getClass().equals(blocks[x][y][z].getClass()))) { | |
right(ChunkMesh, x, y, z, blocks[x][y][z]); | |
} | |
} else | |
right(ChunkMesh, x, y, z, blocks[x][y][z]); | |
} | |
if(!(blocks[x][y][z] instanceof BlockAir)) { | |
if (x > 0) { | |
if (blocks[x-1][y][z].isTransparent() && !(blocks[x-1][y][z].getClass().equals(blocks[x][y][z].getClass()))) { | |
left(ChunkMesh, x, y, z, blocks[x][y][z]); | |
} | |
} else | |
left(ChunkMesh, x, y, z, blocks[x][y][z]); | |
} | |
if(!(blocks[x][y][z] instanceof BlockAir)) { | |
if (y < blocks[x].length - 1) { | |
if (blocks[x][y + 1][z].isTransparent() && !(blocks[x][y+1][z].getClass().equals(blocks[x][y][z].getClass()))) { | |
top(ChunkMesh, x, y, z, blocks[x][y][z]); | |
} | |
} else | |
top(ChunkMesh, x, y, z, blocks[x][y][z]); | |
} | |
if(!(blocks[x][y][z] instanceof BlockAir)) { | |
if (y > 0) { | |
if (blocks[x][y - 1][z].isTransparent() && !(blocks[x][y-1][z].getClass().equals(blocks[x][y][z].getClass()))) { | |
bottom(ChunkMesh, x, y, z, blocks[x][y][z]); | |
} | |
} else | |
bottom(ChunkMesh, x, y, z, blocks[x][y][z]); | |
} | |
if(!(blocks[x][y][z] instanceof BlockAir)) { | |
if (z < blocks[x][y].length - 1) { | |
if (blocks[x][y][z + 1].isTransparent() && !(blocks[x][y][z+1].getClass().equals(blocks[x][y][z].getClass()))) { | |
front(ChunkMesh, x, y, z, blocks[x][y][z]); | |
} | |
} else | |
front(ChunkMesh, x, y, z, blocks[x][y][z]); | |
} | |
if(!(blocks[x][y][z] instanceof BlockAir)) { | |
if (z > 0) { | |
if (blocks[x][y][z - 1].isTransparent() && !(blocks[x][y][z-1].getClass().equals(blocks[x][y][z].getClass()))) { | |
back(ChunkMesh, x, y, z, blocks[x][y][z]); | |
} | |
} else | |
back(ChunkMesh, x, y, z, blocks[x][y][z]); | |
} | |
} | |
} | |
} | |
return ChunkMesh; | |
} | |
static ChunkMesh back(ChunkMesh chunkMesh, int x, int y, int z, Block block) { | |
if(!(block instanceof BlockWater) || !(block instanceof BlockWater && blockss[x][y+1][z] instanceof BlockAir)) | |
chunkMesh.combine(new ChunkMesh(new Vertex[] { | |
//Back face | |
new Vertex(new Vector3f(-0.5f + x, 0.5f + y, -0.5f + z), new Vector3f(0,0,-1), new Vector2f(xStep * (block.getAtlasX(BlockSide.BACK) - 1), yStep * (block.getAtlasY(BlockSide.BACK) - 1))), | |
new Vertex(new Vector3f(-0.5f + x, -0.5f + y, -0.5f + z), new Vector3f(0,0,-1), new Vector2f(xStep * (block.getAtlasX(BlockSide.BACK) - 1), yStep * block.getAtlasY(BlockSide.BACK))), | |
new Vertex(new Vector3f( 0.5f + x, -0.5f + y, -0.5f + z), new Vector3f(0,0,-1), new Vector2f(xStep * block.getAtlasX(BlockSide.BACK), yStep * block.getAtlasY(BlockSide.BACK))), | |
new Vertex(new Vector3f( 0.5f + x, 0.5f + y, -0.5f + z), new Vector3f(0,0,-1), new Vector2f(xStep * block.getAtlasX(BlockSide.BACK), yStep * (block.getAtlasY(BlockSide.BACK) - 1))), | |
}, new int[] { | |
//Back face | |
0 + index * 4, 1 + index * 4, 3 + index * 4, | |
3 + index * 4, 1 + index * 4, 2 + index * 4, | |
})); | |
else | |
chunkMesh.combine(new ChunkMesh(new Vertex[] { | |
//Back face | |
new Vertex(new Vector3f(-0.5f + x, 0.4f + y, -0.5f + z), new Vector3f(0,0,-1), new Vector2f(xStep * (block.getAtlasX(BlockSide.BACK) - 1), yStep * (block.getAtlasY(BlockSide.BACK) - 1))), | |
new Vertex(new Vector3f(-0.5f + x, -0.4f + y, -0.5f + z), new Vector3f(0,0,-1), new Vector2f(xStep * (block.getAtlasX(BlockSide.BACK) - 1), yStep * block.getAtlasY(BlockSide.BACK))), | |
new Vertex(new Vector3f( 0.5f + x, -0.4f + y, -0.5f + z), new Vector3f(0,0,-1), new Vector2f(xStep * block.getAtlasX(BlockSide.BACK), yStep * block.getAtlasY(BlockSide.BACK))), | |
new Vertex(new Vector3f( 0.5f + x, 0.4f + y, -0.5f + z), new Vector3f(0,0,-1), new Vector2f(xStep * block.getAtlasX(BlockSide.BACK), yStep * (block.getAtlasY(BlockSide.BACK) - 1))), | |
}, new int[] { | |
//Back face | |
0 + index * 4, 1 + index * 4, 3 + index * 4, | |
3 + index * 4, 1 + index * 4, 2 + index * 4, | |
})); | |
index ++; | |
return chunkMesh; | |
} | |
static ChunkMesh front(ChunkMesh chunkMesh, int x, int y, int z, Block block) { | |
if(!(block instanceof BlockWater) || !(block instanceof BlockWater && blockss[x][y+1][z] instanceof BlockAir)) | |
chunkMesh.combine(new ChunkMesh(new Vertex[] { | |
//Front face | |
new Vertex(new Vector3f(-0.5f + x, 0.5f + y, 0.5f + z), new Vector3f(0,0,1), new Vector2f(xStep * (block.getAtlasX(BlockSide.FRONT) - 1), yStep * (block.getAtlasY(BlockSide.FRONT) - 1))), | |
new Vertex(new Vector3f(-0.5f + x, -0.5f + y, 0.5f + z), new Vector3f(0,0,1), new Vector2f(xStep * (block.getAtlasX(BlockSide.FRONT) - 1), yStep * block.getAtlasY(BlockSide.FRONT))), | |
new Vertex(new Vector3f( 0.5f + x, -0.5f + y, 0.5f + z), new Vector3f(0,0,1), new Vector2f(xStep * block.getAtlasX(BlockSide.FRONT), yStep * block.getAtlasY(BlockSide.FRONT))), | |
new Vertex(new Vector3f( 0.5f + x, 0.5f + y, 0.5f + z), new Vector3f(0,0,1), new Vector2f(xStep * block.getAtlasX(BlockSide.FRONT), yStep * (block.getAtlasY(BlockSide.FRONT) - 1))), | |
}, new int[] { | |
//Front face | |
0 + index * 4, 1 + index * 4, 3 + index * 4, | |
3 + index * 4, 1 + index * 4, 2 + index * 4, | |
})); | |
else | |
chunkMesh.combine(new ChunkMesh(new Vertex[] { | |
//Front face | |
new Vertex(new Vector3f(-0.5f + x, 0.4f + y, 0.5f + z), new Vector3f(0,0,1), new Vector2f(xStep * (block.getAtlasX(BlockSide.FRONT) - 1), yStep * (block.getAtlasY(BlockSide.FRONT) - 1))), | |
new Vertex(new Vector3f(-0.5f + x, -0.4f + y, 0.5f + z), new Vector3f(0,0,1), new Vector2f(xStep * (block.getAtlasX(BlockSide.FRONT) - 1), yStep * block.getAtlasY(BlockSide.FRONT))), | |
new Vertex(new Vector3f( 0.5f + x, -0.4f + y, 0.5f + z), new Vector3f(0,0,1), new Vector2f(xStep * block.getAtlasX(BlockSide.FRONT), yStep * block.getAtlasY(BlockSide.FRONT))), | |
new Vertex(new Vector3f( 0.5f + x, 0.4f + y, 0.5f + z), new Vector3f(0,0,1), new Vector2f(xStep * block.getAtlasX(BlockSide.FRONT), yStep * (block.getAtlasY(BlockSide.FRONT) - 1))), | |
}, new int[] { | |
//Front face | |
0 + index * 4, 1 + index * 4, 3 + index * 4, | |
3 + index * 4, 1 + index * 4, 2 + index * 4, | |
})); | |
index ++; | |
return chunkMesh; | |
} | |
static ChunkMesh right(ChunkMesh chunkMesh, int x, int y, int z, Block block) { | |
if(!(block instanceof BlockWater) || !(block instanceof BlockWater && blockss[x][y+1][z] instanceof BlockAir)) | |
chunkMesh.combine(new ChunkMesh(new Vertex[] { | |
//Right face | |
new Vertex(new Vector3f( 0.5f + x, 0.5f + y, -0.5f + z), new Vector3f(1,0,0), new Vector2f(xStep * (block.getAtlasX(BlockSide.RIGHT) - 1), yStep * (block.getAtlasY(BlockSide.RIGHT) - 1))), | |
new Vertex(new Vector3f( 0.5f + x, -0.5f + y, -0.5f + z), new Vector3f(1,0,0), new Vector2f(xStep * (block.getAtlasX(BlockSide.RIGHT) - 1), yStep * block.getAtlasY(BlockSide.RIGHT))), | |
new Vertex(new Vector3f( 0.5f + x, -0.5f + y, 0.5f + z), new Vector3f(1,0,0), new Vector2f(xStep * block.getAtlasX(BlockSide.RIGHT), yStep * block.getAtlasY(BlockSide.RIGHT))), | |
new Vertex(new Vector3f( 0.5f + x, 0.5f + y, 0.5f + z), new Vector3f(1,0,0), new Vector2f(xStep * block.getAtlasX(BlockSide.RIGHT), yStep * (block.getAtlasY(BlockSide.RIGHT) - 1))), | |
}, new int[] { | |
//Right face | |
0 + index * 4, 1 + index * 4, 3 + index * 4, | |
3 + index * 4, 1 + index * 4, 2 + index * 4, | |
})); | |
else | |
chunkMesh.combine(new ChunkMesh(new Vertex[] { | |
//Right face | |
new Vertex(new Vector3f( 0.5f + x, 0.4f + y, -0.5f + z), new Vector3f(1,0,0), new Vector2f(xStep * (block.getAtlasX(BlockSide.RIGHT) - 1), yStep * (block.getAtlasY(BlockSide.RIGHT) - 1))), | |
new Vertex(new Vector3f( 0.5f + x, -0.4f + y, -0.5f + z), new Vector3f(1,0,0), new Vector2f(xStep * (block.getAtlasX(BlockSide.RIGHT) - 1), yStep * block.getAtlasY(BlockSide.RIGHT))), | |
new Vertex(new Vector3f( 0.5f + x, -0.4f + y, 0.5f + z), new Vector3f(1,0,0), new Vector2f(xStep * block.getAtlasX(BlockSide.RIGHT), yStep * block.getAtlasY(BlockSide.RIGHT))), | |
new Vertex(new Vector3f( 0.5f + x, 0.4f + y, 0.5f + z), new Vector3f(1,0,0), new Vector2f(xStep * block.getAtlasX(BlockSide.RIGHT), yStep * (block.getAtlasY(BlockSide.RIGHT) - 1))), | |
}, new int[] { | |
//Right face | |
0 + index * 4, 1 + index * 4, 3 + index * 4, | |
3 + index * 4, 1 + index * 4, 2 + index * 4, | |
})); | |
index ++; | |
return chunkMesh; | |
} | |
static ChunkMesh left(ChunkMesh chunkMesh, int x, int y, int z, Block block) { | |
if(!(block instanceof BlockWater) || !(block instanceof BlockWater && blockss[x][y+1][z] instanceof BlockAir)) | |
chunkMesh.combine(new ChunkMesh(new Vertex[] { | |
//Left face | |
new Vertex(new Vector3f(-0.5f + x, 0.5f + y, -0.5f + z), new Vector3f(-1,0,0), new Vector2f(xStep * (block.getAtlasX(BlockSide.LEFT) - 1), yStep * (block.getAtlasY(BlockSide.LEFT) - 1))), | |
new Vertex(new Vector3f(-0.5f + x, -0.5f + y, -0.5f + z), new Vector3f(-1,0,0), new Vector2f(xStep * (block.getAtlasX(BlockSide.LEFT) - 1), yStep * block.getAtlasY(BlockSide.LEFT))), | |
new Vertex(new Vector3f(-0.5f + x, -0.5f + y, 0.5f + z), new Vector3f(-1,0,0), new Vector2f(xStep * block.getAtlasX(BlockSide.LEFT), yStep * block.getAtlasY(BlockSide.LEFT))), | |
new Vertex(new Vector3f(-0.5f + x, 0.5f + y, 0.5f + z), new Vector3f(-1,0,0), new Vector2f(xStep * block.getAtlasX(BlockSide.LEFT), yStep * (block.getAtlasY(BlockSide.LEFT) - 1))), | |
}, new int[] { | |
//Left face | |
0 + index * 4, 1 + index * 4, 3 + index * 4, | |
3 + index * 4, 1 + index * 4, 2 + index * 4, | |
})); | |
else | |
chunkMesh.combine(new ChunkMesh(new Vertex[] { | |
//Left face | |
new Vertex(new Vector3f(-0.5f + x, 0.4f + y, -0.5f + z), new Vector3f(-1,0,0), new Vector2f(xStep * (block.getAtlasX(BlockSide.LEFT) - 1), yStep * (block.getAtlasY(BlockSide.LEFT) - 1))), | |
new Vertex(new Vector3f(-0.5f + x, -0.4f + y, -0.5f + z), new Vector3f(-1,0,0), new Vector2f(xStep * (block.getAtlasX(BlockSide.LEFT) - 1), yStep * block.getAtlasY(BlockSide.LEFT))), | |
new Vertex(new Vector3f(-0.5f + x, -0.4f + y, 0.5f + z), new Vector3f(-1,0,0), new Vector2f(xStep * block.getAtlasX(BlockSide.LEFT), yStep * block.getAtlasY(BlockSide.LEFT))), | |
new Vertex(new Vector3f(-0.5f + x, 0.4f + y, 0.5f + z), new Vector3f(-1,0,0), new Vector2f(xStep * block.getAtlasX(BlockSide.LEFT), yStep * (block.getAtlasY(BlockSide.LEFT) - 1))), | |
}, new int[] { | |
//Left face | |
0 + index * 4, 1 + index * 4, 3 + index * 4, | |
3 + index * 4, 1 + index * 4, 2 + index * 4, | |
})); | |
index ++; | |
return chunkMesh; | |
} | |
static ChunkMesh top(ChunkMesh chunkMesh, int x, int y, int z, Block block) { | |
if(!(block instanceof BlockWater) || !(block instanceof BlockWater && blockss[x][y+1][z] instanceof BlockAir)) | |
chunkMesh.combine(new ChunkMesh(new Vertex[] { | |
//Top face | |
new Vertex(new Vector3f(-0.5f + x, 0.5f + y, 0.5f + z), new Vector3f(0,1,0), new Vector2f(xStep * (block.getAtlasX(BlockSide.TOP) - 1), yStep * (block.getAtlasY(BlockSide.TOP) - 1))), | |
new Vertex(new Vector3f(-0.5f + x, 0.5f + y, -0.5f + z), new Vector3f(0,1,0), new Vector2f(xStep * (block.getAtlasX(BlockSide.TOP) - 1), yStep * block.getAtlasY(BlockSide.TOP))), | |
new Vertex(new Vector3f( 0.5f + x, 0.5f + y, -0.5f + z), new Vector3f(0,1,0), new Vector2f(xStep * block.getAtlasX(BlockSide.TOP), yStep * block.getAtlasY(BlockSide.TOP))), | |
new Vertex(new Vector3f( 0.5f + x, 0.5f + y, 0.5f + z), new Vector3f(0,1,0), new Vector2f(xStep * block.getAtlasX(BlockSide.TOP), yStep * (block.getAtlasY(BlockSide.TOP) - 1))), | |
}, new int[] { | |
//Top face | |
0 + index * 4, 1 + index * 4, 3 + index * 4, | |
3 + index * 4, 1 + index * 4, 2 + index * 4, | |
})); | |
else | |
chunkMesh.combine(new ChunkMesh(new Vertex[] { | |
//Top face | |
new Vertex(new Vector3f(-0.5f + x, 0.4f + y, 0.5f + z), new Vector3f(0,1,0), new Vector2f(xStep * (block.getAtlasX(BlockSide.TOP) - 1), yStep * (block.getAtlasY(BlockSide.TOP) - 1))), | |
new Vertex(new Vector3f(-0.5f + x, 0.4f + y, -0.5f + z), new Vector3f(0,1,0), new Vector2f(xStep * (block.getAtlasX(BlockSide.TOP) - 1), yStep * block.getAtlasY(BlockSide.TOP))), | |
new Vertex(new Vector3f( 0.5f + x, 0.4f + y, -0.5f + z), new Vector3f(0,1,0), new Vector2f(xStep * block.getAtlasX(BlockSide.TOP), yStep * block.getAtlasY(BlockSide.TOP))), | |
new Vertex(new Vector3f( 0.5f + x, 0.4f + y, 0.5f + z), new Vector3f(0,1,0), new Vector2f(xStep * block.getAtlasX(BlockSide.TOP), yStep * (block.getAtlasY(BlockSide.TOP) - 1))), | |
}, new int[] { | |
//Top face | |
0 + index * 4, 1 + index * 4, 3 + index * 4, | |
3 + index * 4, 1 + index * 4, 2 + index * 4, | |
})); | |
index ++; | |
return chunkMesh; | |
} | |
static ChunkMesh bottom(ChunkMesh chunkMesh, int x, int y, int z, Block block) { | |
chunkMesh.combine(new ChunkMesh(new Vertex[] { | |
//Bottom face | |
new Vertex(new Vector3f(-0.5f + x, -0.5f + y, 0.5f + z), new Vector3f(0,-1,0), new Vector2f(xStep * (block.getAtlasX(BlockSide.BOTTOM) - 1), yStep * (block.getAtlasY(BlockSide.BOTTOM) - 1))), | |
new Vertex(new Vector3f(-0.5f + x, -0.5f + y, -0.5f + z), new Vector3f(0,-1,0), new Vector2f(xStep * (block.getAtlasX(BlockSide.BOTTOM) - 1), yStep * block.getAtlasY(BlockSide.BOTTOM))), | |
new Vertex(new Vector3f( 0.5f + x, -0.5f + y, -0.5f + z), new Vector3f(0,-1,0), new Vector2f(xStep * block.getAtlasX(BlockSide.BOTTOM), yStep * block.getAtlasY(BlockSide.BOTTOM))), | |
new Vertex(new Vector3f( 0.5f + x, -0.5f + y, 0.5f + z), new Vector3f(0,-1,0), new Vector2f(xStep * block.getAtlasX(BlockSide.BOTTOM), yStep * (block.getAtlasY(BlockSide.BOTTOM) - 1))), | |
}, new int[] { | |
//Bottom face | |
0 + index * 4, 1 + index * 4, 3 + index * 4, | |
3 + index * 4, 1 + index * 4, 2 + index * 4, | |
})); | |
index ++; | |
return chunkMesh; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment