Search in sources :

Example 1 with BlockAppearance

use of org.terasology.engine.world.block.BlockAppearance 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);
    }
}
Also used : Side(org.terasology.engine.math.Side) Colorc(org.terasology.nui.Colorc) BlockAppearance(org.terasology.engine.world.block.BlockAppearance) Vector3ic(org.joml.Vector3ic) Color(org.terasology.nui.Color) Block(org.terasology.engine.world.block.Block) BlockMeshPart(org.terasology.engine.world.block.shapes.BlockMeshPart)

Example 2 with BlockAppearance

use of org.terasology.engine.world.block.BlockAppearance 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);
}
Also used : BlockPart(org.terasology.engine.world.block.BlockPart) BlockAppearance(org.terasology.engine.world.block.BlockAppearance) Vector2f(org.joml.Vector2f) BlockTile(org.terasology.engine.world.block.tiles.BlockTile) Quaternionf(org.joml.Quaternionf) BlockMeshPart(org.terasology.engine.world.block.shapes.BlockMeshPart)

Aggregations

BlockAppearance (org.terasology.engine.world.block.BlockAppearance)2 BlockMeshPart (org.terasology.engine.world.block.shapes.BlockMeshPart)2 Quaternionf (org.joml.Quaternionf)1 Vector2f (org.joml.Vector2f)1 Vector3ic (org.joml.Vector3ic)1 Side (org.terasology.engine.math.Side)1 Block (org.terasology.engine.world.block.Block)1 BlockPart (org.terasology.engine.world.block.BlockPart)1 BlockTile (org.terasology.engine.world.block.tiles.BlockTile)1 Color (org.terasology.nui.Color)1 Colorc (org.terasology.nui.Colorc)1