Improve chunk re-rendering

This commit is contained in:
Drew DeVault 2015-09-28 08:33:10 -04:00
parent 512fabf0b5
commit 7b5154ff6f
3 changed files with 45 additions and 10 deletions

View File

@ -87,18 +87,26 @@ namespace TrueCraft.Client.Modules
public void Update(GameTime gameTime)
{
var any = false;
Mesh _mesh;
while (IncomingChunks.TryTake(out _mesh))
{
any = true;
var mesh = _mesh as ChunkMesh;
int existing = -1;
if (ActiveMeshes.Contains(mesh.Chunk.Coordinates))
existing = ChunkMeshes.FindIndex(m => m.Chunk.Coordinates == mesh.Chunk.Coordinates);
ActiveMeshes.Add(mesh.Chunk.Coordinates);
ChunkMeshes.Add(mesh);
if (existing != -1)
ChunkMeshes.RemoveAt(existing);
{
int existing = ChunkMeshes.FindIndex(m => m.Chunk.Coordinates == mesh.Chunk.Coordinates);
ChunkMeshes[existing] = mesh;
}
else
{
ActiveMeshes.Add(mesh.Chunk.Coordinates);
ChunkMeshes.Add(mesh);
}
}
if (any)
// In theory this would ready the chunks, but it doesn't work for some reason
Game.FlushMainThreadActions();
}
private static readonly BlendState ColorWriteDisable = new BlendState
@ -119,6 +127,8 @@ namespace TrueCraft.Client.Modules
{
chunks++;
ChunkMeshes[i].Draw(OpaqueEffect, 0);
if (!ChunkMeshes[i].IsReady || ChunkMeshes[i].Submeshes != 2)
Console.WriteLine("Warning: rendered chunk that was not ready");
}
}

View File

@ -30,6 +30,8 @@ namespace TrueCraft.Client.Rendering
private TrueCraftGame _game;
private GraphicsDevice _graphicsDevice;
private int _submeshes = 0;
private bool _isReady = false;
protected VertexBuffer _vertices; // ChunkMesh uses these but external classes shouldn't, so I've made them protected.
protected IndexBuffer[] _indices;
@ -47,9 +49,10 @@ namespace TrueCraft.Client.Rendering
_game.PendingMainThreadActions.Add(() =>
{
_vertices = new VertexBuffer(_graphicsDevice, VertexPositionNormalColorTexture.VertexDeclaration,
_vertices = new VertexBuffer(_graphicsDevice, VertexPositionNormalColorTexture.VertexDeclaration,
(value.Length + 1), BufferUsage.WriteOnly);
_vertices.SetData(value);
_isReady = true;
});
if (_recalculateBounds)
@ -57,6 +60,22 @@ namespace TrueCraft.Client.Rendering
}
}
public bool IsReady
{
get
{
return _isReady;
}
}
public int Submeshes
{
get
{
return _submeshes;
}
}
/// <summary>
/// Gets the bounding box for this mesh.
/// </summary>
@ -118,6 +137,8 @@ namespace TrueCraft.Client.Rendering
_indices[index] = new IndexBuffer(_graphicsDevice, typeof(int),
(indices.Length + 1), BufferUsage.WriteOnly);
_indices[index].SetData(indices);
if (index + 1 > _submeshes)
_submeshes = index + 1;
});
}
}

View File

@ -247,13 +247,17 @@ namespace TrueCraft.Client
new PngWriter().Write(RenderTarget, stream);
}
protected override void Update(GameTime gameTime)
public void FlushMainThreadActions()
{
GameTime = gameTime;
Action action;
if (PendingMainThreadActions.TryTake(out action))
action();
}
protected override void Update(GameTime gameTime)
{
GameTime = gameTime;
FlushMainThreadActions();
IChunk chunk;
var adjusted = Client.World.World.FindBlockPosition(