rasterized occlusion culling #38
No files matched your search
+30
@@ -1,5 +1,6 @@
|
||||
package org.embeddedt.embeddium.impl.render.chunk.bench.voxel;
|
||||
|
||||
import org.embeddedt.embeddium.impl.render.chunk.occlusion.OccluderBoxBuilder;
|
||||
import org.embeddedt.embeddium.impl.render.chunk.occlusion.SectionVisibilityBuilder;
|
||||
import org.embeddedt.embeddium.impl.render.chunk.occlusion.VisibilityEncoding;
|
||||
|
||||
@@ -102,6 +103,35 @@ public final class VoxelSlice {
|
||||
return occluder.computeVisibilityEncoding();
|
||||
}
|
||||
|
||||
public int[] occlusionData() {
|
||||
if (this.empty) {
|
||||
return null;
|
||||
}
|
||||
|
||||
var builder = new OccluderBoxBuilder();
|
||||
byte[] blocks = this.blocks;
|
||||
|
||||
for (int y = 0; y < 16; y++) {
|
||||
for (int z = 0; z < 16; z++) {
|
||||
int row = ((y + 1) * AREA) + ((z + 1) * SIZE) + 1;
|
||||
|
||||
for (int x = 0; x < 16; x++) {
|
||||
int block = blocks[row + x];
|
||||
|
||||
if (VoxelWorld.isOpaque(block)) {
|
||||
builder.markOpaque(x, y, z);
|
||||
}
|
||||
|
||||
if (VoxelWorld.isRenderable(block)) {
|
||||
builder.markRenderable(x, y, z);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
public long[] opacityBits() {
|
||||
var bits = new long[64];
|
||||
|
||||
|
||||
+25
-5
@@ -11,20 +11,40 @@ import java.util.BitSet;
|
||||
// can keep using that and supply opacity here instead of paying for a second flood fill it doesn't need
|
||||
public final class OccluderBoxBuilder {
|
||||
private final BitSet blocks = new BitSet(SectionVisibilityBuilder.TOTAL_BLOCKS);
|
||||
private final RenderableBounds bounds = new RenderableBounds();
|
||||
|
||||
public void markOpaque(int x, int y, int z) {
|
||||
this.blocks.set(SectionVisibilityBuilder.getIndex(x & 15, y & 15, z & 15));
|
||||
}
|
||||
|
||||
public int @Nullable [] build() {
|
||||
return boxesFrom(this.blocks);
|
||||
public void markRenderable(int x, int y, int z) {
|
||||
this.bounds.mark(x & 15, y & 15, z & 15);
|
||||
}
|
||||
|
||||
static int @Nullable [] boxesFrom(BitSet blocks) {
|
||||
if (blocks.isEmpty()) {
|
||||
public int @Nullable [] build() {
|
||||
return dataFrom(this.blocks, this.bounds);
|
||||
}
|
||||
|
||||
public static int bounds(int[] data) {
|
||||
return data[0];
|
||||
}
|
||||
|
||||
static int @Nullable [] dataFrom(BitSet blocks, RenderableBounds bounds) {
|
||||
if (blocks.isEmpty() && bounds.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return OccluderBoxes.from(Arrays.copyOf(blocks.toLongArray(), BoxFinder.INTERIOR_CACHE_WORDS));
|
||||
int[] boxes = blocks.isEmpty() ? null
|
||||
: OccluderBoxes.from(Arrays.copyOf(blocks.toLongArray(), BoxFinder.INTERIOR_CACHE_WORDS));
|
||||
|
||||
int count = boxes == null ? 0 : boxes.length;
|
||||
int[] data = new int[count + 1];
|
||||
data[0] = bounds.packed();
|
||||
|
||||
if (boxes != null) {
|
||||
System.arraycopy(boxes, 0, data, 1, count);
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
}
|
||||
+25
-25
@@ -14,9 +14,18 @@ public final class RasterOccluder extends BoxOccluder {
|
||||
/** Beyond 7 chunks, expand tests by a block to absorb rasterization error. From canvas's trial and error. */
|
||||
private static final int FUZZ_SQUARED_CHUNK_DIST = 7 * 7;
|
||||
|
||||
private int maxDrawnSquaredDist;
|
||||
private int backtrackCount;
|
||||
|
||||
public enum SectionVisibility {
|
||||
/** nothing of the section is visible, and nothing behind it can be reached through it */
|
||||
HIDDEN,
|
||||
/** the section draws nothing visible, but the space it occupies is not fully covered, so
|
||||
* traversal must continue through it or geometry behind it would be lost */
|
||||
TRAVERSABLE,
|
||||
/** the section is visible and can be used to occlude other sections */
|
||||
VISIBLE
|
||||
}
|
||||
|
||||
public RasterOccluder() {
|
||||
super(new PerspectiveRasterizer());
|
||||
}
|
||||
@@ -45,7 +54,6 @@ public final class RasterOccluder extends BoxOccluder {
|
||||
|
||||
var transform = viewport.getTransform();
|
||||
|
||||
this.maxDrawnSquaredDist = 0;
|
||||
this.backtrackCount = 0;
|
||||
|
||||
invalidate();
|
||||
@@ -54,42 +62,34 @@ public final class RasterOccluder extends BoxOccluder {
|
||||
}
|
||||
|
||||
/**
|
||||
* {@return whether any part of the section is unoccluded}
|
||||
*
|
||||
* <p>Must be called before {@link #drawOccluders}, or a section would occlude itself.
|
||||
* tests the tight bounds of what the section draws first. a section whose geometry is hidden
|
||||
* may still have unoccluded empty space, so a second test decides whether traversal continues.
|
||||
* must run before occlude(), or a section would occlude itself.
|
||||
*/
|
||||
public boolean isSectionVisible(int originX, int originY, int originZ, int squaredChunkDist) {
|
||||
public SectionVisibility testSection(int originX, int originY, int originZ, int squaredChunkDist, int bounds) {
|
||||
prepareRegion(originX, originY, originZ,
|
||||
PackedBox.rangeFromSquareChunkDist(squaredChunkDist), squaredChunkDist);
|
||||
|
||||
if (squaredChunkDist <= NEAR_SQUARED_CHUNK_DIST) {
|
||||
return true;
|
||||
return SectionVisibility.VISIBLE;
|
||||
}
|
||||
|
||||
boolean visible = isBoxVisible(PackedBox.FULL_BOX,
|
||||
squaredChunkDist >= FUZZ_SQUARED_CHUNK_DIST ? 1 : 0);
|
||||
int fuzz = squaredChunkDist >= FUZZ_SQUARED_CHUNK_DIST ? 1 : 0;
|
||||
|
||||
if (!visible && squaredChunkDist < this.maxDrawnSquaredDist) {
|
||||
if (isBoxVisible(bounds, fuzz)) {
|
||||
return SectionVisibility.VISIBLE;
|
||||
}
|
||||
|
||||
if (squaredChunkDist < maxSquaredChunkDistance()) {
|
||||
this.backtrackCount++;
|
||||
}
|
||||
|
||||
return visible;
|
||||
// only worth asking when the bounds were tighter than the section itself
|
||||
if (bounds != PackedBox.FULL_BOX && isBoxVisible(PackedBox.FULL_BOX, fuzz)) {
|
||||
return SectionVisibility.TRAVERSABLE;
|
||||
}
|
||||
|
||||
public void drawOccluders(int[] boxes, int squaredChunkDist) {
|
||||
int range = PackedBox.rangeFromSquareChunkDist(squaredChunkDist);
|
||||
|
||||
for (int box : boxes) {
|
||||
if (range > PackedBox.range(box)) {
|
||||
break;
|
||||
}
|
||||
|
||||
occludeBox(box);
|
||||
}
|
||||
|
||||
if (squaredChunkDist > this.maxDrawnSquaredDist) {
|
||||
this.maxDrawnSquaredDist = squaredChunkDist;
|
||||
}
|
||||
return SectionVisibility.HIDDEN;
|
||||
}
|
||||
|
||||
// the buffer holds coverage only, with no depth, so it's correct only if sections are drawn
|
||||
|
||||
Reference in new issue
Block a user