Skip to content

Commit 9071531

Browse files
committed
DMeshSO now automatically switches to more complex view-mesh decomp if mesh is larger that 500k tris (maybe should be even smaller?)
1 parent b1bd5e4 commit 9071531

File tree

2 files changed

+68
-15
lines changed

2 files changed

+68
-15
lines changed

sceneObjects/DMeshSO.cs

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ public class DMeshSO : BaseSO, SpatialQueryableSO
3131
// a background thread, to avoid making mesh copies. Functions that
3232
// internally modify .mesh will lock this first.
3333

34-
IViewMeshManager viewMeshes;
35-
3634
bool enable_spatial = true;
3735
DMeshAABBTree3 spatial;
3836

@@ -49,16 +47,49 @@ public virtual DMeshSO Create(DMesh3 mesh, SOMaterial setMaterial)
4947

5048
this.mesh = mesh;
5149

52-
//viewMeshes = new LinearDecompViewMeshManager(this);
53-
viewMeshes = new TrivialViewMeshManager(this);
50+
5451

5552
on_mesh_changed();
56-
viewMeshes.ValidateViewMeshes();
53+
validate_view_meshes();
5754

5855
return this;
5956
}
6057

6158

59+
const int mesh_decomp_size_thresh = 500000;
60+
IViewMeshManager view_meshes = null;
61+
IViewMeshManager ViewMeshes {
62+
get {
63+
if ( view_meshes == null ) {
64+
if (Mesh.TriangleCount > mesh_decomp_size_thresh)
65+
view_meshes = new LinearDecompViewMeshManager(this) { MaxSubmeshSize = 128000 };
66+
else
67+
view_meshes = new TrivialViewMeshManager(this);
68+
}
69+
return view_meshes;
70+
}
71+
}
72+
void release_view_meshes()
73+
{
74+
if (view_meshes != null) {
75+
view_meshes.InvalidateViewMeshes();
76+
view_meshes.Dispose();
77+
view_meshes = null;
78+
}
79+
}
80+
void validate_view_meshes()
81+
{
82+
// if view meshes are invalid, and mesh is too big, create piecewise decomp panager
83+
if ( (view_meshes != null)
84+
&& (view_meshes is TrivialViewMeshManager)
85+
&& (view_meshes.AreMeshesValid == false)
86+
&& (Mesh.TriangleCount > mesh_decomp_size_thresh) ) {
87+
release_view_meshes();
88+
}
89+
ViewMeshes.ValidateViewMeshes();
90+
}
91+
92+
6293

6394
// To reduce memory usage, when we disconnect a DMeshSO we can
6495
// discard temporary data structures. Also, when we destroy it,
@@ -67,13 +98,13 @@ public virtual DMeshSO Create(DMesh3 mesh, SOMaterial setMaterial)
6798
override public void Connect(bool bRestore)
6899
{
69100
if ( bRestore ) {
70-
viewMeshes.ValidateViewMeshes();
101+
validate_view_meshes();
71102
}
72103
}
73104
override public void Disconnect(bool bDestroying)
74105
{
75106
this.spatial = null;
76-
viewMeshes.InvalidateViewMeshes();
107+
release_view_meshes();
77108
if ( bDestroying ) {
78109
this.mesh = null;
79110
}
@@ -185,7 +216,7 @@ public void ReplaceMesh(DMesh3 newMesh, bool bTakeOwnership = true)
185216
}
186217

187218
on_mesh_changed();
188-
viewMeshes.ValidateViewMeshes();
219+
validate_view_meshes();
189220
post_mesh_modified();
190221
}
191222

@@ -262,9 +293,9 @@ public void UpdateVertices(DMesh3 sourceMesh, bool bNormals = true, bool bColors
262293

263294
// fast update of view meshes for vertex deformations/changes
264295
void fast_mesh_update(bool bNormals, bool bColors) {
265-
viewMeshes.FastUpdateVertices(bNormals, bColors);
296+
ViewMeshes.FastUpdateVertices(bNormals, bColors);
266297
on_mesh_changed(true, false);
267-
viewMeshes.ValidateViewMeshes();
298+
validate_view_meshes();
268299
}
269300

270301

@@ -300,7 +331,7 @@ public void notify_mesh_edited(GeometryEditTypes editType)
300331
fast_mesh_update(true, true);
301332
} else {
302333
on_mesh_changed();
303-
viewMeshes.ValidateViewMeshes();
334+
validate_view_meshes();
304335
}
305336
post_mesh_modified();
306337
}
@@ -312,7 +343,7 @@ void on_mesh_changed(bool bInvalidateSpatial = true, bool bInvalidateDecomp = tr
312343

313344
// discard existing mesh GOs
314345
if (bInvalidateDecomp) {
315-
viewMeshes.InvalidateViewMeshes();
346+
ViewMeshes.InvalidateViewMeshes();
316347
}
317348
}
318349

util/ViewMeshManagers.cs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ namespace f3
1111
/// The ViewMeshManager does this. This may involve multiple render meshes
1212
/// at the engine level.
1313
/// </summary>
14-
public interface IViewMeshManager
14+
public interface IViewMeshManager : IDisposable
1515
{
16+
bool AreMeshesValid { get; }
17+
1618
/// <summary>
1719
/// Generate new view meshes if they don't exist. Should not
1820
/// regenerate unless Invalidate() has been called.
@@ -54,6 +56,17 @@ public TrivialViewMeshManager(DMeshSO sourceSO)
5456
SourceSO = sourceSO;
5557
}
5658

59+
public void Dispose()
60+
{
61+
InvalidateViewMeshes();
62+
}
63+
64+
//
65+
// AAAHHH should be re-using these GOs !!
66+
//
67+
68+
69+
public bool AreMeshesValid { get { return decomp_valid; } }
5770

5871
public virtual void ValidateViewMeshes()
5972
{
@@ -103,6 +116,8 @@ public class LinearDecompViewMeshManager : IViewMeshManager, IMeshComponentManag
103116
{
104117
public DMeshSO SourceSO;
105118

119+
public int MaxSubmeshSize = 64000; // unity uint-mesh limit
120+
106121
protected DMesh3 mesh {
107122
get { return SourceSO.Mesh; }
108123
}
@@ -123,13 +138,20 @@ public LinearDecompViewMeshManager(DMeshSO sourceSO)
123138
displayComponents = new List<DisplayMeshComponent>();
124139
}
125140

141+
public void Dispose()
142+
{
143+
InvalidateViewMeshes();
144+
}
145+
146+
public bool AreMeshesValid { get { return decomp_valid; } }
126147

127148
public virtual void ValidateViewMeshes()
128149
{
129150
if (decomp_valid)
130151
return;
131152

132-
decomp = new MeshDecomposition(mesh, this);
153+
decomp = new MeshDecomposition(mesh, this)
154+
{ MaxComponentSize = this.MaxSubmeshSize };
133155
decomp.BuildLinear();
134156
decomp = null;
135157

@@ -157,7 +179,7 @@ public virtual void FastUpdateVertices(bool bNormals, bool bColors)
157179
public void AddComponent(MeshDecomposition.Component C)
158180
{
159181
fMesh submesh = new fMesh(C.triangles, mesh, C.source_vertices, true, true, true);
160-
fMeshGameObject submesh_go = GameObjectFactory.CreateMeshGO("component", submesh, false);
182+
fMeshGameObject submesh_go = GameObjectFactory.CreateMeshGO("component", submesh, true);
161183
submesh_go.SetMaterial(SourceSO.CurrentMaterial, true);
162184
submesh_go.SetLayer(SourceSO.RootGameObject.GetLayer());
163185
displayComponents.Add(new DisplayMeshComponent() {

0 commit comments

Comments
 (0)