Skip to content

Commit 560264b

Browse files
committed
Optimize hatch preview rendering
1 parent 7a6636d commit 560264b

File tree

5 files changed

+41
-37
lines changed

5 files changed

+41
-37
lines changed

src/client/java/aztech/modern_industrialization/MIClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ private static void init(FMLConstructModEvent ignored) {
166166
(mc, parentScreen) -> AutoConfig.getConfigScreen(MIConfig.class, parentScreen).get());
167167

168168
modBus.addListener(RegisterRenderBuffersEvent.class, event -> {
169-
event.registerRenderBuffer(MIRenderTypes.solidHighlight());
169+
event.registerRenderBuffer(MIRenderTypes.cutoutHighlight());
170170
});
171171

172172
// Warn if neither JEI nor REI is present!

src/client/java/aztech/modern_industrialization/client/MIRenderTypes.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,11 @@
2626
import com.mojang.blaze3d.vertex.DefaultVertexFormat;
2727
import com.mojang.blaze3d.vertex.VertexFormat;
2828
import net.minecraft.client.renderer.RenderType;
29+
import net.minecraft.client.renderer.texture.TextureAtlas;
2930

3031
public class MIRenderTypes {
3132
private static RenderType MACHINE_WRENCH_OVERLAY;
32-
private static RenderType SOLID_HIGHLIGHT; // used by hatch preview and wrong block highlight
33+
private static RenderType CUTOUT_HIGHLIGHT; // used by hatch preview and wrong block highlight
3334

3435
public static RenderType machineOverlay() {
3536
if (MACHINE_WRENCH_OVERLAY == null) {
@@ -38,11 +39,11 @@ public static RenderType machineOverlay() {
3839
return MACHINE_WRENCH_OVERLAY;
3940
}
4041

41-
public static RenderType solidHighlight() {
42-
if (SOLID_HIGHLIGHT == null) {
43-
SOLID_HIGHLIGHT = Factory.makeSolidHighlight();
42+
public static RenderType cutoutHighlight() {
43+
if (CUTOUT_HIGHLIGHT == null) {
44+
CUTOUT_HIGHLIGHT = Factory.makeCutoutHighlight();
4445
}
45-
return SOLID_HIGHLIGHT;
46+
return CUTOUT_HIGHLIGHT;
4647
}
4748

4849
// This is a subclass to get access to a bunch of fields and classes.
@@ -63,14 +64,14 @@ private static RenderType makeMachineOverlay() {
6364
.createCompositeState(false));
6465
}
6566

66-
private static RenderType makeSolidHighlight() {
67+
private static RenderType makeCutoutHighlight() {
6768
// Use block vertex format to use the fast path in BufferBuilder, even if the shader doesn't use the extra vertex attributes.
68-
return create("solid_highlight", DefaultVertexFormat.BLOCK, VertexFormat.Mode.QUADS, 65536, false, false,
69+
return create("cutout_highlight", DefaultVertexFormat.BLOCK, VertexFormat.Mode.QUADS, 65536, false, false,
6970
CompositeState.builder()
7071
.setTransparencyState(NO_TRANSPARENCY)
71-
.setTextureState(NO_TEXTURE)
72-
.setLightmapState(NO_LIGHTMAP)
73-
.setShaderState(POSITION_COLOR_SHADER)
72+
.setTextureState(new TextureStateShard(TextureAtlas.LOCATION_BLOCKS, false, false))
73+
.setLightmapState(LIGHTMAP)
74+
.setShaderState(POSITION_COLOR_TEX_LIGHTMAP_SHADER)
7475
.createCompositeState(false));
7576
}
7677
}

src/client/java/aztech/modern_industrialization/machines/multiblocks/MultiblockMachineBER.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public void render(MultiblockMachineBlockEntity be, float tickDelta, PoseStack m
6666
// Highlight placeable hatches in green
6767
matrices.translate(-0.005, -0.005, -0.005);
6868
matrices.scale(1.01f, 1.01f, 1.01f);
69-
RenderHelper.drawOverlay(matrices, vcp, 111f / 256, 1, 111f / 256, 15728880, overlay);
69+
RenderHelper.drawOverlay(matrices, vcp, overlay);
7070
}
7171
}
7272
if (drawHighlights) {

src/client/java/aztech/modern_industrialization/util/RenderHelper.java

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
import net.minecraft.client.renderer.BlockEntityWithoutLevelRenderer;
5252
import net.minecraft.client.renderer.GameRenderer;
5353
import net.minecraft.client.renderer.LevelRenderer;
54+
import net.minecraft.client.renderer.LightTexture;
5455
import net.minecraft.client.renderer.MultiBufferSource;
5556
import net.minecraft.client.renderer.Sheets;
5657
import net.minecraft.client.renderer.block.model.BakedQuad;
@@ -76,40 +77,40 @@
7677
import org.joml.Matrix4f;
7778

7879
public class RenderHelper {
79-
private static final Supplier<BakedQuad[]> OVERLAY_QUADS;
80-
private static final float W = 0.05f;
81-
private static final ResourceLocation LOCKED_TEXTURE_LOCATION = MI.id("block/locked");
80+
private static final ResourceLocation HATCH_PLACEMENT_OVERLAY_LOCATION = MI.id("block/hatch_placement_overlay");
81+
@Nullable
82+
private static OverlayQuads overlayQuads;
83+
84+
private record OverlayQuads(BakedQuad[] quads, TextureAtlasSprite sprite) {
85+
}
8286

83-
public static void drawOverlay(PoseStack ms, MultiBufferSource vcp, float r, float g, float b, int light, int overlay) {
84-
VertexConsumer vc = vcp.getBuffer(MIRenderTypes.solidHighlight());
85-
for (BakedQuad overlayQuad : OVERLAY_QUADS.get()) {
86-
vc.putBulkData(ms.last(), overlayQuad, r, g, b, 1.0f, light, overlay);
87+
public static void drawOverlay(PoseStack ms, MultiBufferSource vcp, int overlay) {
88+
var sprite = Minecraft.getInstance().getTextureAtlas(InventoryMenu.BLOCK_ATLAS).apply(HATCH_PLACEMENT_OVERLAY_LOCATION);
89+
if (overlayQuads == null || overlayQuads.sprite() != sprite) {
90+
overlayQuads = new OverlayQuads(buildOverlayQuads(sprite), sprite);
91+
}
92+
VertexConsumer vc = vcp.getBuffer(MIRenderTypes.cutoutHighlight());
93+
for (BakedQuad overlayQuad : overlayQuads.quads) {
94+
vc.putBulkData(ms.last(), overlayQuad, 1.0f, 1.0f, 1.0f, 1.0f, LightTexture.FULL_BRIGHT, /* not used by shader */ overlay);
8795
}
8896
}
8997

90-
static {
91-
OVERLAY_QUADS = Suppliers.memoize(() -> {
92-
var overlayQuads = new BakedQuad[24];
93-
QuadEmitter emitter = new QuadBuffer();
94-
for (Direction direction : Direction.values()) {
95-
emitter.emit();
96-
emitter.square(direction, 0, 0, 1, W, 0);
97-
overlayQuads[direction.get3DDataValue() * 4] = emitter.toBakedQuad(null);
98-
emitter.square(direction, 0, 1 - W, 1, 1, 0);
99-
overlayQuads[direction.get3DDataValue() * 4 + 1] = emitter.toBakedQuad(null);
100-
emitter.square(direction, 0, W, W, 1 - W, 0);
101-
overlayQuads[direction.get3DDataValue() * 4 + 2] = emitter.toBakedQuad(null);
102-
emitter.square(direction, 1 - W, W, 1, 1 - W, 0);
103-
overlayQuads[direction.get3DDataValue() * 4 + 3] = emitter.toBakedQuad(null);
104-
}
105-
return overlayQuads;
106-
});
98+
private static BakedQuad[] buildOverlayQuads(TextureAtlasSprite sprite) {
99+
var overlayQuads = new BakedQuad[6];
100+
QuadEmitter emitter = new QuadBuffer();
101+
for (Direction direction : Direction.values()) {
102+
emitter.emit();
103+
emitter.square(direction, 0, 0, 1, 1, 0);
104+
emitter.spriteBake(sprite, MutableQuadView.BAKE_LOCK_UV);
105+
overlayQuads[direction.get3DDataValue()] = emitter.toBakedQuad(sprite);
106+
}
107+
return overlayQuads;
107108
}
108109

109110
private static final Supplier<BakedQuad[]> CUBE_QUADS;
110111

111112
public static void drawCube(PoseStack ms, MultiBufferSource vcp, float r, float g, float b, int light, int overlay) {
112-
VertexConsumer vc = vcp.getBuffer(MIRenderTypes.solidHighlight());
113+
VertexConsumer vc = vcp.getBuffer(MIRenderTypes.cutoutHighlight());
113114
for (BakedQuad cubeQuad : CUBE_QUADS.get()) {
114115
vc.putBulkData(ms.last(), cubeQuad, r, g, b, 1.0f, light, overlay);
115116
}
@@ -226,6 +227,8 @@ public static void quadWithAlpha(VertexConsumer consumer, PoseStack.Pose matrixE
226227
consumer.putBulkData(matrixEntry, quad, red, green, blue, alpha, light, overlay);
227228
}
228229

230+
private static final ResourceLocation LOCKED_TEXTURE_LOCATION = MI.id("block/locked");
231+
229232
public static void drawLockedTexture(BlockEntity entity, PoseStack matrices, MultiBufferSource vertexConsumers, int colorRgb) {
230233
VertexConsumer vc = vertexConsumers.getBuffer(Sheets.cutoutBlockSheet());
231234
var sprite = Minecraft.getInstance().getTextureAtlas(InventoryMenu.BLOCK_ATLAS).apply(LOCKED_TEXTURE_LOCATION);
665 Bytes
Loading

0 commit comments

Comments
 (0)