From 7237480d04b9ed161966174bd355b292ae79ae52 Mon Sep 17 00:00:00 2001 From: Sollace Date: Sat, 28 Apr 2018 21:23:08 +0200 Subject: [PATCH] When you're sick and tired of staring at this unreadable code --- .../model/armour/ModelPonyArmor.java | 6 +- .../model/components/PonyElytra.java | 2 +- .../model/player/ModelEarthPony.java | 2 +- .../render/AbstractPonyRenderer.java | 2 +- .../render/plane/ModelPlane.java | 80 +++++++++---------- .../minelittlepony/util/coordinates/Box.java | 36 +++++++++ .../minelittlepony/util/coordinates/Quad.java | 10 +++ .../util/coordinates/Vertex.java | 27 +++++++ 8 files changed, 118 insertions(+), 47 deletions(-) create mode 100644 src/main/java/com/minelittlepony/util/coordinates/Box.java create mode 100644 src/main/java/com/minelittlepony/util/coordinates/Quad.java create mode 100644 src/main/java/com/minelittlepony/util/coordinates/Vertex.java diff --git a/src/main/java/com/minelittlepony/model/armour/ModelPonyArmor.java b/src/main/java/com/minelittlepony/model/armour/ModelPonyArmor.java index fe44b19f..fadc3f49 100644 --- a/src/main/java/com/minelittlepony/model/armour/ModelPonyArmor.java +++ b/src/main/java/com/minelittlepony/model/armour/ModelPonyArmor.java @@ -126,8 +126,8 @@ public class ModelPonyArmor extends ModelMobPony { bipedRightArm = new PonyRenderer(this, 0, 16); bipedRightLeg = new PonyRenderer(this, 0, 16); - bipedLeftArm = new PonyRenderer(this, 0, 16).mirror(); - bipedLeftLeg = new PonyRenderer(this, 0, 16).mirror(); + bipedLeftArm = new PonyRenderer(this, 0, 16).flipX(); + bipedLeftLeg = new PonyRenderer(this, 0, 16).flipX(); unicornArmRight = new PonyRenderer(this, 0, 16); unicornArmLeft = new PonyRenderer(this, 0, 16); @@ -175,7 +175,7 @@ public class ModelPonyArmor extends ModelMobPony { .box(-2, -6, -2, 4, 12, 4, stretch); rightLegging.offset(THIRDP_ARM_CENTRE_X, THIRDP_ARM_CENTRE_Y, THIRDP_ARM_CENTRE_Z) .around(-3, yOffset, 0) - .mirror().box(-2, -6, -2, 4, 12, 4, stretch); + .flipX().box(-2, -6, -2, 4, 12, 4, stretch); } protected void syncLegs() { diff --git a/src/main/java/com/minelittlepony/model/components/PonyElytra.java b/src/main/java/com/minelittlepony/model/components/PonyElytra.java index 8aef37f2..1f669a85 100644 --- a/src/main/java/com/minelittlepony/model/components/PonyElytra.java +++ b/src/main/java/com/minelittlepony/model/components/PonyElytra.java @@ -20,7 +20,7 @@ public class PonyElytra extends ModelBase { public PonyElytra() { leftWing .box(-10, 0, 0, 10, 20, 2, 1); - rightWing.mirror().box( 0, 0, 0, 10, 20, 2, 1); + rightWing.flipX().box( 0, 0, 0, 10, 20, 2, 1); } @Override diff --git a/src/main/java/com/minelittlepony/model/player/ModelEarthPony.java b/src/main/java/com/minelittlepony/model/player/ModelEarthPony.java index dbf83658..69e2eaef 100644 --- a/src/main/java/com/minelittlepony/model/player/ModelEarthPony.java +++ b/src/main/java/com/minelittlepony/model/player/ModelEarthPony.java @@ -514,7 +514,7 @@ public class ModelEarthPony extends AbstractPonyModel { .box(-4, -4, -4, 8, 8, 8, stretch) .tex(12, 16) .box(-4, -6, 1, 2, 2, 2, stretch) - .mirror() + .flipX() .box(2, -6, 1, 2, 2, 2, stretch); ((PonyRenderer)bipedHeadwear).offset(HEAD_CENTRE_X, HEAD_CENTRE_Y, HEAD_CENTRE_Z) diff --git a/src/main/java/com/minelittlepony/render/AbstractPonyRenderer.java b/src/main/java/com/minelittlepony/render/AbstractPonyRenderer.java index b13caa00..77e9d76b 100644 --- a/src/main/java/com/minelittlepony/render/AbstractPonyRenderer.java +++ b/src/main/java/com/minelittlepony/render/AbstractPonyRenderer.java @@ -43,7 +43,7 @@ public abstract class AbstractPonyRenderer> ex /** * Flips the mirror flag. All faces are mirrored until this is called again. */ - public T mirror() { + public T flipX() { return mirror(!mirror); } diff --git a/src/main/java/com/minelittlepony/render/plane/ModelPlane.java b/src/main/java/com/minelittlepony/render/plane/ModelPlane.java index d4beb59b..a1cedd46 100644 --- a/src/main/java/com/minelittlepony/render/plane/ModelPlane.java +++ b/src/main/java/com/minelittlepony/render/plane/ModelPlane.java @@ -1,75 +1,73 @@ package com.minelittlepony.render.plane; -import net.minecraft.client.model.ModelBox; -import net.minecraft.client.model.PositionTextureVertex; -import net.minecraft.client.model.TexturedQuad; import net.minecraft.client.renderer.BufferBuilder; import javax.annotation.Nonnull; -public class ModelPlane extends ModelBox { +import com.minelittlepony.util.coordinates.*; - private TexturedQuad quad; +public class ModelPlane extends Box { + + private Quad quad; public boolean hidden = false; - public ModelPlane(PlaneRenderer renderer, int textureX, int textureY, float x, float y, float z, int w, int h, int d, float scale, Face face) { - super(renderer, textureX, textureY, x, y, z, w, h, d, scale, false); + public ModelPlane(PlaneRenderer renderer, int textureX, int textureY, float xMin, float yMin, float zMin, int w, int h, int d, float scale, Face face) { + super(renderer, textureX, textureY, xMin, yMin, zMin, w, h, d, scale, false); - float x2 = x + w + scale; - float y2 = y + h + scale; - float z2 = z + d + scale; + float xMax = xMin + w + scale; + float yMax = yMin + h + scale; + float zMax = zMin + d + scale; - x -= scale; - y -= scale; - z -= scale; + xMin -= scale; + yMin -= scale; + zMin -= scale; if (renderer.mirror) { - float v = x2; - x2 = x; - x = v; + float v = xMax; + xMax = xMin; + xMin = v; } if (renderer.mirrory) { - float v = y2; - y2 = y; - y = v; + float v = yMax; + yMax = yMin; + yMin = v; } if (renderer.mirrorz) { - float v = z2; - z2 = z; - z = v; - + float v = zMax; + zMax = zMin; + zMin = v; } // w:west e:east d:down u:up s:south n:north - PositionTextureVertex wds = new PositionTextureVertex(x , y , z , 0, 0); - PositionTextureVertex eds = new PositionTextureVertex(x2, y , z , 0, 8); - PositionTextureVertex eus = new PositionTextureVertex(x2, y2, z , 8, 8); - PositionTextureVertex wus = new PositionTextureVertex(x , y2, z , 8, 0); - PositionTextureVertex wdn = new PositionTextureVertex(x , y , z2, 0, 0); - PositionTextureVertex edn = new PositionTextureVertex(x2, y , z2, 0, 8); - PositionTextureVertex eun = new PositionTextureVertex(x2, y2, z2, 8, 8); - PositionTextureVertex wun = new PositionTextureVertex(x , y2, z2, 8, 0); + Vertex wds = vert(xMin , yMin , zMin , 0, 0); + Vertex eds = vert(xMax, yMin , zMin , 0, 8); + Vertex eus = vert(xMax, yMax, zMin , 8, 8); + Vertex wus = vert(xMin , yMax, zMin , 8, 0); + Vertex wdn = vert(xMin , yMin , zMax, 0, 0); + Vertex edn = vert(xMax, yMin , zMax, 0, 8); + Vertex eun = vert(xMax, yMax, zMax, 8, 8); + Vertex wun = vert(xMin , yMax, zMax, 8, 0); - if (face == Face.EAST) { // North/Front (was East) - quad = new TexturedQuad(new PositionTextureVertex[]{edn, eds, eus, eun}, textureX, textureY, textureX + d, textureY + h, renderer.textureWidth, renderer.textureHeight); + if (face == Face.EAST) { + quad = quad(textureX, d, textureY, h, edn, eds, eus, eun); } - if (face == Face.WEST) { // South/Back (was West) - quad = new TexturedQuad(new PositionTextureVertex[]{wds, wdn, wun, wus}, textureX, textureY, textureX + d, textureY + h, renderer.textureWidth, renderer.textureHeight); + if (face == Face.WEST) { + quad = quad(textureX, d, textureY, h, wds, wdn, wun, wus); } - if (face == Face.UP) { // Up - quad = new TexturedQuad(new PositionTextureVertex[]{edn, wdn, wds, eds}, textureX, textureY, textureX + w, textureY + d, renderer.textureWidth, renderer.textureHeight); + if (face == Face.UP) { + quad = quad(textureX, w, textureY, h, edn, wdn, wds, eds); } - if (face == Face.DOWN) { // Down - quad = new TexturedQuad(new PositionTextureVertex[]{eus, wus, wun, eun}, textureX, textureY, textureX + w, textureY + d, renderer.textureWidth, renderer.textureHeight); + if (face == Face.DOWN) { + quad = quad(textureX, w, textureY, d, eus, wus, wun, eun); } if (face == Face.SOUTH) { // East/Left (was South) - quad = new TexturedQuad(new PositionTextureVertex[]{eds, wds, wus, eus}, textureX, textureY, textureX + w, textureY + h, renderer.textureWidth, renderer.textureHeight); + quad = quad(textureX, w, textureY, h, eds, wds, wus, eus); } if (face == Face.NORTH) { // West/Right (was North) - quad = new TexturedQuad(new PositionTextureVertex[]{wdn, edn, eun, wun}, textureX, textureY, textureX + w, textureY + h, renderer.textureWidth, renderer.textureHeight); + quad = quad(textureX, w, textureY, h, wdn, edn, eun, wun); } if (renderer.mirror || renderer.mirrory || renderer.mirrorz) { diff --git a/src/main/java/com/minelittlepony/util/coordinates/Box.java b/src/main/java/com/minelittlepony/util/coordinates/Box.java new file mode 100644 index 00000000..c735c0bd --- /dev/null +++ b/src/main/java/com/minelittlepony/util/coordinates/Box.java @@ -0,0 +1,36 @@ +package com.minelittlepony.util.coordinates; + +import net.minecraft.client.model.ModelBox; +import net.minecraft.client.model.ModelRenderer; + +public abstract class Box extends ModelBox { + + protected final T parent; + + public Box(T renderer, int texU, int texV, float x, float y, float z, int dx, int dy, int dz, float delta) { + super(renderer, texU, texV, x, y, z, dx, dy, dz, delta); + parent = renderer; + } + + public Box(T renderer, int texU, int texV, float x, float y, float z, int dx, int dy, int dz, float delta, boolean mirror) { + super(renderer, texU, texV, x, y, z, dx, dy, dz, delta, mirror); + parent = renderer; + } + + /** + * Creates a new vertex mapping the given (x, y, z) coordinates to a texture offset. + */ + protected Vertex vert(float x, float y, float z, int texX, int texY) { + return new Vertex(x, y, z, texX, texY); + } + + /** + * Creates a new quad with the given spacial vertices. + */ + protected Quad quad(int startX, int width, int startY, int height, Vertex ...verts) { + return new Quad(verts, + startX, startY, + startX + width, startY + height, + parent.textureWidth, parent.textureHeight); + } +} \ No newline at end of file diff --git a/src/main/java/com/minelittlepony/util/coordinates/Quad.java b/src/main/java/com/minelittlepony/util/coordinates/Quad.java new file mode 100644 index 00000000..ea43f510 --- /dev/null +++ b/src/main/java/com/minelittlepony/util/coordinates/Quad.java @@ -0,0 +1,10 @@ +package com.minelittlepony.util.coordinates; + +import net.minecraft.client.model.TexturedQuad; + +public class Quad extends TexturedQuad { + + public Quad(Vertex[] vertices, int texcoordU1, int texcoordV1, int texcoordU2, int texcoordV2, float textureWidth, float textureHeight) { + super(vertices, texcoordU1, texcoordV1, texcoordU2, texcoordV2, textureWidth, textureHeight); + } +} \ No newline at end of file diff --git a/src/main/java/com/minelittlepony/util/coordinates/Vertex.java b/src/main/java/com/minelittlepony/util/coordinates/Vertex.java new file mode 100644 index 00000000..5ea593ea --- /dev/null +++ b/src/main/java/com/minelittlepony/util/coordinates/Vertex.java @@ -0,0 +1,27 @@ +package com.minelittlepony.util.coordinates; + +import net.minecraft.client.model.PositionTextureVertex; + +public class Vertex extends PositionTextureVertex { + + public Vertex(float x, float y, float z, int texX, int texY) { + super(x, y, z, texX, texY); + } + + public Vertex(Vertex old, float texX, float texY) { + super(old, texX, texY); + } + + public Vertex setTexturePosition(float texX, float texY) { + texturePositionX = texX; + texturePositionY = texY; + return this; + } + + /** + * Creates a new vertex mapping the given (x, y, z) coordinates to a texture offset. + */ + public static Vertex vert(float x, float y, float z, int texX, int texY) { + return new Vertex(x, y, z, texX, texY); + } +}