Skip to content

Commit 86c14a7

Browse files
ThatRedoxleMaik
andauthored
Improve AABB model performance. (#1845)
* Improve AABB performance. * Move box textures into a variable. * Improve javadoc of aabb model. --------- Co-authored-by: Maik Marschner <leMaik@leMaik.de>
1 parent 4a5a15f commit 86c14a7

File tree

1 file changed

+55
-24
lines changed

1 file changed

+55
-24
lines changed

chunky/src/java/se/llbit/chunky/model/AABBModel.java

Lines changed: 55 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,47 +8,76 @@
88
import se.llbit.math.Vector3;
99

1010
import java.util.Arrays;
11-
import java.util.List;
1211
import java.util.Objects;
1312
import java.util.Random;
1413

1514
/**
16-
* A block model that is made out of textured AABBs.
15+
* A block model that is made out of textured axis-aligned bounding boxes (AABBs).
1716
*/
1817
@PluginApi
1918
public abstract class AABBModel implements BlockModel {
2019

2120
/**
2221
* Different UV mapping methods.
23-
* - None: No change in mapping
24-
* - ROTATE_90: Rotate 90 degrees clockwise
25-
* - ROTATE_180: Rotate 180 degrees
26-
* - ROTATE_270: Rotate 270 degrees clockwise (90 degrees counterclockwise)
27-
* - FLIP_U: Flip along the X axis (u = 1 - u)
28-
* - FLIP_V: Flip along the Y axis (v = 1 - v)
29-
* <p>
30-
* Note: a value of {@code null} is equivalent to {@code NONE}
3122
*/
3223
public enum UVMapping {
24+
/**
25+
* No change in mapping.
26+
*/
3327
NONE,
28+
/**
29+
* Rotate by 90 degrees clockwise.
30+
*/
3431
ROTATE_90,
32+
/**
33+
* Rotate by 180 degrees.
34+
*/
3535
ROTATE_180,
36+
/**
37+
* Rotate 270 degrees clockwise (90 degrees counter clockwise).
38+
*/
3639
ROTATE_270,
40+
/**
41+
* Mirror horizontally (u = 1 - u).
42+
*/
3743
FLIP_U,
44+
/**
45+
* Mirror vertically (v = 1 - v).
46+
*/
3847
FLIP_V
3948
}
4049

50+
/**
51+
* Get the boxes for this model.
52+
*
53+
* @return An array of boxes.
54+
*/
4155
@PluginApi
4256
public abstract AABB[] getBoxes();
4357

58+
/**
59+
* Get textures for the boxes.
60+
*
61+
* @return An array of textures for the boxes, each in north, east, south, west, top, bottom order.
62+
*/
4463
@PluginApi
4564
public abstract Texture[][] getTextures();
4665

66+
/**
67+
* Get tints for the boxes. If an entry is <code>null</code> or this method returns <code>null</code>, it is equivalent to {@link Tint#NONE}.
68+
*
69+
* @return An array of tints for the boxes, each in north, east, south, west, top, bottom order.
70+
*/
4771
@PluginApi
4872
public Tint[][] getTints() {
4973
return null;
5074
}
5175

76+
/**
77+
* Get UV mappings for the boxes. If an entry is <code>null</code> or this method returns <code>null</code>, it is equivalent to {@link UVMapping#NONE}.
78+
*
79+
* @return An array of UV mappings for the boxes, each in north, east, south, west, top, bottom order.
80+
*/
5281
@PluginApi
5382
public UVMapping[][] getUVMapping() {
5483
return null;
@@ -81,45 +110,47 @@ public boolean intersect(Ray ray, Scene scene) {
81110
ray.t = Double.POSITIVE_INFINITY;
82111
for (int i = 0; i < boxes.length; ++i) {
83112
if (boxes[i].intersect(ray)) {
113+
Texture[] texturesBox = textures[i];
84114
Tint[] tintedFacesBox = tintedFaces != null ? tintedFaces[i] : null;
115+
UVMapping[] mappingBox = mapping != null ? mapping[i] : null;
85116
Vector3 n = ray.getNormal();
86117
if (n.y > 0) { // top
87118
ray.v = 1 - ray.v;
88-
if (intersectFace(ray, scene, textures[i][4],
89-
mapping != null ? mapping[i][4] : null
119+
if (intersectFace(ray, texturesBox[4],
120+
mappingBox != null ? mappingBox[4] : null
90121
)) {
91122
tint = tintedFacesBox != null ? tintedFacesBox[4] : Tint.NONE;
92123
hit = true;
93124
}
94125
} else if (n.y < 0) { // bottom
95-
if (intersectFace(ray, scene, textures[i][5],
96-
mapping != null ? mapping[i][5] : null)) {
126+
if (intersectFace(ray, texturesBox[5],
127+
mappingBox != null ? mappingBox[5] : null)) {
97128
hit = true;
98129
tint = tintedFacesBox != null ? tintedFacesBox[5] : Tint.NONE;
99130
}
100131
} else if (n.z < 0) { // north
101-
if (intersectFace(ray, scene, textures[i][0],
102-
mapping != null ? mapping[i][0] : null
132+
if (intersectFace(ray, texturesBox[0],
133+
mappingBox != null ? mappingBox[0] : null
103134
)) {
104135
hit = true;
105136
tint = tintedFacesBox != null ? tintedFacesBox[0] : Tint.NONE;
106137
}
107138
} else if (n.z > 0) { // south
108-
if (intersectFace(ray, scene, textures[i][2],
109-
mapping != null ? mapping[i][2] : null
139+
if (intersectFace(ray, texturesBox[2],
140+
mappingBox != null ? mappingBox[2] : null
110141
)) {
111142
hit = true;
112143
tint = tintedFacesBox != null ? tintedFacesBox[2] : Tint.NONE;
113144
}
114145
} else if (n.x < 0) { // west
115-
if (intersectFace(ray, scene, textures[i][3],
116-
mapping != null ? mapping[i][3] : null)) {
146+
if (intersectFace(ray, texturesBox[3],
147+
mappingBox != null ? mappingBox[3] : null)) {
117148
hit = true;
118149
tint = tintedFacesBox != null ? tintedFacesBox[3] : Tint.NONE;
119150
}
120151
} else if (n.x > 0) { // east
121-
if (intersectFace(ray, scene, textures[i][1],
122-
mapping != null ? mapping[i][1] : null)) {
152+
if (intersectFace(ray, texturesBox[1],
153+
mappingBox != null ? mappingBox[1] : null)) {
123154
hit = true;
124155
tint = tintedFacesBox != null ? tintedFacesBox[1] : Tint.NONE;
125156
}
@@ -141,7 +172,7 @@ public boolean intersect(Ray ray, Scene scene) {
141172
return hit;
142173
}
143174

144-
private boolean intersectFace(Ray ray, Scene scene, Texture texture, UVMapping mapping) {
175+
private boolean intersectFace(Ray ray, Texture texture, UVMapping mapping) {
145176
// This is the method that handles intersecting faces of all AABB-based models.
146177
// Do normal mapping, parallax occlusion mapping, specular maps and all the good stuff here!
147178

@@ -186,7 +217,7 @@ private boolean intersectFace(Ray ray, Scene scene, Texture texture, UVMapping m
186217
@Override
187218
public boolean isBiomeDependant() {
188219
Tint[][] tints = getTints();
189-
if(tints == null)
220+
if (tints == null)
190221
return false;
191222

192223
return Arrays.stream(tints)

0 commit comments

Comments
 (0)