| 1 | package net.minecraft.client.model.geom;
|
| 2 |
|
| 3 | import com.mojang.blaze3d.vertex.PoseStack;
|
| 4 | import com.mojang.blaze3d.vertex.VertexConsumer;
|
| 5 | import java.util.ArrayList;
|
| 6 | import java.util.HashMap;
|
| 7 | import java.util.List;
|
| 8 | import java.util.Map;
|
| 9 | import java.util.NoSuchElementException;
|
| 10 | import java.util.Set;
|
| 11 | import java.util.Map.Entry;
|
| 12 | import java.util.function.BiConsumer;
|
| 13 | import java.util.function.Consumer;
|
| 14 | import java.util.function.Function;
|
| 15 | import net.fabricmc.api.EnvType;
|
| 16 | import net.fabricmc.api.Environment;
|
| 17 | import net.minecraft.core.Direction;
|
| 18 | import net.minecraft.core.Direction.Axis;
|
| 19 | import net.minecraft.util.RandomSource;
|
| 20 | import net.minecraft.util.Util;
|
| 21 | import org.joml.Matrix3f;
|
| 22 | import org.joml.Matrix4f;
|
| 23 | import org.joml.Quaternionf;
|
| 24 | import org.joml.Vector3f;
|
| 25 | import org.joml.Vector3fc;
|
| 26 | import org.jspecify.annotations.Nullable;
|
| 27 |
|
| 28 | @Environment(EnvType.CLIENT)
|
| 29 | public final class ModelPart {
|
| 30 | public static final float DEFAULT_SCALE = 1.0F;
|
| 31 | public float x;
|
| 32 | public float y;
|
| 33 | public float z;
|
| 34 | public float xRot;
|
| 35 | public float yRot;
|
| 36 | public float zRot;
|
| 37 | public float xScale = 1.0F;
|
| 38 | public float yScale = 1.0F;
|
| 39 | public float zScale = 1.0F;
|
| 40 | public boolean visible = true;
|
| 41 | public boolean skipDraw;
|
| 42 | private final List<ModelPart.Cube> cubes;
|
| 43 | private final Map<String, ModelPart> children;
|
| 44 | private PartPose initialPose = PartPose.ZERO;
|
| 45 |
|
| 46 | public ModelPart(final List<ModelPart.Cube> cubes, final Map<String, ModelPart> children) {
|
| 47 | this.cubes = cubes;
|
| 48 | this.children = children;
|
| 49 | }
|
| 50 |
|
| 51 | public PartPose storePose() {
|
| 52 | return PartPose.offsetAndRotation(this.x, this.y, this.z, this.xRot, this.yRot, this.zRot);
|
| 53 | }
|
| 54 |
|
| 55 | public PartPose getInitialPose() {
|
| 56 | return this.initialPose;
|
| 57 | }
|
| 58 |
|
| 59 | public void setInitialPose(final PartPose initialPose) {
|
| 60 | this.initialPose = initialPose;
|
| 61 | }
|
| 62 |
|
| 63 | public void resetPose() {
|
| 64 | this.loadPose(this.initialPose);
|
| 65 | }
|
| 66 |
|
| 67 | public void loadPose(final PartPose pose) {
|
| 68 | this.x = pose.x();
|
| 69 | this.y = pose.y();
|
| 70 | this.z = pose.z();
|
| 71 | this.xRot = pose.xRot();
|
| 72 | this.yRot = pose.yRot();
|
| 73 | this.zRot = pose.zRot();
|
| 74 | this.xScale = pose.xScale();
|
| 75 | this.yScale = pose.yScale();
|
| 76 | this.zScale = pose.zScale();
|
| 77 | }
|
| 78 |
|
| 79 | public boolean hasChild(final String name) {
|
| 80 | return this.children.containsKey(name);
|
| 81 | }
|
| 82 |
|
| 83 | public ModelPart getChild(final String name) {
|
| 84 | ModelPart result = this.children.get(name);
|
| 85 | if (result == null) {
|
| 86 | throw new NoSuchElementException("Can't find part " + name);
|
| 87 | } else {
|
| 88 | return result;
|
| 89 | }
|
| 90 | }
|
| 91 |
|
| 92 | public void setPos(final float x, final float y, final float z) {
|
| 93 | this.x = x;
|
| 94 | this.y = y;
|
| 95 | this.z = z;
|
| 96 | }
|
| 97 |
|
| 98 | public void setRotation(final float xRot, final float yRot, final float zRot) {
|
| 99 | this.xRot = xRot;
|
| 100 | this.yRot = yRot;
|
| 101 | this.zRot = zRot;
|
| 102 | }
|
| 103 |
|
| 104 | public void render(final PoseStack poseStack, final VertexConsumer buffer, final int lightCoords, final int overlayCoords) {
|
| 105 | this.render(poseStack, buffer, lightCoords, overlayCoords, -1);
|
| 106 | }
|
| 107 |
|
| 108 | public void render(final PoseStack poseStack, final VertexConsumer buffer, final int lightCoords, final int overlayCoords, final int color) {
|
| 109 | if (this.visible) {
|
| 110 | if (!this.cubes.isEmpty() || !this.children.isEmpty()) {
|
| 111 | poseStack.pushPose();
|
| 112 | this.translateAndRotate(poseStack);
|
| 113 | if (!this.skipDraw) {
|
| 114 | this.compile(poseStack.last(), buffer, lightCoords, overlayCoords, color);
|
| 115 | }
|
| 116 |
|
| 117 | for (ModelPart child : this.children.values()) {
|
| 118 | child.render(poseStack, buffer, lightCoords, overlayCoords, color);
|
| 119 | }
|
| 120 |
|
| 121 | poseStack.popPose();
|
| 122 | }
|
| 123 | }
|
| 124 | }
|
| 125 |
|
| 126 | public void rotateBy(final Quaternionf rotation) {
|
| 127 | Matrix3f oldRotation = new Matrix3f().rotationZYX(this.zRot, this.yRot, this.xRot);
|
| 128 | Matrix3f newRotation = oldRotation.rotate(rotation);
|
| 129 | Vector3f newAngles = newRotation.getEulerAnglesZYX(new Vector3f());
|
| 130 | this.setRotation(newAngles.x, newAngles.y, newAngles.z);
|
| 131 | }
|
| 132 |
|
| 133 | public void getExtentsForGui(final PoseStack poseStack, final Consumer<Vector3fc> output) {
|
| 134 | this.visit(poseStack, (pose, partPath, cubeIndex, cube) -> {
|
| 135 | for (ModelPart.Polygon polygon : cube.polygons) {
|
| 136 | for (ModelPart.Vertex vertex : polygon.vertices()) {
|
| 137 | float x = vertex.worldX();
|
| 138 | float y = vertex.worldY();
|
| 139 | float z = vertex.worldZ();
|
| 140 | Vector3f pos = pose.pose().transformPosition(x, y, z, new Vector3f());
|
| 141 | output.accept(pos);
|
| 142 | }
|
| 143 | }
|
| 144 | });
|
| 145 | }
|
| 146 |
|
| 147 | public void visit(final PoseStack poseStack, final ModelPart.Visitor visitor) {
|
| 148 | this.visit(poseStack, visitor, "");
|
| 149 | }
|
| 150 |
|
| 151 | private void visit(final PoseStack poseStack, final ModelPart.Visitor visitor, final String path) {
|
| 152 | if (!this.cubes.isEmpty() || !this.children.isEmpty()) {
|
| 153 | poseStack.pushPose();
|
| 154 | this.translateAndRotate(poseStack);
|
| 155 | PoseStack.Pose pose = poseStack.last();
|
| 156 |
|
| 157 | for (int i = 0; i < this.cubes.size(); i++) {
|
| 158 | visitor.visit(pose, path, i, this.cubes.get(i));
|
| 159 | }
|
| 160 |
|
| 161 | String childPath = path + "/";
|
| 162 | this.children.forEach((name, child) -> child.visit(poseStack, visitor, childPath + name));
|
| 163 | poseStack.popPose();
|
| 164 | }
|
| 165 | }
|
| 166 |
|
| 167 | public void translateAndRotate(final PoseStack poseStack) {
|
| 168 | poseStack.translate(this.x / 16.0F, this.y / 16.0F, this.z / 16.0F);
|
| 169 | if (this.xRot != 0.0F || this.yRot != 0.0F || this.zRot != 0.0F) {
|
| 170 | poseStack.rotate(new Quaternionf().rotationZYX(this.zRot, this.yRot, this.xRot));
|
| 171 | }
|
| 172 |
|
| 173 | if (this.xScale != 1.0F || this.yScale != 1.0F || this.zScale != 1.0F) {
|
| 174 | poseStack.scale(this.xScale, this.yScale, this.zScale);
|
| 175 | }
|
| 176 | }
|
| 177 |
|
| 178 | private void compile(final PoseStack.Pose pose, final VertexConsumer builder, final int lightCoords, final int overlayCoords, final int color) {
|
| 179 | for (ModelPart.Cube cube : this.cubes) {
|
| 180 | cube.compile(pose, builder, lightCoords, overlayCoords, color);
|
| 181 | }
|
| 182 | }
|
| 183 |
|
| 184 | public ModelPart.Cube getRandomCube(final RandomSource random) {
|
| 185 | return Util.getRandom(this.cubes, random);
|
| 186 | }
|
| 187 |
|
| 188 | public boolean isEmpty() {
|
| 189 | return this.cubes.isEmpty();
|
| 190 | }
|
| 191 |
|
| 192 | public void offsetPos(final Vector3f offset) {
|
| 193 | this.x = this.x + offset.x();
|
| 194 | this.y = this.y + offset.y();
|
| 195 | this.z = this.z + offset.z();
|
| 196 | }
|
| 197 |
|
| 198 | public void offsetRotation(final Vector3f offset) {
|
| 199 | this.xRot = this.xRot + offset.x();
|
| 200 | this.yRot = this.yRot + offset.y();
|
| 201 | this.zRot = this.zRot + offset.z();
|
| 202 | }
|
| 203 |
|
| 204 | public void offsetScale(final Vector3f offset) {
|
| 205 | this.xScale = this.xScale + offset.x();
|
| 206 | this.yScale = this.yScale + offset.y();
|
| 207 | this.zScale = this.zScale + offset.z();
|
| 208 | }
|
| 209 |
|
| 210 | public List<ModelPart> getAllParts() {
|
| 211 | List<ModelPart> allParts = new ArrayList<>();
|
| 212 | allParts.add(this);
|
| 213 | this.addAllChildren((name, part) -> allParts.add(part));
|
| 214 | return List.copyOf(allParts);
|
| 215 | }
|
| 216 |
|
| 217 | public Function<String, @Nullable ModelPart> createPartLookup() {
|
| 218 | Map<String, ModelPart> parts = new HashMap<>();
|
| 219 | parts.put("root", this);
|
| 220 | this.addAllChildren(parts::putIfAbsent);
|
| 221 | return parts::get;
|
| 222 | }
|
| 223 |
|
| 224 | private void addAllChildren(final BiConsumer<String, ModelPart> output) {
|
| 225 | for (Entry<String, ModelPart> entry : this.children.entrySet()) {
|
| 226 | output.accept(entry.getKey(), entry.getValue());
|
| 227 | }
|
| 228 |
|
| 229 | for (ModelPart part : this.children.values()) {
|
| 230 | part.addAllChildren(output);
|
| 231 | }
|
| 232 | }
|
| 233 |
|
| 234 | @Environment(EnvType.CLIENT)
|
| 235 | public static class Cube {
|
| 236 | public final ModelPart.Polygon[] polygons;
|
| 237 | public final float minX;
|
| 238 | public final float minY;
|
| 239 | public final float minZ;
|
| 240 | public final float maxX;
|
| 241 | public final float maxY;
|
| 242 | public final float maxZ;
|
| 243 |
|
| 244 | public Cube(
|
| 245 | final int xTexOffs,
|
| 246 | final int yTexOffs,
|
| 247 | float minX,
|
| 248 | float minY,
|
| 249 | float minZ,
|
| 250 | final float width,
|
| 251 | final float height,
|
| 252 | final float depth,
|
| 253 | final float growX,
|
| 254 | final float growY,
|
| 255 | final float growZ,
|
| 256 | final boolean mirror,
|
| 257 | final float xTexSize,
|
| 258 | final float yTexSize,
|
| 259 | final Set<Direction> visibleFaces
|
| 260 | ) {
|
| 261 | this.minX = minX;
|
| 262 | this.minY = minY;
|
| 263 | this.minZ = minZ;
|
| 264 | this.maxX = minX + width;
|
| 265 | this.maxY = minY + height;
|
| 266 | this.maxZ = minZ + depth;
|
| 267 | this.polygons = new ModelPart.Polygon[visibleFaces.size()];
|
| 268 | float maxX = minX + width;
|
| 269 | float maxY = minY + height;
|
| 270 | float maxZ = minZ + depth;
|
| 271 | minX -= growX;
|
| 272 | minY -= growY;
|
| 273 | minZ -= growZ;
|
| 274 | maxX += growX;
|
| 275 | maxY += growY;
|
| 276 | maxZ += growZ;
|
| 277 | if (mirror) {
|
| 278 | float tmp = maxX;
|
| 279 | maxX = minX;
|
| 280 | minX = tmp;
|
| 281 | }
|
| 282 |
|
| 283 | ModelPart.Vertex t0 = new ModelPart.Vertex(minX, minY, minZ, 0.0F, 0.0F);
|
| 284 | ModelPart.Vertex t1 = new ModelPart.Vertex(maxX, minY, minZ, 0.0F, 8.0F);
|
| 285 | ModelPart.Vertex t2 = new ModelPart.Vertex(maxX, maxY, minZ, 8.0F, 8.0F);
|
| 286 | ModelPart.Vertex t3 = new ModelPart.Vertex(minX, maxY, minZ, 8.0F, 0.0F);
|
| 287 | ModelPart.Vertex l0 = new ModelPart.Vertex(minX, minY, maxZ, 0.0F, 0.0F);
|
| 288 | ModelPart.Vertex l1 = new ModelPart.Vertex(maxX, minY, maxZ, 0.0F, 8.0F);
|
| 289 | ModelPart.Vertex l2 = new ModelPart.Vertex(maxX, maxY, maxZ, 8.0F, 8.0F);
|
| 290 | ModelPart.Vertex l3 = new ModelPart.Vertex(minX, maxY, maxZ, 8.0F, 0.0F);
|
| 291 | float u0 = xTexOffs;
|
| 292 | float u1 = xTexOffs + depth;
|
| 293 | float u2 = xTexOffs + depth + width;
|
| 294 | float u22 = xTexOffs + depth + width + width;
|
| 295 | float u3 = xTexOffs + depth + width + depth;
|
| 296 | float u4 = xTexOffs + depth + width + depth + width;
|
| 297 | float v0 = yTexOffs;
|
| 298 | float v1 = yTexOffs + depth;
|
| 299 | float v2 = yTexOffs + depth + height;
|
| 300 | int pos = 0;
|
| 301 | if (visibleFaces.contains(Direction.DOWN)) {
|
| 302 | this.polygons[pos++] = new ModelPart.Polygon(new ModelPart.Vertex[]{l1, l0, t0, t1}, u1, v0, u2, v1, xTexSize, yTexSize, mirror, Direction.DOWN);
|
| 303 | }
|
| 304 |
|
| 305 | if (visibleFaces.contains(Direction.UP)) {
|
| 306 | this.polygons[pos++] = new ModelPart.Polygon(new ModelPart.Vertex[]{t2, t3, l3, l2}, u2, v1, u22, v0, xTexSize, yTexSize, mirror, Direction.UP);
|
| 307 | }
|
| 308 |
|
| 309 | if (visibleFaces.contains(Direction.WEST)) {
|
| 310 | this.polygons[pos++] = new ModelPart.Polygon(new ModelPart.Vertex[]{t0, l0, l3, t3}, u0, v1, u1, v2, xTexSize, yTexSize, mirror, Direction.WEST);
|
| 311 | }
|
| 312 |
|
| 313 | if (visibleFaces.contains(Direction.NORTH)) {
|
| 314 | this.polygons[pos++] = new ModelPart.Polygon(new ModelPart.Vertex[]{t1, t0, t3, t2}, u1, v1, u2, v2, xTexSize, yTexSize, mirror, Direction.NORTH);
|
| 315 | }
|
| 316 |
|
| 317 | if (visibleFaces.contains(Direction.EAST)) {
|
| 318 | this.polygons[pos++] = new ModelPart.Polygon(new ModelPart.Vertex[]{l1, t1, t2, l2}, u2, v1, u3, v2, xTexSize, yTexSize, mirror, Direction.EAST);
|
| 319 | }
|
| 320 |
|
| 321 | if (visibleFaces.contains(Direction.SOUTH)) {
|
| 322 | this.polygons[pos] = new ModelPart.Polygon(new ModelPart.Vertex[]{l0, l1, l2, l3}, u3, v1, u4, v2, xTexSize, yTexSize, mirror, Direction.SOUTH);
|
| 323 | }
|
| 324 | }
|
| 325 |
|
| 326 | public void compile(final PoseStack.Pose pose, final VertexConsumer builder, final int lightCoords, final int overlayCoords, final int color) {
|
| 327 | Matrix4f matrix = pose.pose();
|
| 328 | Vector3f scratchVector = new Vector3f();
|
| 329 |
|
| 330 | for (ModelPart.Polygon polygon : this.polygons) {
|
| 331 | Vector3f normal = pose.transformNormal(polygon.normal, scratchVector);
|
| 332 | float nx = normal.x();
|
| 333 | float ny = normal.y();
|
| 334 | float nz = normal.z();
|
| 335 |
|
| 336 | for (ModelPart.Vertex vertex : polygon.vertices) {
|
| 337 | float x = vertex.worldX();
|
| 338 | float y = vertex.worldY();
|
| 339 | float z = vertex.worldZ();
|
| 340 | Vector3f pos = matrix.transformPosition(x, y, z, scratchVector);
|
| 341 | builder.addVertex(pos.x(), pos.y(), pos.z(), color, vertex.u, vertex.v, overlayCoords, lightCoords, nx, ny, nz);
|
| 342 | }
|
| 343 | }
|
| 344 | }
|
| 345 | }
|
| 346 |
|
| 347 | /**
|
| 348 | * Access widened by fabric-transitive-access-wideners-v1 to accessible
|
| 349 | */
|
| 350 | @Environment(EnvType.CLIENT)
|
| 351 | public record Polygon(ModelPart.Vertex[] vertices, Vector3fc normal) {
|
| 352 | public Polygon(
|
| 353 | final ModelPart.Vertex[] vertices,
|
| 354 | final float u0,
|
| 355 | final float v0,
|
| 356 | final float u1,
|
| 357 | final float v1,
|
| 358 | final float xTexSize,
|
| 359 | final float yTexSize,
|
| 360 | final boolean mirror,
|
| 361 | final Direction facing
|
| 362 | ) {
|
| 363 | this(vertices, (mirror ? mirrorFacing(facing) : facing).getUnitVec3f());
|
| 364 | float us = 0.0F / xTexSize;
|
| 365 | float vs = 0.0F / yTexSize;
|
| 366 | vertices[0] = vertices[0].remap(u1 / xTexSize - us, v0 / yTexSize + vs);
|
| 367 | vertices[1] = vertices[1].remap(u0 / xTexSize + us, v0 / yTexSize + vs);
|
| 368 | vertices[2] = vertices[2].remap(u0 / xTexSize + us, v1 / yTexSize - vs);
|
| 369 | vertices[3] = vertices[3].remap(u1 / xTexSize - us, v1 / yTexSize - vs);
|
| 370 | if (mirror) {
|
| 371 | int length = vertices.length;
|
| 372 |
|
| 373 | for (int i = 0; i < length / 2; i++) {
|
| 374 | ModelPart.Vertex tmp = vertices[i];
|
| 375 | vertices[i] = vertices[length - 1 - i];
|
| 376 | vertices[length - 1 - i] = tmp;
|
| 377 | }
|
| 378 | }
|
| 379 | }
|
| 380 |
|
| 381 | private static Direction mirrorFacing(final Direction facing) {
|
| 382 | return facing.getAxis() == Axis.X ? facing.getOpposite() : facing;
|
| 383 | }
|
| 384 | }
|
| 385 |
|
| 386 | /**
|
| 387 | * Access widened by fabric-transitive-access-wideners-v1 to accessible
|
| 388 | */
|
| 389 | @Environment(EnvType.CLIENT)
|
| 390 | public record Vertex(float x, float y, float z, float u, float v) {
|
| 391 | public static final float SCALE_FACTOR = 16.0F;
|
| 392 |
|
| 393 | public ModelPart.Vertex remap(final float u, final float v) {
|
| 394 | return new ModelPart.Vertex(this.x, this.y, this.z, u, v);
|
| 395 | }
|
| 396 |
|
| 397 | public float worldX() {
|
| 398 | return this.x / 16.0F;
|
| 399 | }
|
| 400 |
|
| 401 | public float worldY() {
|
| 402 | return this.y / 16.0F;
|
| 403 | }
|
| 404 |
|
| 405 | public float worldZ() {
|
| 406 | return this.z / 16.0F;
|
| 407 | }
|
| 408 | }
|
| 409 |
|
| 410 | @FunctionalInterface
|
| 411 | @Environment(EnvType.CLIENT)
|
| 412 | public interface Visitor {
|
| 413 | void visit(final PoseStack.Pose pose, final String partPath, final int cubeIndex, final ModelPart.Cube cube);
|
| 414 | }
|
| 415 | }
|
| 416 |
|