Smarter translucency sorting #42
+42
-42
@@ -1,7 +1,5 @@
|
|||||||
package org.embeddedt.embeddium.impl.render.chunk.sorting;
|
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.model.quad.properties.ModelQuadFacing;
|
||||||
import org.embeddedt.embeddium.impl.util.sorting.MergeSort;
|
import org.embeddedt.embeddium.impl.util.sorting.MergeSort;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
@@ -230,55 +228,57 @@ public final class TranslucentSorter {
|
|||||||
return topologicalOrder(quads, all);
|
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) {
|
static int @Nullable [] topologicalOrder(QuadSet quads, int[] group) {
|
||||||
int n = group.length;
|
int n = group.length;
|
||||||
|
|
||||||
IntArrayList[] successors = new IntArrayList[n];
|
// 0 = unvisited, 1 = on the stack, 2 = finished
|
||||||
int[] predecessors = new int[n];
|
byte[] state = new byte[n];
|
||||||
|
int[] stack = new int[n], next = 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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int[] out = new int[n];
|
int[] out = new int[n];
|
||||||
int o = 0;
|
int finished = 0;
|
||||||
|
|
||||||
while (!ready.isEmpty()) {
|
for (int root = 0; root < n; root++) {
|
||||||
int current = ready.dequeueInt();
|
if (state[root] != 0) continue;
|
||||||
out[o++] = group[current];
|
|
||||||
|
|
||||||
IntArrayList next = successors[current];
|
int depth = 0;
|
||||||
if (next == null) continue;
|
stack[0] = root;
|
||||||
|
next[0] = 0;
|
||||||
|
state[root] = 1;
|
||||||
|
|
||||||
for (int i = 0; i < next.size(); i++) {
|
while (depth >= 0) {
|
||||||
int successor = next.getInt(i);
|
int current = stack[depth];
|
||||||
|
int candidate = next[depth];
|
||||||
|
|
||||||
if (--predecessors[successor] == 0) {
|
// advance to the next quad that must be drawn after the current one
|
||||||
ready.enqueue(successor);
|
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