use of org.joml.Vector2i 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.Vector2i in project Terasology by MovingBlocks.
the class LwjglCanvasRenderer method drawTextureBordered.
@Override
public void drawTextureBordered(UITextureRegion texture, Rectanglei regionRectangle, Border border, boolean tile, float ux, float uy, float uw, float uh, float alpha) {
if (!((TextureRegion) texture).getTexture().isLoaded()) {
return;
}
Rectanglei region = new Rectanglei(regionRectangle);
if (!currentTextureCropRegion.equals(requestedCropRegion) && !(currentTextureCropRegion.containsRectangle(region) && requestedCropRegion.containsRectangle(region))) {
textureMat.setFloat4(CROPPING_BOUNDARIES_PARAM, requestedCropRegion.minX, requestedCropRegion.maxX, requestedCropRegion.minY, requestedCropRegion.maxY);
currentTextureCropRegion = requestedCropRegion;
}
Vector2i textureSize = new Vector2i(TeraMath.ceilToInt(texture.getWidth() * uw), TeraMath.ceilToInt(texture.getHeight() * uh));
TextureCacheKey key = new TextureCacheKey(textureSize, new Vector2i(region.getSizeX(), region.getSizeY()), border, tile);
usedTextures.add(key);
Mesh mesh = cachedTextures.get(key);
if (mesh == null || mesh.isDisposed()) {
MeshBuilder builder = new MeshBuilder();
float topTex = (float) border.getTop() / textureSize.y;
float leftTex = (float) border.getLeft() / textureSize.x;
float bottomTex = 1f - (float) border.getBottom() / textureSize.y;
float rightTex = 1f - (float) border.getRight() / textureSize.x;
int centerHoriz = region.getSizeX() - border.getTotalWidth();
int centerVert = region.getSizeY() - border.getTotalHeight();
float top = (float) border.getTop() / region.getSizeY();
float left = (float) border.getLeft() / region.getSizeX();
float bottom = 1f - (float) border.getBottom() / region.getSizeY();
float right = 1f - (float) border.getRight() / region.getSizeX();
if (border.getTop() != 0) {
if (border.getLeft() != 0) {
addRectPoly(builder, 0, 0, left, top, 0, 0, leftTex, topTex);
}
if (tile) {
addTiles(builder, new Rectanglei(border.getLeft(), 0).setSize(centerHoriz, border.getTop()), new Rectanglef(left, 0, right, top), new Vector2i(textureSize.x - border.getTotalWidth(), border.getTop()), new Rectanglef(leftTex, 0, rightTex, topTex));
} else {
addRectPoly(builder, left, 0, right, top, leftTex, 0, rightTex, topTex);
}
if (border.getRight() != 0) {
addRectPoly(builder, right, 0, 1, top, rightTex, 0, 1, topTex);
}
}
if (border.getLeft() != 0) {
if (tile) {
addTiles(builder, new Rectanglei(0, border.getTop()).setSize(border.getLeft(), centerVert), new Rectanglef(0, top, left, bottom), new Vector2i(border.getLeft(), textureSize.y - border.getTotalHeight()), new Rectanglef(0, topTex, leftTex, bottomTex));
} else {
addRectPoly(builder, 0, top, left, bottom, 0, topTex, leftTex, bottomTex);
}
}
if (tile) {
addTiles(builder, new Rectanglei(border.getLeft(), border.getTop()).setSize(centerHoriz, centerVert), new Rectanglef(left, top, right, bottom), new Vector2i(textureSize.x - border.getTotalWidth(), textureSize.y - border.getTotalHeight()), new Rectanglef(leftTex, topTex, rightTex, bottomTex));
} else {
addRectPoly(builder, left, top, right, bottom, leftTex, topTex, rightTex, bottomTex);
}
if (border.getRight() != 0) {
if (tile) {
addTiles(builder, new Rectanglei(region.getSizeX() - border.getRight(), border.getTop()).setSize(border.getRight(), centerVert), new Rectanglef(right, top, 1, bottom), new Vector2i(border.getRight(), textureSize.y - border.getTotalHeight()), new Rectanglef(rightTex, topTex, 1, bottomTex));
} else {
addRectPoly(builder, right, top, 1, bottom, rightTex, topTex, 1, bottomTex);
}
}
if (border.getBottom() != 0) {
if (border.getLeft() != 0) {
addRectPoly(builder, 0, bottom, left, 1, 0, bottomTex, leftTex, 1);
}
if (tile) {
addTiles(builder, new Rectanglei(border.getLeft(), region.getSizeY() - border.getBottom()).setSize(centerHoriz, border.getBottom()), new Rectanglef(left, bottom, right, 1), new Vector2i(textureSize.x - border.getTotalWidth(), border.getBottom()), new Rectanglef(leftTex, bottomTex, rightTex, 1));
} else {
addRectPoly(builder, left, bottom, right, 1, leftTex, bottomTex, rightTex, 1);
}
if (border.getRight() != 0) {
addRectPoly(builder, right, bottom, 1, 1, rightTex, bottomTex, 1, 1);
}
}
mesh = builder.build();
cachedTextures.put(key, mesh);
}
textureMat.setFloat2("scale", region.getSizeX(), region.getSizeY());
textureMat.setFloat2("offset", region.minX, region.minY);
Rectanglef textureArea = texture.getRegion();
textureMat.setFloat2("texOffset", textureArea.minX + ux * textureArea.lengthX(), textureArea.minY + uy * textureArea.lengthY());
textureMat.setFloat2("texSize", uw * textureArea.lengthX(), uh * textureArea.lengthY());
textureMat.setTexture("tex", ((TextureRegion) texture).getTexture());
textureMat.setFloat4("color", 1, 1, 1, alpha);
textureMat.bindTextures();
mesh.render();
}
use of org.joml.Vector2i in project Terasology by MovingBlocks.
the class AbstractSpawner method findSpawnPosition.
/**
* Tries to find a suitable spawning point based on {@link SurfacesFacet} and {@link ElevationFacet}.
* @param searchRadius the radius within a suitable spawning point will be searched
* @param world the facet-based world
* @param pos the desired 2D position in that world
* @return a 3D position above the surface and sea level or <code>null</code> if none was found
* @throws IllegalStateException if no required facets can be created.
*/
protected Vector3f findSpawnPosition(World world, Vector2i pos, int searchRadius) {
Vector3i ext = new Vector3i(searchRadius, searchRadius, searchRadius);
Vector3i desiredPos = new Vector3i(pos.x(), getStartHeight(world, pos), pos.y());
// try and find somewhere in this region a spot to land
BlockRegion spawnArea = new BlockRegion(desiredPos).expand(ext);
Region worldRegion = world.getWorldData(spawnArea);
Function<Vector2ic, Optional<Float>> getWorld;
// check if generation uses sea level and surface height facets
SurfacesFacet surfacesFacet = worldRegion.getFacet(SurfacesFacet.class);
ElevationFacet elevationFacet = worldRegion.getFacet(ElevationFacet.class);
SpawnHeightFacet spawnHeightFacet = worldRegion.getFacet(SpawnHeightFacet.class);
if (spawnHeightFacet != null) {
getWorld = v -> spawnHeightFacet.getWorld(v.x(), v.y());
} else if (elevationFacet != null) {
if (surfacesFacet != null) {
getWorld = v -> surfacesFacet.getPrimarySurface(elevationFacet, v.x(), v.y());
} else {
getWorld = v -> Optional.of(elevationFacet.getWorld(v.x(), v.y()));
}
} else {
throw new IllegalStateException("No spawn height facet or elevation facet facet found. Can't place spawn point.");
}
Function<Vector2ic, Optional<Integer>> getSeaLevel;
SeaLevelFacet seaLevelFacet = worldRegion.getFacet(SeaLevelFacet.class);
StrictlySparseSeaLevelFacet sparseSeaLevelFacet = worldRegion.getFacet(StrictlySparseSeaLevelFacet.class);
if (sparseSeaLevelFacet != null) {
getSeaLevel = v -> sparseSeaLevelFacet.getSeaLevel(v.x(), v.y());
} else if (seaLevelFacet != null) {
getSeaLevel = v -> Optional.of(seaLevelFacet.getSeaLevel());
} else {
getSeaLevel = v -> Optional.of(0);
}
int spiralRad = searchRadius / 2 - 1;
SpiralIterable spiral = SpiralIterable.clockwise(pos).maxRadius(spiralRad).scale(2).build();
for (Vector2ic test : spiral) {
Optional<Float> val = getWorld.apply(test);
if (!val.isPresent()) {
continue;
}
int height = TeraMath.floorToInt(val.get());
if (!getSeaLevel.apply(test).isPresent() || height >= getSeaLevel.apply(test).get()) {
return new Vector3f(test.x(), height, test.y());
}
}
// nothing above sea level found
for (Vector2ic test : spiral) {
Optional<Float> val = getWorld.apply(test);
if (!val.isPresent()) {
continue;
}
return new Vector3f(test.x(), TeraMath.floorToInt(val.get()), test.y());
}
throw new IllegalStateException("No spawn location found");
}
use of org.joml.Vector2i in project Terasology by MovingBlocks.
the class SpiralIterable method iterator.
@Override
public Iterator<Vector2ic> iterator() {
return new Iterator<Vector2ic>() {
private int radius = 1;
private int leg;
private int x = -1;
private int y;
private int index;
private final Vector2i pos = new Vector2i();
@Override
public Vector2ic next() {
if (index >= maxArea) {
throw new NoSuchElementException("radius has been reached");
}
switch(leg) {
case 0:
++x;
if (x == radius) {
++leg;
}
break;
case 1:
++y;
if (y == radius) {
++leg;
}
break;
case 2:
--x;
if (-x == radius) {
++leg;
}
break;
case 3:
--y;
if (-y == radius) {
leg = 0;
++radius;
}
break;
}
index++;
int finalX = center.x() + x * scale;
int finalY = center.y() + (clockwise ? y : -y) * scale;
pos.set(finalX, finalY);
return pos;
}
@Override
public boolean hasNext() {
return index < maxArea;
}
@Override
public void remove() {
throw new UnsupportedOperationException("remove");
}
};
}
use of org.joml.Vector2i in project Terasology by MovingBlocks.
the class DocumentRenderer method getDocumentPreferredSize.
public static Vector2i getDocumentPreferredSize(DocumentData documentData, Font defaultFont, Color defaultColor, int availableWidth) {
DefaultDocumentRenderStyle defaultDocumentRenderStyle = new DefaultDocumentRenderStyle(defaultFont, defaultColor);
DocumentRenderStyle documentRenderStyle = getDocumentRenderStyle(defaultDocumentRenderStyle, documentData);
Collection<ParagraphData> paragraphs = documentData.getParagraphs();
int minParagraphsWidth = getParagraphsMinimumWidth(availableWidth, documentRenderStyle, paragraphs);
int documentSideMargins = documentRenderStyle.getDocumentMarginLeft().getValue(availableWidth) + documentRenderStyle.getDocumentMarginRight().getValue(availableWidth);
int documentWidth = Math.max(availableWidth, minParagraphsWidth + documentSideMargins);
ContainerFlowContainerRenderSpace containerRenderSpace = new ContainerFlowContainerRenderSpace(documentWidth);
int preferredHeight = Math.max(getParagraphsPreferredHeight(documentRenderStyle, paragraphs, containerRenderSpace, 0), containerRenderSpace.getNextClearYPosition(ParagraphRenderStyle.ClearStyle.BOTH));
int documentVerticalMargins = documentRenderStyle.getDocumentMarginTop().getValue(documentWidth) + documentRenderStyle.getDocumentMarginBottom().getValue(documentWidth);
// Bring back the document indents to sides
return new Vector2i(documentWidth, preferredHeight + documentVerticalMargins);
}
Aggregations