Skip to content

Commit

Permalink
Review changes
Browse files Browse the repository at this point in the history
  • Loading branch information
msz-rai committed Sep 19, 2023
1 parent ecde6e2 commit 1820cef
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 44 deletions.
48 changes: 45 additions & 3 deletions Assets/RGLUnityPlugin/Scripts/LowLevelWrappers/RGLMeshObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,44 @@

namespace RGLUnityPlugin
{
public static class RGLObjectHelper
{
internal static bool TryCreateRGLObject<T>(T meshSource, out IRGLObject rglObject) where T : UnityEngine.Object
{
try
{
if (meshSource is MeshRenderer mr)
{
rglObject = new RGLMeshRendererObject(mr);
}
else if (meshSource is SkinnedMeshRenderer smr)
{
rglObject = new RGLSkinnedMeshRendererObject(smr);
}
else if (meshSource is Collider collider)
{
rglObject = new RGLColliderObject(collider);
}
else if (meshSource is Terrain terrain)
{
rglObject = new RGLTerrainObject(terrain);
}
else
{
Debug.LogError($"Could not create RGLObject from type '{typeof(T)}'");
rglObject = null;
return false;
}
}
catch (Exception e)
{
Debug.LogWarning($"Cannot create RGLObject from '{meshSource.name}': {e.Message}. Skipping...");
rglObject = null;
return false;
}
return true;
}
}

internal interface IRGLObject
{
Expand Down Expand Up @@ -215,9 +253,13 @@ public RGLMeshRendererObject(MeshRenderer meshRenderer, Func<Matrix4x4> override
protected override RGLMesh GetRGLMeshFrom(MeshRenderer meshRenderer)
{
var meshFilter = meshRenderer.GetComponent<MeshFilter>();
if (meshFilter == null || meshFilter.sharedMesh == null)
if (meshFilter == null)
{
throw new NotSupportedException($"Mesh renderer '{meshRenderer.gameObject.name}' has no MeshFilter component");
}
if (meshFilter.sharedMesh == null)
{
throw new RGLException($"Shared mesh of '{meshRenderer.gameObject.name}' is null");
throw new NotSupportedException($"Shared mesh of '{meshRenderer.gameObject.name}' is null");
}
return RGLMeshSharingManager.RegisterRGLMeshInstance(meshFilter.sharedMesh);
}
Expand Down Expand Up @@ -251,7 +293,7 @@ protected override RGLMesh GetRGLMeshFrom(SkinnedMeshRenderer skinnedMeshRendere
{
if (skinnedMeshRenderer.sharedMesh == null)
{
throw new RGLException($"Shared mesh of '{skinnedMeshRenderer.gameObject}' is null");
throw new NotSupportedException($"Shared skinned mesh of '{skinnedMeshRenderer.gameObject}' is null");
}
// Skinned meshes cannot be shared by using RGLMeshSharingManager
return new RGLSkinnedMesh(skinnedMeshRenderer.gameObject.GetInstanceID(), skinnedMeshRenderer);
Expand Down
46 changes: 5 additions & 41 deletions Assets/RGLUnityPlugin/Scripts/SceneManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ private static IEnumerable<IRGLObject> IntoRGLObjectsUsingColliders(IEnumerable<
continue;
}

if (TryCreateRGLObject(collider, out IRGLObject rglObject))
if (RGLObjectHelper.TryCreateRGLObject(collider, out IRGLObject rglObject))
{
yield return rglObject;
}
Expand Down Expand Up @@ -302,7 +302,7 @@ private static IEnumerable<IRGLObject> IntoRGLObjectsHybrid(IEnumerable<GameObje

if (renderer is MeshRenderer mr)
{
if (TryCreateRGLObject(renderer, out IRGLObject rglObject))
if (RGLObjectHelper.TryCreateRGLObject(renderer, out IRGLObject rglObject))
{
yield return rglObject;
}
Expand All @@ -311,7 +311,7 @@ private static IEnumerable<IRGLObject> IntoRGLObjectsHybrid(IEnumerable<GameObje

foreach (var collider in collidersToYield)
{
if (TryCreateRGLObject(collider, out IRGLObject rglObject))
if (RGLObjectHelper.TryCreateRGLObject(collider, out IRGLObject rglObject))
{
yield return rglObject;
}
Expand All @@ -327,7 +327,7 @@ private static IEnumerable<IRGLObject> IntoRGLObjectsUsingMeshes(IEnumerable<Gam
{
foreach (var renderer in GetUniqueRenderersInGameObjects(gameObjects))
{
if (TryCreateRGLObject(renderer, out IRGLObject rglObject))
if (RGLObjectHelper.TryCreateRGLObject(renderer, out IRGLObject rglObject))
{
yield return rglObject;
}
Expand All @@ -340,7 +340,7 @@ private static IEnumerable<IRGLObject> IntoRGLTerrainObjects(IEnumerable<GameObj
{
if (gameObject.TryGetComponent<Terrain>(out var terrain))
{
if (TryCreateRGLObject(terrain, out IRGLObject rglObject))
if (RGLObjectHelper.TryCreateRGLObject(terrain, out IRGLObject rglObject))
{
yield return rglObject;
}
Expand Down Expand Up @@ -410,41 +410,5 @@ private static bool IsNotActiveOrParentHasLidar(GameObject gameObject)
{
return !gameObject.activeInHierarchy || gameObject.GetComponentsInParent<LidarSensor>().Length != 0;
}

private static bool TryCreateRGLObject<T>(T meshSource, out IRGLObject rglObject) where T : UnityEngine.Object
{
try
{
if (meshSource is MeshRenderer mr)
{
rglObject = new RGLMeshRendererObject(mr);
}
else if (meshSource is SkinnedMeshRenderer smr)
{
rglObject = new RGLSkinnedMeshRendererObject(smr);
}
else if (meshSource is Collider collider)
{
rglObject = new RGLColliderObject(collider);
}
else if (meshSource is Terrain terrain)
{
rglObject = new RGLTerrainObject(terrain);
}
else
{
Debug.LogError($"Could not create RGLObject from type '{typeof(T)}'");
rglObject = null;
return false;
}
}
catch (RGLException e)
{
Debug.LogWarning($"Cannot create RGLObject from '{meshSource.name}': {e.Message}. Skipping...");
rglObject = null;
return false;
}
return true;
}
}
}

0 comments on commit 1820cef

Please sign in to comment.