Skip to content

Commit 2a1241c

Browse files
committed
inline some single use constructors and normalisation to avoid object allocation
1 parent e7969e3 commit 2a1241c

File tree

2 files changed

+19
-18
lines changed

2 files changed

+19
-18
lines changed

src/main/java/mekanism/client/model/MekanismJavaModel.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,10 @@ private static void visitAndRender(List<ModelPart.Cube> cubes, PoseStack matrix,
107107
quad.vertices[1].pos.div(16, v1);
108108
quad.vertices[2].pos.div(16, v2);
109109
quad.vertices[3].pos.div(16, v3);
110-
lines.add(new Line(v0, v1));
111-
lines.add(new Line(v1, v2));
112-
lines.add(new Line(v2, v3));
113-
lines.add(new Line(v3, v0));
110+
lines.add(Line.from(v0, v1));
111+
lines.add(Line.from(v1, v2));
112+
lines.add(Line.from(v2, v3));
113+
lines.add(Line.from(v3, v0));
114114
}
115115
}
116116
RenderTickHandler.renderVertexWireFrame(lines, buffer, pose, poseNormal, pos, normal);

src/main/java/mekanism/client/render/lib/Outlines.java

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import net.neoforged.neoforge.client.model.IQuadTransformer;
1717
import net.neoforged.neoforge.client.model.data.ModelData;
1818
import org.jetbrains.annotations.Nullable;
19+
import org.joml.Math;
1920
import org.joml.Vector3f;
2021

2122
public class Outlines {
@@ -51,10 +52,10 @@ public void vertex(float pX, float pY, float pZ) {
5152
vertices[vertexIndex++] = new Vector3f(pX, pY, pZ);
5253
if (vertexIndex == 4) {
5354
vertexIndex = 0;
54-
lines.add(new Line(vertices[0], vertices[1]));
55-
lines.add(new Line(vertices[1], vertices[2]));
56-
lines.add(new Line(vertices[2], vertices[3]));
57-
lines.add(new Line(vertices[3], vertices[0]));
55+
lines.add(Line.from(vertices[0], vertices[1]));
56+
lines.add(Line.from(vertices[1], vertices[2]));
57+
lines.add(Line.from(vertices[2], vertices[3]));
58+
lines.add(Line.from(vertices[3], vertices[0]));
5859
Arrays.fill(vertices, null);
5960
}
6061
}
@@ -74,16 +75,16 @@ public void unpack(BakedQuad pQuad) {
7475

7576
public record Line(float x1, float y1, float z1, float x2, float y2, float z2, float nX, float nY, float nZ, int hash) {
7677

77-
public Line(float x1, float y1, float z1, float x2, float y2, float z2, float nX, float nY, float nZ) {
78-
this(x1, y1, z1, x2, y2, z2, nX, nY, nZ, calculateHash(x1, y1, z1, x2, y2, z2));
79-
}
80-
81-
public Line(Vector3f v1, Vector3f v2, Vector3f normal) {
82-
this(v1.x, v1.y, v1.z, v2.x, v2.y, v2.z, normal.x, normal.y, normal.z);
83-
}
84-
85-
public Line(Vector3f v1, Vector3f v2) {
86-
this(v1, v2, v2.sub(v1, new Vector3f()).normalize());
78+
public static Line from(Vector3f v1, Vector3f v2) {
79+
// normalise by the distance between the points
80+
float nX = v2.x - v1.x;
81+
float nY = v2.y - v1.y;
82+
float nZ = v2.z - v1.z;
83+
float scalar = Math.invsqrt(Math.fma(nX, nX, Math.fma(nY, nY, nZ * nZ)));
84+
nX = nX * scalar;
85+
nY = nY * scalar;
86+
nZ = nZ * scalar;
87+
return new Line(v1.x, v1.y, v1.z, v2.x, v2.y, v2.z, nX, nY, nZ, calculateHash(v1.x, v1.y, v1.z, v2.x, v2.y, v2.z));
8788
}
8889

8990
private static int calculateHash(float x1, float y1, float z1, float x2, float y2, float z2) {

0 commit comments

Comments
 (0)