2026-09-13 10:34:51 +03:00
|
|
|
package online.astradev.content.rail;
|
|
|
|
|
|
2026-09-13 21:55:44 +03:00
|
|
|
import static org.junit.jupiter.api.Assertions.*;
|
|
|
|
|
|
2026-09-13 10:34:51 +03:00
|
|
|
import com.google.gson.*;
|
2026-09-13 21:55:44 +03:00
|
|
|
|
2026-09-13 10:34:51 +03:00
|
|
|
import net.minecraft.world.level.block.state.properties.RailShape;
|
2026-09-13 21:55:44 +03:00
|
|
|
|
2026-09-13 10:34:51 +03:00
|
|
|
import org.joml.Vector3f;
|
|
|
|
|
import org.junit.jupiter.api.Test;
|
2026-09-13 21:55:44 +03:00
|
|
|
|
|
|
|
|
import java.io.InputStreamReader;
|
|
|
|
|
import java.nio.charset.StandardCharsets;
|
2026-09-13 10:34:51 +03:00
|
|
|
|
|
|
|
|
class RailDisplayTransformTest {
|
2026-09-13 21:55:44 +03:00
|
|
|
@Test
|
|
|
|
|
void everyOverlayVertexMatchesNativeRailPlane() throws Exception {
|
|
|
|
|
for (int age = 0; age < 4; age++)
|
|
|
|
|
for (boolean powered : new boolean[] {false, true})
|
|
|
|
|
for (var shape :
|
|
|
|
|
new RailShape[] {
|
|
|
|
|
RailShape.NORTH_SOUTH,
|
|
|
|
|
RailShape.EAST_WEST,
|
|
|
|
|
RailShape.ASCENDING_NORTH,
|
|
|
|
|
RailShape.ASCENDING_SOUTH,
|
|
|
|
|
RailShape.ASCENDING_EAST,
|
|
|
|
|
RailShape.ASCENDING_WEST
|
|
|
|
|
}) {
|
|
|
|
|
String path =
|
|
|
|
|
"/defaults/resources/assets/astracontent/models/rail_display/"
|
|
|
|
|
+ age
|
|
|
|
|
+ "/"
|
|
|
|
|
+ shape.getSerializedName()
|
|
|
|
|
+ (powered ? "_on" : "")
|
|
|
|
|
+ ".json";
|
|
|
|
|
try (var stream = getClass().getResourceAsStream(path)) {
|
|
|
|
|
assertNotNull(stream, path);
|
|
|
|
|
var model =
|
|
|
|
|
JsonParser.parseReader(
|
|
|
|
|
new InputStreamReader(
|
|
|
|
|
stream, StandardCharsets.UTF_8))
|
|
|
|
|
.getAsJsonObject();
|
|
|
|
|
for (var value : model.getAsJsonArray("elements")) {
|
|
|
|
|
var element = value.getAsJsonObject();
|
|
|
|
|
var from = vector(element.getAsJsonArray("from"));
|
|
|
|
|
var to = vector(element.getAsJsonArray("to"));
|
|
|
|
|
for (float x : new float[] {from.x, to.x})
|
|
|
|
|
for (float z : new float[] {from.z, to.z}) {
|
|
|
|
|
var point = new Vector3f(x, from.y, z);
|
|
|
|
|
if (element.has("rotation")) {
|
|
|
|
|
var rotation = element.getAsJsonObject("rotation");
|
|
|
|
|
var origin = vector(rotation.getAsJsonArray("origin"));
|
|
|
|
|
float radians =
|
|
|
|
|
(float)
|
|
|
|
|
Math.toRadians(
|
|
|
|
|
rotation.get("angle").getAsFloat());
|
|
|
|
|
point.sub(origin);
|
|
|
|
|
if (rotation.get("rescale").getAsBoolean()) {
|
|
|
|
|
float scale = 1 / (float) Math.cos(radians);
|
|
|
|
|
point.mul(1, scale, scale);
|
|
|
|
|
}
|
|
|
|
|
point.rotateX(radians).add(origin);
|
|
|
|
|
}
|
|
|
|
|
// Item models are centered at .5, then the vanilla item
|
|
|
|
|
// renderer turns them by pi.
|
|
|
|
|
point.div(16)
|
|
|
|
|
.sub(.5f, .5f, .5f)
|
|
|
|
|
.rotateY((float) Math.PI)
|
|
|
|
|
.rotate(RailDisplayTransform.orientation(shape))
|
|
|
|
|
.add(.5f, .5f, .5f);
|
|
|
|
|
float nativeHeight =
|
|
|
|
|
1f / 16
|
|
|
|
|
+ switch (shape) {
|
|
|
|
|
case ASCENDING_NORTH -> 1 - point.z;
|
|
|
|
|
case ASCENDING_SOUTH -> point.z;
|
|
|
|
|
case ASCENDING_EAST -> point.x;
|
|
|
|
|
case ASCENDING_WEST -> 1 - point.x;
|
|
|
|
|
default -> 0;
|
|
|
|
|
};
|
|
|
|
|
assertTrue(
|
|
|
|
|
point.y > nativeHeight && point.y - nativeHeight < .01,
|
|
|
|
|
path
|
|
|
|
|
+ " vertex="
|
|
|
|
|
+ point
|
|
|
|
|
+ " rail height="
|
|
|
|
|
+ nativeHeight);
|
|
|
|
|
assertTrue(
|
|
|
|
|
point.x > -.01
|
|
|
|
|
&& point.x < 1.01
|
|
|
|
|
&& point.z > -.01
|
|
|
|
|
&& point.z < 1.01,
|
|
|
|
|
path + " stays inside rail footprint");
|
|
|
|
|
}
|
2026-09-13 10:34:51 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-09-13 21:55:44 +03:00
|
|
|
|
|
|
|
|
private static Vector3f vector(JsonArray a) {
|
|
|
|
|
return new Vector3f(a.get(0).getAsFloat(), a.get(1).getAsFloat(), a.get(2).getAsFloat());
|
|
|
|
|
}
|
2026-09-13 10:34:51 +03:00
|
|
|
}
|