Reuse existing mesh assets in MjMeshFilter

This commit is contained in:
Bálint Hodossy
2022-12-06 16:11:03 +00:00
parent 0d608282dd
commit 8312f37e7e
@@ -48,6 +48,12 @@ public class MjMeshFilter : MonoBehaviour {
throw new ArgumentException("Unsupported geom shape detected"); throw new ArgumentException("Unsupported geom shape detected");
} }
if(_geom.ShapeType == MjShapeComponent.ShapeTypes.Mesh) {
MjMeshShape meshShape = _geom.Shape as MjMeshShape;
_meshFilter.sharedMesh = meshShape.Mesh;
return;
}
DisposeCurrentMesh(); DisposeCurrentMesh();
var mesh = new Mesh(); var mesh = new Mesh();
@@ -73,7 +79,7 @@ public class MjMeshFilter : MonoBehaviour {
// Dynamically created meshes with no references are only disposed automatically on scene changes. // Dynamically created meshes with no references are only disposed automatically on scene changes.
// This prevents resource leaks in case the host environment doesn't reload scenes. // This prevents resource leaks in case the host environment doesn't reload scenes.
private void DisposeCurrentMesh() { private void DisposeCurrentMesh() {
if (_meshFilter.sharedMesh != null) { if (_meshFilter.sharedMesh != null && _geom.ShapeType != MjShapeComponent.ShapeTypes.Mesh) {
#if UNITY_EDITOR #if UNITY_EDITOR
DestroyImmediate(_meshFilter.sharedMesh); DestroyImmediate(_meshFilter.sharedMesh);
#else #else