Smarter translucency sorting #42

Merged
embeddedt merged 24 commits from rdh/celeritas:ts into stonecutter 2026-09-16 11:25:42 -04:00
3 changed files with 32 additions and 9 deletions
Showing only changes of commit d6a8cc0868 - Show all commits

No files matched your search

@@ -122,4 +122,10 @@ public record QuadSet(int count, float[] centers, float[] bounds, float[] normal
return nx * (nx >= 0 ? maxX : minX) + ny * (ny >= 0 ? maxY : minY) + nz * (nz >= 0 ? maxZ : minZ); return nx * (nx >= 0 ? maxX : minX) + ny * (ny >= 0 ? maxY : minY) + nz * (nz >= 0 ? maxZ : minZ);
} }
} }
/** if p must be drawn before q */
public boolean isSeenThrough(int p, int q) {
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;
}
} }
@@ -1,6 +1,6 @@
package org.embeddedt.embeddium.impl.render.chunk.sorting; package org.embeddedt.embeddium.impl.render.chunk.sorting;
public sealed interface SortState permits SortState.None, SortState.Static, SortState.Resortable { 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 */
@@ -39,7 +39,7 @@ public sealed interface SortState permits SortState.None, SortState.Static, Sort
@Override @Override
public SortState compactForStorage() { public SortState compactForStorage() {
return this; return NONE;
} }
} }
@@ -116,19 +116,17 @@ public final class TranslucentSorter {
return SortState.NONE; return SortState.NONE;
} }
float[] normals = quads.normals(); float nx = quads.normalX(reference), ny = quads.normalY(reference), nz = quads.normalZ(reference);
int referenceOffset = reference * 3;
float nx = normals[referenceOffset], ny = normals[referenceOffset + 1], nz = normals[referenceOffset + 2];
BitSet flipped = new BitSet(quadCount); BitSet flipped = new BitSet(quadCount);
boolean allParallel = true;
for (int quadIdx = 0; quadIdx < quadCount; quadIdx++) { for (int quadIdx = 0; quadIdx < quadCount; quadIdx++) {
int normalOffset = quadIdx * 3; float dot = quads.normalDot(quadIdx, reference);
float dot = nx * normals[normalOffset] + ny * normals[normalOffset + 1] + nz * normals[normalOffset + 2];
if (Math.abs(dot) < PARALLEL_THRESHOLD) { if (Math.abs(dot) < PARALLEL_THRESHOLD) {
// distinct normals, so which quad is in front depends on where the camera is allParallel = false;
return new SortState.Dynamic(quads.centers()); break;
} }
if (dot < 0) { if (dot < 0) {
@@ -137,10 +135,22 @@ public final class TranslucentSorter {
} }
} }
if (allParallel) {
// parallel but spread over several planes: the order cannot change, so sort once and keep only the result // parallel but spread over several planes: the order cannot change, so sort once and keep only the result
return new SortState.Static(sortNormalRelative(quads.centers(), quadCount, nx, ny, nz, flipped)); return new SortState.Static(sortNormalRelative(quads.centers(), quadCount, nx, ny, nz, flipped));
} }
if (quadCount <= MAX_TOPO_QUADS) {
int[] order = topologicalOrder(quads);
if (order != null) {
return new SortState.Static(order);
}
}
return new SortState.Dynamic(quads.centers());
}
/** /**
* The first quad with a non-zero normal, or -1 if every quad is degenerate * The first quad with a non-zero normal, or -1 if every quad is degenerate
*/ */
@@ -201,4 +211,11 @@ public final class TranslucentSorter {
return true; return true;
} }
public static final int MAX_TOPO_QUADS = 2048;
/** returns null if the relation has a cycle */
public static int @Nullable [] topologicalOrder(QuadSet quads) {
return null;
}
} }