| 1 | package net.minecraft.client.renderer.feature;
|
| 2 |
|
| 3 | import com.mojang.blaze3d.systems.RenderSystem;
|
| 4 | import com.mojang.blaze3d.vertex.PoseStack;
|
| 5 | import com.mojang.blaze3d.vertex.VertexConsumer;
|
| 6 | import java.util.List;
|
| 7 | import net.fabricmc.api.EnvType;
|
| 8 | import net.fabricmc.api.Environment;
|
| 9 | import net.minecraft.client.gui.Font;
|
| 10 | import net.minecraft.client.gui.font.TextRenderable;
|
| 11 | import net.minecraft.client.renderer.feature.submit.SubmitNode;
|
| 12 | import net.minecraft.client.renderer.gizmos.DrawableGizmoPrimitives;
|
| 13 | import net.minecraft.client.renderer.rendertype.RenderTypes;
|
| 14 | import net.minecraft.client.renderer.state.level.CameraRenderState;
|
| 15 | import net.minecraft.gizmos.TextGizmo.Style;
|
| 16 | import net.minecraft.util.Mth;
|
| 17 | import net.minecraft.world.phys.Vec3;
|
| 18 | import org.joml.Matrix4f;
|
| 19 | import org.joml.Matrix4fc;
|
| 20 | import org.joml.Vector4f;
|
| 21 |
|
| 22 | @Environment(EnvType.CLIENT)
|
| 23 | public class GizmoFeatureRenderer extends RenderTypeFeatureRenderer<GizmoFeatureRenderer.Submit> {
|
| 24 | public static final FeatureRendererType<GizmoFeatureRenderer.Submit> TYPE = FeatureRendererType.create("Gizmo");
|
| 25 | private final PoseStack poseStack = new PoseStack();
|
| 26 |
|
| 27 | @Override
|
| 28 | protected void buildGroup(final FeatureFrameContext context, final List<GizmoFeatureRenderer.Submit> submits) {
|
| 29 | Matrix4f modelViewMatrix = RenderSystem.getModelViewMatrixCopy();
|
| 30 |
|
| 31 | for (GizmoFeatureRenderer.Submit submit : submits) {
|
| 32 | CameraRenderState camera = submit.camera;
|
| 33 | this.buildQuads(submit.group.quads(), camera);
|
| 34 | this.buildTriangleFans(submit.group.triangleFans(), camera);
|
| 35 | this.buildLines(submit.group.lines(), camera, modelViewMatrix, submit.group.opaque());
|
| 36 | this.buildTexts(submit.group.texts(), camera, context.font());
|
| 37 | this.buildPoints(submit.group.points(), camera);
|
| 38 | }
|
| 39 | }
|
| 40 |
|
| 41 | private void buildTexts(final List<DrawableGizmoPrimitives.Text> texts, final CameraRenderState camera, final Font font) {
|
| 42 | if (!texts.isEmpty() && camera.initialized) {
|
| 43 | double camX = camera.pos.x();
|
| 44 | double camY = camera.pos.y();
|
| 45 | double camZ = camera.pos.z();
|
| 46 |
|
| 47 | for (DrawableGizmoPrimitives.Text text : texts) {
|
| 48 | Style style = text.style();
|
| 49 | this.poseStack.pushPose();
|
| 50 | this.poseStack.translate((float)(text.pos().x() - camX), (float)(text.pos().y() - camY), (float)(text.pos().z() - camZ));
|
| 51 | this.poseStack.rotate(camera.orientation);
|
| 52 | this.poseStack.scale(style.scale() / 16.0F, -style.scale() / 16.0F, style.scale() / 16.0F);
|
| 53 | float fontX;
|
| 54 | if (style.adjustLeft().isEmpty()) {
|
| 55 | fontX = -font.width(text.text()) / 2.0F;
|
| 56 | } else {
|
| 57 | fontX = (float)(-style.adjustLeft().getAsDouble()) / style.scale();
|
| 58 | }
|
| 59 |
|
| 60 | final Matrix4f pose = this.poseStack.last().pose();
|
| 61 | Font.PreparedText preparedText = font.prepareText(text.text(), fontX, 0.0F, style.color(), false, 0);
|
| 62 | preparedText.visit(new Font.GlyphVisitor() {
|
| 63 | @Override
|
| 64 | public void acceptRenderable(final TextRenderable renderable) {
|
| 65 | VertexConsumer buffer = GizmoFeatureRenderer.this.getVertexBuilder(renderable.renderType(Font.DisplayMode.NORMAL));
|
| 66 | renderable.render(pose, buffer, 15728880, false);
|
| 67 | }
|
| 68 | });
|
| 69 | this.poseStack.popPose();
|
| 70 | }
|
| 71 | }
|
| 72 | }
|
| 73 |
|
| 74 | private void buildLines(final List<DrawableGizmoPrimitives.Line> lines, final CameraRenderState camera, final Matrix4fc modelViewMatrix, final boolean opaque) {
|
| 75 | if (!lines.isEmpty()) {
|
| 76 | VertexConsumer builder = this.getVertexBuilder(opaque ? RenderTypes.lines() : RenderTypes.linesTranslucentNoDepthWrite());
|
| 77 | PoseStack.Pose pose = this.poseStack.last();
|
| 78 | Vector4f start = new Vector4f();
|
| 79 | Vector4f end = new Vector4f();
|
| 80 | Vector4f startViewSpace = new Vector4f();
|
| 81 | Vector4f endViewSpace = new Vector4f();
|
| 82 | Vector4f intersectionInWorld = new Vector4f();
|
| 83 | double camX = camera.pos.x();
|
| 84 | double camY = camera.pos.y();
|
| 85 | double camZ = camera.pos.z();
|
| 86 |
|
| 87 | for (DrawableGizmoPrimitives.Line line : lines) {
|
| 88 | start.set(line.start().x() - camX, line.start().y() - camY, line.start().z() - camZ, 1.0);
|
| 89 | end.set(line.end().x() - camX, line.end().y() - camY, line.end().z() - camZ, 1.0);
|
| 90 | start.mul(modelViewMatrix, startViewSpace);
|
| 91 | end.mul(modelViewMatrix, endViewSpace);
|
| 92 | boolean startIsBehindCamera = startViewSpace.z > -0.05F;
|
| 93 | boolean endIsBehindCamera = endViewSpace.z > -0.05F;
|
| 94 | if (!startIsBehindCamera || !endIsBehindCamera) {
|
| 95 | if (startIsBehindCamera || endIsBehindCamera) {
|
| 96 | float denom = endViewSpace.z - startViewSpace.z;
|
| 97 | if (Math.abs(denom) < 1.0E-9F) {
|
| 98 | continue;
|
| 99 | }
|
| 100 |
|
| 101 | float intersection = Mth.clamp((-0.05F - startViewSpace.z) / denom, 0.0F, 1.0F);
|
| 102 | start.lerp(end, intersection, intersectionInWorld);
|
| 103 | if (startIsBehindCamera) {
|
| 104 | start.set(intersectionInWorld);
|
| 105 | } else {
|
| 106 | end.set(intersectionInWorld);
|
| 107 | }
|
| 108 | }
|
| 109 |
|
| 110 | builder.addVertex(pose, start.x, start.y, start.z)
|
| 111 | .setNormal(pose, end.x - start.x, end.y - start.y, end.z - start.z)
|
| 112 | .setColor(line.color())
|
| 113 | .setLineWidth(line.width());
|
| 114 | builder.addVertex(pose, end.x, end.y, end.z)
|
| 115 | .setNormal(pose, end.x - start.x, end.y - start.y, end.z - start.z)
|
| 116 | .setColor(line.color())
|
| 117 | .setLineWidth(line.width());
|
| 118 | }
|
| 119 | }
|
| 120 | }
|
| 121 | }
|
| 122 |
|
| 123 | private void buildTriangleFans(final List<DrawableGizmoPrimitives.TriangleFan> triangleFans, final CameraRenderState camera) {
|
| 124 | if (!triangleFans.isEmpty()) {
|
| 125 | PoseStack.Pose pose = this.poseStack.last();
|
| 126 | double camX = camera.pos.x();
|
| 127 | double camY = camera.pos.y();
|
| 128 | double camZ = camera.pos.z();
|
| 129 |
|
| 130 | for (DrawableGizmoPrimitives.TriangleFan triangleFan : triangleFans) {
|
| 131 | VertexConsumer builder = this.getVertexBuilder(RenderTypes.debugTriangleFan());
|
| 132 |
|
| 133 | for (Vec3 point : triangleFan.points()) {
|
| 134 | builder.addVertex(pose, (float)(point.x() - camX), (float)(point.y() - camY), (float)(point.z() - camZ)).setColor(triangleFan.color());
|
| 135 | }
|
| 136 | }
|
| 137 | }
|
| 138 | }
|
| 139 |
|
| 140 | private void buildQuads(final List<DrawableGizmoPrimitives.Quad> quads, final CameraRenderState camera) {
|
| 141 | if (!quads.isEmpty()) {
|
| 142 | VertexConsumer builder = this.getVertexBuilder(RenderTypes.debugFilledBox());
|
| 143 | PoseStack.Pose pose = this.poseStack.last();
|
| 144 | double camX = camera.pos.x();
|
| 145 | double camY = camera.pos.y();
|
| 146 | double camZ = camera.pos.z();
|
| 147 |
|
| 148 | for (DrawableGizmoPrimitives.Quad quad : quads) {
|
| 149 | builder.addVertex(pose, (float)(quad.a().x() - camX), (float)(quad.a().y() - camY), (float)(quad.a().z() - camZ)).setColor(quad.color());
|
| 150 | builder.addVertex(pose, (float)(quad.b().x() - camX), (float)(quad.b().y() - camY), (float)(quad.b().z() - camZ)).setColor(quad.color());
|
| 151 | builder.addVertex(pose, (float)(quad.c().x() - camX), (float)(quad.c().y() - camY), (float)(quad.c().z() - camZ)).setColor(quad.color());
|
| 152 | builder.addVertex(pose, (float)(quad.d().x() - camX), (float)(quad.d().y() - camY), (float)(quad.d().z() - camZ)).setColor(quad.color());
|
| 153 | }
|
| 154 | }
|
| 155 | }
|
| 156 |
|
| 157 | private void buildPoints(final List<DrawableGizmoPrimitives.Point> points, final CameraRenderState camera) {
|
| 158 | if (!points.isEmpty()) {
|
| 159 | VertexConsumer builder = this.getVertexBuilder(RenderTypes.debugPoint());
|
| 160 | PoseStack.Pose pose = this.poseStack.last();
|
| 161 | double camX = camera.pos.x();
|
| 162 | double camY = camera.pos.y();
|
| 163 | double camZ = camera.pos.z();
|
| 164 |
|
| 165 | for (DrawableGizmoPrimitives.Point point : points) {
|
| 166 | Vec3 pos = point.pos();
|
| 167 | builder.addVertex(pose, (float)(pos.x() - camX), (float)(pos.y() - camY), (float)(pos.z() - camZ)).setColor(point.color()).setLineWidth(point.size());
|
| 168 | }
|
| 169 | }
|
| 170 | }
|
| 171 |
|
| 172 | @Environment(EnvType.CLIENT)
|
| 173 | public record Submit(DrawableGizmoPrimitives.Group group, CameraRenderState camera) implements SubmitNode {
|
| 174 | @Override
|
| 175 | public FeatureRendererType<GizmoFeatureRenderer.Submit> featureType() {
|
| 176 | return GizmoFeatureRenderer.TYPE;
|
| 177 | }
|
| 178 | }
|
| 179 | }
|
| 180 |
|