From 55c791161a84f160bfd6cc6d3ba2830c8b931b35 Mon Sep 17 00:00:00 2001 From: froslanz Date: Fri, 30 Oct 2026 11:56:03 -0400 Subject: [PATCH] Capture entity model submissions for chams rendering - ChamsRenderer accumulates pose-transformed model vertices into fill quads and outline lines while the entity pass runs, and replays them through the no-depth-test Layers pipelines so chams see through walls - MixinSubmitNodeCollection hooks submitModel/submitModelPart; all submission paths funnel through it (SubmitNodeStorage delegates via order(0)), so one mixin covers everything without double capture --- .../render/MixinSubmitNodeCollection.java | 43 +++ .../cc/fros/util/render/ChamsRenderer.java | 278 ++++++++++++++++++ src/main/resources/fros.mixins.json | 1 + 3 files changed, 322 insertions(+) create mode 100644 src/main/java/cc/fros/mixin/render/MixinSubmitNodeCollection.java create mode 100644 src/main/java/cc/fros/util/render/ChamsRenderer.java diff --git a/src/main/java/cc/fros/mixin/render/MixinSubmitNodeCollection.java b/src/main/java/cc/fros/mixin/render/MixinSubmitNodeCollection.java new file mode 100644 index 0000000..69877dd --- /dev/null +++ b/src/main/java/cc/fros/mixin/render/MixinSubmitNodeCollection.java @@ -0,0 +1,43 @@ +package cc.fros.mixin.render; + +import cc.fros.util.render.ChamsRenderer; +import com.mojang.blaze3d.vertex.PoseStack; +import net.minecraft.client.model.Model; +import net.minecraft.client.model.geom.ModelPart; +import net.minecraft.client.renderer.SubmitNodeCollection; +import net.minecraft.client.renderer.feature.ModelFeatureRenderer; +import net.minecraft.client.renderer.rendertype.RenderType; +import net.minecraft.client.renderer.texture.TextureAtlasSprite; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +/** + * Every entity model submission funnels through here: {@code SubmitNodeStorage} + * delegates to {@code order(0)} and renderer code calling {@code collector.order(n)} + * lands on the same collection, so this single mixin captures all entity models + * without double-capturing. + */ +@Mixin(SubmitNodeCollection.class) +public class MixinSubmitNodeCollection { + + @Inject(method = "submitModel", at = @At("HEAD")) + private void chamsCaptureModel(Model model, Object state, PoseStack pose, RenderType renderType, + int light, int overlay, int tint, TextureAtlasSprite sprite, int index, + ModelFeatureRenderer.CrumblingOverlay crumblingOverlay, CallbackInfo ci) { + if (ChamsRenderer.isActive()) { + ChamsRenderer.captureModel(model, state, pose); + } + } + + @Inject(method = "submitModelPart", at = @At("HEAD")) + private void chamsCapturePart(ModelPart part, PoseStack pose, RenderType renderType, + int light, int overlay, TextureAtlasSprite sprite, boolean cull, boolean inverted, + int index, ModelFeatureRenderer.CrumblingOverlay crumblingOverlay, + int layer, CallbackInfo ci) { + if (ChamsRenderer.isActive()) { + ChamsRenderer.capturePart(part, pose); + } + } +} diff --git a/src/main/java/cc/fros/util/render/ChamsRenderer.java b/src/main/java/cc/fros/util/render/ChamsRenderer.java new file mode 100644 index 0000000..e65e60d --- /dev/null +++ b/src/main/java/cc/fros/util/render/ChamsRenderer.java @@ -0,0 +1,278 @@ +package cc.fros.util.render; + +import com.mojang.blaze3d.vertex.BufferBuilder; +import com.mojang.blaze3d.vertex.DefaultVertexFormat; +import com.mojang.blaze3d.vertex.PoseStack; +import com.mojang.blaze3d.vertex.Tesselator; +import com.mojang.blaze3d.vertex.VertexConsumer; +import com.mojang.blaze3d.vertex.VertexFormat; +import net.minecraft.client.model.Model; +import net.minecraft.client.model.geom.ModelPart; +import net.minecraft.client.renderer.entity.state.EntityRenderState; +import net.minecraft.world.entity.Entity; +import org.joml.Matrix4fc; +import org.joml.Vector3fc; + +import java.util.Arrays; +import java.util.IdentityHashMap; + +/** + * Captures entity model geometry while it is submitted for rendering and replays it + * through the no-depth-test pipelines from {@link Layers}, so chams render through + * walls. Vertices arrive pose-transformed and camera-relative, exactly the space the + * box helpers in {@link RenderUtil} draw in. + */ +public final class ChamsRenderer { + + private ChamsRenderer() { + throw new AssertionError(); + } + + // frame context, set per entity by the dispatcher mixin + private static boolean active; + private static boolean doFill; + private static boolean doOutline; + private static int fillColor; + private static int outlineColor; + private static float drawWidth; + + // state -> entity for the current frame (for friend/damage lookup in the dispatcher) + private static final IdentityHashMap ENTITIES = new IdentityHashMap<>(); + + public static void track(EntityRenderState state, Entity entity) { + ENTITIES.put(state, entity); + } + + public static Entity entityOf(EntityRenderState state) { + return ENTITIES.get(state); + } + + public static void setContext(boolean fill, boolean outline, int fillArgb, int outlineArgb, float width) { + active = true; + doFill = fill; + doOutline = outline; + fillColor = fillArgb; + outlineColor = outlineArgb; + drawWidth = width; + partial = 0; + } + + public static void clearContext() { + active = false; + partial = 0; + } + + public static boolean isActive() { + return active; + } + + public static void captureModel(Model model, Object state, PoseStack pose) { + if (!active) return; + try { + @SuppressWarnings({"unchecked", "rawtypes"}) + Model raw = model; + raw.setupAnim(state); + raw.renderToBuffer(pose, CONSUMER, 0, 0, -1); + } catch (Throwable ignored) { + } + } + + public static void capturePart(ModelPart part, PoseStack pose) { + if (!active) return; + try { + part.render(pose, CONSUMER, 0, 0); + } catch (Throwable ignored) { + } + } + + // ---- vertex capture ---- + + private static final Consumer CONSUMER = new Consumer(); + private static int partial; + private static final float[] QUAD_X = new float[4]; + private static final float[] QUAD_Y = new float[4]; + private static final float[] QUAD_Z = new float[4]; + + private static void emit(float x, float y, float z) { + QUAD_X[partial] = x; + QUAD_Y[partial] = y; + QUAD_Z[partial] = z; + if (++partial == 4) { + partial = 0; + flushQuad(); + } + } + + private static void flushQuad() { + if (doFill) { + for (int i = 0; i < 4; i++) { + addFill(QUAD_X[i], QUAD_Y[i], QUAD_Z[i], fillColor); + } + } + if (doOutline) { + for (int i = 0; i < 4; i++) { + int j = (i + 1) & 3; + addLine(QUAD_X[i], QUAD_Y[i], QUAD_Z[i], QUAD_X[j], QUAD_Y[j], QUAD_Z[j], outlineColor); + } + } + } + + // ---- geometry buffers ---- + + private static float[] fillPos = new float[3072]; + private static int[] fillCol = new int[1024]; + private static int fillCount; + + private static float[] linePos = new float[3072]; + private static float[] lineNormal = new float[3072]; + private static int[] lineCol = new int[1024]; + private static int lineCount; + + private static void addFill(float x, float y, float z, int color) { + if ((fillCount + 1) * 3 > fillPos.length) { + fillPos = Arrays.copyOf(fillPos, fillPos.length * 2); + fillCol = Arrays.copyOf(fillCol, fillCol.length * 2); + } + int p = fillCount * 3; + fillPos[p] = x; + fillPos[p + 1] = y; + fillPos[p + 2] = z; + fillCol[fillCount] = color; + fillCount++; + } + + private static void addLine(float x1, float y1, float z1, float x2, float y2, float z2, int color) { + if ((lineCount + 2) * 3 > linePos.length) { + linePos = Arrays.copyOf(linePos, linePos.length * 2); + lineNormal = Arrays.copyOf(lineNormal, lineNormal.length * 2); + lineCol = Arrays.copyOf(lineCol, lineCol.length * 2); + } + float nx = x2 - x1; + float ny = y2 - y1; + float nz = z2 - z1; + int p = lineCount * 3; + linePos[p] = x1; + linePos[p + 1] = y1; + linePos[p + 2] = z1; + lineNormal[p] = nx; + lineNormal[p + 1] = ny; + lineNormal[p + 2] = nz; + lineCol[lineCount] = color; + p += 3; + linePos[p] = x2; + linePos[p + 1] = y2; + linePos[p + 2] = z2; + lineNormal[p] = nx; + lineNormal[p + 1] = ny; + lineNormal[p + 2] = nz; + lineCol[lineCount + 1] = color; + lineCount += 2; + } + + // ---- draw ---- + + public static void draw(PoseStack pose) { + if (fillCount > 0) { + BufferBuilder buffer = Tesselator.getInstance() + .begin(VertexFormat.Mode.QUADS, DefaultVertexFormat.POSITION_COLOR); + for (int i = 0; i < fillCount; i++) { + int p = i * 3; + buffer.addVertex(pose.last().pose(), fillPos[p], fillPos[p + 1], fillPos[p + 2]) + .setColor(fillCol[i]); + } + Layers.quads().draw(buffer.buildOrThrow()); + fillCount = 0; + } + + if (lineCount > 0) { + BufferBuilder buffer = Tesselator.getInstance() + .begin(VertexFormat.Mode.LINES, DefaultVertexFormat.POSITION_COLOR_NORMAL_LINE_WIDTH); + for (int i = 0; i < lineCount; i++) { + int p = i * 3; + buffer.addVertex(pose.last().pose(), linePos[p], linePos[p + 1], linePos[p + 2]) + .setColor(lineCol[i]) + .setNormal(lineNormal[p], lineNormal[p + 1], lineNormal[p + 2]) + .setLineWidth(drawWidth); + } + Layers.lines().draw(buffer.buildOrThrow()); + lineCount = 0; + } + + clearContext(); + ENTITIES.clear(); + } + + // ---- consumer ---- + + private static final class Consumer implements VertexConsumer { + @Override + public VertexConsumer addVertex(float x, float y, float z) { + emit(x, y, z); + return this; + } + + @Override + public void addVertex(float x, float y, float z, int color, float u, float v, int overlay, int light, float nx, float ny, float nz) { + emit(x, y, z); + } + + @Override + public VertexConsumer addVertex(Matrix4fc matrix, float x, float y, float z) { + emit(matrix.m00() * x + matrix.m01() * y + matrix.m02() * z + matrix.m03(), + matrix.m10() * x + matrix.m11() * y + matrix.m12() * z + matrix.m13(), + matrix.m20() * x + matrix.m21() * y + matrix.m22() * z + matrix.m23()); + return this; + } + + @Override + public VertexConsumer addVertex(PoseStack.Pose pose, float x, float y, float z) { + return addVertex(pose.pose(), x, y, z); + } + + @Override + public VertexConsumer addVertex(Vector3fc vertex) { + emit(vertex.x(), vertex.y(), vertex.z()); + return this; + } + + @Override + public VertexConsumer setColor(int red, int green, int blue, int alpha) { + return this; + } + + @Override + public VertexConsumer setColor(int color) { + return this; + } + + @Override + public VertexConsumer setColor(float red, float green, float blue, float alpha) { + return this; + } + + @Override + public VertexConsumer setUv(float u, float v) { + return this; + } + + @Override + public VertexConsumer setUv1(int u, int v) { + return this; + } + + @Override + public VertexConsumer setUv2(int u, int v) { + return this; + } + + @Override + public VertexConsumer setNormal(float x, float y, float z) { + return this; + } + + @Override + public VertexConsumer setLineWidth(float width) { + return this; + } + } +} diff --git a/src/main/resources/fros.mixins.json b/src/main/resources/fros.mixins.json index c5a0bb7..e588557 100644 --- a/src/main/resources/fros.mixins.json +++ b/src/main/resources/fros.mixins.json @@ -18,6 +18,7 @@ "render.MixinChatComponent", "render.MixinDeltaTrackerTimer", "render.MixinLevelRenderer", + "render.MixinSubmitNodeCollection", "render.gui.MixinCommandSuggestions", "render.gui.MixinGui" ],