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

Commit

Permalink
-Fix rolling index render slots
Browse files Browse the repository at this point in the history
  • Loading branch information
Cassunshine committed Dec 5, 2023
1 parent 39e5491 commit ca1774b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 19 deletions.
9 changes: 5 additions & 4 deletions Client/Rendering/World/ChunkRenderSlot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,6 @@ public void Move(ivec3 absolutePos, LoadedChunkSection chunks) {
//Should never be null bc this only has 1 callsite that already null checks it
targetChunk = chunks.GetChunkAbsolute(RealPosition);
lastVersion = null;

if (targetChunk == null || targetChunk.ChunkPosition != absolutePos)
throw new InvalidOperationException("");
}


Expand All @@ -73,7 +70,9 @@ public void SetMesh(ChunkMesh mesh) {
}

public override void Dispose() {
mesh?.Dispose();
lock (MeshLock) {
mesh?.Dispose();
}
}

public class ChunkMesh : IDisposable {
Expand Down Expand Up @@ -130,6 +129,8 @@ public void Render() {

public void Dispose() {
RenderSystem.GraphicsDevice.DisposeWhenIdle(Buffer);
RenderSystem.GraphicsDevice.DisposeWhenIdle(UniformBuffer);
RenderSystem.GraphicsDevice.DisposeWhenIdle(UniformResourceSet);
}


Expand Down
35 changes: 20 additions & 15 deletions Client/Rendering/World/ChunkRenderer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using GlmSharp;
using Veldrid;
using Voxel.Client.Rendering.Models;
Expand All @@ -18,6 +19,7 @@ public class ChunkRenderer : Renderer {
public LoadedChunkSection chunks;

private ChunkRenderSlot[]? renderSlots;
private List<ChunkRenderSlot> createdRenderSlots = new();
private int renderDistance = 0;
private int realRenderDistance = 0;

Expand Down Expand Up @@ -110,7 +112,7 @@ public override void Render(double delta) {
CommandList.SetGraphicsResourceSet(1, TerrainAtlas.AtlasResourceSet);

CommandList.SetIndexBuffer(RenderSystem.CommonIndexBuffer, IndexFormat.UInt32);
foreach (var slot in renderSlots)
foreach (var slot in createdRenderSlots)
slot.Render(delta);

//Console.Out.WriteLine();
Expand All @@ -119,25 +121,19 @@ public override void Render(double delta) {
public void SetRenderDistance(int distance) {
chunks.Resize(distance, distance);
if (renderSlots != null)
{
foreach (var slot in renderSlots)
slot.Dispose(); //Todo - Cache and re-use instead of dispose
Array.Fill(renderSlots, null);
}

renderDistance = distance;
realRenderDistance = renderDistance * 2 + 1;
int totalChunks = realRenderDistance * realRenderDistance * realRenderDistance;
var totalChunks = realRenderDistance * realRenderDistance * realRenderDistance;
renderSlots = new ChunkRenderSlot[totalChunks];

for (int x = 0; x < realRenderDistance; x++)
for (int y = 0; y < realRenderDistance; y++)
for (int z = 0; z < realRenderDistance; z++) {
var absolutePos = (new ivec3(x, y, z) - renderDistance) + renderPosition;
var slot = new ChunkRenderSlot(Client);
renderSlots[GetLoopedArrayIndex(absolutePos)] = slot;
slot.Move(absolutePos, chunks);
}

//Sort by distance so that closer chunks are rebuilt first.
Array.Sort(renderSlots, (a, b) => (a.RealPosition - renderPosition).LengthSqr.CompareTo((b.RealPosition - renderPosition).LengthSqr));
renderPosition = ivec3.MinValue;
SetRenderPosition(Client.GameRenderer.MainCamera.position);
}

public void SetRenderPosition(dvec3 worldPosition) {
Expand All @@ -154,12 +150,21 @@ public void SetRenderPosition(dvec3 worldPosition) {
for (int y = 0; y < realRenderDistance; y++)
for (int z = 0; z < realRenderDistance; z++) {
var absolutePos = (new ivec3(x, y, z) - renderDistance) + renderPosition;
var slot = renderSlots[GetLoopedArrayIndex(absolutePos)];
var index = GetLoopedArrayIndex(absolutePos);
var slot = renderSlots[index];


if (slot == null)
{
renderSlots[index] = slot = new ChunkRenderSlot(Client);
createdRenderSlots.Add(slot);
}

slot.Move(absolutePos, chunks);
}

//Sort by distance so that closer chunks are rebuilt first.
Array.Sort(renderSlots, (a, b) => (a.RealPosition - renderPosition).LengthSqr.CompareTo((b.RealPosition - renderPosition).LengthSqr));
createdRenderSlots.Sort((a, b) => (a.RealPosition - renderPosition).LengthSqr.CompareTo((b.RealPosition - renderPosition).LengthSqr));
}


Expand Down

0 comments on commit ca1774b

Please sign in to comment.