use of org.terasology.nui.Colorc in project Terasology by MovingBlocks.
the class VertexGLAttributeTest method testColorBinding.
@Test
public void testColorBinding() {
VertexResourceBuilder builder = new VertexResourceBuilder();
VertexAttributeBinding<Colorc, Color> a1 = builder.add(0, GLAttributes.COLOR_4_F_VERTEX_ATTRIBUTE);
VertexResource resource = builder.build();
a1.put(Color.white);
a1.put(Color.red);
a1.put(Color.green);
a1.put(Color.blue);
assertEquals(4, a1.getPosition());
int stride = Float.BYTES * 4;
resource.writeBuffer(buffer -> {
assertEquals(4 * 4 * Float.BYTES, buffer.limit());
assertEquals(1.0f, buffer.getFloat(Float.BYTES * 0), 0.001f);
assertEquals(1.0f, buffer.getFloat(Float.BYTES * 1), 0.001f);
assertEquals(1.0f, buffer.getFloat(Float.BYTES * 2), 0.001f);
assertEquals(1.0f, buffer.getFloat(Float.BYTES * 3), 0.001f);
assertEquals(1.0f, buffer.getFloat(stride + Float.BYTES * 0));
assertEquals(0, buffer.getFloat(stride + Float.BYTES * 1));
assertEquals(0, buffer.getFloat(stride + Float.BYTES * 2));
assertEquals(1.0f, buffer.getFloat(stride + Float.BYTES * 3));
assertEquals(0, buffer.getFloat((2 * stride) + Float.BYTES * 0));
assertEquals(1.0f, buffer.getFloat((2 * stride) + Float.BYTES * 1));
assertEquals(0, buffer.getFloat((2 * stride) + Float.BYTES * 2));
assertEquals(1.0f, buffer.getFloat((2 * stride) + Float.BYTES * 3));
assertEquals(0, buffer.getFloat((3 * stride) + Float.BYTES * 0));
assertEquals(0, buffer.getFloat((3 * stride) + Float.BYTES * 1));
assertEquals(1.0f, buffer.getFloat((3 * stride) + Float.BYTES * 2));
assertEquals(1.0f, buffer.getFloat((3 * stride) + Float.BYTES * 3));
});
}
use of org.terasology.nui.Colorc 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.nui.Colorc in project Terasology by MovingBlocks.
the class LwjglCanvasRenderer method drawText.
@Override
public void drawText(String text, Font font, HorizontalAlign hAlign, VerticalAlign vAlign, Rectanglei absoluteRegionRectangle, Colorc color, Colorc shadowColor, float alpha, boolean underlined) {
Rectanglei absoluteRegion = new Rectanglei(absoluteRegionRectangle);
TextCacheKey key = new TextCacheKey(text, font, absoluteRegion.getSizeX(), hAlign, color, shadowColor, underlined);
usedText.add(key);
Map<Material, Mesh> fontMesh = cachedText.get(key);
List<String> lines = TextLineBuilder.getLines(font, text, absoluteRegion.getSizeX());
if (fontMesh != null) {
for (Mesh mesh : fontMesh.values()) {
if (mesh.isDisposed()) {
fontMesh = null;
break;
}
}
}
if (fontMesh == null) {
fontMesh = fontMeshBuilder.createTextMesh((org.terasology.engine.rendering.assets.font.Font) font, lines, absoluteRegion.getSizeX(), hAlign, color, shadowColor, underlined);
cachedText.put(key, fontMesh);
}
Vector2i offset = new Vector2i(absoluteRegion.minX, absoluteRegion.minY);
offset.y += vAlign.getOffset(lines.size() * font.getLineHeight(), absoluteRegion.lengthY());
fontMesh.entrySet().stream().filter(entry -> entry.getKey().isRenderable()).forEach(entry -> {
entry.getKey().bindTextures();
entry.getKey().setMatrix4("projectionMatrix", projMatrix);
entry.getKey().setMatrix4("modelViewMatrix", modelMatrixStack);
entry.getKey().setFloat4(CROPPING_BOUNDARIES_PARAM, requestedCropRegion.minX, requestedCropRegion.maxX, requestedCropRegion.minY, requestedCropRegion.maxY);
entry.getKey().setFloat2("offset", offset.x, offset.y);
entry.getKey().setFloat("alpha", alpha);
entry.getValue().render();
});
}
use of org.terasology.nui.Colorc in project Terasology by MovingBlocks.
the class VertexGLAttributeTest method testColorPackedBinding.
@Test
public void testColorPackedBinding() {
VertexResourceBuilder builder = new VertexResourceBuilder();
VertexAttributeBinding<Colorc, Color> a1 = builder.add(0, GLAttributes.COLOR_4_PACKED_VERTEX_ATTRIBUTE);
VertexResource resource = builder.build();
a1.put(Color.white);
a1.put(Color.red);
a1.put(Color.green);
a1.put(Color.blue);
assertEquals(4, a1.getPosition());
int stride = Byte.BYTES * 4;
resource.writeBuffer(buffer -> {
assertEquals(4 * 4 * Byte.BYTES, buffer.limit());
assertEquals(255, Byte.toUnsignedInt(buffer.get(Byte.BYTES * 0)));
assertEquals(255, Byte.toUnsignedInt(buffer.get(Byte.BYTES * 1)));
assertEquals(255, Byte.toUnsignedInt(buffer.get(Byte.BYTES * 2)));
assertEquals(255, Byte.toUnsignedInt(buffer.get(Byte.BYTES * 3)));
assertEquals(255, Byte.toUnsignedInt(buffer.get(stride + Byte.BYTES * 0)));
assertEquals(0, Byte.toUnsignedInt(buffer.get(stride + Byte.BYTES * 1)));
assertEquals(0, Byte.toUnsignedInt(buffer.get(stride + Byte.BYTES * 2)));
assertEquals(255, Byte.toUnsignedInt(buffer.get(stride + Byte.BYTES * 3)));
assertEquals(0, Byte.toUnsignedInt(buffer.get((2 * stride) + Byte.BYTES * 0)));
assertEquals(255, Byte.toUnsignedInt(buffer.get((2 * stride) + Byte.BYTES * 1)));
assertEquals(0, Byte.toUnsignedInt(buffer.get((2 * stride) + Byte.BYTES * 2)));
assertEquals(255, Byte.toUnsignedInt(buffer.get((2 * stride) + Byte.BYTES * 3)));
assertEquals(0, Byte.toUnsignedInt(buffer.get((3 * stride) + Byte.BYTES * 0)));
assertEquals(0, Byte.toUnsignedInt(buffer.get((3 * stride) + Byte.BYTES * 1)));
assertEquals(255, Byte.toUnsignedInt(buffer.get((3 * stride) + Byte.BYTES * 2)));
assertEquals(255, Byte.toUnsignedInt(buffer.get((3 * stride) + Byte.BYTES * 3)));
});
}
Aggregations