Skip to content

Commit 786917e

Browse files
committed
Add initial support for new model rotations introduced in 25w46a
1 parent f630409 commit 786917e

File tree

11 files changed

+315
-99
lines changed

11 files changed

+315
-99
lines changed

core/src/main/java/de/bluecolored/bluemap/core/map/hires/ArrayTileModel.java

Lines changed: 78 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -252,40 +252,97 @@ public ArrayTileModel rotate(
252252
return rotateByQuaternion(start, count, qx, qy, qz, qw);
253253
}
254254

255-
@Override
256-
public ArrayTileModel rotate(
255+
public ArrayTileModel rotateXYZ(
257256
int start, int count,
258257
float pitch, float yaw, float roll
259258
) {
260-
261259
double
260+
halfPitch = Math.toRadians(pitch) * 0.5,
261+
sx = TrigMath.sin(halfPitch),
262+
cx = TrigMath.cos(halfPitch),
263+
262264
halfYaw = Math.toRadians(yaw) * 0.5,
263-
qy1 = TrigMath.sin(halfYaw),
264-
qw1 = TrigMath.cos(halfYaw),
265+
sy = TrigMath.sin(halfYaw),
266+
cy = TrigMath.cos(halfYaw),
265267

268+
halfRoll = Math.toRadians(roll) * 0.5,
269+
sz = TrigMath.sin(halfRoll),
270+
cz = TrigMath.cos(halfRoll),
271+
272+
cycz = cy * cz,
273+
sysz = sy * sz,
274+
sycz = sy * cz,
275+
cysz = cy * sz;
276+
277+
return rotateByQuaternion(
278+
start, count,
279+
sx * cycz + cx * sysz,
280+
cx * sycz - sx * cysz,
281+
cx * cysz + sx * sycz,
282+
cx * cycz - sx * sysz
283+
);
284+
}
285+
286+
public ArrayTileModel rotateZYX(
287+
int start, int count,
288+
float pitch, float yaw, float roll
289+
) {
290+
double
266291
halfPitch = Math.toRadians(pitch) * 0.5,
267-
qx2 = TrigMath.sin(halfPitch),
268-
qw2 = TrigMath.cos(halfPitch),
292+
sx = TrigMath.sin(halfPitch),
293+
cx = TrigMath.cos(halfPitch),
294+
295+
halfYaw = Math.toRadians(yaw) * 0.5,
296+
sy = TrigMath.sin(halfYaw),
297+
cy = TrigMath.cos(halfYaw),
269298

270299
halfRoll = Math.toRadians(roll) * 0.5,
271-
qz3 = TrigMath.sin(halfRoll),
272-
qw3 = TrigMath.cos(halfRoll);
300+
sz = TrigMath.sin(halfRoll),
301+
cz = TrigMath.cos(halfRoll),
302+
303+
cycz = cy * cz,
304+
sysz = sy * sz,
305+
sycz = sy * cz,
306+
cysz = cy * sz;
307+
308+
return rotateByQuaternion(
309+
start, count,
310+
cx * cycz + sx * sysz,
311+
sx * cycz - cx * sysz,
312+
cx * sycz + sx * cysz,
313+
cx * cysz - sx * sycz
314+
);
315+
}
273316

274-
// multiply 1 with 2
317+
public ArrayTileModel rotateYXZ(
318+
int start, int count,
319+
float pitch, float yaw, float roll
320+
) {
275321
double
276-
qxA = qw1 * qx2,
277-
qyA = qy1 * qw2,
278-
qzA = - qy1 * qx2,
279-
qwA = qw1 * qw2;
322+
halfPitch = Math.toRadians(pitch) * 0.5,
323+
sx = TrigMath.sin(halfPitch),
324+
cx = TrigMath.cos(halfPitch),
280325

281-
// multiply with 3
282-
double
283-
qx = qxA * qw3 + qyA * qz3,
284-
qy = qyA * qw3 - qxA * qz3,
285-
qz = qwA * qz3 + qzA * qw3,
286-
qw = qwA * qw3 - qzA * qz3;
326+
halfYaw = Math.toRadians(yaw) * 0.5,
327+
sy = TrigMath.sin(halfYaw),
328+
cy = TrigMath.cos(halfYaw),
287329

288-
return rotateByQuaternion(start, count, qx, qy, qz, qw);
330+
halfRoll = Math.toRadians(roll) * 0.5,
331+
sz = TrigMath.sin(halfRoll),
332+
cz = TrigMath.cos(halfRoll),
333+
334+
cysx = cy * sx,
335+
sycx = sy * cx,
336+
sysx = sy * sx,
337+
cycx = cy * cx;
338+
339+
return rotateByQuaternion(
340+
start, count,
341+
cysx * cz + sycx * sz,
342+
sycx * cz - cysx * sz,
343+
cycx * sz - sysx * cz,
344+
cycx * cz + sysx * sz
345+
);
289346
}
290347

291348
@Override

core/src/main/java/de/bluecolored/bluemap/core/map/hires/TileModel.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,17 @@ TileModel rotate(
7070
float angle, float axisX, float axisY, float axisZ
7171
);
7272

73-
TileModel rotate(
73+
TileModel rotateXYZ(
74+
int start, int count,
75+
float pitch, float yaw, float roll
76+
);
77+
78+
TileModel rotateZYX(
79+
int start, int count,
80+
float pitch, float yaw, float roll
81+
);
82+
83+
TileModel rotateYXZ(
7484
int start, int count,
7585
float pitch, float yaw, float roll
7686
);

core/src/main/java/de/bluecolored/bluemap/core/map/hires/TileModelView.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,18 @@ public TileModelView rotate(float angle, float axisX, float axisY, float axisZ)
8585
return this;
8686
}
8787

88-
public TileModelView rotate(float pitch, float yaw, float roll) {
89-
tileModel.rotate(start, size, pitch, yaw, roll);
88+
public TileModelView rotateXYZ(float pitch, float yaw, float roll) {
89+
tileModel.rotateXYZ(start, size, pitch, yaw, roll);
90+
return this;
91+
}
92+
93+
public TileModelView rotateZYX(float pitch, float yaw, float roll) {
94+
tileModel.rotateZYX(start, size, pitch, yaw, roll);
95+
return this;
96+
}
97+
98+
public TileModelView rotateYXZ(float pitch, float yaw, float roll) {
99+
tileModel.rotateYXZ(start, size, pitch, yaw, roll);
90100
return this;
91101
}
92102

core/src/main/java/de/bluecolored/bluemap/core/map/hires/VoidTileModel.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,23 @@ public TileModel rotate(
110110
}
111111

112112
@Override
113-
public TileModel rotate(
113+
public TileModel rotateXYZ(
114+
int start, int count,
115+
float pitch, float yaw, float roll
116+
) {
117+
return this;
118+
}
119+
120+
@Override
121+
public TileModel rotateZYX(
122+
int start, int count,
123+
float pitch, float yaw, float roll
124+
) {
125+
return this;
126+
}
127+
128+
@Override
129+
public TileModel rotateYXZ(
114130
int start, int count,
115131
float pitch, float yaw, float roll
116132
) {

core/src/main/java/de/bluecolored/bluemap/core/map/hires/entity/EntityModelRenderer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public void render(Entity entity, BlockNeighborhood block, TileModelView tileMod
6565
tileModel.initialize(modelStart);
6666

6767
// apply entity rotation
68-
tileModel.rotate(entity.getRotation().getY(), entity.getRotation().getX(), 0f);
68+
tileModel.rotateYXZ(entity.getRotation().getY(), entity.getRotation().getX(), 0f);
6969
}
7070

7171
}

core/src/main/java/de/bluecolored/bluemap/core/resources/pack/resourcepack/blockstate/Variant.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public class Variant {
4444
private BlockRendererType renderer = BlockRendererType.DEFAULT;
4545

4646
private ResourcePath<Model> model = ResourcePack.MISSING_BLOCK_MODEL;
47-
private float x = 0, y = 0;
47+
private float x = 0, y = 0, z = 0;
4848
private boolean uvlock = false;
4949
private double weight = 1;
5050

@@ -56,28 +56,30 @@ public Variant(ResourcePath<Model> model) {
5656
init();
5757
}
5858

59-
public Variant(ResourcePath<Model> model, float x, float y) {
59+
public Variant(ResourcePath<Model> model, float x, float y, float z) {
6060
this.model = model;
6161
this.x = x;
6262
this.y = y;
63+
this.z = z;
6364
init();
6465
}
6566

66-
public Variant(ResourcePath<Model> model, float x, float y, boolean uvlock, double weight) {
67+
public Variant(ResourcePath<Model> model, float x, float y, float z, boolean uvlock, double weight) {
6768
this.model = model;
6869
this.x = x;
6970
this.y = y;
71+
this.z = z;
7072
this.uvlock = uvlock;
7173
this.weight = weight;
7274
init();
7375
}
7476

7577
@PostDeserialize
7678
private void init() {
77-
this.transformed = x != 0 || y != 0;
79+
this.transformed = x != 0 || y != 0 || z != 0;
7880
this.transformMatrix = new MatrixM4f()
7981
.translate(-0.5f, -0.5f, -0.5f)
80-
.rotate(-x, -y, 0)
82+
.rotateYXZ(-x, -y, -z)
8183
.translate(0.5f, 0.5f, 0.5f);
8284
}
8385

core/src/main/java/de/bluecolored/bluemap/core/resources/pack/resourcepack/entitystate/Part.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,16 @@
2525
package de.bluecolored.bluemap.core.resources.pack.resourcepack.entitystate;
2626

2727
import com.flowpowered.math.vector.Vector3f;
28-
import com.google.gson.Gson;
29-
import com.google.gson.annotations.JsonAdapter;
30-
import com.google.gson.reflect.TypeToken;
31-
import com.google.gson.stream.JsonReader;
3228
import de.bluecolored.bluemap.core.map.hires.entity.EntityRendererType;
33-
import de.bluecolored.bluemap.core.resources.adapter.AbstractTypeAdapterFactory;
3429
import de.bluecolored.bluemap.core.resources.ResourcePath;
3530
import de.bluecolored.bluemap.core.resources.adapter.PostDeserialize;
3631
import de.bluecolored.bluemap.core.resources.pack.resourcepack.ResourcePack;
3732
import de.bluecolored.bluemap.core.resources.pack.resourcepack.model.Model;
3833
import de.bluecolored.bluemap.core.util.math.MatrixM4f;
39-
import lombok.*;
40-
41-
import java.io.IOException;
34+
import lombok.AccessLevel;
35+
import lombok.Getter;
36+
import lombok.NoArgsConstructor;
37+
import lombok.Setter;
4238

4339
@SuppressWarnings("FieldMayBeFinal")
4440
@NoArgsConstructor(access = AccessLevel.PRIVATE)
@@ -71,7 +67,7 @@ public Part(ResourcePath<Model> model, Vector3f position, Vector3f rotation) {
7167
private void init() {
7268
this.transformed = !position.equals(Vector3f.ZERO) || !rotation.equals(Vector3f.ZERO);
7369
this.transformMatrix = new MatrixM4f()
74-
.rotate(rotation.getX(), rotation.getY(), rotation.getZ())
70+
.rotateYXZ(-rotation.getX(), -rotation.getY(), -rotation.getZ())
7571
.translate(position.getX(), position.getY(), position.getZ());
7672
}
7773
}

core/src/main/java/de/bluecolored/bluemap/core/resources/pack/resourcepack/model/Rotation.java

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -24,34 +24,26 @@
2424
*/
2525
package de.bluecolored.bluemap.core.resources.pack.resourcepack.model;
2626

27-
import com.flowpowered.math.TrigMath;
2827
import com.flowpowered.math.vector.Vector3f;
29-
import com.flowpowered.math.vector.Vector3i;
30-
import com.google.gson.Gson;
31-
import com.google.gson.annotations.JsonAdapter;
32-
import com.google.gson.reflect.TypeToken;
33-
import com.google.gson.stream.JsonReader;
34-
import de.bluecolored.bluemap.core.resources.adapter.AbstractTypeAdapterFactory;
3528
import de.bluecolored.bluemap.core.resources.adapter.PostDeserialize;
3629
import de.bluecolored.bluemap.core.util.math.Axis;
3730
import de.bluecolored.bluemap.core.util.math.MatrixM4f;
31+
import de.bluecolored.bluemap.core.util.math.VectorM3f;
3832
import lombok.AccessLevel;
3933
import lombok.Getter;
4034
import lombok.NoArgsConstructor;
4135

42-
import java.io.IOException;
43-
4436
@SuppressWarnings("FieldMayBeFinal")
4537
@NoArgsConstructor(access = AccessLevel.PRIVATE)
4638
@Getter
4739
public class Rotation {
4840
private static final Vector3f DEFAULT_ORIGIN = new Vector3f(8, 8, 8);
49-
private static final double FIT_TO_BLOCK_SCALE_MULTIPLIER = 2 - Math.sqrt(2);
5041

5142
public static final Rotation ZERO = new Rotation();
5243
static { ZERO.init(); }
5344

5445
private Vector3f origin = DEFAULT_ORIGIN;
46+
private float x, y, z;
5547
private Axis axis = Axis.Y;
5648
private float angle = 0;
5749
private boolean rescale = false;
@@ -66,27 +58,43 @@ public Rotation(Vector3f origin, Axis axis, float angle, boolean rescale) {
6658
init();
6759
}
6860

61+
public Rotation(Vector3f origin, float x, float y, float z, boolean rescale) {
62+
this.origin = origin;
63+
this.x = x;
64+
this.y = y;
65+
this.z = z;
66+
this.rescale = rescale;
67+
init();
68+
}
69+
6970
@PostDeserialize
7071
private void init() {
71-
Vector3i axisAngle = axis.toVector();
72+
73+
// angle/axis notation takes precedence
74+
if (angle != 0) {
75+
x = y = z = 0;
76+
switch (axis) {
77+
case X -> x = angle;
78+
case Y -> y = angle;
79+
case Z -> z = angle;
80+
}
81+
}
7282

7383
matrix = new MatrixM4f();
74-
if (angle != 0f) {
84+
if (x != 0 || y != 0 || z != 0) {
7585
matrix.translate(-origin.getX(), -origin.getY(), -origin.getZ());
76-
matrix.rotate(
77-
angle,
78-
axisAngle.getX(),
79-
axisAngle.getY(),
80-
axisAngle.getZ()
81-
);
86+
matrix.rotateYXZ(x, y, z);
8287

8388
if (rescale) {
84-
float scale = (float) (Math.abs(TrigMath.sin(angle * TrigMath.DEG_TO_RAD)) * FIT_TO_BLOCK_SCALE_MULTIPLIER);
85-
matrix.scale(
86-
(1 - axisAngle.getX()) * scale + 1,
87-
(1 - axisAngle.getY()) * scale + 1,
88-
(1 - axisAngle.getZ()) * scale + 1
89-
);
89+
VectorM3f axisVector = new VectorM3f(0, 0, 0);
90+
float sX = 1f / axisVector.set(1, 0, 0).rotateAndScale(matrix).absolute().max();
91+
float sY = 1f / axisVector.set(0, 1, 0).rotateAndScale(matrix).absolute().max();
92+
float sZ = 1f / axisVector.set(0, 0, 1).rotateAndScale(matrix).absolute().max();
93+
94+
matrix.identity();
95+
matrix.translate(-origin.getX(), -origin.getY(), -origin.getZ());
96+
matrix.scale(sX, sY, sZ);
97+
matrix.rotateYXZ(x, y, z);
9098
}
9199

92100
matrix.translate(origin.getX(), origin.getY(), origin.getZ());

0 commit comments

Comments
 (0)