Smarter translucency sorting #42

Merged
embeddedt merged 24 commits from rdh/celeritas:ts into stonecutter 2026-09-16 11:25:42 -04:00
Showing only changes of commit 62a85cea4d - Show all commits
@@ -80,7 +80,7 @@ public final class PartitionTree implements SortState.Resortable {
if (planes.length > 0) break;
}
if (planes.length == 0) {
return null; // unpartitionable
return partitionIntersectors(quads, group, collectedCuts);
}
int slabCount = planes.length + 1;
@@ -126,6 +126,54 @@ public final class PartitionTree implements SortState.Resortable {
return new Split(axis, planes, slabs, onPlane);
}
private static @Nullable Node partitionIntersectors(QuadSet quads, int[] group, FloatArrayList[] collectedCuts) {
int n = group.length;
if (n > TranslucentSorter.MAX_TOPO_QUADS) return null;
int[] degree = new int[n];
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (intersects(quads, group[i], group[j])) {
degree[i]++;
degree[j]++;
}
}
}
boolean[] primary = new boolean[n];
int count = 0;
while (true) {
int best = -1;
for (int i = 0; i < n; i++) {
if (degree[i] > 0 && (best < 0 || degree[i] > degree[best])) best = i;
}
if (best < 0) break;
primary[best] = true;
count++;
degree[best] = 0;
for (int j = 0; j < n; j++) {
if (!primary[j] && intersects(quads, group[best], group[j])) degree[j]--;
}
}
if (count == 0 || count == n) return null;
int[] first = new int[count], rest = new int[n - count];
for (int i = 0, f = 0, r = 0; i < n; i++) {
if (primary[i]) first[f++] = group[i]; else rest[r++] = group[i];
}
Node a = partition(quads, first, 2, collectedCuts);
Node b = a == null ? null : partition(quads, rest, 2, collectedCuts);
return b == null ? null : new Fixed(a, b);
}
private static boolean intersects(QuadSet quads, int p, int q) {
for (int axis = 0; axis < 3; axis++) {
if (quads.boundsMin(p, axis) >= quads.boundsMax(q, axis) - QuadSet.EPSILON
|| quads.boundsMin(q, axis) >= quads.boundsMax(p, axis) - QuadSet.EPSILON) return false;
}
return true;
}
private static boolean isAllCoplanar(QuadSet quads, int[] group) {
for (int i = 1; i < group.length; i++) {
if (!quads.isCoplanar(group[0], group[i])) {
@@ -225,6 +273,10 @@ public final class PartitionTree implements SortState.Resortable {
return pos + leaf.quads.length;
}
if (node instanceof Fixed fixed) {
return write(fixed.rest, camX, camY, camZ, order, write(fixed.first, camX, camY, camZ, order, pos));
}
Split split = (Split) node;
float camCoord = switch (split.axis) {
case 0 -> camX;
@@ -285,7 +337,8 @@ public final class PartitionTree implements SortState.Resortable {
private static final Node EMPTY = new Leaf(IntArrays.EMPTY_ARRAY);
sealed interface Node permits Leaf, Split {}
sealed interface Node permits Leaf, Split, Fixed {}
record Leaf(int[] quads) implements Node {}
record Split(int axis, float[] planes, Node[] slabs, int[][] onPlane) implements Node {}
record Fixed(Node first, Node rest) implements Node {}
}