重新添加RenderHelper

This commit is contained in:
Light_Quanta 2025-03-26 21:19:48 +08:00
parent 0cf015bd7c
commit 69ff0f6151
No known key found for this signature in database
GPG key ID: 11A39A1B8C890959
4 changed files with 263 additions and 19 deletions

View file

@ -0,0 +1,242 @@
package com.atsuishio.superbwarfare.client;
import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.*;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.renderer.GameRenderer;
import net.minecraft.resources.ResourceLocation;
import org.joml.Matrix4f;
public class RenderHelper {
// code from GuiGraphics
/**
* Blits a portion of the texture specified by the atlas location onto the screen at the given coordinates.
*
* @param atlasLocation the location of the texture atlas.
* @param x the x-coordinate of the blit position.
* @param y the y-coordinate of the blit position.
* @param uOffset the horizontal texture coordinate offset.
* @param vOffset the vertical texture coordinate offset.
* @param uWidth the width of the blitted portion in texture coordinates.
* @param vHeight the height of the blitted portion in texture coordinates.
*/
public static void preciseBlit(GuiGraphics gui, ResourceLocation atlasLocation, float x, float y, float uOffset, float vOffset, float uWidth, float vHeight) {
preciseBlit(gui, atlasLocation, x, y, 0, uOffset, vOffset, uWidth, vHeight, 256, 256);
}
/**
* Blits a portion of the texture specified by the atlas location onto the screen at the given coordinates with a blit offset and texture coordinates.
*
* @param atlasLocation the location of the texture atlas.
* @param x the x-coordinate of the blit position.
* @param y the y-coordinate of the blit position.
* @param blitOffset the z-level offset for rendering order.
* @param uOffset the horizontal texture coordinate offset.
* @param vOffset the vertical texture coordinate offset.
* @param uWidth the width of the blitted portion in texture coordinates.
* @param vHeight the height of the blitted portion in texture coordinates.
* @param textureWidth the width of the texture.
* @param textureHeight the height of the texture.
*/
public static void preciseBlit(
GuiGraphics gui, ResourceLocation atlasLocation,
float x, float y,
float blitOffset,
float uOffset, float vOffset,
float uWidth, float vHeight,
float textureWidth, float textureHeight
) {
preciseBlit(
gui, atlasLocation,
x, x + uWidth,
y, y + vHeight,
blitOffset,
uWidth, vHeight,
uOffset, vOffset,
textureWidth, textureHeight
);
}
/**
* Blits a portion of the texture specified by the atlas location onto the screen at the given position and dimensions with texture coordinates.
*
* @param atlasLocation the location of the texture atlas.
* @param x the x-coordinate of the top-left corner of the blit
* position.
* @param y the y-coordinate of the top-left corner of the blit
* position.
* @param width the width of the blitted portion.
* @param height the height of the blitted portion.
* @param uOffset the horizontal texture coordinate offset.
* @param vOffset the vertical texture coordinate offset.
* @param uWidth the width of the blitted portion in texture coordinates.
* @param vHeight the height of the blitted portion in texture coordinates.
* @param textureWidth the width of the texture.
* @param textureHeight the height of the texture.
*/
public static void preciseBlit(
GuiGraphics gui, ResourceLocation atlasLocation,
float x, float y,
float width, float height,
float uOffset, float vOffset,
float uWidth, float vHeight,
float textureWidth, float textureHeight
) {
preciseBlit(
gui, atlasLocation, x, x + width, y, y + height, 0, uWidth, vHeight, uOffset, vOffset, textureWidth, textureHeight
);
}
/**
* Blits a portion of the texture specified by the atlas location onto the screen at the given position and dimensions with texture coordinates.
*
* @param atlasLocation the location of the texture atlas.
* @param x the x-coordinate of the top-left corner of the blit
* position.
* @param y the y-coordinate of the top-left corner of the blit
* position.
* @param uOffset the horizontal texture coordinate offset.
* @param vOffset the vertical texture coordinate offset.
* @param width the width of the blitted portion.
* @param height the height of the blitted portion.
* @param textureWidth the width of the texture.
* @param textureHeight the height of the texture.
*/
public static void preciseBlit(
GuiGraphics gui,
ResourceLocation atlasLocation,
float x, float y,
float uOffset, float vOffset,
float width, float height,
float textureWidth, float textureHeight
) {
preciseBlit(gui, atlasLocation, x, y, width, height, uOffset, vOffset, width, height, textureWidth, textureHeight);
}
/**
* Performs the inner blit operation for rendering a texture with the specified coordinates and texture coordinates.
*
* @param atlasLocation the location of the texture atlas.
* @param x1 the x-coordinate of the first corner of the blit position.
* @param x2 the x-coordinate of the second corner of the blit position
* .
* @param y1 the y-coordinate of the first corner of the blit position.
* @param y2 the y-coordinate of the second corner of the blit position
* .
* @param blitOffset the z-level offset for rendering order.
* @param uWidth the width of the blitted portion in texture coordinates.
* @param vHeight the height of the blitted portion in texture coordinates.
* @param uOffset the horizontal texture coordinate offset.
* @param vOffset the vertical texture coordinate offset.
* @param textureWidth the width of the texture.
* @param textureHeight the height of the texture.
*/
public static void preciseBlit(
GuiGraphics gui, ResourceLocation atlasLocation,
float x1, float x2,
float y1, float y2,
float blitOffset,
float uWidth, float vHeight,
float uOffset, float vOffset,
float textureWidth, float textureHeight
) {
innerBlit(
gui, atlasLocation,
x1, x2,
y1, y2,
blitOffset,
(uOffset + 0.0F) / textureWidth,
(uOffset + uWidth) / textureWidth,
(vOffset + 0.0F) / textureHeight,
(vOffset + vHeight) / textureHeight
);
}
/**
* Performs the inner blit operation for rendering a texture with the specified coordinates and texture coordinates without color tfloating.
*
* @param atlasLocation the location of the texture atlas.
* @param x1 the x-coordinate of the first corner of the blit position.
* @param x2 the x-coordinate of the second corner of the blit position
* .
* @param y1 the y-coordinate of the first corner of the blit position.
* @param y2 the y-coordinate of the second corner of the blit position
* .
* @param blitOffset the z-level offset for rendering order.
* @param minU the minimum horizontal texture coordinate.
* @param maxU the maximum horizontal texture coordinate.
* @param minV the minimum vertical texture coordinate.
* @param maxV the maximum vertical texture coordinate.
*/
public static void innerBlit(
GuiGraphics gui,
ResourceLocation atlasLocation,
float x1, float x2,
float y1, float y2,
float blitOffset,
float minU, float maxU,
float minV, float maxV
) {
RenderSystem.setShaderTexture(0, atlasLocation);
RenderSystem.setShader(GameRenderer::getPositionTexShader);
Matrix4f matrix4f = gui.pose().last().pose();
BufferBuilder bufferbuilder = Tesselator.getInstance().begin(VertexFormat.Mode.QUADS, DefaultVertexFormat.POSITION_TEX);
bufferbuilder.addVertex(matrix4f, x1, y1, blitOffset).setUv(minU, minV);
bufferbuilder.addVertex(matrix4f, x1, y2, blitOffset).setUv(minU, maxV);
bufferbuilder.addVertex(matrix4f, x2, y2, blitOffset).setUv(maxU, maxV);
bufferbuilder.addVertex(matrix4f, x2, y1, blitOffset).setUv(maxU, minV);
BufferUploader.drawWithShader(bufferbuilder.buildOrThrow());
}
/**
* Performs the inner blit operation for rendering a texture with the specified coordinates, texture coordinates, and color tfloat.
*
* @param atlasLocation the location of the texture atlas.
* @param x1 the x-coordinate of the first corner of the blit position.
* @param x2 the x-coordinate of the second corner of the blit position
* .
* @param y1 the y-coordinate of the first corner of the blit position.
* @param y2 the y-coordinate of the second corner of the blit position
* .
* @param blitOffset the z-level offset for rendering order.
* @param minU the minimum horizontal texture coordinate.
* @param maxU the maximum horizontal texture coordinate.
* @param minV the minimum vertical texture coordinate.
* @param maxV the maximum vertical texture coordinate.
* @param red the red component of the color tfloat.
* @param green the green component of the color tfloat.
* @param blue the blue component of the color tfloat.
* @param alpha the alpha component of the color tfloat.
*/
public static void innerBlit(
GuiGraphics gui,
ResourceLocation atlasLocation,
float x1, float x2,
float y1, float y2,
float blitOffset,
float minU, float maxU,
float minV, float maxV,
float red, float green, float blue, float alpha
) {
RenderSystem.setShaderTexture(0, atlasLocation);
RenderSystem.setShader(GameRenderer::getPositionTexColorShader);
RenderSystem.enableBlend();
Matrix4f matrix4f = gui.pose().last().pose();
BufferBuilder bufferbuilder = Tesselator.getInstance().begin(VertexFormat.Mode.QUADS, DefaultVertexFormat.POSITION_TEX_COLOR);
bufferbuilder.addVertex(matrix4f, x1, y1, blitOffset)
.setUv(minU, minV)
.setColor(red, green, blue, alpha);
bufferbuilder.addVertex(matrix4f, x1, y2, blitOffset)
.setUv(minU, maxV)
.setColor(red, green, blue, alpha);
bufferbuilder.addVertex(matrix4f, x2, y2, blitOffset)
.setUv(maxU, maxV)
.setColor(red, green, blue, alpha);
bufferbuilder.addVertex(matrix4f, x2, y1, blitOffset)
.setUv(maxU, minV)
.setColor(red, green, blue, alpha);
BufferUploader.drawWithShader(bufferbuilder.buildOrThrow());
RenderSystem.disableBlend();
}
}

View file

@ -12,6 +12,7 @@ import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.player.Inventory;
import net.neoforged.api.distmarker.Dist;
import net.neoforged.api.distmarker.OnlyIn;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.List;
@ -53,14 +54,14 @@ public class ChargingStationScreen extends AbstractContainerScreen<ChargingStati
}
@Override
public void render(GuiGraphics pGuiGraphics, int pMouseX, int pMouseY, float pPartialTick) {
public void render(@NotNull GuiGraphics pGuiGraphics, int pMouseX, int pMouseY, float pPartialTick) {
this.renderBackground(pGuiGraphics, pMouseX, pMouseY, pPartialTick);
super.render(pGuiGraphics, pMouseX, pMouseY, pPartialTick);
this.renderTooltip(pGuiGraphics, pMouseX, pMouseY);
}
@Override
protected void renderTooltip(GuiGraphics pGuiGraphics, int pX, int pY) {
protected void renderTooltip(@NotNull GuiGraphics pGuiGraphics, int pX, int pY) {
super.renderTooltip(pGuiGraphics, pX, pY);
int i = (this.width - this.imageWidth) / 2;
@ -79,7 +80,7 @@ public class ChargingStationScreen extends AbstractContainerScreen<ChargingStati
class ShowRangeButton extends AbstractButton {
@Override
protected void renderWidget(GuiGraphics pGuiGraphics, int pMouseX, int pMouseY, float pPartialTick) {
protected void renderWidget(@NotNull GuiGraphics pGuiGraphics, int pMouseX, int pMouseY, float pPartialTick) {
this.setMessage(ChargingStationScreen.this.menu.showRange() ? Component.translatable("container.superbwarfare.charging_station.hide_range") : Component.translatable("container.superbwarfare.charging_station.show_range"));
super.renderWidget(pGuiGraphics, pMouseX, pMouseY, pPartialTick);
}
@ -95,8 +96,7 @@ public class ChargingStationScreen extends AbstractContainerScreen<ChargingStati
}
@Override
protected void updateWidgetNarration(NarrationElementOutput pNarrationElementOutput) {
protected void updateWidgetNarration(@NotNull NarrationElementOutput pNarrationElementOutput) {
}
}

View file

@ -2,6 +2,7 @@ package com.atsuishio.superbwarfare.client.screens;
import com.atsuishio.superbwarfare.ModUtils;
import com.atsuishio.superbwarfare.block.entity.FuMO25BlockEntity;
import com.atsuishio.superbwarfare.client.RenderHelper;
import com.atsuishio.superbwarfare.menu.FuMO25Menu;
import com.atsuishio.superbwarfare.tools.FormatTool;
import com.mojang.blaze3d.platform.GlStateManager;
@ -115,9 +116,8 @@ public class FuMO25Screen extends AbstractContainerScreen<FuMO25Menu> {
double moveX = (entity.getX() - pos.getX()) / range * 74;
double moveZ = (entity.getZ() - pos.getZ()) / range * 74;
// TODO preciseBlit
// RenderHelper.preciseBlit(guiGraphics, TEXTURE, (float) (centerX + moveX), (float) (centerY + moveZ),
// 233, 167, 4, 4, 358, 328);
RenderHelper.preciseBlit(guiGraphics, TEXTURE, (float) (centerX + moveX), (float) (centerY + moveZ),
233, 167, 4, 4, 358, 328);
}
poseStack.popPose();
@ -157,7 +157,9 @@ public class FuMO25Screen extends AbstractContainerScreen<FuMO25Menu> {
if (this.currentTarget != null) {
StringBuilder sb = new StringBuilder();
if (currentTarget.getDisplayName() != null) {
sb.append(currentTarget.getDisplayName().getString());
}
if (currentTarget instanceof LivingEntity living) {
sb.append(" (HP: ").append(FormatTool.format1D(living.getHealth()))
.append("/").append(FormatTool.format1D(living.getMaxHealth())).append(")");
@ -220,7 +222,7 @@ public class FuMO25Screen extends AbstractContainerScreen<FuMO25Menu> {
}
@Override
public void render(GuiGraphics pGuiGraphics, int pMouseX, int pMouseY, float pPartialTick) {
public void render(@NotNull GuiGraphics pGuiGraphics, int pMouseX, int pMouseY, float pPartialTick) {
this.renderBackground(pGuiGraphics, pMouseX, pMouseY, pPartialTick);
pGuiGraphics.pose().pushPose();
super.render(pGuiGraphics, pMouseX, pMouseY, pPartialTick);
@ -229,7 +231,7 @@ public class FuMO25Screen extends AbstractContainerScreen<FuMO25Menu> {
}
@Override
protected void renderTooltip(GuiGraphics pGuiGraphics, int pX, int pY) {
protected void renderTooltip(@NotNull GuiGraphics pGuiGraphics, int pX, int pY) {
super.renderTooltip(pGuiGraphics, pX, pY);
int i = (this.width - this.imageWidth) / 2;
@ -246,7 +248,7 @@ public class FuMO25Screen extends AbstractContainerScreen<FuMO25Menu> {
// 本方法留空
@Override
protected void renderLabels(GuiGraphics pGuiGraphics, int pMouseX, int pMouseY) {
protected void renderLabels(@NotNull GuiGraphics pGuiGraphics, int pMouseX, int pMouseY) {
}
@Override
@ -295,7 +297,7 @@ public class FuMO25Screen extends AbstractContainerScreen<FuMO25Menu> {
}
@Override
protected void renderWidget(GuiGraphics pGuiGraphics, int pMouseX, int pMouseY, float pPartialTick) {
protected void renderWidget(@NotNull GuiGraphics pGuiGraphics, int pMouseX, int pMouseY, float pPartialTick) {
if (FuMO25Screen.this.menu.getFuncType() == 3 && FuMO25Screen.this.menu.getSlot(0).getItem().isEmpty()) {
pGuiGraphics.blit(TEXTURE, this.getX(), this.getY(), 148, this.isHovered() ? 311 : 295, 29, 15, 358, 328);
} else {
@ -304,7 +306,7 @@ public class FuMO25Screen extends AbstractContainerScreen<FuMO25Menu> {
}
@Override
protected void updateWidgetNarration(NarrationElementOutput pNarrationElementOutput) {
protected void updateWidgetNarration(@NotNull NarrationElementOutput pNarrationElementOutput) {
}
}

View file

@ -14,6 +14,7 @@ import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.item.ItemStack;
import net.neoforged.api.distmarker.Dist;
import net.neoforged.api.distmarker.OnlyIn;
import org.jetbrains.annotations.NotNull;
@OnlyIn(Dist.CLIENT)
public class ReforgingTableScreen extends AbstractContainerScreen<ReforgingTableMenu> {
@ -36,7 +37,7 @@ public class ReforgingTableScreen extends AbstractContainerScreen<ReforgingTable
}
@Override
public void render(GuiGraphics pGuiGraphics, int pMouseX, int pMouseY, float pPartialTick) {
public void render(@NotNull GuiGraphics pGuiGraphics, int pMouseX, int pMouseY, float pPartialTick) {
this.renderBackground(pGuiGraphics, pMouseX, pMouseY, pPartialTick);
super.render(pGuiGraphics, pMouseX, pMouseY, pPartialTick);
@ -117,7 +118,7 @@ public class ReforgingTableScreen extends AbstractContainerScreen<ReforgingTable
}
@Override
protected void updateWidgetNarration(NarrationElementOutput pNarrationElementOutput) {
protected void updateWidgetNarration(@NotNull NarrationElementOutput pNarrationElementOutput) {
}
}
@ -164,7 +165,7 @@ public class ReforgingTableScreen extends AbstractContainerScreen<ReforgingTable
}
@Override
protected void updateWidgetNarration(NarrationElementOutput pNarrationElementOutput) {
protected void updateWidgetNarration(@NotNull NarrationElementOutput pNarrationElementOutput) {
}
}
@ -211,8 +212,7 @@ public class ReforgingTableScreen extends AbstractContainerScreen<ReforgingTable
}
@Override
protected void updateWidgetNarration(NarrationElementOutput pNarrationElementOutput) {
protected void updateWidgetNarration(@NotNull NarrationElementOutput pNarrationElementOutput) {
}
}
}