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 66 additions and 24 deletions
Showing only changes of commit 407e193946 - Show all commits

No files matched your search

@@ -1,5 +1,7 @@
package org.embeddedt.embeddium.impl.render.chunk.sorting; package org.embeddedt.embeddium.impl.render.chunk.sorting;
import org.embeddedt.embeddium.impl.model.quad.properties.ModelQuadFacing;
public record QuadSet(int count, float[] centers, float[] bounds, float[] normals, float[] dots, byte[] facings) { public record QuadSet(int count, float[] centers, float[] bounds, float[] normals, float[] dots, byte[] facings) {
static final float EPSILON = 1e-4f; static final float EPSILON = 1e-4f;
@@ -55,8 +57,7 @@ public record QuadSet(int count, float[] centers, float[] bounds, float[] normal
} }
public boolean isCoplanar(int a, int b) { public boolean isCoplanar(int a, int b) {
int ao = a * 3, bo = b * 3; float normalDot = normalDot(a, b);
float normalDot = normals[ao] * normals[bo] + normals[ao + 1] * normals[bo + 1] + normals[ao + 2] * normals[bo + 2];
if (Math.abs(Math.abs(normalDot) - 1.0f) > EPSILON) { if (Math.abs(Math.abs(normalDot) - 1.0f) > EPSILON) {
return false; return false;
@@ -65,4 +66,60 @@ public record QuadSet(int count, float[] centers, float[] bounds, float[] normal
float offsetB = normalDot < 0 ? -dots[b] : dots[b]; float offsetB = normalDot < 0 ? -dots[b] : dots[b];
return Math.abs(dots[a] - offsetB) < EPSILON; return Math.abs(dots[a] - offsetB) < EPSILON;
} }
public float normalDot(int a, int b) {
int ao = a * 3, bo = b * 3;
return normals[ao] * normals[bo] + normals[ao + 1] * normals[bo + 1] + normals[ao + 2] * normals[bo + 2];
}
public boolean isDegenerate(int quad) {
int normalOffset = quad * 3;
return normals[normalOffset] == 0 && normals[normalOffset + 1] == 0 && normals[normalOffset + 2] == 0;
}
public float normalX(int quad) {
return normals[quad * 3];
}
public float normalY(int quad) {
return normals[quad * 3 + 1];
}
public float normalZ(int quad) {
return normals[quad * 3 + 2];
}
public ModelQuadFacing facing(int quad) {
return ModelQuadFacing.VALUES[facings[quad]];
}
public Bounds union() {
float minX = Float.POSITIVE_INFINITY, minY = Float.POSITIVE_INFINITY, minZ = Float.POSITIVE_INFINITY;
float maxX = Float.NEGATIVE_INFINITY, maxY = Float.NEGATIVE_INFINITY, maxZ = Float.NEGATIVE_INFINITY;
for (int quad = 0; quad < count; quad++) {
int boundOffset = quad * 6;
minX = Math.min(minX, bounds[boundOffset + 0]);
minY = Math.min(minY, bounds[boundOffset + 1]);
minZ = Math.min(minZ, bounds[boundOffset + 2]);
maxX = Math.max(maxX, bounds[boundOffset + 3]);
maxY = Math.max(maxY, bounds[boundOffset + 4]);
maxZ = Math.max(maxZ, bounds[boundOffset + 5]);
}
return new Bounds(minX, minY, minZ, maxX, maxY, maxZ);
}
public record Bounds(float minX, float minY, float minZ, float maxX, float maxY, float maxZ) {
public float minDot(float nx, float ny, float nz) {
return nx * (nx >= 0 ? minX : maxX) + ny * (ny >= 0 ? minY : maxY) + nz * (nz >= 0 ? minZ : maxZ);
}
public float maxDot(float nx, float ny, float nz) {
return nx * (nx >= 0 ? maxX : minX) + ny * (ny >= 0 ? maxY : minY) + nz * (nz >= 0 ? maxZ : minZ);
}
}
} }
@@ -174,25 +174,10 @@ public final class TranslucentSorter {
* through the others. * through the others.
*/ */
private static boolean allOutwardOnBoundingBox(QuadSet quads) { private static boolean allOutwardOnBoundingBox(QuadSet quads) {
float[] bounds = quads.bounds(); QuadSet.Bounds box = quads.union();
int quadCount = quads.count();
float[] boxMin = { bounds[0], bounds[1], bounds[2] }; for (int quadIdx = 0; quadIdx < quads.count(); quadIdx++) {
float[] boxMax = { bounds[3], bounds[4], bounds[5] }; ModelQuadFacing facing = quads.facing(quadIdx);
for (int quadIdx = 1; quadIdx < quadCount; quadIdx++) {
int boundsOffset = quadIdx * 6;
for (int axis = 0; axis < 3; axis++) {
boxMin[axis] = Math.min(boxMin[axis], bounds[boundsOffset + axis]);
boxMax[axis] = Math.max(boxMax[axis], bounds[boundsOffset + 3 + axis]);
}
}
byte[] facings = quads.facings();
for (int quadIdx = 0; quadIdx < quadCount; quadIdx++) {
ModelQuadFacing facing = ModelQuadFacing.VALUES[facings[quadIdx]];
if (!facing.isDirection()) { if (!facing.isDirection()) {
// A slanted or degenerate quad is not flat on any face of the box // A slanted or degenerate quad is not flat on any face of the box
@@ -205,11 +190,11 @@ public final class TranslucentSorter {
float quadMin = quads.minDot(quadIdx, nx, ny, nz); float quadMin = quads.minDot(quadIdx, nx, ny, nz);
float quadMax = quads.maxDot(quadIdx, nx, ny, nz); float quadMax = quads.maxDot(quadIdx, nx, ny, nz);
int axis = facing.getAxis().ordinal(); if (Math.abs(quadMax - quadMin) > QuadSet.EPSILON) {
boolean positive = facing.getAxis().getFacing(true) == facing; return false;
float boxFar = positive ? boxMax[axis] : -boxMin[axis]; }
if (Math.abs(quadMax - quadMin) > QuadSet.EPSILON || Math.abs(quadMax - boxFar) > QuadSet.EPSILON) { if (Math.abs(quadMax - box.maxDot(nx, ny, nz)) > QuadSet.EPSILON) {
return false; return false;
} }
} }