Fix beacons not enabling/disabling correctly

This commit is contained in:
James Seibel
2024-08-09 07:25:24 -05:00
parent 0d6ec3d836
commit 7c11bb4258
5 changed files with 65 additions and 72 deletions
@@ -147,7 +147,7 @@ public class DhLightingEngine
{
for (int relZ = 0; relZ < LodUtil.CHUNK_WIDTH; relZ++)
{
// set each pos' sky light all the way down until a opaque block is hit
// set each pos' sky light all the way down until an opaque block is hit
for (int y = maxY; y >= minY; y--)
{
IBlockStateWrapper block = chunk.getBlockState(relX, y, relZ);
@@ -189,7 +189,7 @@ public class LodQuadTree extends QuadTree<LodRenderSection> implements IDebugRen
try
{
LodRenderSection renderSection = this.getValue(pos);
if (renderSection != null && renderSection.renderingEnabled)
if (renderSection != null && renderSection.getRenderingEnabled())
{
renderSection.uploadRenderDataToGpuAsync();
}
@@ -296,7 +296,7 @@ public class LodQuadTree extends QuadTree<LodRenderSection> implements IDebugRen
if (DhSectionPos.getDetailLevel(sectionPos) > expectedDetailLevel)
{
// section detail level too high //
boolean thisPosIsRendering = renderSection.renderingEnabled;
boolean thisPosIsRendering = renderSection.getRenderingEnabled();
boolean allChildrenSectionsAreLoaded = true;
// recursively update all child render sections
@@ -314,24 +314,8 @@ public class LodQuadTree extends QuadTree<LodRenderSection> implements IDebugRen
}
else
{
if (renderSection.renderingEnabled
&& Config.Client.Advanced.Debugging.DebugWireframe.showRenderSectionStatus.get())
{
// show that this position has just been disabled
DebugRenderer.makeParticle(
new DebugRenderer.BoxParticle(
new DebugRenderer.Box(renderSection.pos, 128f, 156f, 0.09f, Color.CYAN.darker()),
0.2, 32f
)
);
}
// all child positions are loaded, disable this section and enable its children.
if (renderSection.renderingEnabled)
{
this.level.unloadBeaconBeamsInPos(renderSection.pos);
}
renderSection.renderingEnabled = false;
renderSection.setRenderingEnabled(false);
// walk back down the tree and enable the child sections //TODO there are probably more efficient ways of doing this, but this will work for now
for (int i = 0; i < 4; i++)
@@ -382,17 +366,14 @@ public class LodQuadTree extends QuadTree<LodRenderSection> implements IDebugRen
if (!parentSectionIsRendering && renderSection.canRender())
{
// if rendering is already enabled we don't have to re-enable it
if (!renderSection.renderingEnabled)
if (!renderSection.getRenderingEnabled())
{
renderSection.renderingEnabled = true;
this.level.loadBeaconBeamsInPos(renderSection.pos);
// delete/disable children, all of them will be a lower detail level than requested
quadNode.deleteAllChildren((childRenderSection) ->
{
if (childRenderSection != null)
{
if (childRenderSection.renderingEnabled)
if (childRenderSection.getRenderingEnabled())
{
// show that this position's rendering has been disabled due to a parent rendering
DebugRenderer.makeParticle(
@@ -403,10 +384,12 @@ public class LodQuadTree extends QuadTree<LodRenderSection> implements IDebugRen
);
}
childRenderSection.renderingEnabled = false;
childRenderSection.setRenderingEnabled(false);
childRenderSection.close();
}
});
renderSection.setRenderingEnabled(true);
}
}
@@ -70,7 +70,7 @@ public class LodRenderSection implements IDebugRenderable, AutoCloseable
private final LodQuadTree quadTree;
public boolean renderingEnabled = false;
private boolean renderingEnabled = false;
private boolean canRender = false;
/** this reference is necessary so we can determine what VBO to render */
@@ -392,6 +392,38 @@ public class LodRenderSection implements IDebugRenderable, AutoCloseable
public boolean canRender() { return this.canRender; }
public boolean getRenderingEnabled() { return this.renderingEnabled; }
public void setRenderingEnabled(boolean enabled)
{
// some logic should only be run when enabling/disabling
// a section for the first time
boolean stateChanged = (this.renderingEnabled != enabled);
if (stateChanged)
{
if (enabled)
{
this.level.loadBeaconBeamsInPos(this.pos);
}
else
{
this.level.unloadBeaconBeamsInPos(this.pos);
if (Config.Client.Advanced.Debugging.DebugWireframe.showRenderSectionStatus.get())
{
// show that this position has just been disabled
DebugRenderer.makeParticle(
new DebugRenderer.BoxParticle(
new DebugRenderer.Box(this.pos, 128f, 156f, 0.09f, Color.CYAN.darker()),
0.2, 32f
)
);
}
}
}
this.renderingEnabled = enabled;
}
public boolean gpuUploadInProgress() { return this.buildAndUploadRenderDataToGpuFuture != null; }
@@ -311,7 +311,7 @@ public class RenderBufferHandler implements AutoCloseable
}
ColumnRenderBuffer buffer = renderSection.renderBuffer;
if (buffer == null || !renderSection.renderingEnabled)
if (buffer == null || !renderSection.getRenderingEnabled())
{
continue;
}
@@ -22,7 +22,6 @@ package com.seibel.distanthorizons.core.render.renderer.generic;
import com.seibel.distanthorizons.api.enums.rendering.EDhApiBlockMaterial;
import com.seibel.distanthorizons.api.interfaces.render.IDhApiRenderableBoxGroup;
import com.seibel.distanthorizons.api.objects.math.DhApiVec3d;
import com.seibel.distanthorizons.api.objects.math.DhApiVec3f;
import com.seibel.distanthorizons.api.objects.render.DhApiRenderableBox;
import com.seibel.distanthorizons.api.objects.render.DhApiRenderableBoxGroupShading;
import com.seibel.distanthorizons.core.config.Config;
@@ -42,7 +41,6 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
public class BeaconRenderHandler
{
@@ -56,7 +54,7 @@ public class BeaconRenderHandler
private final BeaconBeamRepo beaconBeamRepo;
private final IDhApiRenderableBoxGroup beaconBoxGroup;
private final HashMap<DhBlockPos, AtomicInteger> beaconRefCountByBlockPos = new HashMap<>();
private final HashSet<DhBlockPos> beaconBlockPosSet = new HashSet<>();
@@ -143,7 +141,7 @@ public class BeaconRenderHandler
{
// beam no longer exists at position, remove from DB
this.beaconBeamRepo.deleteWithKey(beaconPos); // TODO broken when updating adjacent chunks
this.stopRenderingBeaconAtPos(beaconPos, true);
this.stopRenderingBeaconAtPos(beaconPos);
}
}
@@ -168,7 +166,7 @@ public class BeaconRenderHandler
for (int i = 0; i < existingBeamList.size(); i++)
{
BeaconBeamDTO beam = existingBeamList.get(i);
this.stopRenderingBeaconAtPos(beam.pos, false);
this.stopRenderingBeaconAtPos(beam.pos);
}
}
@@ -180,52 +178,32 @@ public class BeaconRenderHandler
private void startRenderingBeacon(BeaconBeamDTO beacon)
{
this.beaconRefCountByBlockPos.compute(beacon.pos, (beamPos, beaconRefCount) ->
if (this.beaconBlockPosSet.add(beacon.pos))
{
if (beaconRefCount == null) { beaconRefCount = new AtomicInteger(); }
if (beaconRefCount.getAndIncrement() == 0)
{
DhApiRenderableBox beaconBox = new DhApiRenderableBox(
new DhApiVec3d(beacon.pos.x, beacon.pos.y+1, beacon.pos.z),
new DhApiVec3d(beacon.pos.x+1, BEAM_TOP_Y, beacon.pos.z+1),
beacon.color,
EDhApiBlockMaterial.ILLUMINATED
);
this.beaconBoxGroup.add(beaconBox);
this.beaconBoxGroup.triggerBoxChange();
}
return beaconRefCount;
});
DhApiRenderableBox beaconBox = new DhApiRenderableBox(
new DhApiVec3d(beacon.pos.x, beacon.pos.y+1, beacon.pos.z),
new DhApiVec3d(beacon.pos.x+1, BEAM_TOP_Y, beacon.pos.z+1),
beacon.color,
EDhApiBlockMaterial.ILLUMINATED
);
this.beaconBoxGroup.add(beaconBox);
this.beaconBoxGroup.triggerBoxChange();
}
}
private void stopRenderingBeaconAtPos(DhBlockPos beaconPos, boolean ignoreReferenceCount)
private void stopRenderingBeaconAtPos(DhBlockPos beaconPos)
{
this.beaconRefCountByBlockPos.compute(beaconPos, (pos, beaconRefCount) ->
if (this.beaconBlockPosSet.remove(beaconPos))
{
// ignoring the reference count is needed when deleting beacons
if (ignoreReferenceCount
||
// respecting the reference count is used when unloading beacons
(
beaconRefCount != null
&& beaconRefCount.decrementAndGet() <= 0
))
this.beaconBoxGroup.removeIf((box) ->
{
this.beaconBoxGroup.removeIf((box) ->
box.minPos.x == beaconPos.x
return box.minPos.x == beaconPos.x
&& box.minPos.y == beaconPos.y+1 // plus 1 because the beam starts above the beacon
&& box.minPos.z == beaconPos.z
);
this.beaconBoxGroup.triggerBoxChange();
return null;
}
else
{
return beaconRefCount;
}
});
&& box.minPos.z == beaconPos.z;
});
this.beaconBoxGroup.triggerBoxChange();
}
}
private void updateBeaconColor(BeaconBeamDTO newBeam)