add better BlockRotation system

This commit is contained in:
Lukas 2020-08-23 21:08:53 +02:00
parent cbf73f02d1
commit c6a19ebf79
9 changed files with 80 additions and 110 deletions

View File

@ -265,11 +265,6 @@ public enum BlockProperties {
BOTTOM, BOTTOM,
NOT_BOTTOM, NOT_BOTTOM,
// log, portal
AXIS_X,
AXIS_Y,
AXIS_Z,
// trapwire // trapwire
DISARMED, DISARMED,
ARMED, ARMED,

View File

@ -40,5 +40,10 @@ public enum BlockRotation {
ASCENDING_SOUTH, ASCENDING_SOUTH,
UP, UP,
DOWN DOWN,
// log, portal
AXIS_X,
AXIS_Y,
AXIS_Z,
} }

View File

@ -336,12 +336,6 @@ public class Blocks {
propertyHashMap.put("right", BlockProperties.HINGE_RIGHT); propertyHashMap.put("right", BlockProperties.HINGE_RIGHT);
propertiesMapping.put("hinge", propertyHashMap); 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 = new HashMap<>();
propertyHashMap.put("floor", BlockProperties.FLOOR); propertyHashMap.put("floor", BlockProperties.FLOOR);
propertyHashMap.put("wall", BlockProperties.WALL); propertyHashMap.put("wall", BlockProperties.WALL);
@ -434,6 +428,9 @@ public class Blocks {
rotationMapping.put("ascending_south", BlockRotation.ASCENDING_SOUTH); rotationMapping.put("ascending_south", BlockRotation.ASCENDING_SOUTH);
rotationMapping.put("north_south", BlockRotation.NORTH_SOUTH); rotationMapping.put("north_south", BlockRotation.NORTH_SOUTH);
rotationMapping.put("east_west", BlockRotation.EAST_WEST); 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) { public static Block getBlockByLegacy(int protocolId, int protocolMetaData) {
@ -471,6 +468,9 @@ public class Blocks {
} else if (propertiesJSON.has("rotation")) { } else if (propertiesJSON.has("rotation")) {
rotation = rotationMapping.get(propertiesJSON.get("rotation").getAsString()); rotation = rotationMapping.get(propertiesJSON.get("rotation").getAsString());
propertiesJSON.remove("rotation"); propertiesJSON.remove("rotation");
} else if (propertiesJSON.has("axis")) {
rotation = rotationMapping.get(propertiesJSON.get("axis").getAsString());
propertiesJSON.remove("axis");
} }
HashSet<BlockProperties> properties = new HashSet<>(); HashSet<BlockProperties> properties = new HashSet<>();
for (String propertyName : propertiesJSON.keySet()) { for (String propertyName : propertiesJSON.keySet()) {

View File

@ -19,15 +19,13 @@ import de.bixilon.minosoft.render.texture.InFaceUV;
import javafx.util.Pair; import javafx.util.Pair;
public class Face { public class Face {
int textureRotation; final SubBlockPosition[] positions;
SubBlockPosition[] positions;
InFaceUV uv; InFaceUV uv;
public Face(Pair<Float, Float> texture, InFaceUV uv, SubBlockPosition[] facePositions, int textureRotation) { public Face(Pair<Float, Float> texture, InFaceUV uv, SubBlockPosition[] facePositions) {
positions = facePositions; positions = facePositions;
this.uv = uv; this.uv = uv;
this.uv.prepare(texture); this.uv.prepare(texture);
this.textureRotation = textureRotation;
} }
public Face() { public Face() {
@ -36,7 +34,7 @@ public class Face {
public void draw(BlockPosition pos) { public void draw(BlockPosition pos) {
for (int i = 0; i < positions.length; i++) { for (int i = 0; i < positions.length; i++) {
uv.draw(i + textureRotation); uv.draw(i);
positions[i].draw(pos); positions[i].draw(pos);
} }
} }

View File

@ -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) // 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 de.bixilon.minosoft.render.blockModels.Face.FaceOrientation;
import java.util.HashMap; import java.util.HashMap;
@ -59,7 +60,15 @@ public class Cuboid {
} }
} }
public SubBlockPosition[] getFacePositions(FaceOrientation orientation) { public SubBlockPosition[] getFacePositions(FaceOrientation orientation, BlockRotation rotation) {
return facePositionMap.get(orientation); 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;
} }
} }

View File

@ -15,7 +15,6 @@ package de.bixilon.minosoft.render.blockModels.subBlocks;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import de.bixilon.minosoft.game.datatypes.objectLoader.blocks.Block; 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.game.datatypes.objectLoader.blocks.BlockRotation;
import de.bixilon.minosoft.render.blockModels.Face.Face; import de.bixilon.minosoft.render.blockModels.Face.Face;
import de.bixilon.minosoft.render.blockModels.Face.FaceOrientation; import de.bixilon.minosoft.render.blockModels.Face.FaceOrientation;
@ -60,7 +59,7 @@ public class SubBlock {
orientation, variables); 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<String, String> variables) { private static String getRealTextureName(String textureName, HashMap<String, String> variables) {
@ -112,98 +111,18 @@ public class SubBlock {
if (!textureCoordinates.containsKey(orientation)) { if (!textureCoordinates.containsKey(orientation)) {
continue; continue;
} }
if (block.getRotation().equals(BlockRotation.DOWN)) { result.add(prepareFace(orientation, block.getRotation(), adjacentBlocks));
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));
} }
return result; return result;
} }
private Face prepareFace(FaceOrientation textureDirection, FaceOrientation faceDirection, private Face prepareFace(FaceOrientation faceDirection, BlockRotation rotation,
HashMap<FaceOrientation, Boolean> adjacentBlocks, int textureRotation) { HashMap<FaceOrientation, Boolean> adjacentBlocks) {
if (adjacentBlocks.get(faceDirection) && !cullFaceTextures.get(faceDirection)) { if (adjacentBlocks.get(faceDirection) && !cullFaceTextures.get(faceDirection)) {
return new Face(); return new Face();
} }
return new Face(textureCoordinates.get(textureDirection), uv.get(textureDirection), return new Face(textureCoordinates.get(faceDirection), uv.get(faceDirection),
cuboid.getFacePositions(faceDirection), textureRotation); cuboid.getFacePositions(faceDirection, rotation));
}
private Face prepareFace(FaceOrientation textureDirection, FaceOrientation faceDirection,
HashMap<FaceOrientation, Boolean> adjacentBlocks) {
return prepareFace(textureDirection, faceDirection,
adjacentBlocks, 0);
} }
public boolean isFull() { public boolean isFull() {

View File

@ -14,7 +14,9 @@
package de.bixilon.minosoft.render.blockModels.subBlocks; package de.bixilon.minosoft.render.blockModels.subBlocks;
import com.google.gson.JsonArray; 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.game.datatypes.world.BlockPosition;
import de.bixilon.minosoft.render.blockModels.Face.Axis;
import static de.bixilon.minosoft.render.blockModels.Face.RenderConstants.blockRes; import static de.bixilon.minosoft.render.blockModels.Face.RenderConstants.blockRes;
import static org.lwjgl.opengl.GL11.glVertex3f; import static org.lwjgl.opengl.GL11.glVertex3f;
@ -22,6 +24,17 @@ import static org.lwjgl.opengl.GL11.glVertex3f;
public class SubBlockPosition { public class SubBlockPosition {
float x, y, z; 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) { public SubBlockPosition(JsonArray json) {
x = json.get(0).getAsFloat(); x = json.get(0).getAsFloat();
y = json.get(1).getAsFloat(); y = json.get(1).getAsFloat();
@ -54,4 +67,24 @@ public class SubBlockPosition {
pos.getY() + y / blockRes, pos.getY() + y / blockRes,
pos.getZ() + z / 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;
}
}
} }

View File

@ -25,6 +25,12 @@ public class SubBlockRotation {
Axis direction; Axis direction;
float angle; float angle;
public SubBlockRotation(SubBlockPosition origin, Axis direction, float angle) {
this.origin = origin;
this.direction = direction;
this.angle = angle;
}
public SubBlockRotation(JsonObject rotation) { public SubBlockRotation(JsonObject rotation) {
origin = new SubBlockPosition(rotation.get("origin").getAsJsonArray()); origin = new SubBlockPosition(rotation.get("origin").getAsJsonArray());
String axis = rotation.get("axis").getAsString(); String axis = rotation.get("axis").getAsString();
@ -37,14 +43,19 @@ public class SubBlockRotation {
break; break;
case "z": case "z":
direction = Axis.Z; direction = Axis.Z;
break;
} }
angle = rotation.get("angle").getAsFloat(); angle = rotation.get("angle").getAsFloat();
} }
private static Pair<Float, Float> rotate(float x, float y, float angle) { public static Pair<Float, Float> rotate(float x, float y, float angle) {
return new Pair<Float, Float>( float angleRad = (float) Math.toRadians(angle);
x * (float) cos(angle) + y * (float) sin(angle), float newX = x * (float) cos(angleRad) + y * (float) sin(angleRad);
-x * (float) sin(angle) + y * (float) cos(angle) float newY = -x * (float) sin(angleRad) + y * (float) cos(angleRad);
return new Pair<>(
newX,
newY
); );
} }