DrawableGizmoPrimitives.java

net.minecraft.client.renderer.gizmos.DrawableGizmoPrimitives

信息

  • 全限定名:net.minecraft.client.renderer.gizmos.DrawableGizmoPrimitives
  • 类型:public class
  • 包:net.minecraft.client.renderer.gizmos
  • 源码路径:src/main/java/net/minecraft/client/renderer/gizmos/DrawableGizmoPrimitives.java
  • 起始行号:L23
  • 实现:GizmoPrimitives
  • 职责:

    TODO

字段/常量

  • opaque

    • 类型: DrawableGizmoPrimitives.Group
    • 修饰符: private final
    • 源码定位: L24
    • 说明:

      TODO

  • translucent

    • 类型: DrawableGizmoPrimitives.Group
    • 修饰符: private final
    • 源码定位: L25
    • 说明:

      TODO

  • isEmpty

    • 类型: boolean
    • 修饰符: private
    • 源码定位: L26
    • 说明:

      TODO

内部类/嵌套类型

  • net.minecraft.client.renderer.gizmos.DrawableGizmoPrimitives.Group

    • 类型: record
    • 修饰符: private
    • 源码定位: L72
    • 说明:

      TODO

  • net.minecraft.client.renderer.gizmos.DrawableGizmoPrimitives.Line

    • 类型: record
    • 修饰符: private
    • 源码定位: L213
    • 说明:

      TODO

  • net.minecraft.client.renderer.gizmos.DrawableGizmoPrimitives.Point

    • 类型: record
    • 修饰符: private
    • 源码定位: L217
    • 说明:

      TODO

  • net.minecraft.client.renderer.gizmos.DrawableGizmoPrimitives.Quad

    • 类型: record
    • 修饰符: private
    • 源码定位: L221
    • 说明:

      TODO

  • net.minecraft.client.renderer.gizmos.DrawableGizmoPrimitives.Text

    • 类型: record
    • 修饰符: private
    • 源码定位: L225
    • 说明:

      TODO

  • net.minecraft.client.renderer.gizmos.DrawableGizmoPrimitives.TriangleFan

    • 类型: record
    • 修饰符: private
    • 源码定位: L229
    • 说明:

      TODO

构造器

方法

下面的方法块按源码顺序生成。

private DrawableGizmoPrimitives.Group getGroup(int color) @ L28

  • 方法名:getGroup
  • 源码定位:L28
  • 返回类型:DrawableGizmoPrimitives.Group
  • 修饰符:private

参数:

  • color: int

说明:

TODO

public void addPoint(Vec3 pos, int color, float size) @ L32

  • 方法名:addPoint
  • 源码定位:L32
  • 返回类型:void
  • 修饰符:public

参数:

  • pos: Vec3
  • color: int
  • size: float

说明:

TODO

public void addLine(Vec3 start, Vec3 end, int color, float width) @ L38

  • 方法名:addLine
  • 源码定位:L38
  • 返回类型:void
  • 修饰符:public

参数:

  • start: Vec3
  • end: Vec3
  • color: int
  • width: float

说明:

TODO

public void addTriangleFan(Vec3[] points, int color) @ L44

  • 方法名:addTriangleFan
  • 源码定位:L44
  • 返回类型:void
  • 修饰符:public

参数:

  • points: Vec3[]
  • color: int

说明:

TODO

public void addQuad(Vec3 a, Vec3 b, Vec3 c, Vec3 d, int color) @ L50

  • 方法名:addQuad
  • 源码定位:L50
  • 返回类型:void
  • 修饰符:public

参数:

  • a: Vec3
  • b: Vec3
  • c: Vec3
  • d: Vec3
  • color: int

说明:

TODO

public void addText(Vec3 pos, String text, TextGizmo.Style style) @ L56

  • 方法名:addText
  • 源码定位:L56
  • 返回类型:void
  • 修饰符:public

参数:

  • pos: Vec3
  • text: String
  • style: TextGizmo.Style

说明:

TODO

public void render(PoseStack poseStack, MultiBufferSource bufferSource, CameraRenderState camera, Matrix4fc modelViewMatrix) @ L62

  • 方法名:render
  • 源码定位:L62
  • 返回类型:void
  • 修饰符:public

参数:

  • poseStack: PoseStack
  • bufferSource: MultiBufferSource
  • camera: CameraRenderState
  • modelViewMatrix: Matrix4fc

说明:

TODO

public boolean isEmpty() @ L67

  • 方法名:isEmpty
  • 源码定位:L67
  • 返回类型:boolean
  • 修饰符:public

参数:

说明:

TODO

代码

@OnlyIn(Dist.CLIENT)
public class DrawableGizmoPrimitives implements GizmoPrimitives {
    private final DrawableGizmoPrimitives.Group opaque = new DrawableGizmoPrimitives.Group(true);
    private final DrawableGizmoPrimitives.Group translucent = new DrawableGizmoPrimitives.Group(false);
    private boolean isEmpty = true;
 
    private DrawableGizmoPrimitives.Group getGroup(int color) {
        return ARGB.alpha(color) < 255 ? this.translucent : this.opaque;
    }
 
    @Override
    public void addPoint(Vec3 pos, int color, float size) {
        this.getGroup(color).points.add(new DrawableGizmoPrimitives.Point(pos, color, size));
        this.isEmpty = false;
    }
 
    @Override
    public void addLine(Vec3 start, Vec3 end, int color, float width) {
        this.getGroup(color).lines.add(new DrawableGizmoPrimitives.Line(start, end, color, width));
        this.isEmpty = false;
    }
 
    @Override
    public void addTriangleFan(Vec3[] points, int color) {
        this.getGroup(color).triangleFans.add(new DrawableGizmoPrimitives.TriangleFan(points, color));
        this.isEmpty = false;
    }
 
    @Override
    public void addQuad(Vec3 a, Vec3 b, Vec3 c, Vec3 d, int color) {
        this.getGroup(color).quads.add(new DrawableGizmoPrimitives.Quad(a, b, c, d, color));
        this.isEmpty = false;
    }
 
    @Override
    public void addText(Vec3 pos, String text, TextGizmo.Style style) {
        this.getGroup(style.color()).texts.add(new DrawableGizmoPrimitives.Text(pos, text, style));
        this.isEmpty = false;
    }
 
    public void render(PoseStack poseStack, MultiBufferSource bufferSource, CameraRenderState camera, Matrix4fc modelViewMatrix) {
        this.opaque.render(poseStack, bufferSource, camera, modelViewMatrix);
        this.translucent.render(poseStack, bufferSource, camera, modelViewMatrix);
    }
 
    public boolean isEmpty() {
        return this.isEmpty;
    }
 
    @OnlyIn(Dist.CLIENT)
    private record Group(
        boolean opaque,
        List<DrawableGizmoPrimitives.Line> lines,
        List<DrawableGizmoPrimitives.Quad> quads,
        List<DrawableGizmoPrimitives.TriangleFan> triangleFans,
        List<DrawableGizmoPrimitives.Text> texts,
        List<DrawableGizmoPrimitives.Point> points
    ) {
        private Group(boolean opaque) {
            this(opaque, new ArrayList<>(), new ArrayList<>(), new ArrayList<>(), new ArrayList<>(), new ArrayList<>());
        }
 
        public void render(PoseStack poseStack, MultiBufferSource bufferSource, CameraRenderState camera, Matrix4fc modelViewMatrix) {
            this.renderQuads(poseStack, bufferSource, camera);
            this.renderTriangleFans(poseStack, bufferSource, camera);
            this.renderLines(poseStack, bufferSource, camera, modelViewMatrix);
            this.renderTexts(poseStack, bufferSource, camera);
            this.renderPoints(poseStack, bufferSource, camera);
        }
 
        private void renderTexts(PoseStack poseStack, MultiBufferSource bufferSource, CameraRenderState camera) {
            Minecraft minecraft = Minecraft.getInstance();
            Font font = minecraft.font;
            if (camera.initialized) {
                double camX = camera.pos.x();
                double camY = camera.pos.y();
                double camZ = camera.pos.z();
 
                for (DrawableGizmoPrimitives.Text text : this.texts) {
                    poseStack.pushPose();
                    poseStack.translate((float)(text.pos().x() - camX), (float)(text.pos().y() - camY), (float)(text.pos().z() - camZ));
                    poseStack.mulPose(camera.orientation);
                    poseStack.scale(text.style.scale() / 16.0F, -text.style.scale() / 16.0F, text.style.scale() / 16.0F);
                    float fontX;
                    if (text.style.adjustLeft().isEmpty()) {
                        fontX = -font.width(text.text) / 2.0F;
                    } else {
                        fontX = (float)(-text.style.adjustLeft().getAsDouble()) / text.style.scale();
                    }
 
                    font.drawInBatch(
                        text.text, fontX, 0.0F, text.style.color(), false, poseStack.last().pose(), bufferSource, Font.DisplayMode.NORMAL, 0, 15728880
                    );
                    poseStack.popPose();
                }
            }
        }
 
        private void renderLines(PoseStack poseStack, MultiBufferSource bufferSource, CameraRenderState camera, Matrix4fc modelViewMatrix) {
            VertexConsumer builder = bufferSource.getBuffer(this.opaque ? RenderTypes.lines() : RenderTypes.linesTranslucent());
            PoseStack.Pose pose = poseStack.last();
            Vector4f start = new Vector4f();
            Vector4f end = new Vector4f();
            Vector4f startViewSpace = new Vector4f();
            Vector4f endViewSpace = new Vector4f();
            Vector4f intersectionInWorld = new Vector4f();
            double camX = camera.pos.x();
            double camY = camera.pos.y();
            double camZ = camera.pos.z();
 
            for (DrawableGizmoPrimitives.Line line : this.lines) {
                start.set(line.start().x() - camX, line.start().y() - camY, line.start().z() - camZ, 1.0);
                end.set(line.end().x() - camX, line.end().y() - camY, line.end().z() - camZ, 1.0);
                start.mul(modelViewMatrix, startViewSpace);
                end.mul(modelViewMatrix, endViewSpace);
                boolean startIsBehindCamera = startViewSpace.z > -0.05F;
                boolean endIsBehindCamera = endViewSpace.z > -0.05F;
                if (!startIsBehindCamera || !endIsBehindCamera) {
                    if (startIsBehindCamera || endIsBehindCamera) {
                        float denom = endViewSpace.z - startViewSpace.z;
                        if (Math.abs(denom) < 1.0E-9F) {
                            continue;
                        }
 
                        float intersection = Mth.clamp((-0.05F - startViewSpace.z) / denom, 0.0F, 1.0F);
                        start.lerp(end, intersection, intersectionInWorld);
                        if (startIsBehindCamera) {
                            start.set(intersectionInWorld);
                        } else {
                            end.set(intersectionInWorld);
                        }
                    }
 
                    builder.addVertex(pose, start.x, start.y, start.z)
                        .setNormal(pose, end.x - start.x, end.y - start.y, end.z - start.z)
                        .setColor(line.color())
                        .setLineWidth(line.width());
                    builder.addVertex(pose, end.x, end.y, end.z)
                        .setNormal(pose, end.x - start.x, end.y - start.y, end.z - start.z)
                        .setColor(line.color())
                        .setLineWidth(line.width());
                }
            }
        }
 
        private void renderTriangleFans(PoseStack poseStack, MultiBufferSource bufferSource, CameraRenderState camera) {
            PoseStack.Pose pose = poseStack.last();
            double camX = camera.pos.x();
            double camY = camera.pos.y();
            double camZ = camera.pos.z();
 
            for (DrawableGizmoPrimitives.TriangleFan triangleFan : this.triangleFans) {
                VertexConsumer builder = bufferSource.getBuffer(RenderTypes.debugTriangleFan());
 
                for (Vec3 point : triangleFan.points()) {
                    builder.addVertex(pose, (float)(point.x() - camX), (float)(point.y() - camY), (float)(point.z() - camZ)).setColor(triangleFan.color());
                }
            }
        }
 
        private void renderQuads(PoseStack poseStack, MultiBufferSource bufferSource, CameraRenderState camera) {
            VertexConsumer builder = bufferSource.getBuffer(RenderTypes.debugFilledBox());
            PoseStack.Pose pose = poseStack.last();
            double camX = camera.pos.x();
            double camY = camera.pos.y();
            double camZ = camera.pos.z();
 
            for (DrawableGizmoPrimitives.Quad quad : this.quads) {
                builder.addVertex(pose, (float)(quad.a().x() - camX), (float)(quad.a().y() - camY), (float)(quad.a().z() - camZ)).setColor(quad.color());
                builder.addVertex(pose, (float)(quad.b().x() - camX), (float)(quad.b().y() - camY), (float)(quad.b().z() - camZ)).setColor(quad.color());
                builder.addVertex(pose, (float)(quad.c().x() - camX), (float)(quad.c().y() - camY), (float)(quad.c().z() - camZ)).setColor(quad.color());
                builder.addVertex(pose, (float)(quad.d().x() - camX), (float)(quad.d().y() - camY), (float)(quad.d().z() - camZ)).setColor(quad.color());
            }
        }
 
        private void renderPoints(PoseStack poseStack, MultiBufferSource bufferSource, CameraRenderState camera) {
            VertexConsumer builder = bufferSource.getBuffer(RenderTypes.debugPoint());
            PoseStack.Pose pose = poseStack.last();
            double camX = camera.pos.x();
            double camY = camera.pos.y();
            double camZ = camera.pos.z();
 
            for (DrawableGizmoPrimitives.Point point : this.points) {
                builder.addVertex(pose, (float)(point.pos.x() - camX), (float)(point.pos.y() - camY), (float)(point.pos.z() - camZ))
                    .setColor(point.color())
                    .setLineWidth(point.size());
            }
        }
    }
 
    @OnlyIn(Dist.CLIENT)
    private record Line(Vec3 start, Vec3 end, int color, float width) {
    }
 
    @OnlyIn(Dist.CLIENT)
    private record Point(Vec3 pos, int color, float size) {
    }
 
    @OnlyIn(Dist.CLIENT)
    private record Quad(Vec3 a, Vec3 b, Vec3 c, Vec3 d, int color) {
    }
 
    @OnlyIn(Dist.CLIENT)
    private record Text(Vec3 pos, String text, TextGizmo.Style style) {
    }
 
    @OnlyIn(Dist.CLIENT)
    private record TriangleFan(Vec3[] points, int color) {
    }
}

引用的其他类

  • PoseStack

    • 引用位置: 参数
  • Minecraft

    • 引用位置: 方法调用
    • 关联成员: Minecraft.getInstance()
  • MultiBufferSource

    • 引用位置: 参数
  • RenderTypes

    • 引用位置: 方法调用
    • 关联成员: RenderTypes.debugFilledBox(), RenderTypes.debugPoint(), RenderTypes.debugTriangleFan(), RenderTypes.lines(), RenderTypes.linesTranslucent()
  • CameraRenderState

    • 引用位置: 参数
  • GizmoPrimitives

    • 引用位置: 实现
  • TextGizmo

    • 引用位置: 参数
  • ARGB

    • 引用位置: 方法调用
    • 关联成员: ARGB.alpha()
  • Mth

    • 引用位置: 方法调用
    • 关联成员: Mth.clamp()
  • Vec3

    • 引用位置: 参数