Smarter translucency sorting #42

Merged
embeddedt merged 24 commits from rdh/celeritas:ts into stonecutter 2026-09-16 11:25:42 -04:00
2 changed files with 28 additions and 8 deletions
Showing only changes of commit 63b910add3 - Show all commits

No files matched your search

@@ -4,6 +4,7 @@ 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.embeddedt.embeddium.impl.util.sorting.MergeSort;
import org.jetbrains.annotations.Nullable;
import java.util.Collection;
@@ -68,10 +69,15 @@ public final class PartitionTree implements SortState.Resortable {
}
private static Node partition(QuadSet quads, int[] group, int parentAxis, FloatArrayList[] collectedCuts) {
if (group.length <= 1 || isAllCoplanar(quads, group)) {
if (group.length <= 1) {
return new Leaf(group);
}
int[] parallel = parallelOrder(quads, group);
if (parallel != null) {
return new Leaf(parallel);
}
int axis = -1;
float[] planes = FloatArrays.EMPTY_ARRAY;
for (int i = 0; i < 3; i++) {
@@ -174,13 +180,27 @@ public final class PartitionTree implements SortState.Resortable {
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])) {
return false;
private static int @Nullable [] parallelOrder(QuadSet quads, int[] group) {
int reference = group[0];
float nx = quads.normalX(reference), ny = quads.normalY(reference), nz = quads.normalZ(reference);
float[] centers = quads.centers();
int far = TranslucentSorter.FAKE_STATIC_CAMERA_OFFSET;
float camX = centers[reference * 3] + nx * far, camY = centers[reference * 3 + 1] + ny * far, camZ = centers[reference * 3 + 2] + nz * far;
float[] keys = new float[group.length];
for (int i = 0; i < group.length; i++) {
int quad = group[i];
float dot = quads.normalDot(quad, reference);
if (Math.abs(dot) < 1 - QuadSet.EPSILON) return null;
int c = quad * 3;
keys[i] = (nx * (centers[c] - camX) + ny * (centers[c + 1] - camY) + nz * (centers[c + 2] - camZ)) * (dot < 0 ? 1 : -1);
}
}
return true;
int[] byKey = MergeSort.mergeSort(keys);
int[] order = new int[group.length];
for (int i = 0; i < order.length; i++) order[i] = group[byKey[i]];
return order;
}
private static float[] cutsOn(QuadSet quads, int[] group, int axis) {
@@ -13,7 +13,7 @@ public final class TranslucentSorter {
* How far along the shared normal the imaginary camera used by {@link #sortNormalRelative} sits. Any value
* comfortably beyond the section is equivalent, since only the ordering of the projections matters.
*/
private static final int FAKE_STATIC_CAMERA_OFFSET = 1000;
static final int FAKE_STATIC_CAMERA_OFFSET = 1000;
/**
* How closely two normals must agree to be treated as parallel. Only +/-1 truly imply a shared normal, but