Smarter translucency sorting #42
No files matched your search
+255
@@ -0,0 +1,255 @@
|
|||||||
|
package org.embeddedt.embeddium.impl.render.chunk.sorting;
|
||||||
|
|
||||||
|
import it.unimi.dsi.fastutil.floats.FloatArrayList;
|
||||||
|
import it.unimi.dsi.fastutil.floats.FloatArrays;
|
||||||
|
import it.unimi.dsi.fastutil.ints.IntArrayList;
|
||||||
|
import it.unimi.dsi.fastutil.ints.IntArrays;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
|
public final class PartitionTree implements SortState.Resortable {
|
||||||
|
|
||||||
|
private final Node root;
|
||||||
|
private final int quadCount;
|
||||||
|
private final float[][] cutPlanes;
|
||||||
|
|
||||||
|
private PartitionTree(Node root, int quadCount, float[][] cutPlanes) {
|
||||||
|
this.root = root;
|
||||||
|
this.quadCount = quadCount;
|
||||||
|
this.cutPlanes = cutPlanes;
|
||||||
|
}
|
||||||
|
|
||||||
|
// null when some slab has no free cut
|
||||||
|
public static @Nullable PartitionTree build(QuadSet quads) {
|
||||||
|
int[] all = new int[quads.count()];
|
||||||
|
for (int i = 0; i < all.length; i++) all[i] = i;
|
||||||
|
|
||||||
|
FloatArrayList[] cuts = { new FloatArrayList(), new FloatArrayList(), new FloatArrayList() };
|
||||||
|
Node root = partition(quads, all, 2, cuts);
|
||||||
|
if (root == null) return null;
|
||||||
|
|
||||||
|
float[][] cutPlanes = new float[3][];
|
||||||
|
for (int a = 0; a < 3; a++) {
|
||||||
|
cutPlanes[a] = sortAndDedup(cuts[a]);
|
||||||
|
}
|
||||||
|
return new PartitionTree(root, quads.count(), cutPlanes);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static float[] sortAndDedup(FloatArrayList floats) {
|
||||||
|
float[] array = floats.toFloatArray();
|
||||||
|
FloatArrays.quickSort(array);
|
||||||
|
|
||||||
|
int n = 0;
|
||||||
|
for (int i = 0; i < array.length; i++) {
|
||||||
|
if (n == 0 || array[i] != array[n - 1]) {
|
||||||
|
array[n++] = array[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return FloatArrays.copy(array, 0, n);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Node partition(QuadSet quads, int[] group, int parentAxis, FloatArrayList[] collectedCuts) {
|
||||||
|
if (group.length <= 1 || isAllCoplanar(quads, group)) {
|
||||||
|
return new Leaf(group);
|
||||||
|
}
|
||||||
|
|
||||||
|
int axis = -1;
|
||||||
|
float[] planes = FloatArrays.EMPTY_ARRAY;
|
||||||
|
for (int i = 0; i < 3; i++) {
|
||||||
|
axis = (parentAxis + 1 + i) % 3;
|
||||||
|
planes = cutsOn(quads, group, axis);
|
||||||
|
if (planes.length > 0) break;
|
||||||
|
}
|
||||||
|
if (planes.length == 0) {
|
||||||
|
return null; // unpartitionable
|
||||||
|
}
|
||||||
|
|
||||||
|
int slabCount = planes.length + 1;
|
||||||
|
IntArrayList[] onPlaneLists = new IntArrayList[planes.length];
|
||||||
|
for (int i = 0; i < planes.length; i++) onPlaneLists[i] = new IntArrayList();
|
||||||
|
IntArrayList[] slabLists = new IntArrayList[slabCount];
|
||||||
|
for (int i = 0; i < slabCount; i++) slabLists[i] = new IntArrayList();
|
||||||
|
|
||||||
|
for (int quad : group) {
|
||||||
|
float lo = quads.boundsMin(quad, axis), hi = quads.boundsMax(quad, axis);
|
||||||
|
|
||||||
|
// slab = number of planes at or below this quad's lo
|
||||||
|
int slab = 0;
|
||||||
|
while (slab < planes.length && planes[slab] <= lo + QuadSet.EPSILON) slab++;
|
||||||
|
|
||||||
|
if (hi - lo <= QuadSet.EPSILON && slab > 0 && Math.abs(planes[slab - 1] - lo) <= QuadSet.EPSILON) {
|
||||||
|
onPlaneLists[slab - 1].add(quad);
|
||||||
|
} else {
|
||||||
|
slabLists[slab].add(quad);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Node[] slabs = new Node[slabCount];
|
||||||
|
for (int i = 0; i < slabCount; i++) {
|
||||||
|
int[] slabGroup = slabLists[i].toIntArray();
|
||||||
|
if (slabGroup.length == 0) {
|
||||||
|
slabs[i] = EMPTY;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
Node child = partition(quads, slabGroup, axis, collectedCuts);
|
||||||
|
if (child == null) return null;
|
||||||
|
slabs[i] = child;
|
||||||
|
}
|
||||||
|
|
||||||
|
int[][] onPlane = new int[planes.length][];
|
||||||
|
for (int i = 0; i < planes.length; i++) {
|
||||||
|
onPlane[i] = onPlaneLists[i].toIntArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
for (float plane : planes) {
|
||||||
|
collectedCuts[axis].add(plane);
|
||||||
|
}
|
||||||
|
return new Split(axis, planes, slabs, onPlane);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean isAllCoplanar(QuadSet quads, int[] group) {
|
||||||
|
for (int i = 1; i < group.length; i++) {
|
||||||
|
if (!quads.isCoplanar(group[0], group[i])) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static float[] cutsOn(QuadSet quads, int[] group, int axis) {
|
||||||
|
int n = group.length;
|
||||||
|
|
||||||
|
// a quad either spans an interval on this axis, or lies flat on a single plane
|
||||||
|
float[] starts = new float[n], ends = new float[n], points = new float[n];
|
||||||
|
int startCount = 0, endCount = 0, pointCount = 0;
|
||||||
|
|
||||||
|
for (int quad : group) {
|
||||||
|
float lo = quads.boundsMin(quad, axis), hi = quads.boundsMax(quad, axis);
|
||||||
|
|
||||||
|
if (hi - lo <= QuadSet.EPSILON) {
|
||||||
|
points[pointCount++] = lo;
|
||||||
|
} else {
|
||||||
|
starts[startCount++] = lo;
|
||||||
|
ends[endCount++] = hi;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
FloatArrays.quickSort(starts, 0, startCount);
|
||||||
|
FloatArrays.quickSort(ends, 0, endCount);
|
||||||
|
FloatArrays.quickSort(points, 0, pointCount);
|
||||||
|
|
||||||
|
FloatArrayList cuts = new FloatArrayList();
|
||||||
|
|
||||||
|
int nextStart = 0, nextEnd = 0, nextPoint = 0;
|
||||||
|
int opened = 0;
|
||||||
|
int below = 0;
|
||||||
|
int sinceCut = 0;
|
||||||
|
|
||||||
|
// only ends/planes can be cuts since a gap is always available at the last end before it,
|
||||||
|
// and a cut anywhere else in that gap would separate the same quads
|
||||||
|
while (nextEnd < endCount || nextPoint < pointCount) {
|
||||||
|
float plane = nextPoint == pointCount ? ends[nextEnd]
|
||||||
|
: nextEnd == endCount ? points[nextPoint]
|
||||||
|
: Math.min(ends[nextEnd], points[nextPoint]);
|
||||||
|
|
||||||
|
// ends first, then quads on the plane, then starts: a quad beginning here is not yet in the way
|
||||||
|
while (nextEnd < endCount && ends[nextEnd] <= plane + QuadSet.EPSILON) {
|
||||||
|
nextEnd++;
|
||||||
|
below++;
|
||||||
|
sinceCut++;
|
||||||
|
}
|
||||||
|
|
||||||
|
int onPlane = 0;
|
||||||
|
|
||||||
|
while (nextPoint < pointCount && points[nextPoint] <= plane + QuadSet.EPSILON) {
|
||||||
|
nextPoint++;
|
||||||
|
onPlane++;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (nextStart < startCount && starts[nextStart] < plane - QuadSet.EPSILON) {
|
||||||
|
nextStart++;
|
||||||
|
opened++;
|
||||||
|
}
|
||||||
|
|
||||||
|
int above = n - below - onPlane;
|
||||||
|
boolean free = opened == nextEnd; // nothing spans the plane
|
||||||
|
boolean useful = (below > 0 ? 1 : 0) + (onPlane > 0 ? 1 : 0) + (above > 0 ? 1 : 0) >= 2;
|
||||||
|
boolean separatesSomethingNew = onPlane > 0 || sinceCut > 0;
|
||||||
|
|
||||||
|
if (free && useful && separatesSomethingNew) {
|
||||||
|
cuts.add(plane);
|
||||||
|
sinceCut = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// quads on this plane are behind the next one either way
|
||||||
|
below += onPlane;
|
||||||
|
sinceCut += onPlane;
|
||||||
|
}
|
||||||
|
|
||||||
|
return cuts.toFloatArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
public float[] cutPlanes(int axis) {
|
||||||
|
return cutPlanes[axis];
|
||||||
|
}
|
||||||
|
|
||||||
|
public int @Nullable [] order(float camX, float camY, float camZ) {
|
||||||
|
int[] order = new int[quadCount];
|
||||||
|
write(root, camX, camY, camZ, order, 0);
|
||||||
|
return order;
|
||||||
|
}
|
||||||
|
|
||||||
|
// returns the position just past the last quad written
|
||||||
|
private static int write(Node node, float camX, float camY, float camZ, int[] order, int pos) {
|
||||||
|
if (node instanceof Leaf leaf) {
|
||||||
|
System.arraycopy(leaf.quads, 0, order, pos, leaf.quads.length);
|
||||||
|
return pos + leaf.quads.length;
|
||||||
|
}
|
||||||
|
|
||||||
|
Split split = (Split) node;
|
||||||
|
float camCoord = switch (split.axis) {
|
||||||
|
case 0 -> camX;
|
||||||
|
case 1 -> camY;
|
||||||
|
case 2 -> camZ;
|
||||||
|
default -> throw new AssertionError();
|
||||||
|
};
|
||||||
|
|
||||||
|
int slab = 0;
|
||||||
|
while (slab < split.planes.length && split.planes[slab] <= camCoord + QuadSet.EPSILON) slab++;
|
||||||
|
|
||||||
|
// slabs below the camera's, farthest first, each followed by the plane above it
|
||||||
|
for (int i = 0; i < slab; i++) {
|
||||||
|
pos = write(split.slabs[i], camX, camY, camZ, order, pos);
|
||||||
|
pos = writePlane(split.onPlane[i], order, pos);
|
||||||
|
}
|
||||||
|
|
||||||
|
// slabs above the camera's, farthest first, each followed by the plane below it
|
||||||
|
for (int i = split.slabs.length - 1; i > slab; i--) {
|
||||||
|
pos = write(split.slabs[i], camX, camY, camZ, order, pos);
|
||||||
|
pos = writePlane(split.onPlane[i - 1], order, pos);
|
||||||
|
}
|
||||||
|
|
||||||
|
// the camera's own slab is nearest, drawn last
|
||||||
|
return write(split.slabs[slab], camX, camY, camZ, order, pos);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int writePlane(int[] quads, int[] order, int pos) {
|
||||||
|
System.arraycopy(quads, 0, order, pos, quads.length);
|
||||||
|
return pos + quads.length;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int debugIndex() {
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int quadCount() {
|
||||||
|
return quadCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static final Node EMPTY = new Leaf(IntArrays.EMPTY_ARRAY);
|
||||||
|
|
||||||
|
sealed interface Node permits Leaf, Split {}
|
||||||
|
record Leaf(int[] quads) implements Node {}
|
||||||
|
record Split(int axis, float[] planes, Node[] slabs, int[][] onPlane) implements Node {}
|
||||||
|
}
|
||||||
@@ -128,4 +128,12 @@ public record QuadSet(int count, float[] centers, float[] bounds, float[] normal
|
|||||||
return !isCoplanar(p, q) && minDot(p, normalX(q), normalY(q), normalZ(q)) < dots[q] - EPSILON
|
return !isCoplanar(p, q) && minDot(p, normalX(q), normalY(q), normalZ(q)) < dots[q] - EPSILON
|
||||||
&& maxDot(q, normalX(p), normalY(p), normalZ(p)) > dots[p] + EPSILON;
|
&& maxDot(q, normalX(p), normalY(p), normalZ(p)) > dots[p] + EPSILON;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public float boundsMin(int quad, int axis) {
|
||||||
|
return bounds[quad * 6 + axis];
|
||||||
|
}
|
||||||
|
|
||||||
|
public float boundsMax(int quad, int axis) {
|
||||||
|
return bounds[quad * 6 + 3 + axis];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
+3
-3
@@ -4,7 +4,7 @@ public sealed interface SortState {
|
|||||||
SortState NONE = new None();
|
SortState NONE = new None();
|
||||||
|
|
||||||
/** Variant names for the debug overlay, indexed by {@link #debugIndex()} and ordered by sorting cost */
|
/** Variant names for the debug overlay, indexed by {@link #debugIndex()} and ordered by sorting cost */
|
||||||
String[] DEBUG_NAMES = { "NONE", "STATIC", "DYNAMIC" };
|
String[] DEBUG_NAMES = { "NONE", "STATIC", "TREE", "DYNAMIC" };
|
||||||
|
|
||||||
/** Position in {@link #DEBUG_NAMES}, doubling as the cost ranking used to pick a section's highest variant */
|
/** Position in {@link #DEBUG_NAMES}, doubling as the cost ranking used to pick a section's highest variant */
|
||||||
int debugIndex();
|
int debugIndex();
|
||||||
@@ -18,7 +18,7 @@ public sealed interface SortState {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** A state whose order depends on the camera, and so survives compaction to be re-sorted on every move */
|
/** A state whose order depends on the camera, and so survives compaction to be re-sorted on every move */
|
||||||
sealed interface Resortable extends SortState permits Dynamic {
|
sealed interface Resortable extends SortState permits Dynamic, PartitionTree {
|
||||||
@Override
|
@Override
|
||||||
default Resortable compactForStorage() {
|
default Resortable compactForStorage() {
|
||||||
return this;
|
return this;
|
||||||
@@ -66,7 +66,7 @@ public sealed interface SortState {
|
|||||||
record Dynamic(float[] centers) implements Resortable {
|
record Dynamic(float[] centers) implements Resortable {
|
||||||
@Override
|
@Override
|
||||||
public int debugIndex() {
|
public int debugIndex() {
|
||||||
return 2;
|
return 3;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
+7
@@ -80,6 +80,10 @@ public final class TranslucentSorter {
|
|||||||
return sortByDistance(dynamic.centers(), dynamic.quadCount(), camX, camY, camZ);
|
return sortByDistance(dynamic.centers(), dynamic.quadCount(), camX, camY, camZ);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (state instanceof PartitionTree tree) {
|
||||||
|
return tree.order(camX, camY, camZ);
|
||||||
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -150,6 +154,9 @@ public final class TranslucentSorter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
PartitionTree tree = PartitionTree.build(quads);
|
||||||
|
if (tree != null) return tree;
|
||||||
|
|
||||||
return new SortState.Dynamic(quads.centers());
|
return new SortState.Dynamic(quads.centers());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user