Skip to content

Commit

Permalink
Merge pull request #1219 from MercuryGryph/dev/1.21
Browse files Browse the repository at this point in the history
调整优化铁砧锤操作环渲染代码
  • Loading branch information
ZhuRuoLing authored Dec 2, 2024
2 parents 61b2f67 + 47f874b commit 8131df2
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@
import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.blaze3d.vertex.Tesselator;
import com.mojang.blaze3d.vertex.VertexFormat;
import dev.dubhe.anvilcraft.AnvilCraft;
import dev.dubhe.anvilcraft.api.input.IMouseHandlerExtension;
import dev.dubhe.anvilcraft.api.hammer.IHasHammerEffect;
import dev.dubhe.anvilcraft.api.input.IMouseHandlerExtension;
import dev.dubhe.anvilcraft.api.input.KeyboardInputActionIgnorable;
import dev.dubhe.anvilcraft.client.init.ModRenderTypes;
import dev.dubhe.anvilcraft.client.init.ModShaders;
Expand All @@ -23,7 +22,6 @@
import net.minecraft.client.renderer.RenderType;
import net.minecraft.core.BlockPos;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.properties.Property;
Expand All @@ -46,13 +44,19 @@ public class AnvilHammerScreen extends Screen implements IHasHammerEffect, Keybo
public static final int CLOSING_ANIMATION_T = 150;//ms
public static final float ZOOM = 13.5f;
public static final int IGNORE_CURSOR_MOVE_LENGTH = 15;
public static final int BACKGROUND_WIDTH = 256;

public static final ResourceLocation BACKGROUND = AnvilCraft.of("textures/gui/selector/select_ring.png");
public static final ResourceLocation SELECTION = AnvilCraft.of("textures/gui/selector/selected_part.png");

private static final MethodHandle PROPERTY_TOSTRING;

private static final int RING_COLOR = 0x88000000;
private static final int RING_INNER_DIAMETER = 55;
private static final int RING_OUTER_DIAMETER = 105;

private static final int SELECTION_EFFECT_COLOR = 0xddFFFF00;
private static final int SELECTION_EFFECT_RADIUS = 20;

private static final float TEXT_SCALE = 1f;
private static final int TEXT_COLOR = 0xfdfdfd;

static {
MethodType mt = MethodType.methodType(
String.class,
Expand Down Expand Up @@ -115,8 +119,7 @@ protected void init() {
detectionEnd,
state,
Component.literal(
"%s: %s".formatted(
property.getName(),
"%s".formatted(
PROPERTY_TOSTRING.invokeWithArguments(
property,
state.getValue(property)
Expand Down Expand Up @@ -180,9 +183,9 @@ private void renderProgressAnimation(GuiGraphics guiGraphics, float progress, fl
guiGraphics,
this.width / 2f,
this.height / 2f,
0x88000000,
55 * progress,
105 * progress
RING_COLOR,
RING_INNER_DIAMETER * progress,
RING_OUTER_DIAMETER * progress
);
poseStack.popPose();
float finalProgress = progress;
Expand All @@ -199,8 +202,8 @@ private void renderProgressAnimation(GuiGraphics guiGraphics, float progress, fl
guiGraphics,
center.x,
center.y,
0xddFFFF00,
20
SELECTION_EFFECT_COLOR,
SELECTION_EFFECT_RADIUS
);
});
for (SelectionItem value : items) {
Expand All @@ -223,15 +226,14 @@ private void renderProgressAnimation(GuiGraphics guiGraphics, float progress, fl
int textAlpha = (int) (progress * 0xff) << 24;
poseStack.pushPose();
float coordinateScale = 0.7f;
float textScale = 0.8f;
float offsetX = 0.1f * this.width;
float offsetY = 0.1f * this.height;
float adjustedX = (x - offsetX) / coordinateScale;
float adjustedY = (y - offsetY - 20) / coordinateScale;
poseStack.translate(offsetX, offsetY, 0);
poseStack.scale(coordinateScale, coordinateScale, coordinateScale);
poseStack.translate(adjustedX, adjustedY, 0);
poseStack.scale(textScale / coordinateScale, textScale / coordinateScale, textScale / coordinateScale);
poseStack.scale(TEXT_SCALE / coordinateScale, TEXT_SCALE / coordinateScale, TEXT_SCALE / coordinateScale);
guiGraphics.drawCenteredString(
minecraft.font,
value.description,
Expand Down Expand Up @@ -265,22 +267,13 @@ public void render(GuiGraphics guiGraphics, int mouseX, int mouseY, float partia
renderProgressAnimation(guiGraphics, progress, centerX, centerY);
return;
}
// guiGraphics.blit(
// BACKGROUND,
// (this.width - BACKGROUND_WIDTH) / 2,
// (this.height - BACKGROUND_WIDTH) / 2,
// 0,
// 0,
// 256,
// 256
// );
renderRing(
guiGraphics,
this.width / 2f,
this.height / 2f,
0x88000000,
55,
105
RING_COLOR,
RING_INNER_DIAMETER,
RING_OUTER_DIAMETER
);
renderSelection(guiGraphics);
for (SelectionItem value : items) {
Expand All @@ -297,7 +290,6 @@ public void render(GuiGraphics guiGraphics, int mouseX, int mouseY, float partia
);
poseStack.pushPose();
float coordinateScale = 0.7f;
float textScale = 0.8f;
float offsetX = 0.1f * this.width;
float offsetY = 0.1f * this.height;
float adjustedX = (x - offsetX) / coordinateScale;
Expand All @@ -306,13 +298,13 @@ public void render(GuiGraphics guiGraphics, int mouseX, int mouseY, float partia
poseStack.translate(offsetX, offsetY, 0);
poseStack.scale(coordinateScale, coordinateScale, coordinateScale);
poseStack.translate(adjustedX, adjustedY, 0);
poseStack.scale(textScale / coordinateScale, textScale / coordinateScale, textScale / coordinateScale);
poseStack.scale(TEXT_SCALE / coordinateScale, TEXT_SCALE / coordinateScale, TEXT_SCALE / coordinateScale);
guiGraphics.drawCenteredString(
minecraft.font,
value.description,
0,
0,
0xfffdfdfd
(0xff << 24) | TEXT_COLOR
);
poseStack.popPose();
}
Expand All @@ -327,8 +319,8 @@ private void renderSelection(GuiGraphics guiGraphics) {
guiGraphics,
it.center.x,
it.center.y,
0xddFFFF00,
20
SELECTION_EFFECT_COLOR,
SELECTION_EFFECT_RADIUS
);
});
}
Expand Down Expand Up @@ -453,9 +445,6 @@ private static void renderRing(
ModShaders.getRingShader()
.safeGetUniform("Center")
.set(centerX * guiScale, centerY * guiScale);
ModShaders.getRingShader()
.safeGetUniform("FramebufferSize")
.set((float) window.getWidth(), (float) window.getHeight());
ModShaders.getRingShader()
.safeGetUniform("InnerDiameter")
.set(innerDiameter * guiScale);
Expand Down
15 changes: 6 additions & 9 deletions src/main/resources/assets/anvilcraft/shaders/core/ring.fsh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
in vec4 vertexColor;

uniform vec4 ColorModulator;
uniform vec2 FramebufferSize;
uniform vec2 Center;
uniform float InnerDiameter;
uniform float OuterDiameter;
Expand All @@ -12,19 +11,17 @@ uniform float AntiAliasingRadius;
out vec4 fragColor;

void main() {
vec2 fragPos = vec2(gl_FragCoord.x, FramebufferSize.y - gl_FragCoord.y);
float distance = length(fragPos - Center);
float distance = distance(gl_FragCoord.xy, Center);
vec4 color = vec4(0, 0, 0, 0);
if (distance <= OuterDiameter + AntiAliasingRadius &&distance >= InnerDiameter - AntiAliasingRadius) {

if (distance <= OuterDiameter + AntiAliasingRadius && distance >= InnerDiameter - AntiAliasingRadius) {
color = vertexColor;
}

if (distance >= InnerDiameter - AntiAliasingRadius &&distance <= InnerDiameter + AntiAliasingRadius) {
if (distance >= InnerDiameter - AntiAliasingRadius && distance <= InnerDiameter + AntiAliasingRadius) {
color.a *= smoothstep(InnerDiameter - AntiAliasingRadius, InnerDiameter + AntiAliasingRadius, distance);
} else {
if (distance >= OuterDiameter - AntiAliasingRadius && distance <= OuterDiameter + AntiAliasingRadius) {
color.a *= smoothstep(OuterDiameter + AntiAliasingRadius, OuterDiameter - AntiAliasingRadius, distance);
}
} else if (distance >= OuterDiameter - AntiAliasingRadius && distance <= OuterDiameter + AntiAliasingRadius) {
color.a *= smoothstep(OuterDiameter + AntiAliasingRadius, OuterDiameter - AntiAliasingRadius, distance);
}

fragColor = color * ColorModulator;
Expand Down
11 changes: 1 addition & 10 deletions src/main/resources/assets/anvilcraft/shaders/core/ring.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,6 @@
0.0
]
},
{
"name": "FramebufferSize",
"type": "float",
"count": 2,
"values": [
0.0,
0.0
]
},
{
"name": "InnerDiameter",
"type": "float",
Expand All @@ -46,7 +37,7 @@
"type": "float",
"count": 1,
"values": [
1.5
1.25
]
}
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ out vec4 fragColor;

void main() {
vec2 fragPos = vec2(gl_FragCoord.x, FramebufferSize.y - gl_FragCoord.y);
float distance = length(fragPos - Center);
float distance = distance(fragPos, Center);
vec4 color = vec4(0, 0, 0, 0);
if (distance <= Radius) {
color = vertexColor;
color.a = 1 - smoothstep(0f, Radius, distance);
color.a = smoothstep(Radius, 0f, distance);
}

fragColor = color * ColorModulator;
Expand Down

0 comments on commit 8131df2

Please sign in to comment.