Smarter translucency sorting #42
+42
-42
@@ -1,7 +1,5 @@
|
||||
package org.embeddedt.embeddium.impl.render.chunk.sorting;
|
||||
|
||||
import it.unimi.dsi.fastutil.ints.IntArrayFIFOQueue;
|
||||
import it.unimi.dsi.fastutil.ints.IntArrayList;
|
||||
import org.embeddedt.embeddium.impl.model.quad.properties.ModelQuadFacing;
|
||||
import org.embeddedt.embeddium.impl.util.sorting.MergeSort;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
@@ -230,55 +228,57 @@ public final class TranslucentSorter {
|
||||
return topologicalOrder(quads, all);
|
||||
}
|
||||
|
||||
/** the group's quads in an order correct from every camera, or null if their visibility relation has a cycle */
|
||||
/**
|
||||
* The group's quads in an order correct from every camera, or null if their visibility relation has a cycle.
|
||||
* A depth-first search over implicit edges: an acyclic group costs every pair, a cyclic one only until the first
|
||||
* back edge, which dense cyclic geometry produces almost immediately.
|
||||
*/
|
||||
static int @Nullable [] topologicalOrder(QuadSet quads, int[] group) {
|
||||
int n = group.length;
|
||||
|
||||
IntArrayList[] successors = new IntArrayList[n];
|
||||
int[] predecessors = new int[n];
|
||||
|
||||
// build the graph up
|
||||
for (int quadIdx = 0; quadIdx < n; quadIdx++) {
|
||||
for (int otherIdx = 0; otherIdx < n; otherIdx++) {
|
||||
if (quadIdx != otherIdx && quads.isSeenThrough(group[quadIdx], group[otherIdx])) {
|
||||
if (successors[quadIdx] == null) {
|
||||
successors[quadIdx] = new IntArrayList();
|
||||
}
|
||||
|
||||
successors[quadIdx].add(otherIdx);
|
||||
predecessors[otherIdx]++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// kahn's algorithm: emit quads with no predecessors and remove them from the graph
|
||||
// until either all quads are emitted or a cycle is detected
|
||||
IntArrayFIFOQueue ready = new IntArrayFIFOQueue(n);
|
||||
for (int quadIdx = 0; quadIdx < n; quadIdx++) {
|
||||
if (predecessors[quadIdx] == 0) {
|
||||
ready.enqueue(quadIdx);
|
||||
}
|
||||
}
|
||||
|
||||
// 0 = unvisited, 1 = on the stack, 2 = finished
|
||||
byte[] state = new byte[n];
|
||||
int[] stack = new int[n], next = new int[n];
|
||||
int[] out = new int[n];
|
||||
int o = 0;
|
||||
int finished = 0;
|
||||
|
||||
while (!ready.isEmpty()) {
|
||||
int current = ready.dequeueInt();
|
||||
out[o++] = group[current];
|
||||
for (int root = 0; root < n; root++) {
|
||||
if (state[root] != 0) continue;
|
||||
|
||||
IntArrayList next = successors[current];
|
||||
if (next == null) continue;
|
||||
int depth = 0;
|
||||
stack[0] = root;
|
||||
next[0] = 0;
|
||||
state[root] = 1;
|
||||
|
||||
for (int i = 0; i < next.size(); i++) {
|
||||
int successor = next.getInt(i);
|
||||
while (depth >= 0) {
|
||||
int current = stack[depth];
|
||||
int candidate = next[depth];
|
||||
|
||||
if (--predecessors[successor] == 0) {
|
||||
ready.enqueue(successor);
|
||||
}
|
||||
}
|
||||
// advance to the next quad that must be drawn after the current one
|
||||
while (candidate < n && (state[candidate] == 2 || candidate == current
|
||||
|| !quads.isSeenThrough(group[current], group[candidate]))) {
|
||||
candidate++;
|
||||
}
|
||||
|
||||
return o == n ? out : null;
|
||||
if (candidate == n) {
|
||||
state[current] = 2;
|
||||
out[n - 1 - finished++] = group[current]; // reverse postorder: predecessors first
|
||||
depth--;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (state[candidate] == 1) {
|
||||
return null; // back edge: a cycle
|
||||
}
|
||||
|
||||
next[depth] = candidate + 1;
|
||||
depth++;
|
||||
stack[depth] = candidate;
|
||||
next[depth] = 0;
|
||||
state[candidate] = 1;
|
||||
}
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user