| 1 | package com.mojang.blaze3d.vertex;
|
| 2 |
|
| 3 | import net.fabricmc.api.EnvType;
|
| 4 | import net.fabricmc.api.Environment;
|
| 5 | import net.minecraft.core.Direction;
|
| 6 | import org.joml.Matrix3f;
|
| 7 | import org.joml.Matrix3fc;
|
| 8 | import org.joml.Matrix4f;
|
| 9 | import org.joml.Matrix4fc;
|
| 10 | import org.joml.Vector3f;
|
| 11 |
|
| 12 | @Environment(EnvType.CLIENT)
|
| 13 | public class SheetedDecalTextureGenerator implements VertexConsumer {
|
| 14 | private final VertexConsumer delegate;
|
| 15 | private final Matrix4f cameraInversePose;
|
| 16 | private final Matrix3f normalInversePose;
|
| 17 | private final float textureScale;
|
| 18 | private final Vector3f worldPos = new Vector3f();
|
| 19 | private final Vector3f normal = new Vector3f();
|
| 20 | private float x;
|
| 21 | private float y;
|
| 22 | private float z;
|
| 23 |
|
| 24 | public SheetedDecalTextureGenerator(final VertexConsumer delegate, final PoseStack.Pose cameraPose, final float textureScale) {
|
| 25 | this.delegate = delegate;
|
| 26 | this.cameraInversePose = new Matrix4f(cameraPose.pose()).invert();
|
| 27 | this.normalInversePose = new Matrix3f(cameraPose.normal()).invert();
|
| 28 | this.textureScale = textureScale;
|
| 29 | }
|
| 30 |
|
| 31 | @Override
|
| 32 | public VertexConsumer addVertex(final float x, final float y, final float z) {
|
| 33 | this.x = x;
|
| 34 | this.y = y;
|
| 35 | this.z = z;
|
| 36 | this.delegate.addVertex(x, y, z);
|
| 37 | return this;
|
| 38 | }
|
| 39 |
|
| 40 | @Override
|
| 41 | public VertexConsumer setColor(final int r, final int g, final int b, final int a) {
|
| 42 | this.delegate.setColor(-1);
|
| 43 | return this;
|
| 44 | }
|
| 45 |
|
| 46 | @Override
|
| 47 | public VertexConsumer setColor(final int color) {
|
| 48 | this.delegate.setColor(-1);
|
| 49 | return this;
|
| 50 | }
|
| 51 |
|
| 52 | @Override
|
| 53 | public VertexConsumer setUv(final float u, final float v) {
|
| 54 | return this;
|
| 55 | }
|
| 56 |
|
| 57 | @Override
|
| 58 | public VertexConsumer setUv1(final int u, final int v) {
|
| 59 | this.delegate.setUv1(u, v);
|
| 60 | return this;
|
| 61 | }
|
| 62 |
|
| 63 | @Override
|
| 64 | public VertexConsumer setUv2(final int u, final int v) {
|
| 65 | this.delegate.setUv2(u, v);
|
| 66 | return this;
|
| 67 | }
|
| 68 |
|
| 69 | @Override
|
| 70 | public VertexConsumer setUv3(final float u, final float v) {
|
| 71 | this.delegate.setUv3(u, v);
|
| 72 | return this;
|
| 73 | }
|
| 74 |
|
| 75 | @Override
|
| 76 | public VertexConsumer setNormal(final float x, final float y, final float z) {
|
| 77 | this.delegate.setNormal(x, y, z);
|
| 78 | Vector3f normal = this.normalInversePose.transform(x, y, z, this.normal);
|
| 79 | Direction direction = Direction.getApproximateNearest(normal.x(), normal.y(), normal.z());
|
| 80 | Vector3f worldPos = this.cameraInversePose.transformPosition(this.x, this.y, this.z, this.worldPos);
|
| 81 | worldPos.rotateY((float) Math.PI);
|
| 82 | worldPos.rotateX((float) (-Math.PI / 2));
|
| 83 | worldPos.rotate(direction.getRotation());
|
| 84 | this.delegate.setUv(-worldPos.x() * this.textureScale, -worldPos.y() * this.textureScale);
|
| 85 | return this;
|
| 86 | }
|
| 87 |
|
| 88 | public static void setSheetedDecalUv(
|
| 89 | final Vector3f position,
|
| 90 | final Vector3f normal,
|
| 91 | final Matrix4fc cameraInversePose,
|
| 92 | final Matrix3fc normalInversePose,
|
| 93 | final float textureScale,
|
| 94 | final VertexConsumer delegate
|
| 95 | ) {
|
| 96 | Vector3f transformedNormal = normalInversePose.transform(normal.x(), normal.y(), normal.z(), new Vector3f());
|
| 97 | Direction direction = Direction.getApproximateNearest(transformedNormal.x(), transformedNormal.y(), transformedNormal.z());
|
| 98 | Vector3f worldPos = cameraInversePose.transformPosition(position.x, position.y, position.z, new Vector3f());
|
| 99 | worldPos.rotateY((float) Math.PI);
|
| 100 | worldPos.rotateX((float) (-Math.PI / 2));
|
| 101 | worldPos.rotate(direction.getRotation());
|
| 102 | delegate.setUv3(-worldPos.x() * textureScale, -worldPos.y() * textureScale);
|
| 103 | }
|
| 104 |
|
| 105 | @Override
|
| 106 | public VertexConsumer setLineWidth(final float width) {
|
| 107 | this.delegate.setLineWidth(width);
|
| 108 | return this;
|
| 109 | }
|
| 110 | }
|
| 111 |
|