| 1 | package com.mojang.blaze3d.vertex;
|
| 2 |
|
| 3 | import net.fabricmc.api.EnvType;
|
| 4 | import net.fabricmc.api.Environment;
|
| 5 | import net.minecraft.client.model.geom.builders.UVPair;
|
| 6 | import net.minecraft.client.resources.model.geometry.BakedQuad;
|
| 7 | import org.joml.Matrix3f;
|
| 8 | import org.joml.Matrix3x2fc;
|
| 9 | import org.joml.Matrix4f;
|
| 10 | import org.joml.Matrix4fc;
|
| 11 | import org.joml.Vector2f;
|
| 12 | import org.joml.Vector3f;
|
| 13 | import org.joml.Vector3fc;
|
| 14 |
|
| 15 | @Environment(EnvType.CLIENT)
|
| 16 | public interface VertexConsumer {
|
| 17 | VertexConsumer addVertex(float x, float y, float z);
|
| 18 |
|
| 19 | VertexConsumer setColor(int r, int g, int b, int a);
|
| 20 |
|
| 21 | VertexConsumer setColor(int color);
|
| 22 |
|
| 23 | VertexConsumer setUv(float u, float v);
|
| 24 |
|
| 25 | VertexConsumer setUv1(int u, int v);
|
| 26 |
|
| 27 | VertexConsumer setUv2(int u, int v);
|
| 28 |
|
| 29 | VertexConsumer setUv3(float u, float v);
|
| 30 |
|
| 31 | VertexConsumer setNormal(float x, float y, float z);
|
| 32 |
|
| 33 | VertexConsumer setLineWidth(float width);
|
| 34 |
|
| 35 | default void addVertex(
|
| 36 | final float x,
|
| 37 | final float y,
|
| 38 | final float z,
|
| 39 | final int color,
|
| 40 | final float u,
|
| 41 | final float v,
|
| 42 | final int overlayCoords,
|
| 43 | final int lightCoords,
|
| 44 | final float nx,
|
| 45 | final float ny,
|
| 46 | final float nz
|
| 47 | ) {
|
| 48 | this.addVertex(x, y, z);
|
| 49 | this.setColor(color);
|
| 50 | this.setUv(u, v);
|
| 51 | this.setOverlay(overlayCoords);
|
| 52 | this.setLight(lightCoords);
|
| 53 | this.setNormal(nx, ny, nz);
|
| 54 | }
|
| 55 |
|
| 56 | default VertexConsumer setColor(final float r, final float g, final float b, final float a) {
|
| 57 | return this.setColor((int)(r * 255.0F), (int)(g * 255.0F), (int)(b * 255.0F), (int)(a * 255.0F));
|
| 58 | }
|
| 59 |
|
| 60 | default VertexConsumer setLight(final int packedLightCoords) {
|
| 61 | return this.setUv2(packedLightCoords & 65535, packedLightCoords >> 16 & 65535);
|
| 62 | }
|
| 63 |
|
| 64 | default VertexConsumer setOverlay(final int packedOverlayCoords) {
|
| 65 | return this.setUv1(packedOverlayCoords & 65535, packedOverlayCoords >> 16 & 65535);
|
| 66 | }
|
| 67 |
|
| 68 | default void putBlockBakedQuad(final float x, final float y, final float z, final BakedQuad quad, final QuadInstance instance) {
|
| 69 | Vector3fc normal = quad.direction().getUnitVec3f();
|
| 70 | int lightEmission = quad.materialInfo().lightEmission();
|
| 71 |
|
| 72 | for (int vertex = 0; vertex < 4; vertex++) {
|
| 73 | Vector3fc pos = quad.position(vertex);
|
| 74 | long packedUv = quad.packedUV(vertex);
|
| 75 | int vertexColor = instance.getColor(vertex);
|
| 76 | int light = instance.getLightCoordsWithEmission(vertex, lightEmission);
|
| 77 | float u = UVPair.unpackU(packedUv);
|
| 78 | float v = UVPair.unpackV(packedUv);
|
| 79 | this.addVertex(pos.x() + x, pos.y() + y, pos.z() + z, vertexColor, u, v, instance.overlayCoords(), light, normal.x(), normal.y(), normal.z());
|
| 80 | }
|
| 81 | }
|
| 82 |
|
| 83 | default void putBakedQuad(final PoseStack.Pose pose, final BakedQuad quad, final QuadInstance instance) {
|
| 84 | Vector3fc normalVec = quad.direction().getUnitVec3f();
|
| 85 | Matrix4f matrix = pose.pose();
|
| 86 | Vector3f normal = pose.transformNormal(normalVec, new Vector3f());
|
| 87 | int lightEmission = quad.materialInfo().lightEmission();
|
| 88 |
|
| 89 | for (int vertex = 0; vertex < 4; vertex++) {
|
| 90 | Vector3fc position = quad.position(vertex);
|
| 91 | long packedUv = quad.packedUV(vertex);
|
| 92 | int vertexColor = instance.getColor(vertex);
|
| 93 | int light = instance.getLightCoordsWithEmission(vertex, lightEmission);
|
| 94 | Vector3f pos = matrix.transformPosition(position, new Vector3f());
|
| 95 | float u = UVPair.unpackU(packedUv);
|
| 96 | float v = UVPair.unpackV(packedUv);
|
| 97 | this.addVertex(pos.x(), pos.y(), pos.z(), vertexColor, u, v, instance.overlayCoords(), light, normal.x(), normal.y(), normal.z());
|
| 98 | }
|
| 99 | }
|
| 100 |
|
| 101 | default void putBakedQuadWithGlint(final PoseStack.Pose pose, final BakedQuad quad, final QuadInstance instance, final PoseStack.Pose sheetedDecalPose) {
|
| 102 | Vector3fc normalVec = quad.direction().getUnitVec3f();
|
| 103 | Matrix4f matrix = pose.pose();
|
| 104 | Vector3f normal = pose.transformNormal(normalVec, new Vector3f());
|
| 105 | int lightEmission = quad.materialInfo().lightEmission();
|
| 106 | Matrix4f cameraInversePose = new Matrix4f(sheetedDecalPose.pose()).invert();
|
| 107 | Matrix3f normalInversePose = new Matrix3f(sheetedDecalPose.normal()).invert();
|
| 108 |
|
| 109 | for (int vertex = 0; vertex < 4; vertex++) {
|
| 110 | Vector3fc position = quad.position(vertex);
|
| 111 | long packedUv = quad.packedUV(vertex);
|
| 112 | int vertexColor = instance.getColor(vertex);
|
| 113 | int light = instance.getLightCoordsWithEmission(vertex, lightEmission);
|
| 114 | Vector3f pos = matrix.transformPosition(position, new Vector3f());
|
| 115 | float u = UVPair.unpackU(packedUv);
|
| 116 | float v = UVPair.unpackV(packedUv);
|
| 117 | this.addVertex(pos.x(), pos.y(), pos.z(), vertexColor, u, v, instance.overlayCoords(), light, normal.x(), normal.y(), normal.z());
|
| 118 | SheetedDecalTextureGenerator.setSheetedDecalUv(pos, normal, cameraInversePose, normalInversePose, 0.0078125F, this);
|
| 119 | }
|
| 120 | }
|
| 121 |
|
| 122 | default VertexConsumer addVertex(final Vector3fc position) {
|
| 123 | return this.addVertex(position.x(), position.y(), position.z());
|
| 124 | }
|
| 125 |
|
| 126 | default VertexConsumer addVertex(final PoseStack.Pose pose, final Vector3fc position) {
|
| 127 | return this.addVertex(pose, position.x(), position.y(), position.z());
|
| 128 | }
|
| 129 |
|
| 130 | default VertexConsumer addVertex(final PoseStack.Pose pose, final float x, final float y, final float z) {
|
| 131 | return this.addVertex(pose.pose(), x, y, z);
|
| 132 | }
|
| 133 |
|
| 134 | default VertexConsumer addVertex(final Matrix4fc pose, final float x, final float y, final float z) {
|
| 135 | Vector3f pos = pose.transformPosition(x, y, z, new Vector3f());
|
| 136 | return this.addVertex(pos.x(), pos.y(), pos.z());
|
| 137 | }
|
| 138 |
|
| 139 | default VertexConsumer addVertexWith2DPose(final Matrix3x2fc pose, final float x, final float y) {
|
| 140 | Vector2f pos = pose.transformPosition(x, y, new Vector2f());
|
| 141 | return this.addVertex(pos.x(), pos.y(), 0.0F);
|
| 142 | }
|
| 143 |
|
| 144 | default VertexConsumer setNormal(final PoseStack.Pose pose, final float x, final float y, final float z) {
|
| 145 | Vector3f normal = pose.transformNormal(x, y, z, new Vector3f());
|
| 146 | return this.setNormal(normal.x(), normal.y(), normal.z());
|
| 147 | }
|
| 148 |
|
| 149 | default VertexConsumer setNormal(final PoseStack.Pose pose, final Vector3fc normal) {
|
| 150 | return this.setNormal(pose, normal.x(), normal.y(), normal.z());
|
| 151 | }
|
| 152 | }
|
| 153 |
|