Skip to content

Commit

Permalink
Fix painting orientation in pre-1.17 worlds. (#1679)
Browse files Browse the repository at this point in the history
* Infer painting orientation from the position instead of using the angle from the entity tag.

* Implement better fix for the painting rotation.
  • Loading branch information
leMaik committed Jan 14, 2024
1 parent 2b43166 commit 2ab384a
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 37 deletions.
77 changes: 46 additions & 31 deletions chunky/src/java/se/llbit/chunky/entity/PaintingEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,19 @@
*/
package se.llbit.chunky.entity;

import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import se.llbit.chunky.PersistentSettings;
import se.llbit.chunky.resources.Texture;
import se.llbit.chunky.world.Material;
import se.llbit.chunky.world.material.TextureMaterial;
import se.llbit.json.JsonObject;
import se.llbit.json.JsonValue;
import se.llbit.math.Quad;
import se.llbit.math.QuickMath;
import se.llbit.math.Transform;
import se.llbit.math.Vector3;
import se.llbit.math.Vector4;
import se.llbit.math.*;
import se.llbit.math.primitive.Primitive;

import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;

public class PaintingEntity extends Entity {

static class Painting {
Expand All @@ -50,32 +46,31 @@ public Painting(Texture painting, int w, int h) {

double offset = -1 / 16.;
double off = 0;
int pw = w * 16;
int ph = h * 16;

quads = new Quad[]{
// north (front)
new Quad(new Vector3(w, 0, offset), new Vector3(0, 0, offset),
new Vector3(w, h, offset), new Vector4(0, 1, 0, 1)),
// north (front)
new Quad(new Vector3(w, 0, offset), new Vector3(0, 0, offset),
new Vector3(w, h, offset), new Vector4(0, 1, 0, 1)),

// south (back)
new Quad(new Vector3(0, 0, off), new Vector3(w, 0, off), new Vector3(0, h, off),
new Vector4(0, w / 4., 1, 1 - h / 4.)),
// south (back)
new Quad(new Vector3(0, 0, off), new Vector3(w, 0, off), new Vector3(0, h, off),
new Vector4(0, w / 4., 1, 1 - h / 4.)),

// west (left)
new Quad(new Vector3(0, 0, offset), new Vector3(0, 0, off), new Vector3(0, h, offset),
new Vector4(0, 1 / 64., 1 - h / 4., 1)),
// west (left)
new Quad(new Vector3(0, 0, offset), new Vector3(0, 0, off), new Vector3(0, h, offset),
new Vector4(0, 1 / 64., 1 - h / 4., 1)),

// east (right)
new Quad(new Vector3(w, 0, off), new Vector3(w, 0, offset), new Vector3(w, h, off),
new Vector4(0, 1 / 64., 1 - h / 4., 1)),
// east (right)
new Quad(new Vector3(w, 0, off), new Vector3(w, 0, offset), new Vector3(w, h, off),
new Vector4(0, 1 / 64., 1 - h / 4., 1)),

// top
new Quad(new Vector3(w, h, offset), new Vector3(0, h, offset), new Vector3(w, h, off),
new Vector4(0, w / 4., 1 - 1 / 64., 1)),
// top
new Quad(new Vector3(w, h, offset), new Vector3(0, h, offset), new Vector3(w, h, off),
new Vector4(0, w / 4., 1 - 1 / 64., 1)),

// bottom
new Quad(new Vector3(0, 0, offset), new Vector3(w, 0, offset), new Vector3(0, 0, off),
new Vector4(0, w / 4., 1, 1 - 1 / 64.)),};
// bottom
new Quad(new Vector3(0, 0, offset), new Vector3(w, 0, offset), new Vector3(0, 0, off),
new Vector4(0, w / 4., 1, 1 - 1 / 64.)),};
material = new TextureMaterial(painting);
}
}
Expand Down Expand Up @@ -148,6 +143,26 @@ public PaintingEntity(Vector3 position, String art, double angle) {
this.angle = angle;
}

public PaintingEntity(Vector3 position, String art, int facing) {
super(position);
this.art = art;
switch (facing) {
case 1:
this.angle = 90;
break;
case 2:
this.angle = 180;
break;
case 3:
this.angle = 270;
break;
case 0:
default:
this.angle = 0;
break;
}
}

@Override
public Collection<Primitive> primitives(Vector3 offset) {
Collection<Primitive> primitives = new LinkedList<>();
Expand All @@ -157,7 +172,7 @@ public Collection<Primitive> primitives(Vector3 offset) {
}
double rot = QuickMath.degToRad(180 - angle);
Transform transform = Transform.NONE.translate(painting.ox, painting.oy, 0.5 / 16).rotateY(rot)
.translate(position.x + offset.x, position.y + offset.y, position.z + offset.z);
.translate(position.x + offset.x, position.y + offset.y, position.z + offset.z);
Quad[] quads = painting.quads;
quads[0].addTriangles(primitives, painting.material, transform); // front face
for (int i = 1; i < quads.length; i++) { // other faces
Expand Down
20 changes: 14 additions & 6 deletions chunky/src/java/se/llbit/chunky/renderer/scene/Scene.java
Original file line number Diff line number Diff line change
Expand Up @@ -1030,15 +1030,23 @@ public synchronized void loadChunks(TaskTracker taskTracker, World world, Collec

if (y >= yClipMin && y < yClipMax) {
String id = tag.get("id").stringValue("");
// Before 1.12 paintings had id=Painting.
// After 1.12 paintings had id=minecraft:painting.
if ((id.equals("minecraft:painting") || id.equals("Painting")) && entityLoadingPreferences.shouldLoadClass(PaintingEntity.class)) {
// Before 1.12 paintings had id=Painting.
// After 1.12 paintings had id=minecraft:painting.
float yaw = tag.get("Rotation").get(0).floatValue();

Tag paintingVariant = NbtUtil.getTagFromNames(tag, "Motive", "variant");
entities.add(new PaintingEntity(new Vector3(x, y, z), paintingVariant.stringValue(), yaw));
int facing = (tag.get("facing").isError())
? tag.get("Facing").byteValue(0) // pre 1.17
: tag.get("facing").byteValue(0); // 1.17+
entities.add(new PaintingEntity(
new Vector3(x, y, z),
paintingVariant.stringValue(),
facing
));
} else if (id.equals("minecraft:armor_stand") && entityLoadingPreferences.shouldLoadClass(ArmorStand.class)) {
actors.add(new ArmorStand(new Vector3(x, y, z), tag));
actors.add(new ArmorStand(
new Vector3(x, y, z),
tag
));
}
}
}
Expand Down

0 comments on commit 2ab384a

Please sign in to comment.