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 34 additions and 11 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);
}
}
/** 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;
public sealed interface SortState permits SortState.None, SortState.Static, SortState.Resortable {
public sealed interface SortState {
SortState NONE = new None();
/** 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
public SortState compactForStorage() {
return this;
return NONE;
}
}
@@ -116,19 +116,17 @@ public final class TranslucentSorter {
return SortState.NONE;
}
float[] normals = quads.normals();
int referenceOffset = reference * 3;
float nx = normals[referenceOffset], ny = normals[referenceOffset + 1], nz = normals[referenceOffset + 2];
float nx = quads.normalX(reference), ny = quads.normalY(reference), nz = quads.normalZ(reference);
BitSet flipped = new BitSet(quadCount);
boolean allParallel = true;
for (int quadIdx = 0; quadIdx < quadCount; quadIdx++) {
int normalOffset = quadIdx * 3;
float dot = nx * normals[normalOffset] + ny * normals[normalOffset + 1] + nz * normals[normalOffset + 2];
float dot = quads.normalDot(quadIdx, reference);
if (Math.abs(dot) < PARALLEL_THRESHOLD) {
// distinct normals, so which quad is in front depends on where the camera is
return new SortState.Dynamic(quads.centers());
allParallel = false;
break;
}
if (dot < 0) {
@@ -137,8 +135,20 @@ public final class TranslucentSorter {
}
}
// 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));
if (allParallel) {
// 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));
}
if (quadCount <= MAX_TOPO_QUADS) {
int[] order = topologicalOrder(quads);
if (order != null) {
return new SortState.Static(order);
}
}
return new SortState.Dynamic(quads.centers());
}
/**
@@ -201,4 +211,11 @@ public final class TranslucentSorter {
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;
}
}