Skip to content

Commit

Permalink
Improve tiling of things like fluids in GUIs by marking which directi…
Browse files Browse the repository at this point in the history
…on the tiling is growing so it looks more natural for which part is "cut off". This primarily improves the looks of gauges
  • Loading branch information
pupnewfster committed Feb 11, 2021
1 parent 0c9d7fb commit 40e8a5a
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 81 deletions.
81 changes: 69 additions & 12 deletions src/main/java/mekanism/client/gui/GuiUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,13 @@ public static void drawSprite(MatrixStack matrix, int x, int y, int width, int h
RenderSystem.disableBlend();
}

public static void drawTiledSprite(MatrixStack matrix, int xPosition, int yPosition, int yOffset, int desiredWidth, int desiredHeight, TextureAtlasSprite sprite, int textureWidth,
int textureHeight, int zLevel) {
public static void drawTiledSprite(MatrixStack matrix, int xPosition, int yPosition, int yOffset, int desiredWidth, int desiredHeight, TextureAtlasSprite sprite,
int textureWidth, int textureHeight, int zLevel, TilingDirection tilingDirection) {
drawTiledSprite(matrix, xPosition, yPosition, yOffset, desiredWidth, desiredHeight, sprite, textureWidth, textureHeight, zLevel, tilingDirection, true);
}

public static void drawTiledSprite(MatrixStack matrix, int xPosition, int yPosition, int yOffset, int desiredWidth, int desiredHeight, TextureAtlasSprite sprite,
int textureWidth, int textureHeight, int zLevel, TilingDirection tilingDirection, boolean blendAlpha) {
if (desiredWidth == 0 || desiredHeight == 0 || textureWidth == 0 || textureHeight == 0) {
return;
}
Expand All @@ -186,8 +191,10 @@ public static void drawTiledSprite(MatrixStack matrix, int xPosition, int yPosit
float vMax = sprite.getMaxV();
float uDif = uMax - uMin;
float vDif = vMax - vMin;
RenderSystem.enableBlend();
RenderSystem.enableAlphaTest();
if (blendAlpha) {
RenderSystem.enableBlend();
RenderSystem.enableAlphaTest();
}
BufferBuilder vertexBuffer = Tessellator.getInstance().getBuffer();
vertexBuffer.begin(GL11.GL_QUADS, DefaultVertexFormats.POSITION_TEX);
Matrix4f matrix4f = matrix.getLast().getMatrix();
Expand All @@ -199,7 +206,16 @@ public static void drawTiledSprite(MatrixStack matrix, int xPosition, int yPosit
int x = xPosition + (xTile * textureWidth);
int maskRight = textureWidth - width;
int shiftedX = x + textureWidth - maskRight;
float uMaxLocal = uMax - (uDif * maskRight / textureWidth);
float uLocalDif = uDif * maskRight / textureWidth;
float uLocalMin;
float uLocalMax;
if (tilingDirection.right) {
uLocalMin = uMin;
uLocalMax = uMax - uLocalDif;
} else {
uLocalMin = uMin + uLocalDif;
uLocalMax = uMax;
}
for (int yTile = 0; yTile <= yTileCount; yTile++) {
int height = (yTile == yTileCount) ? yRemainder : textureHeight;
if (height == 0) {
Expand All @@ -209,17 +225,28 @@ public static void drawTiledSprite(MatrixStack matrix, int xPosition, int yPosit
}
int y = yStart - ((yTile + 1) * textureHeight);
int maskTop = textureHeight - height;
float vMaxLocal = vMax - (vDif * maskTop / textureHeight);
vertexBuffer.pos(matrix4f, x, y + textureHeight, zLevel).tex(uMin, vMaxLocal).endVertex();
vertexBuffer.pos(matrix4f, shiftedX, y + textureHeight, zLevel).tex(uMaxLocal, vMaxLocal).endVertex();
vertexBuffer.pos(matrix4f, shiftedX, y + maskTop, zLevel).tex(uMaxLocal, vMin).endVertex();
vertexBuffer.pos(matrix4f, x, y + maskTop, zLevel).tex(uMin, vMin).endVertex();
float vLocalDif = vDif * maskTop / textureHeight;
float vLocalMin;
float vLocalMax;
if (tilingDirection.down) {
vLocalMin = vMin;
vLocalMax = vMax - vLocalDif;
} else {
vLocalMin = vMin + vLocalDif;
vLocalMax = vMax;
}
vertexBuffer.pos(matrix4f, x, y + textureHeight, zLevel).tex(uLocalMin, vLocalMax).endVertex();
vertexBuffer.pos(matrix4f, shiftedX, y + textureHeight, zLevel).tex(uLocalMax, vLocalMax).endVertex();
vertexBuffer.pos(matrix4f, shiftedX, y + maskTop, zLevel).tex(uLocalMax, vLocalMin).endVertex();
vertexBuffer.pos(matrix4f, x, y + maskTop, zLevel).tex(uLocalMin, vLocalMin).endVertex();
}
}
vertexBuffer.finishDrawing();
WorldVertexBufferUploader.draw(vertexBuffer);
RenderSystem.disableAlphaTest();
RenderSystem.disableBlend();
if (blendAlpha) {
RenderSystem.disableAlphaTest();
RenderSystem.disableBlend();
}
}

// reverse-order iteration over children w/ built-in GuiElement check, runs a basic anyMatch with checker
Expand Down Expand Up @@ -261,4 +288,34 @@ public static void renderItem(MatrixStack matrix, ItemRenderer renderer, @Nonnul
}
}
}

/**
* Represents which direction our tiling is done when extending past the max size.
*/
public enum TilingDirection {
/**
* Textures are being tiled/filled from top left to bottom right.
*/
DOWN_RIGHT(true, true),
/**
* Textures are being tiled/filled from top right to bottom left.
*/
DOWN_LEFT(true, false),
/**
* Textures are being tiled/filled from bottom left to top right.
*/
UP_RIGHT(false, true),
/**
* Textures are being tiled/filled from bottom right to top left.
*/
UP_LEFT(false, false);

private final boolean down;
private final boolean right;

TilingDirection(boolean down, boolean right) {
this.down = down;
this.right = right;
}
}
}
6 changes: 4 additions & 2 deletions src/main/java/mekanism/client/gui/element/GuiElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import mekanism.api.text.ILangEntry;
import mekanism.client.gui.GuiMekanism;
import mekanism.client.gui.GuiUtils;
import mekanism.client.gui.GuiUtils.TilingDirection;
import mekanism.client.gui.IGuiWrapper;
import mekanism.client.render.IFancyFontRenderer;
import mekanism.client.render.MekanismRenderer;
Expand Down Expand Up @@ -360,8 +361,9 @@ protected void playClickSound() {
super.playDownSound(minecraft.getSoundHandler());
}

protected void drawTiledSprite(MatrixStack matrix, int xPosition, int yPosition, int yOffset, int desiredWidth, int desiredHeight, TextureAtlasSprite sprite) {
GuiUtils.drawTiledSprite(matrix, xPosition, yPosition, yOffset, desiredWidth, desiredHeight, sprite, 16, 16, getBlitOffset());
protected void drawTiledSprite(MatrixStack matrix, int xPosition, int yPosition, int yOffset, int desiredWidth, int desiredHeight, TextureAtlasSprite sprite,
TilingDirection tilingDirection) {
GuiUtils.drawTiledSprite(matrix, xPosition, yPosition, yOffset, desiredWidth, desiredHeight, sprite, 16, 16, getBlitOffset(), tilingDirection);
}

public enum ButtonBackground {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import mekanism.api.chemical.pigment.Pigment;
import mekanism.api.chemical.slurry.Slurry;
import mekanism.client.gui.GuiMekanismTile;
import mekanism.client.gui.GuiUtils.TilingDirection;
import mekanism.client.gui.IGuiWrapper;
import mekanism.client.gui.element.bar.GuiChemicalBar.ChemicalInfoProvider;
import mekanism.client.jei.interfaces.IJEIIngredientHelper;
Expand Down Expand Up @@ -49,9 +50,9 @@ protected void renderBarOverlay(MatrixStack matrix, int mouseX, int mouseY, floa
MekanismRenderer.color(type);
TextureAtlasSprite icon = MekanismRenderer.getChemicalTexture(type);
if (horizontal) {
drawTiledSprite(matrix, x + 1, y + 1, height - 2, (int) (level * (width - 2)), height - 2, icon);
drawTiledSprite(matrix, x + 1, y + 1, height - 2, (int) (level * (width - 2)), height - 2, icon, TilingDirection.DOWN_RIGHT);
} else {
drawTiledSprite(matrix, x + 1, y + 1, height - 2, width - 2, (int) (level * (height - 2)), icon);
drawTiledSprite(matrix, x + 1, y + 1, height - 2, width - 2, (int) (level * (height - 2)), icon, TilingDirection.DOWN_RIGHT);
}
MekanismRenderer.resetColor();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import mekanism.api.chemical.pigment.PigmentStack;
import mekanism.api.chemical.slurry.SlurryStack;
import mekanism.api.text.TextComponentUtil;
import mekanism.client.gui.GuiUtils.TilingDirection;
import mekanism.client.gui.IGuiWrapper;
import mekanism.client.gui.element.GuiElement;
import mekanism.client.gui.element.GuiRelativeElement;
Expand Down Expand Up @@ -69,12 +70,12 @@ public void drawBackground(@Nonnull MatrixStack matrix, int mouseX, int mouseY,
} else if (target instanceof FluidStack) {
FluidStack stack = (FluidStack) this.target;
MekanismRenderer.color(stack);
drawTiledSprite(matrix, x, y, height, width, height, MekanismRenderer.getFluidTexture(stack, FluidType.STILL));
drawTiledSprite(matrix, x, y, height, width, height, MekanismRenderer.getFluidTexture(stack, FluidType.STILL), TilingDirection.DOWN_RIGHT);
MekanismRenderer.resetColor();
} else if (target instanceof ChemicalStack) {
ChemicalStack<?> stack = (ChemicalStack<?>) this.target;
MekanismRenderer.color(stack);
drawTiledSprite(matrix, x, y, height, width, height, MekanismRenderer.getChemicalTexture(stack.getType()));
drawTiledSprite(matrix, x, y, height, width, height, MekanismRenderer.getChemicalTexture(stack.getType()), TilingDirection.DOWN_RIGHT);
MekanismRenderer.resetColor();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import javax.annotation.Nullable;
import mekanism.api.text.EnumColor;
import mekanism.client.gui.GuiMekanismTile;
import mekanism.client.gui.GuiUtils.TilingDirection;
import mekanism.client.gui.IGuiWrapper;
import mekanism.client.gui.element.GuiTexturedElement;
import mekanism.client.render.MekanismRenderer;
Expand Down Expand Up @@ -67,7 +68,7 @@ public void renderContents(MatrixStack matrix) {
TextureAtlasSprite icon = getIcon();
if (scale > 0 && icon != null) {
applyRenderColor();
drawTiledSprite(matrix, x + 1, y + 1, height - 2, width - 2, scale, icon);
drawTiledSprite(matrix, x + 1, y + 1, height - 2, width - 2, scale, icon, TilingDirection.UP_RIGHT);
MekanismRenderer.resetColor();
}
//Draw the bar overlay
Expand Down
68 changes: 7 additions & 61 deletions src/main/java/mekanism/client/jei/ChemicalStackRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,23 @@
import mekanism.api.math.MathUtils;
import mekanism.api.text.EnumColor;
import mekanism.api.text.TextComponentUtil;
import mekanism.client.gui.GuiUtils;
import mekanism.client.gui.GuiUtils.TilingDirection;
import mekanism.client.render.MekanismRenderer;
import mekanism.common.MekanismLang;
import mekanism.common.util.text.TextUtils;
import mezz.jei.api.gui.drawable.IDrawable;
import mezz.jei.api.ingredients.IIngredientRenderer;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.client.renderer.BufferBuilder;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.client.renderer.WorldVertexBufferUploader;
import net.minecraft.client.renderer.texture.AtlasTexture;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.client.renderer.vertex.DefaultVertexFormats;
import net.minecraft.client.util.ITooltipFlag;
import net.minecraft.util.math.vector.Matrix4f;
import net.minecraft.util.text.ITextComponent;
import net.minecraftforge.fluids.FluidAttributes;
import org.lwjgl.opengl.GL11;

public class ChemicalStackRenderer<STACK extends ChemicalStack<?>> implements IIngredientRenderer<STACK> {

protected static final int TEX_WIDTH = 16;
protected static final int TEX_HEIGHT = 16;
private static final int TEX_WIDTH = 16;
private static final int TEX_HEIGHT = 16;
private static final int MIN_CHEMICAL_HEIGHT = 1; // ensure tiny amounts of chemical are still visible

private final long capacityMb;
Expand Down Expand Up @@ -91,58 +85,10 @@ private void drawChemical(MatrixStack matrix, int xPosition, int yPosition, @Non
desiredHeight = height;
}
Chemical<?> chemical = stack.getType();
drawTiledSprite(matrix, xPosition, yPosition, width, desiredHeight, height, chemical);
}

private void drawTiledSprite(MatrixStack matrix, int xPosition, int yPosition, int desiredWidth, int desiredHeight, int yOffset, @Nonnull Chemical<?> chemical) {
if (desiredWidth == 0 || desiredHeight == 0) {
return;
}
Matrix4f matrix4f = matrix.getLast().getMatrix();
MekanismRenderer.color(chemical);
TextureAtlasSprite sprite = MekanismRenderer.getSprite(chemical.getIcon());
MekanismRenderer.bindTexture(AtlasTexture.LOCATION_BLOCKS_TEXTURE);
int xTileCount = desiredWidth / TEX_WIDTH;
int xRemainder = desiredWidth - (xTileCount * TEX_WIDTH);
int yTileCount = desiredHeight / TEX_HEIGHT;
int yRemainder = desiredHeight - (yTileCount * TEX_HEIGHT);
int yStart = yPosition + yOffset;
int zLevel = 100;
float uMin = sprite.getMinU();
float uMax = sprite.getMaxU();
float vMin = sprite.getMinV();
float vMax = sprite.getMaxV();
float uDif = uMax - uMin;
float vDif = vMax - vMin;
BufferBuilder vertexBuffer = Tessellator.getInstance().getBuffer();
vertexBuffer.begin(GL11.GL_QUADS, DefaultVertexFormats.POSITION_TEX);
for (int xTile = 0; xTile <= xTileCount; xTile++) {
int width = (xTile == xTileCount) ? xRemainder : TEX_WIDTH;
if (width == 0) {
break;
}
int x = xPosition + (xTile * TEX_WIDTH);
int maskRight = TEX_WIDTH - width;
int shiftedX = x + TEX_WIDTH - maskRight;
float uMaxLocal = uMax - (uDif * maskRight / TEX_WIDTH);
for (int yTile = 0; yTile <= yTileCount; yTile++) {
int height = (yTile == yTileCount) ? yRemainder : TEX_HEIGHT;
if (height == 0) {
//Note: We don't want to fully break out because our height will be zero if we are looking to
// draw the remainder, but there is no remainder as it divided evenly
break;
}
int y = yStart - ((yTile + 1) * TEX_HEIGHT);
int maskTop = TEX_HEIGHT - height;
float vMaxLocal = vMax - (vDif * maskTop / TEX_HEIGHT);
vertexBuffer.pos(matrix4f, x, y + TEX_HEIGHT, zLevel).tex(uMin, vMaxLocal).endVertex();
vertexBuffer.pos(matrix4f, shiftedX, y + TEX_HEIGHT, zLevel).tex(uMaxLocal, vMaxLocal).endVertex();
vertexBuffer.pos(matrix4f, shiftedX, y + maskTop, zLevel).tex(uMaxLocal, vMin).endVertex();
vertexBuffer.pos(matrix4f, x, y + maskTop, zLevel).tex(uMin, vMin).endVertex();
}
}
vertexBuffer.finishDrawing();
WorldVertexBufferUploader.draw(vertexBuffer);
//Tile upwards and to the right as the majority of things we render are gauges which look better when tiling upwards
GuiUtils.drawTiledSprite(matrix, xPosition, yPosition, height, width, desiredHeight, MekanismRenderer.getSprite(chemical.getIcon()),
TEX_WIDTH, TEX_HEIGHT, 100, TilingDirection.UP_RIGHT, false);
MekanismRenderer.resetColor();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import mekanism.client.gui.GuiUtils;
import mekanism.client.gui.GuiUtils.TilingDirection;
import mekanism.client.render.MekanismRenderer;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.AbstractGui;
Expand Down Expand Up @@ -32,7 +33,8 @@ public void render(@Nonnull MatrixStack matrix, int x, int y) {
int scale = getScaledLevel(width - 2);
if (scale > 0) {
boolean colored = applyRenderColor();
GuiUtils.drawTiledSprite(matrix, x + 1, y + 1, height - 2, scale, height - 2, icon, 16, 16, 0);
GuiUtils.drawTiledSprite(matrix, x + 1, y + 1, height - 2, scale, height - 2, icon,
16, 16, 0, TilingDirection.DOWN_RIGHT);
if (colored) {
MekanismRenderer.resetColor();
}
Expand Down

0 comments on commit 40e8a5a

Please sign in to comment.