mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-11-30 01:34:04 +00:00
Mirror objects along X and Z in the object transformer, mirroring block states through the platform's own mirror
This commit is contained in:
1 parent
563c2cd276
commit
844e3205ac
9 files changed
+221
-12
No files matched your search
@@ -20,6 +20,7 @@ package art.arcane.iris.core.modules;
|
||||
|
||||
import art.arcane.iris.engine.object.prefab.IrisObjectRotation;
|
||||
import art.arcane.iris.spi.IrisPlatforms;
|
||||
import art.arcane.iris.spi.PlatformBlockState;
|
||||
import art.arcane.iris.util.common.math.IrisBlockVector;
|
||||
import com.volmit.mantle.BlockBox;
|
||||
import com.volmit.object.GeneratedObject;
|
||||
@@ -37,8 +38,10 @@ import java.util.Map;
|
||||
/**
|
||||
* Transforms geometry with the rotation Iris places objects with ({@link IrisObjectRotation}, its axis order
|
||||
* included) about the anchor, turning block states with the object so stairs and logs face the way it does.
|
||||
* A Y mirror flips positions the way Iris hangs an object from a ceiling, without changing block states.
|
||||
* Iris has no way to mirror along X or Z, or to scale geometry, so those requests fail with
|
||||
* Mirrors come first, as {@link ObjectTransform} says: an X or Z mirror flips positions about the anchor and
|
||||
* mirrors block states through the platform's own mirror, so stairs and doors face the mirrored way. A Y mirror
|
||||
* flips positions the way Iris hangs an object from a ceiling, without changing block states, since blocks have
|
||||
* no mirror along Y. Iris has no way to scale geometry, so that request fails with
|
||||
* {@link UnsupportedOperationException} rather than changing the request. Blocks that a turn by an angle that is
|
||||
* not a quarter turn sends to one position keep the last one, as Iris overwrites them when it places.
|
||||
*/
|
||||
@@ -66,9 +69,6 @@ final class IrisObjectTransformer implements ObjectTransformer {
|
||||
}
|
||||
|
||||
private static void requireSupported(ObjectTransform transform) {
|
||||
if (transform.mirrorX() || transform.mirrorZ()) {
|
||||
throw new UnsupportedOperationException("Iris can mirror an object along Y only, not along X or Z: " + transform);
|
||||
}
|
||||
if (transform.scaleX() != NO_SCALE || transform.scaleY() != NO_SCALE || transform.scaleZ() != NO_SCALE) {
|
||||
throw new UnsupportedOperationException("Iris cannot scale an object's geometry: " + transform);
|
||||
}
|
||||
@@ -97,28 +97,43 @@ final class IrisObjectTransformer implements ObjectTransformer {
|
||||
/** One requested turn about an anchor. */
|
||||
private static final class Turn {
|
||||
private final BlockPosition anchor;
|
||||
private final boolean mirrorX;
|
||||
private final boolean mirrorY;
|
||||
private final boolean mirrorZ;
|
||||
private final IrisObjectRotation rotation;
|
||||
private final PlatformBlockStates states = new PlatformBlockStates(key -> IrisPlatforms.get().registries().block(key));
|
||||
|
||||
Turn(BlockPosition anchor, ObjectTransform transform) {
|
||||
this.anchor = anchor;
|
||||
this.mirrorX = transform.mirrorX();
|
||||
this.mirrorY = transform.mirrorY();
|
||||
this.mirrorZ = transform.mirrorZ();
|
||||
this.rotation = IrisObjectRotation.of(transform.rotationX(), transform.rotationY(), transform.rotationZ());
|
||||
}
|
||||
|
||||
BlockPosition position(BlockPosition local) {
|
||||
IrisBlockVector offset = new IrisBlockVector(local.x() - anchor.x(), local.y() - anchor.y(), local.z() - anchor.z());
|
||||
if (mirrorX) {
|
||||
offset.setX(-offset.getX());
|
||||
}
|
||||
if (mirrorY) {
|
||||
offset.setY(-offset.getY());
|
||||
}
|
||||
if (mirrorZ) {
|
||||
offset.setZ(-offset.getZ());
|
||||
}
|
||||
IrisBlockVector turned = rotation.rotate(offset, NO_SPIN, NO_SPIN, NO_SPIN);
|
||||
return new BlockPosition(anchor.x() + (int) Math.round(turned.getX()), anchor.y() + (int) Math.round(turned.getY()),
|
||||
anchor.z() + (int) Math.round(turned.getZ()));
|
||||
}
|
||||
|
||||
com.volmit.block.BlockState state(ObjectBlock block) {
|
||||
return states.toApi(rotation.rotate(states.toPlatform(block.state()), NO_SPIN, NO_SPIN, NO_SPIN));
|
||||
return states.toApi(rotation.rotate(mirrored(states.toPlatform(block.state())), NO_SPIN, NO_SPIN, NO_SPIN));
|
||||
}
|
||||
|
||||
private PlatformBlockState mirrored(PlatformBlockState state) {
|
||||
PlatformBlockState result = mirrorX ? state.mirrorX() : state;
|
||||
return mirrorZ ? result.mirrorZ() : result;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -27,6 +27,7 @@ import org.bukkit.Tag;
|
||||
import org.bukkit.block.BlockFace;
|
||||
import org.bukkit.block.BlockSupport;
|
||||
import org.bukkit.block.data.BlockData;
|
||||
import org.bukkit.block.structure.Mirror;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
@@ -297,6 +298,22 @@ public final class BukkitBlockState implements PlatformBlockState {
|
||||
return cached;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PlatformBlockState mirrorX() {
|
||||
return mirrored(Mirror.FRONT_BACK);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PlatformBlockState mirrorZ() {
|
||||
return mirrored(Mirror.LEFT_RIGHT);
|
||||
}
|
||||
|
||||
private PlatformBlockState mirrored(Mirror mirror) {
|
||||
BlockData copy = data.clone();
|
||||
copy.mirror(mirror);
|
||||
return of(copy);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canAttach(PlatformBlockState support, PlatformFace face) {
|
||||
return ((BlockData) support.nativeHandle()).isFaceSturdy(blockFaceOf(face), BlockSupport.FULL);
|
||||
|
||||
@@ -2,7 +2,9 @@ package art.arcane.iris.core.modules;
|
||||
|
||||
import art.arcane.iris.engine.UsableBlockPlatform;
|
||||
import art.arcane.iris.engine.object.prefab.IrisObjectRotation;
|
||||
import art.arcane.iris.spi.IrisPlatforms;
|
||||
import art.arcane.iris.spi.PlatformBlockState;
|
||||
import art.arcane.iris.spi.PlatformRegistries;
|
||||
import com.volmit.block.BlockState;
|
||||
import com.volmit.mantle.BlockBox;
|
||||
import com.volmit.object.GeneratedObject;
|
||||
@@ -21,8 +23,9 @@ import java.util.Map;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertSame;
|
||||
import static org.junit.Assert.assertThrows;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/** Objects turn about their anchor the way Iris places them; what Iris cannot do is refused. */
|
||||
/** Objects turn and mirror about their anchor the way Iris places them; what Iris cannot do is refused. */
|
||||
public class IrisObjectTransformerTest {
|
||||
private static final BlockState LOG = BlockState.parse("minecraft:oak_log");
|
||||
private static final BlockState LEAVES = BlockState.parse("minecraft:oak_leaves");
|
||||
@@ -95,12 +98,86 @@ public class IrisObjectTransformerTest {
|
||||
assertEquals(2, squeezed.blocks().size());
|
||||
}
|
||||
|
||||
private static ObjectTransform mirrored(boolean x, boolean z) {
|
||||
return new ObjectTransform(0, 0, 0, 1, 1, 1, x, false, z);
|
||||
}
|
||||
|
||||
/** What the platform answers for a state and its mirrors: the platform, not Iris, decides how a block looks mirrored. */
|
||||
private static void mirrorsBy(String key, String alongX, String alongZ) {
|
||||
PlatformRegistries registries = IrisPlatforms.get().registries();
|
||||
PlatformBlockState state = registries.block(key);
|
||||
PlatformBlockState mirroredAlongX = registries.block(alongX);
|
||||
PlatformBlockState mirroredAlongZ = registries.block(alongZ);
|
||||
when(state.mirrorX()).thenReturn(mirroredAlongX);
|
||||
when(state.mirrorZ()).thenReturn(mirroredAlongZ);
|
||||
}
|
||||
|
||||
/** A stair east of the anchor and a marker beside it, in a world whose stairs mirror as the test platform says. */
|
||||
private GeneratedObject stairsEastOfTheAnchor() {
|
||||
mirrorsBy("minecraft:oak_stairs[facing=east]", "minecraft:oak_stairs[facing=west]", "minecraft:oak_stairs[facing=east]");
|
||||
mirrorsBy("minecraft:oak_stairs[facing=west]", "minecraft:oak_stairs[facing=east]", "minecraft:oak_stairs[facing=west]");
|
||||
mirrorsBy("minecraft:oak_stairs[facing=north]", "minecraft:oak_stairs[facing=north]", "minecraft:oak_stairs[facing=south]");
|
||||
return new GeneratedObject(new BlockBox(0, 0, 0, 2, 1, 2), new BlockPosition(0, 0, 0),
|
||||
List.of(new ObjectBlock(new BlockPosition(1, 0, 0), BlockState.parse("minecraft:oak_stairs[facing=east]")),
|
||||
new ObjectBlock(new BlockPosition(0, 0, 1), BlockState.parse("minecraft:oak_stairs[facing=north]"))),
|
||||
List.of(new ObjectMarker(new BlockPosition(1, 0, 1), SPAWN, Map.of("kind", "bird"))));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void mirroringAlongXOrZAndScalingAreRefused() {
|
||||
assertThrows(UnsupportedOperationException.class,
|
||||
() -> transformer.transform(tree, new ObjectTransform(0, 0, 0, 1, 1, 1, true, false, false)));
|
||||
assertThrows(UnsupportedOperationException.class,
|
||||
() -> transformer.transform(tree, new ObjectTransform(0, 0, 0, 1, 1, 1, false, false, true)));
|
||||
public void anXMirrorFlipsXAboutTheAnchorAndMirrorsStatesThroughThePlatform() {
|
||||
GeneratedObject mirrored = transformer.transform(stairsEastOfTheAnchor(), mirrored(true, false));
|
||||
|
||||
assertEquals(List.of(new ObjectBlock(new BlockPosition(-1, 0, 0), BlockState.parse("minecraft:oak_stairs[facing=west]")),
|
||||
new ObjectBlock(new BlockPosition(0, 0, 1), BlockState.parse("minecraft:oak_stairs[facing=north]"))),
|
||||
mirrored.blocks());
|
||||
assertEquals(new BlockPosition(-1, 0, 1), mirrored.markers().getFirst().position());
|
||||
assertEquals(new BlockBox(-1, 0, 0, 1, 1, 2), mirrored.bounds());
|
||||
assertEquals(new BlockPosition(0, 0, 0), mirrored.anchor());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void aZMirrorFlipsZAboutTheAnchorAndMirrorsStatesThroughThePlatform() {
|
||||
GeneratedObject mirrored = transformer.transform(stairsEastOfTheAnchor(), mirrored(false, true));
|
||||
|
||||
assertEquals(List.of(new ObjectBlock(new BlockPosition(0, 0, -1), BlockState.parse("minecraft:oak_stairs[facing=south]")),
|
||||
new ObjectBlock(new BlockPosition(1, 0, 0), BlockState.parse("minecraft:oak_stairs[facing=east]"))),
|
||||
mirrored.blocks().stream().sorted(java.util.Comparator.comparing(ObjectBlock::position)).toList());
|
||||
assertEquals(new BlockPosition(1, 0, -1), mirrored.markers().getFirst().position());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void mirroringBothAxesMirrorsStatesAlongBoth() {
|
||||
GeneratedObject mirrored = transformer.transform(stairsEastOfTheAnchor(), mirrored(true, true));
|
||||
|
||||
assertEquals(List.of(new BlockPosition(-1, 0, 0), new BlockPosition(0, 0, -1)),
|
||||
mirrored.blocks().stream().map(ObjectBlock::position).sorted().toList());
|
||||
assertEquals(List.of("minecraft:oak_stairs[facing=south]", "minecraft:oak_stairs[facing=west]"),
|
||||
mirrored.blocks().stream().map(block -> block.state().key()).sorted().toList());
|
||||
assertEquals(new BlockPosition(-1, 0, -1), mirrored.markers().getFirst().position());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void mirrorsComeBeforeRotation() {
|
||||
GeneratedObject object = new GeneratedObject(new BlockBox(0, 0, 0, 2, 1, 1), new BlockPosition(0, 0, 0),
|
||||
List.of(new ObjectBlock(new BlockPosition(1, 0, 0), LOG)), List.of());
|
||||
|
||||
GeneratedObject both = transformer.transform(object, new ObjectTransform(0, 90, 0, 1, 1, 1, true, false, false));
|
||||
|
||||
// Mirrored first (1,0,0) -> (-1,0,0), then a quarter turn about Y sends +X to -Z, so -X goes to +Z.
|
||||
assertEquals(new BlockPosition(0, 0, 1), both.blocks().getFirst().position());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void aMirrorTheBlockIgnoresLeavesItsStateAsItWas() {
|
||||
GeneratedObject mirrored = transformer.transform(tree, mirrored(true, false));
|
||||
|
||||
assertEquals(List.of(LEAVES, LOG), mirrored.blocks().stream().map(ObjectBlock::state).toList());
|
||||
assertEquals(List.of(new BlockPosition(-1, 1, 0), new BlockPosition(0, 0, 0)),
|
||||
mirrored.blocks().stream().map(ObjectBlock::position).toList());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void scalingIsRefused() {
|
||||
assertThrows(UnsupportedOperationException.class,
|
||||
() -> transformer.transform(tree, new ObjectTransform(0, 0, 0, 2, 1, 1, false, false, false)));
|
||||
}
|
||||
|
||||
@@ -213,6 +213,16 @@ public class TreePlausibilizerTest {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PlatformBlockState mirrorX() {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PlatformBlockState mirrorZ() {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canAttach(PlatformBlockState support, PlatformFace face) {
|
||||
return false;
|
||||
|
||||
@@ -59,6 +59,8 @@ public final class UsableBlockPlatform {
|
||||
when(block.materialKey()).thenReturn(key.split("\\[", 2)[0]);
|
||||
when(block.isSolid()).thenReturn(!key.contains("air"));
|
||||
when(block.isOccluding()).thenReturn(!key.contains("air"));
|
||||
when(block.mirrorX()).thenReturn(block);
|
||||
when(block.mirrorZ()).thenReturn(block);
|
||||
return block;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user