use of org.terasology.engine.world.block.shapes.BlockMeshPart in project Terasology by MovingBlocks.
the class BlockMeshGeneratorSingleShape method generateChunkMesh.
@Override
public void generateChunkMesh(ChunkView view, ChunkMesh chunkMesh, int x, int y, int z) {
final BlockAppearance blockAppearance = block.getPrimaryAppearance();
if (!blockAppearance.hasAppearance()) {
// perf: Skip mesh generation for blocks without appearance, e.g., air blocks.
return;
}
Color colorCache = new Color();
// Gather adjacent blocks
Block[] adjacentBlocks = new Block[Side.allSides().size()];
for (Side side : Side.allSides()) {
Vector3ic offset = side.direction();
Block blockToCheck = view.getBlock(x + offset.x(), y + offset.y(), z + offset.z());
adjacentBlocks[side.ordinal()] = blockToCheck;
}
final ChunkMesh.RenderType renderType = getRenderType(block);
final ChunkVertexFlag vertexFlag = getChunkVertexFlag(view, x, y, z, block);
boolean isRendered = false;
for (final Side side : Side.allSides()) {
if (isSideVisibleForBlockTypes(adjacentBlocks[side.ordinal()], block, side)) {
isRendered = true;
BlockMeshPart blockMeshPart = blockAppearance.getPart(BlockPart.fromSide(side));
// If the selfBlock isn't lowered, some more faces may have to be drawn
if (block.isLiquid()) {
final Block topBlock = adjacentBlocks[Side.TOP.ordinal()];
// Draw horizontal sides if visible from below
if (topBlock.isLiquid() && Side.horizontalSides().contains(side)) {
final Vector3ic offset = side.direction();
final Block adjacentAbove = view.getBlock(x + offset.x(), y + 1, z + offset.z());
final Block adjacent = adjacentBlocks[side.ordinal()];
if (adjacent.isLiquid() && !adjacentAbove.isLiquid()) {
blockMeshPart = block.getTopLiquidMesh(side);
}
} else {
if (blockMeshPart != null) {
blockMeshPart = block.getLowLiquidMesh(side);
}
}
}
if (blockMeshPart != null) {
// TODO: Needs review since the new per-vertex flags introduce a lot of special scenarios - probably a per-side setting?
ChunkVertexFlag sideVertexFlag = vertexFlag;
if (block.isGrass() && side != Side.TOP && side != Side.BOTTOM) {
sideVertexFlag = ChunkVertexFlag.COLOR_MASK;
}
Colorc colorOffset = block.getColorOffset(BlockPart.fromSide(side));
Colorc colorSource = block.getColorSource(BlockPart.fromSide(side)).calcColor(view, x, y, z);
colorCache.setRed(colorSource.rf() * colorOffset.rf()).setGreen(colorSource.gf() * colorOffset.gf()).setBlue(colorSource.bf() * colorOffset.bf()).setAlpha(colorSource.af() * colorOffset.af());
blockMeshPart.appendTo(chunkMesh, view, x, y, z, renderType, colorCache, sideVertexFlag);
}
}
}
if (isRendered && blockAppearance.getPart(BlockPart.CENTER) != null) {
Colorc colorOffset = block.getColorOffset(BlockPart.CENTER);
Colorc colorSource = block.getColorSource(BlockPart.CENTER).calcColor(view, x, y, z);
colorCache.setRed(colorSource.rf() * colorOffset.rf()).setGreen(colorSource.gf() * colorOffset.gf()).setBlue(colorSource.bf() * colorOffset.bf()).setAlpha(colorSource.af() * colorOffset.af());
blockAppearance.getPart(BlockPart.CENTER).appendTo(chunkMesh, view, x, y, z, renderType, colorCache, vertexFlag);
}
}
use of org.terasology.engine.world.block.shapes.BlockMeshPart in project Terasology by MovingBlocks.
the class BlockMeshShapeGenerator method getStandaloneMesh.
@Override
public Mesh getStandaloneMesh() {
if (mesh == null || mesh.isDisposed()) {
Block block = getBlock();
StandardMeshData meshData = new StandardMeshData();
int nextIndex = 0;
Vector3f light0 = new Vector3f(1, 1, 1);
for (BlockPart dir : BlockPart.allParts()) {
BlockMeshPart part = block.getPrimaryAppearance().getPart(dir);
if (part != null) {
for (int i = 0; i < part.size(); i++) {
meshData.position.put(part.getVertex(i));
meshData.color0.put(Color.white);
meshData.normal.put(part.getNormal(i));
meshData.uv0.put(part.getTexCoord(i));
meshData.light0.put(light0);
}
for (int i = 0; i < part.indicesSize(); ++i) {
meshData.indices.put(nextIndex + part.getIndex(i));
}
if (block.isDoubleSided()) {
for (int i = 0; i < part.indicesSize(); i += 3) {
meshData.indices.put(nextIndex + part.getIndex(i + 1));
meshData.indices.put(nextIndex + part.getIndex(i));
meshData.indices.put(nextIndex + part.getIndex(i + 2));
}
}
nextIndex += part.size();
}
}
mesh = Assets.generateAsset(new ResourceUrn(getBaseUrn(), block.getURI().toString()), meshData, Mesh.class);
}
return mesh;
}
use of org.terasology.engine.world.block.shapes.BlockMeshPart in project Terasology by MovingBlocks.
the class BlockBuilder method createAppearance.
private BlockAppearance createAppearance(BlockShape shape, Map<BlockPart, BlockTile> tiles, Rotation rot) {
Map<BlockPart, BlockMeshPart> meshParts = Maps.newEnumMap(BlockPart.class);
Map<BlockPart, Vector2f> textureAtlasPositions = Maps.newEnumMap(BlockPart.class);
for (BlockPart part : BlockPart.values()) {
// TODO: Need to be more sensible with the texture atlas.
// Because things like block particles read from a part that may not exist, we're being fairly lenient
Vector2f atlasPos;
int frameCount;
BlockTile tile = tiles.get(part);
if (tile == null) {
atlasPos = new Vector2f();
frameCount = 1;
} else {
atlasPos = worldAtlas.getTexCoords(tile, shape.getMeshPart(part) != null);
frameCount = tile.getLength();
}
BlockPart targetPart = part.rotate(rot);
textureAtlasPositions.put(targetPart, atlasPos);
if (shape.getMeshPart(part) != null) {
meshParts.put(targetPart, shape.getMeshPart(part).rotate(rot.orientation().get(new Quaternionf())).mapTexCoords(atlasPos, worldAtlas.getRelativeTileSize(), frameCount));
}
}
return new BlockAppearance(meshParts, textureAtlasPositions);
}
use of org.terasology.engine.world.block.shapes.BlockMeshPart in project Terasology by MovingBlocks.
the class BlockBuilder method applyLiquidShapes.
private void applyLiquidShapes(Block block, Map<BlockPart, BlockTile> tiles) {
for (Side side : Side.values()) {
BlockPart part = BlockPart.fromSide(side);
BlockTile blockTile = tiles.get(part);
if (blockTile != null) {
BlockMeshPart lowMeshPart = lowShape.getMeshPart(part).mapTexCoords(worldAtlas.getTexCoords(blockTile, true), worldAtlas.getRelativeTileSize(), blockTile.getLength());
block.setLowLiquidMesh(part.getSide(), lowMeshPart);
BlockMeshPart topMeshPart = topShape.getMeshPart(part).mapTexCoords(worldAtlas.getTexCoords(blockTile, true), worldAtlas.getRelativeTileSize(), blockTile.getLength());
block.setTopLiquidMesh(part.getSide(), topMeshPart);
}
}
}
Aggregations