Created
December 15, 2020 23:01
-
-
Save sbaer/d22ed2aa7739574135f2348d61dad2f5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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