From c6a19ebf7961feb9b064798f04874b6e6905c225 Mon Sep 17 00:00:00 2001 From: Lukas Date: Sun, 23 Aug 2020 21:08:53 +0200 Subject: [PATCH] add better BlockRotation system --- .../objectLoader/blocks/BlockProperties.java | 5 - .../objectLoader/blocks/BlockRotation.java | 7 +- .../datatypes/objectLoader/blocks/Blocks.java | 12 +-- .../render/blockModels/Face/Face.java | 8 +- .../render/blockModels/subBlocks/Cuboid.java | 13 ++- .../blockModels/subBlocks/SubBlock.java | 93 ++----------------- .../subBlocks/SubBlockPosition.java | 33 +++++++ .../subBlocks/SubBlockRotation.java | 19 +++- .../assets/mapping/1.15.2/.editorconfig | 0 9 files changed, 80 insertions(+), 110 deletions(-) delete mode 100644 src/main/resources/assets/mapping/1.15.2/.editorconfig diff --git a/src/main/java/de/bixilon/minosoft/game/datatypes/objectLoader/blocks/BlockProperties.java b/src/main/java/de/bixilon/minosoft/game/datatypes/objectLoader/blocks/BlockProperties.java index 42bc20cfb..84690e487 100644 --- a/src/main/java/de/bixilon/minosoft/game/datatypes/objectLoader/blocks/BlockProperties.java +++ b/src/main/java/de/bixilon/minosoft/game/datatypes/objectLoader/blocks/BlockProperties.java @@ -265,11 +265,6 @@ public enum BlockProperties { BOTTOM, NOT_BOTTOM, - // log, portal - AXIS_X, - AXIS_Y, - AXIS_Z, - // trapwire DISARMED, ARMED, diff --git a/src/main/java/de/bixilon/minosoft/game/datatypes/objectLoader/blocks/BlockRotation.java b/src/main/java/de/bixilon/minosoft/game/datatypes/objectLoader/blocks/BlockRotation.java index 037cf45ec..9dcfef44e 100644 --- a/src/main/java/de/bixilon/minosoft/game/datatypes/objectLoader/blocks/BlockRotation.java +++ b/src/main/java/de/bixilon/minosoft/game/datatypes/objectLoader/blocks/BlockRotation.java @@ -40,5 +40,10 @@ public enum BlockRotation { ASCENDING_SOUTH, UP, - DOWN + DOWN, + + // log, portal + AXIS_X, + AXIS_Y, + AXIS_Z, } diff --git a/src/main/java/de/bixilon/minosoft/game/datatypes/objectLoader/blocks/Blocks.java b/src/main/java/de/bixilon/minosoft/game/datatypes/objectLoader/blocks/Blocks.java index 57c05c3b3..7c672cac7 100644 --- a/src/main/java/de/bixilon/minosoft/game/datatypes/objectLoader/blocks/Blocks.java +++ b/src/main/java/de/bixilon/minosoft/game/datatypes/objectLoader/blocks/Blocks.java @@ -336,12 +336,6 @@ public class Blocks { propertyHashMap.put("right", BlockProperties.HINGE_RIGHT); propertiesMapping.put("hinge", propertyHashMap); - propertyHashMap = new HashMap<>(); - propertyHashMap.put("x", BlockProperties.AXIS_X); - propertyHashMap.put("y", BlockProperties.AXIS_Y); - propertyHashMap.put("z", BlockProperties.AXIS_Z); - propertiesMapping.put("axis", propertyHashMap); - propertyHashMap = new HashMap<>(); propertyHashMap.put("floor", BlockProperties.FLOOR); propertyHashMap.put("wall", BlockProperties.WALL); @@ -434,6 +428,9 @@ public class Blocks { rotationMapping.put("ascending_south", BlockRotation.ASCENDING_SOUTH); rotationMapping.put("north_south", BlockRotation.NORTH_SOUTH); rotationMapping.put("east_west", BlockRotation.EAST_WEST); + rotationMapping.put("x", BlockRotation.AXIS_X); + rotationMapping.put("y", BlockRotation.AXIS_Y); + rotationMapping.put("z", BlockRotation.AXIS_Z); } public static Block getBlockByLegacy(int protocolId, int protocolMetaData) { @@ -471,6 +468,9 @@ public class Blocks { } else if (propertiesJSON.has("rotation")) { rotation = rotationMapping.get(propertiesJSON.get("rotation").getAsString()); propertiesJSON.remove("rotation"); + } else if (propertiesJSON.has("axis")) { + rotation = rotationMapping.get(propertiesJSON.get("axis").getAsString()); + propertiesJSON.remove("axis"); } HashSet properties = new HashSet<>(); for (String propertyName : propertiesJSON.keySet()) { diff --git a/src/main/java/de/bixilon/minosoft/render/blockModels/Face/Face.java b/src/main/java/de/bixilon/minosoft/render/blockModels/Face/Face.java index f5363fcac..259d24e5f 100644 --- a/src/main/java/de/bixilon/minosoft/render/blockModels/Face/Face.java +++ b/src/main/java/de/bixilon/minosoft/render/blockModels/Face/Face.java @@ -19,15 +19,13 @@ import de.bixilon.minosoft.render.texture.InFaceUV; import javafx.util.Pair; public class Face { - int textureRotation; - SubBlockPosition[] positions; + final SubBlockPosition[] positions; InFaceUV uv; - public Face(Pair texture, InFaceUV uv, SubBlockPosition[] facePositions, int textureRotation) { + public Face(Pair texture, InFaceUV uv, SubBlockPosition[] facePositions) { positions = facePositions; this.uv = uv; this.uv.prepare(texture); - this.textureRotation = textureRotation; } public Face() { @@ -36,7 +34,7 @@ public class Face { public void draw(BlockPosition pos) { for (int i = 0; i < positions.length; i++) { - uv.draw(i + textureRotation); + uv.draw(i); positions[i].draw(pos); } } diff --git a/src/main/java/de/bixilon/minosoft/render/blockModels/subBlocks/Cuboid.java b/src/main/java/de/bixilon/minosoft/render/blockModels/subBlocks/Cuboid.java index ef506fe57..4b0f47e54 100644 --- a/src/main/java/de/bixilon/minosoft/render/blockModels/subBlocks/Cuboid.java +++ b/src/main/java/de/bixilon/minosoft/render/blockModels/subBlocks/Cuboid.java @@ -15,6 +15,7 @@ package de.bixilon.minosoft.render.blockModels.subBlocks; // some 3d object with 8 corners, 6 faces and 12 edges (example: cube, but can be deformed) +import de.bixilon.minosoft.game.datatypes.objectLoader.blocks.BlockRotation; import de.bixilon.minosoft.render.blockModels.Face.FaceOrientation; import java.util.HashMap; @@ -59,7 +60,15 @@ public class Cuboid { } } - public SubBlockPosition[] getFacePositions(FaceOrientation orientation) { - return facePositionMap.get(orientation); + public SubBlockPosition[] getFacePositions(FaceOrientation orientation, BlockRotation rotation) { + SubBlockPosition[] positions = facePositionMap.get(orientation); + if (rotation == BlockRotation.NONE || rotation == BlockRotation.NORTH) { + return positions; + } + SubBlockPosition[] result = new SubBlockPosition[positions.length]; + for (int i = 0; i < positions.length; i++) { + result[i] = positions[i].rotated(rotation); + } + return result; } } diff --git a/src/main/java/de/bixilon/minosoft/render/blockModels/subBlocks/SubBlock.java b/src/main/java/de/bixilon/minosoft/render/blockModels/subBlocks/SubBlock.java index f7aefbb70..735c9a170 100644 --- a/src/main/java/de/bixilon/minosoft/render/blockModels/subBlocks/SubBlock.java +++ b/src/main/java/de/bixilon/minosoft/render/blockModels/subBlocks/SubBlock.java @@ -15,7 +15,6 @@ package de.bixilon.minosoft.render.blockModels.subBlocks; import com.google.gson.JsonObject; import de.bixilon.minosoft.game.datatypes.objectLoader.blocks.Block; -import de.bixilon.minosoft.game.datatypes.objectLoader.blocks.BlockProperties; import de.bixilon.minosoft.game.datatypes.objectLoader.blocks.BlockRotation; import de.bixilon.minosoft.render.blockModels.Face.Face; import de.bixilon.minosoft.render.blockModels.Face.FaceOrientation; @@ -60,7 +59,7 @@ public class SubBlock { orientation, variables); } } - isFull = (from.x == 0 && from.y == 0 && from.z == 0) && (to.x == 16 && to.y == 16 && to.z == 16); + isFull = (from.x == 0 && from.y == 0 && from.z == 0) && (to.x == 16 && to.y == 16 && to.z == 16) && rotation == null; } private static String getRealTextureName(String textureName, HashMap variables) { @@ -112,98 +111,18 @@ public class SubBlock { if (!textureCoordinates.containsKey(orientation)) { continue; } - if (block.getRotation().equals(BlockRotation.DOWN)) { - if (orientation.equals(FaceOrientation.UP)) { - result.add(prepareFace(FaceOrientation.DOWN, orientation, - adjacentBlocks)); - continue; - } - if (orientation.equals(FaceOrientation.DOWN)) { - result.add(prepareFace(FaceOrientation.UP, orientation, - adjacentBlocks)); - continue; - } - } else if (block.getProperties().contains(BlockProperties.AXIS_X)) { - if (orientation.equals(FaceOrientation.EAST)) { - result.add(prepareFace(FaceOrientation.UP, orientation, - adjacentBlocks)); - continue; - } - if (orientation.equals(FaceOrientation.WEST)) { - result.add(prepareFace(FaceOrientation.DOWN, orientation, - adjacentBlocks)); - continue; - } - if (orientation.equals(FaceOrientation.UP)) { - result.add(prepareFace(FaceOrientation.EAST, orientation, - adjacentBlocks, 1)); - continue; - } - if (orientation.equals(FaceOrientation.DOWN)) { - result.add(prepareFace(FaceOrientation.WEST, orientation, - adjacentBlocks, 1)); - continue; - } - if (orientation.equals(FaceOrientation.NORTH)) { - result.add(prepareFace(orientation, orientation, - adjacentBlocks, 1)); - continue; - } - if (orientation.equals(FaceOrientation.SOUTH)) { - result.add(prepareFace(orientation, orientation, - adjacentBlocks, 1)); - continue; - } - } else if (block.getProperties().contains(BlockProperties.AXIS_Z)) { - if (orientation.equals(FaceOrientation.EAST)) { - result.add(prepareFace(orientation, orientation, - adjacentBlocks, 1)); - continue; - } - if (orientation.equals(FaceOrientation.WEST)) { - result.add(prepareFace(orientation, orientation, - adjacentBlocks, 1)); - continue; - } - if (orientation.equals(FaceOrientation.UP)) { - result.add(prepareFace(FaceOrientation.EAST, orientation, - adjacentBlocks)); - continue; - } - if (orientation.equals(FaceOrientation.DOWN)) { - result.add(prepareFace(FaceOrientation.WEST, orientation, - adjacentBlocks)); - continue; - } - if (orientation.equals(FaceOrientation.NORTH)) { - result.add(prepareFace(FaceOrientation.UP, orientation, - adjacentBlocks)); - continue; - } - if (orientation.equals(FaceOrientation.SOUTH)) { - result.add(prepareFace(FaceOrientation.DOWN, orientation, - adjacentBlocks)); - continue; - } - } - result.add(prepareFace(orientation, orientation, adjacentBlocks)); + result.add(prepareFace(orientation, block.getRotation(), adjacentBlocks)); } return result; } - private Face prepareFace(FaceOrientation textureDirection, FaceOrientation faceDirection, - HashMap adjacentBlocks, int textureRotation) { + private Face prepareFace(FaceOrientation faceDirection, BlockRotation rotation, + HashMap adjacentBlocks) { if (adjacentBlocks.get(faceDirection) && !cullFaceTextures.get(faceDirection)) { return new Face(); } - return new Face(textureCoordinates.get(textureDirection), uv.get(textureDirection), - cuboid.getFacePositions(faceDirection), textureRotation); - } - - private Face prepareFace(FaceOrientation textureDirection, FaceOrientation faceDirection, - HashMap adjacentBlocks) { - return prepareFace(textureDirection, faceDirection, - adjacentBlocks, 0); + return new Face(textureCoordinates.get(faceDirection), uv.get(faceDirection), + cuboid.getFacePositions(faceDirection, rotation)); } public boolean isFull() { diff --git a/src/main/java/de/bixilon/minosoft/render/blockModels/subBlocks/SubBlockPosition.java b/src/main/java/de/bixilon/minosoft/render/blockModels/subBlocks/SubBlockPosition.java index fee1256dd..b67e54d86 100644 --- a/src/main/java/de/bixilon/minosoft/render/blockModels/subBlocks/SubBlockPosition.java +++ b/src/main/java/de/bixilon/minosoft/render/blockModels/subBlocks/SubBlockPosition.java @@ -14,7 +14,9 @@ package de.bixilon.minosoft.render.blockModels.subBlocks; import com.google.gson.JsonArray; +import de.bixilon.minosoft.game.datatypes.objectLoader.blocks.BlockRotation; import de.bixilon.minosoft.game.datatypes.world.BlockPosition; +import de.bixilon.minosoft.render.blockModels.Face.Axis; import static de.bixilon.minosoft.render.blockModels.Face.RenderConstants.blockRes; import static org.lwjgl.opengl.GL11.glVertex3f; @@ -22,6 +24,17 @@ import static org.lwjgl.opengl.GL11.glVertex3f; public class SubBlockPosition { float x, y, z; + public static final SubBlockPosition middlePos = new SubBlockPosition(8, 8, 8); + + public static final SubBlockRotation westRotator = new SubBlockRotation(middlePos, Axis.Y, 90); + public static final SubBlockRotation eastRotator = new SubBlockRotation(middlePos, Axis.Y, 270); + public static final SubBlockRotation southRotator = new SubBlockRotation(middlePos, Axis.Y, 180); + + public static final SubBlockRotation xAxisRotator = new SubBlockRotation(middlePos, Axis.Z, 90); + public static final SubBlockRotation zAxisRotator = new SubBlockRotation(middlePos, Axis.X, 90); + + public static final SubBlockRotation downRotator = new SubBlockRotation(middlePos, Axis.X, 180); + public SubBlockPosition(JsonArray json) { x = json.get(0).getAsFloat(); y = json.get(1).getAsFloat(); @@ -54,4 +67,24 @@ public class SubBlockPosition { pos.getY() + y / blockRes, pos.getZ() + z / blockRes); } + + public SubBlockPosition rotated(BlockRotation rotation) { + + switch (rotation) { + case EAST: + return eastRotator.apply(this); + case WEST: + return westRotator.apply(this); + case SOUTH: + return southRotator.apply(this); + case AXIS_X: + return xAxisRotator.apply(this); + case AXIS_Z: + return zAxisRotator.apply(this); + case DOWN: + return downRotator.apply(this); + default: + return this; + } + } } diff --git a/src/main/java/de/bixilon/minosoft/render/blockModels/subBlocks/SubBlockRotation.java b/src/main/java/de/bixilon/minosoft/render/blockModels/subBlocks/SubBlockRotation.java index 99241a29c..efd68813e 100644 --- a/src/main/java/de/bixilon/minosoft/render/blockModels/subBlocks/SubBlockRotation.java +++ b/src/main/java/de/bixilon/minosoft/render/blockModels/subBlocks/SubBlockRotation.java @@ -25,6 +25,12 @@ public class SubBlockRotation { Axis direction; float angle; + public SubBlockRotation(SubBlockPosition origin, Axis direction, float angle) { + this.origin = origin; + this.direction = direction; + this.angle = angle; + } + public SubBlockRotation(JsonObject rotation) { origin = new SubBlockPosition(rotation.get("origin").getAsJsonArray()); String axis = rotation.get("axis").getAsString(); @@ -37,14 +43,19 @@ public class SubBlockRotation { break; case "z": direction = Axis.Z; + break; } angle = rotation.get("angle").getAsFloat(); } - private static Pair rotate(float x, float y, float angle) { - return new Pair( - x * (float) cos(angle) + y * (float) sin(angle), - -x * (float) sin(angle) + y * (float) cos(angle) + public static Pair rotate(float x, float y, float angle) { + float angleRad = (float) Math.toRadians(angle); + float newX = x * (float) cos(angleRad) + y * (float) sin(angleRad); + float newY = -x * (float) sin(angleRad) + y * (float) cos(angleRad); + + return new Pair<>( + newX, + newY ); } diff --git a/src/main/resources/assets/mapping/1.15.2/.editorconfig b/src/main/resources/assets/mapping/1.15.2/.editorconfig deleted file mode 100644 index e69de29bb..000000000