use of org.joml.Vector2f in project Terasology by MovingBlocks.
the class LwjglCanvasRenderer method drawTexture.
@Override
public void drawTexture(UITextureRegion texture, Colorc color, ScaleMode mode, Rectanglei absoluteRegionRectangle, float ux, float uy, float uw, float uh, float alpha) {
if (!((TextureRegion) texture).getTexture().isLoaded()) {
return;
}
Rectanglei absoluteRegion = new Rectanglei(absoluteRegionRectangle);
if (!currentTextureCropRegion.equals(requestedCropRegion) && !(currentTextureCropRegion.containsRectangle(absoluteRegion) && requestedCropRegion.containsRectangle(absoluteRegion))) {
textureMat.setFloat4(CROPPING_BOUNDARIES_PARAM, requestedCropRegion.minX, requestedCropRegion.maxX, requestedCropRegion.minY, requestedCropRegion.maxY);
currentTextureCropRegion = requestedCropRegion;
}
Vector2f scale = mode.scaleForRegion(absoluteRegionRectangle, texture.getWidth(), texture.getHeight());
Rectanglef textureArea = new Rectanglef(texture.getRegion());
Mesh mesh = billboard;
switch(mode) {
case TILED:
{
Vector2i textureSize = texture.size();
TextureCacheKey key = new TextureCacheKey(textureSize, new Vector2i(absoluteRegion.getSizeX(), absoluteRegion.getSizeY()));
usedTextures.add(key);
mesh = cachedTextures.get(key);
if (mesh == null || mesh.isDisposed()) {
MeshBuilder builder = new MeshBuilder();
addTiles(builder, absoluteRegion, FULL_REGION, textureSize, FULL_REGION);
mesh = builder.build();
cachedTextures.put(key, mesh);
}
textureMat.setFloat2("scale", scale);
textureMat.setFloat2("offset", absoluteRegion.minX, absoluteRegion.minY);
textureMat.setFloat2("texOffset", textureArea.minX + ux * textureArea.getSizeX(), textureArea.minY + uy * textureArea.getSizeY());
textureMat.setFloat2("texSize", uw * textureArea.getSizeX(), uh * textureArea.getSizeY());
break;
}
case SCALE_FILL:
{
textureMat.setFloat2("offset", absoluteRegion.minX, absoluteRegion.minY);
textureMat.setFloat2("scale", absoluteRegion.getSizeX(), absoluteRegion.getSizeY());
float texBorderX = (scale.x - absoluteRegion.getSizeX()) / scale.x * uw;
float texBorderY = (scale.y - absoluteRegion.getSizeY()) / scale.y * uh;
textureMat.setFloat2("texOffset", textureArea.minX + (ux + 0.5f * texBorderX) * textureArea.getSizeX(), textureArea.minY + (uy + 0.5f * texBorderY) * textureArea.getSizeY());
textureMat.setFloat2("texSize", (uw - texBorderX) * textureArea.getSizeX(), (uh - texBorderY) * textureArea.getSizeY());
break;
}
default:
{
textureMat.setFloat2("scale", scale);
textureMat.setFloat2("offset", absoluteRegion.minX + 0.5f * (absoluteRegion.getSizeX() - scale.x), absoluteRegion.minY + 0.5f * (absoluteRegion.getSizeY() - scale.y));
textureMat.setFloat2("texOffset", textureArea.minX + ux * textureArea.getSizeX(), textureArea.minY + uy * textureArea.getSizeY());
textureMat.setFloat2("texSize", uw * textureArea.getSizeX(), uh * textureArea.getSizeY());
break;
}
}
textureMat.setTexture("tex", ((TextureRegion) texture).getTexture());
textureMat.setFloat4("color", color.rf(), color.gf(), color.bf(), color.af() * alpha);
textureMat.setMatrix4("projectionMatrix", projMatrix);
textureMat.setMatrix4("modelViewMatrix", modelMatrixStack);
textureMat.bindTextures();
mesh.render();
}
use of org.joml.Vector2f in project Terasology by MovingBlocks.
the class BlockMeshPart method appendTo.
public void appendTo(ChunkMesh chunk, ChunkView chunkView, int offsetX, int offsetY, int offsetZ, ChunkMesh.RenderType renderType, Colorc colorOffset, ChunkVertexFlag flags) {
ChunkMesh.VertexElements elements = chunk.getVertexElements(renderType);
for (Vector2f texCoord : texCoords) {
elements.uv0.put(texCoord);
}
int nextIndex = elements.vertexCount;
elements.buffer.reserveElements(nextIndex + vertices.length);
Vector3f pos = new Vector3f();
for (int vIdx = 0; vIdx < vertices.length; ++vIdx) {
elements.color.put(colorOffset);
elements.position.put(pos.set(vertices[vIdx]).add(offsetX, offsetY, offsetZ));
elements.normals.put(normals[vIdx]);
elements.flags.put((byte) (flags.getValue()));
elements.frames.put((byte) (texFrames - 1));
float[] lightingData = calcLightingValuesForVertexPos(chunkView, vertices[vIdx].add(offsetX, offsetY, offsetZ, new Vector3f()), normals[vIdx]);
elements.sunlight.put(lightingData[0]);
elements.blockLight.put(lightingData[1]);
elements.ambientOcclusion.put(lightingData[2]);
}
elements.vertexCount += vertices.length;
for (int index : indices) {
elements.indices.put(index + nextIndex);
}
}
use of org.joml.Vector2f in project Terasology by MovingBlocks.
the class BlockMeshPart method mapTexCoords.
public BlockMeshPart mapTexCoords(Vector2f offset, float width, int frames) {
float normalisedBorder = BORDER * width;
Vector2f[] newTexCoords = new Vector2f[texCoords.length];
for (int i = 0; i < newTexCoords.length; ++i) {
newTexCoords[i] = new Vector2f(offset.x + normalisedBorder + texCoords[i].x * (width - 2 * normalisedBorder), offset.y + normalisedBorder + texCoords[i].y * (width - 2 * normalisedBorder));
}
return new BlockMeshPart(vertices, normals, newTexCoords, indices, frames);
}
use of org.joml.Vector2f in project Terasology by MovingBlocks.
the class WorldAtlasImpl method createTextureAtlas.
private void createTextureAtlas(final Texture texture) {
final Map<Name, Map<Name, SubtextureData>> textureAtlases = Maps.newHashMap();
final Vector2f texSize = new Vector2f(getRelativeTileSize(), getRelativeTileSize());
tileIndexes.forEachEntry((tileUri, index) -> {
Vector2f coords = getTexCoords(index);
SubtextureData subtextureData = new SubtextureData(texture, new Rectanglef(coords, coords).setSize(texSize));
Map<Name, SubtextureData> textureAtlas = textureAtlases.get(tileUri.getModuleName());
if (textureAtlas == null) {
textureAtlas = Maps.newHashMap();
textureAtlases.put(tileUri.getModuleName(), textureAtlas);
}
textureAtlas.put(tileUri.getResourceName(), subtextureData);
return true;
});
for (Map.Entry<Name, Map<Name, SubtextureData>> atlas : textureAtlases.entrySet()) {
AtlasData data = new AtlasData(atlas.getValue());
Assets.generateAsset(new ResourceUrn(atlas.getKey(), new Name("terrain")), data, Atlas.class);
}
}
use of org.joml.Vector2f in project Terasology by MovingBlocks.
the class TextureOffsetGeneratorFunction method onEmission.
@Override
public void onEmission(TextureOffsetGeneratorComponent component, ParticleData particleData, Random random) {
if (component.validOffsets.size() == 0) {
return;
}
final int randomOffsetIndex = random.nextInt(component.validOffsets.size());
final Vector2f randomOffset = component.validOffsets.get(randomOffsetIndex);
particleData.textureOffset.set(randomOffset);
}
Aggregations