@@ -33,6 +33,11 @@ private float MaxFrameTimeInSeconds
3333 get { return ( MaxFrameTime / 1000 ) ; }
3434 }
3535
36+ /// <summary>
37+ /// Whether to create mesh colliders. If unchecked, mesh colliders will be empty and disabled.
38+ /// </summary>
39+ public bool CreateMeshColliders = true ;
40+
3641 private bool drawProcessedMesh = true ;
3742 // Properties
3843 /// <summary>
@@ -97,6 +102,18 @@ private class MeshData
97102 /// </summary>
98103 public readonly Mesh MeshObject = new Mesh ( ) ;
99104
105+ /// <summary>
106+ /// The MeshCollider with which this mesh is associated. Must be set even if
107+ /// no collision mesh will be created.
108+ /// </summary>
109+ public MeshCollider SpatialCollider = null ;
110+
111+ /// <summary>
112+ /// Whether to create collision mesh. If false, the MeshCollider attached to this
113+ /// object will also be disabled when Commit() is called.
114+ /// </summary>
115+ public bool CreateMeshCollider = false ;
116+
100117 /// <summary>
101118 /// Clears the geometry, but does not clear the mesh.
102119 /// </summary>
@@ -118,6 +135,17 @@ public void Commit()
118135 MeshObject . SetTriangles ( tris , 0 ) ;
119136 MeshObject . RecalculateNormals ( ) ;
120137 MeshObject . RecalculateBounds ( ) ;
138+ // The null assignment is required by Unity in order to pick up the new mesh
139+ SpatialCollider . sharedMesh = null ;
140+ if ( CreateMeshCollider )
141+ {
142+ SpatialCollider . sharedMesh = MeshObject ;
143+ SpatialCollider . enabled = true ;
144+ }
145+ else
146+ {
147+ SpatialCollider . enabled = false ;
148+ }
121149 }
122150 }
123151
@@ -180,16 +208,20 @@ private void AddTriangleToSector(Vector3 sector, Vector3 point1, Vector3 point2,
180208 if ( ! meshSectors . TryGetValue ( sector , out nextSectorData ) )
181209 {
182210 nextSectorData = new MeshData ( ) ;
211+ nextSectorData . CreateMeshCollider = CreateMeshColliders ;
183212
184213 int surfaceObjectIndex = SurfaceObjects . Count ;
185214
186- AddSurfaceObject ( CreateSurfaceObject (
187- mesh : nextSectorData . MeshObject ,
188- objectName : string . Format ( "SurfaceUnderstanding Mesh-{0}" , surfaceObjectIndex ) ,
189- parentObject : transform ,
190- meshID : surfaceObjectIndex ,
191- drawVisualMeshesOverride : DrawProcessedMesh
192- ) ) ;
215+ SurfaceObject surfaceObject = CreateSurfaceObject (
216+ mesh : nextSectorData . MeshObject ,
217+ objectName : string . Format ( "SurfaceUnderstanding Mesh-{0}" , surfaceObjectIndex ) ,
218+ parentObject : transform ,
219+ meshID : surfaceObjectIndex ,
220+ drawVisualMeshesOverride : DrawProcessedMesh ) ;
221+
222+ nextSectorData . SpatialCollider = surfaceObject . Collider ;
223+
224+ AddSurfaceObject ( surfaceObject ) ;
193225
194226 // Or make it if this is a new sector.
195227 meshSectors . Add ( sector , nextSectorData ) ;
0 commit comments