Skip to content
This repository has been archived by the owner on Nov 25, 2024. It is now read-only.

Commit

Permalink
Cache chunk queue and visited chunks set
Browse files Browse the repository at this point in the history
  • Loading branch information
Oliver-makes-code committed Jan 22, 2024
1 parent 86a2612 commit 013b062
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions Client/Rendering/World/ChunkRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ public class ChunkRenderer : Renderer {

public readonly Atlas TerrainAtlas;

private readonly Queue<ivec3> ChunkQueue = [];
private readonly HashSet<ivec3> VisitedChunks = [];

private ChunkRenderSlot[]? renderSlots;
private List<ChunkRenderSlot> createdRenderSlots = new();
private int renderDistance = 0;
Expand Down Expand Up @@ -103,10 +106,10 @@ public override void Render(double delta) {
CommandList.SetIndexBuffer(RenderSystem.CommonIndexBuffer, IndexFormat.UInt32);

using (RenderKey.Push()) {
var queue = new Queue<ivec3>();
var visited = new HashSet<ivec3>();
queue.Add(ivec3.Zero);
visited.Add(ivec3.Zero);
ChunkQueue.Clear();
VisitedChunks.Clear();
ChunkQueue.Add(ivec3.Zero);
VisitedChunks.Add(ivec3.Zero);
var rootPos = Client.GameRenderer.MainCamera.position.WorldToChunkPosition();

ivec3[] directions = [
Expand All @@ -117,8 +120,8 @@ public override void Render(double delta) {

var frustum = Client.GameRenderer.MainCamera.Frustum;

while (queue.Count > 0) {
var curr = queue.Remove();
while (ChunkQueue.Count > 0) {
var curr = ChunkQueue.Remove();
var index = GetLoopedArrayIndex(curr + rootPos);
var chunk = renderSlots[index];
if (chunk == null)
Expand All @@ -130,7 +133,7 @@ public override void Render(double delta) {
var slotPos = pos + renderDistance;
var realPos = pos + rootPos;
if (
visited.Contains(pos) ||
VisitedChunks.Contains(pos) ||
(slotPos < 0).Any ||
(slotPos >= realRenderDistance).Any ||
!frustum.TestAABB(new(
Expand All @@ -139,12 +142,12 @@ public override void Render(double delta) {
))
)
continue;
queue.Add(pos);
visited.Add(pos);
ChunkQueue.Add(pos);
VisitedChunks.Add(pos);
}
}

Profiler.SetCurrentMeta($"{visited.Count} / {renderSlots.Length} ({(int)(visited.Count / (float) renderSlots.Length * 100)}%)");
Profiler.SetCurrentMeta($"{VisitedChunks.Count} / {renderSlots.Length} ({(int)(VisitedChunks.Count / (float) renderSlots.Length * 100)}%)");

// foreach (var slot in createdRenderSlots)
// slot.Render(delta);
Expand Down

0 comments on commit 013b062

Please sign in to comment.