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,19 +87,27 @@ namespace TrueCraft.Client.Modules
public void Update(GameTime gameTime) public void Update(GameTime gameTime)
{ {
var any = false;
Mesh _mesh; Mesh _mesh;
while (IncomingChunks.TryTake(out _mesh)) while (IncomingChunks.TryTake(out _mesh))
{ {
any = true;
var mesh = _mesh as ChunkMesh; var mesh = _mesh as ChunkMesh;
int existing = -1;
if (ActiveMeshes.Contains(mesh.Chunk.Coordinates)) if (ActiveMeshes.Contains(mesh.Chunk.Coordinates))
existing = ChunkMeshes.FindIndex(m => m.Chunk.Coordinates == mesh.Chunk.Coordinates); {
int existing = ChunkMeshes.FindIndex(m => m.Chunk.Coordinates == mesh.Chunk.Coordinates);
ChunkMeshes[existing] = mesh;
}
else
{
ActiveMeshes.Add(mesh.Chunk.Coordinates); ActiveMeshes.Add(mesh.Chunk.Coordinates);
ChunkMeshes.Add(mesh); ChunkMeshes.Add(mesh);
if (existing != -1)
ChunkMeshes.RemoveAt(existing);
} }
} }
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 private static readonly BlendState ColorWriteDisable = new BlendState
{ {
@ -119,6 +127,8 @@ namespace TrueCraft.Client.Modules
{ {
chunks++; chunks++;
ChunkMeshes[i].Draw(OpaqueEffect, 0); 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 TrueCraftGame _game;
private GraphicsDevice _graphicsDevice; 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 VertexBuffer _vertices; // ChunkMesh uses these but external classes shouldn't, so I've made them protected.
protected IndexBuffer[] _indices; protected IndexBuffer[] _indices;
@ -50,6 +52,7 @@ namespace TrueCraft.Client.Rendering
_vertices = new VertexBuffer(_graphicsDevice, VertexPositionNormalColorTexture.VertexDeclaration, _vertices = new VertexBuffer(_graphicsDevice, VertexPositionNormalColorTexture.VertexDeclaration,
(value.Length + 1), BufferUsage.WriteOnly); (value.Length + 1), BufferUsage.WriteOnly);
_vertices.SetData(value); _vertices.SetData(value);
_isReady = true;
}); });
if (_recalculateBounds) if (_recalculateBounds)
@ -57,6 +60,22 @@ namespace TrueCraft.Client.Rendering
} }
} }
public bool IsReady
{
get
{
return _isReady;
}
}
public int Submeshes
{
get
{
return _submeshes;
}
}
/// <summary> /// <summary>
/// Gets the bounding box for this mesh. /// Gets the bounding box for this mesh.
/// </summary> /// </summary>
@ -118,6 +137,8 @@ namespace TrueCraft.Client.Rendering
_indices[index] = new IndexBuffer(_graphicsDevice, typeof(int), _indices[index] = new IndexBuffer(_graphicsDevice, typeof(int),
(indices.Length + 1), BufferUsage.WriteOnly); (indices.Length + 1), BufferUsage.WriteOnly);
_indices[index].SetData(indices); _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); new PngWriter().Write(RenderTarget, stream);
} }
protected override void Update(GameTime gameTime) public void FlushMainThreadActions()
{ {
GameTime = gameTime;
Action action; Action action;
if (PendingMainThreadActions.TryTake(out action)) if (PendingMainThreadActions.TryTake(out action))
action(); action();
}
protected override void Update(GameTime gameTime)
{
GameTime = gameTime;
FlushMainThreadActions();
IChunk chunk; IChunk chunk;
var adjusted = Client.World.World.FindBlockPosition( var adjusted = Client.World.World.FindBlockPosition(