Skip to content

Instantly share code, notes, and snippets.

@sbaer
Created December 15, 2020 23:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sbaer/d22ed2aa7739574135f2348d61dad2f5 to your computer and use it in GitHub Desktop.
Save sbaer/d22ed2aa7739574135f2348d61dad2f5 to your computer and use it in GitHub Desktop.
class MeshDisplayCache : IDisposable
{
Dictionary<System.Drawing.Color, Tuple<DisplayMaterial, Mesh>> _meshesToDraw;
public void Add(System.Drawing.Color color, Mesh mesh)
{
if (null == _meshesToDraw)
_meshesToDraw = new Dictionary<System.Drawing.Color, Tuple<DisplayMaterial, Mesh>>();
Tuple<DisplayMaterial, Mesh> data;
if( _meshesToDraw.TryGetValue(color, out data))
{
data.Item2.Append(mesh);
}
else
{
var material = new DisplayMaterial();
material.Diffuse = color;
var m = mesh.DuplicateShallow();
_meshesToDraw.Add(color, new Tuple<DisplayMaterial, Mesh>(material, m));
}
}
public void Draw(DisplayPipeline dp)
{
if (null == _meshesToDraw)
return;
foreach(var kv in _meshesToDraw)
{
dp.DrawMeshShaded(kv.Value.Item2, kv.Value.Item1);
}
}
void DisposeHelper()
{
if (_meshesToDraw != null)
{
foreach(var kv in _meshesToDraw)
{
kv.Value.Item1.Dispose();
kv.Value.Item2.Dispose();
}
}
_meshesToDraw = null;
}
~MeshDisplayCache()
{
DisposeHelper();
}
public void Dispose()
{
DisposeHelper();
GC.SuppressFinalize(this);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment