|
| 1 | +package se.llbit.chunky.model; |
| 2 | + |
| 3 | +import se.llbit.chunky.resources.Texture; |
| 4 | +import se.llbit.math.Quad; |
| 5 | +import se.llbit.math.Ray; |
| 6 | +import se.llbit.math.Vector3; |
| 7 | +import se.llbit.math.Vector4; |
| 8 | + |
| 9 | +public class StonecutterModel { |
| 10 | + private static final Texture bottom = Texture.stonecutterBottom; |
| 11 | + private static final Texture top = Texture.stonecutterTop; |
| 12 | + private static final Texture side = Texture.stonecutterSide; |
| 13 | + private static final Texture saw = Texture.stonecutterSaw; |
| 14 | + private static final Texture[] tex = new Texture[]{ |
| 15 | + top, bottom, side, side, side, side, saw, saw |
| 16 | + }; |
| 17 | + |
| 18 | + private static final Quad[] quadsNorth = new Quad[]{ |
| 19 | + new Quad( |
| 20 | + new Vector3(0 / 16.0, 9 / 16.0, 16 / 16.0), |
| 21 | + new Vector3(16 / 16.0, 9 / 16.0, 16 / 16.0), |
| 22 | + new Vector3(0 / 16.0, 9 / 16.0, 0 / 16.0), |
| 23 | + new Vector4(0 / 16.0, 16 / 16.0, 0 / 16.0, 16 / 16.0) |
| 24 | + ), |
| 25 | + |
| 26 | + new Quad( |
| 27 | + new Vector3(0 / 16.0, 0 / 16.0, 0 / 16.0), |
| 28 | + new Vector3(16 / 16.0, 0 / 16.0, 0 / 16.0), |
| 29 | + new Vector3(0 / 16.0, 0 / 16.0, 16 / 16.0), |
| 30 | + new Vector4(0 / 16.0, 16 / 16.0, 0 / 16.0, 16 / 16.0) |
| 31 | + ), |
| 32 | + new Quad( |
| 33 | + new Vector3(0 / 16.0, 9 / 16.0, 16 / 16.0), |
| 34 | + new Vector3(0 / 16.0, 9 / 16.0, 0 / 16.0), |
| 35 | + new Vector3(0 / 16.0, 0 / 16.0, 16 / 16.0), |
| 36 | + new Vector4(16 / 16.0, 0 / 16.0, 9 / 16.0, 0 / 16.0) |
| 37 | + ), |
| 38 | + new Quad( |
| 39 | + new Vector3(16 / 16.0, 9 / 16.0, 0 / 16.0), |
| 40 | + new Vector3(16 / 16.0, 9 / 16.0, 16 / 16.0), |
| 41 | + new Vector3(16 / 16.0, 0 / 16.0, 0 / 16.0), |
| 42 | + new Vector4(16 / 16.0, 0 / 16.0, 9 / 16.0, 0 / 16.0) |
| 43 | + ), |
| 44 | + new Quad( |
| 45 | + new Vector3(0 / 16.0, 9 / 16.0, 0 / 16.0), |
| 46 | + new Vector3(16 / 16.0, 9 / 16.0, 0 / 16.0), |
| 47 | + new Vector3(0 / 16.0, 0 / 16.0, 0 / 16.0), |
| 48 | + new Vector4(16 / 16.0, 0 / 16.0, 9 / 16.0, 0 / 16.0) |
| 49 | + ), |
| 50 | + new Quad( |
| 51 | + new Vector3(16 / 16.0, 9 / 16.0, 16 / 16.0), |
| 52 | + new Vector3(0 / 16.0, 9 / 16.0, 16 / 16.0), |
| 53 | + new Vector3(16 / 16.0, 0 / 16.0, 16 / 16.0), |
| 54 | + new Vector4(16 / 16.0, 0 / 16.0, 9 / 16.0, 0 / 16.0) |
| 55 | + ), |
| 56 | + new Quad( |
| 57 | + new Vector3(1 / 16.0, 16 / 16.0, 8 / 16.0), |
| 58 | + new Vector3(15 / 16.0, 16 / 16.0, 8 / 16.0), |
| 59 | + new Vector3(1 / 16.0, 9 / 16.0, 8 / 16.0), |
| 60 | + new Vector4(15 / 16.0, 1 / 16.0, 7 / 16.0, 0 / 16.0) |
| 61 | + ), |
| 62 | + new Quad( |
| 63 | + new Vector3(15 / 16.0, 16 / 16.0, 8 / 16.0), |
| 64 | + new Vector3(1 / 16.0, 16 / 16.0, 8 / 16.0), |
| 65 | + new Vector3(15 / 16.0, 9 / 16.0, 8 / 16.0), |
| 66 | + new Vector4(15 / 16.0, 1 / 16.0, 7 / 16.0, 0 / 16.0) |
| 67 | + ) |
| 68 | + }; |
| 69 | + |
| 70 | + static final Quad[][] orientedQuads = new Quad[4][]; |
| 71 | + |
| 72 | + static { |
| 73 | + orientedQuads[0] = quadsNorth; |
| 74 | + orientedQuads[1] = Model.rotateY(orientedQuads[0]); |
| 75 | + orientedQuads[2] = Model.rotateY(orientedQuads[1]); |
| 76 | + orientedQuads[3] = Model.rotateY(orientedQuads[2]); |
| 77 | + } |
| 78 | + |
| 79 | + public static boolean intersect(Ray ray, String facing) { |
| 80 | + boolean hit = false; |
| 81 | + ray.t = Double.POSITIVE_INFINITY; |
| 82 | + |
| 83 | + Quad[] quads = orientedQuads[getOrientationIndex(facing)]; |
| 84 | + for (int i = 0; i < quads.length; ++i) { |
| 85 | + Quad quad = quads[i]; |
| 86 | + if (quad.intersect(ray)) { |
| 87 | + float[] color = tex[i].getColor(ray.u, ray.v); |
| 88 | + if (color[3] > Ray.EPSILON) { |
| 89 | + ray.color.set(color); |
| 90 | + ray.t = ray.tNext; |
| 91 | + ray.n.set(quad.n); |
| 92 | + hit = true; |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + if (hit) { |
| 98 | + ray.distance += ray.t; |
| 99 | + ray.o.scaleAdd(ray.t, ray.d); |
| 100 | + } |
| 101 | + return hit; |
| 102 | + } |
| 103 | + |
| 104 | + private static int getOrientationIndex(String facing) { |
| 105 | + switch (facing) { |
| 106 | + case "north": |
| 107 | + return 0; |
| 108 | + case "east": |
| 109 | + return 1; |
| 110 | + case "south": |
| 111 | + return 2; |
| 112 | + case "west": |
| 113 | + return 3; |
| 114 | + default: |
| 115 | + return 0; |
| 116 | + } |
| 117 | + } |
| 118 | +} |
0 commit comments