Skip to content

Commit

Permalink
Merge pull request #200 from tier4/fix/wrap-rglobject-into-try-catch
Browse files Browse the repository at this point in the history
Wrap RGLObject into try-catch to avoid processing the same objects endlessly
  • Loading branch information
mackierx111 authored Sep 21, 2023
2 parents 2725ece + 1820cef commit 3b4a144
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 25 deletions.
56 changes: 48 additions & 8 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 @@ -60,10 +98,6 @@ protected RGLObject(string identifier, GameObject representedGO, T meshSource)
this.identifier = identifier;
RepresentedGO = representedGO;
rglMesh = GetRGLMeshFrom(meshSource);
if (rglMesh == null)
{
throw new RGLException($"Could not create RGLMesh from gameobject '{representedGO.name}'.");
}
UploadToRGL();
SetIntensityTexture();

Expand Down Expand Up @@ -189,7 +223,7 @@ private void SetIntensityTexture()
}
catch (RGLException)
{
Debug.LogError($"Cannot assign texture: {rglTexture.Texture.name}, to entity: {RepresentedGO.name}");
Debug.LogError($"Cannot assign texture: '{rglTexture.Texture.name}', to entity: '{RepresentedGO.name}'");
throw;
}

Expand Down Expand Up @@ -219,12 +253,14 @@ public RGLMeshRendererObject(MeshRenderer meshRenderer, Func<Matrix4x4> override
protected override RGLMesh GetRGLMeshFrom(MeshRenderer meshRenderer)
{
var meshFilter = meshRenderer.GetComponent<MeshFilter>();
if (meshFilter == null)
{
throw new NotSupportedException($"Mesh renderer '{meshRenderer.gameObject.name}' has no MeshFilter component");
}
if (meshFilter.sharedMesh == null)
{
Debug.LogWarning($"Shared mesh of {meshRenderer.gameObject.name} is null, skipping");
return null;
throw new NotSupportedException($"Shared mesh of '{meshRenderer.gameObject.name}' is null");
}

return RGLMeshSharingManager.RegisterRGLMeshInstance(meshFilter.sharedMesh);
}

Expand Down Expand Up @@ -255,6 +291,10 @@ public RGLSkinnedMeshRendererObject(SkinnedMeshRenderer skinnedMeshRenderer) :

protected override RGLMesh GetRGLMeshFrom(SkinnedMeshRenderer skinnedMeshRenderer)
{
if (skinnedMeshRenderer.sharedMesh == 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
35 changes: 18 additions & 17 deletions Assets/RGLUnityPlugin/Scripts/SceneManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,10 @@ private static IEnumerable<IRGLObject> IntoRGLObjectsUsingColliders(IEnumerable<
continue;
}

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

if (renderer is MeshRenderer mr)
{
yield return new RGLMeshRendererObject(mr);
if (RGLObjectHelper.TryCreateRGLObject(renderer, out IRGLObject rglObject))
{
yield return rglObject;
}
}
}

foreach (var collider in collidersToYield)
{
yield return new RGLColliderObject(collider);
if (RGLObjectHelper.TryCreateRGLObject(collider, out IRGLObject rglObject))
{
yield return rglObject;
}
}
}

Expand All @@ -318,20 +327,9 @@ private static IEnumerable<IRGLObject> IntoRGLObjectsUsingMeshes(IEnumerable<Gam
{
foreach (var renderer in GetUniqueRenderersInGameObjects(gameObjects))
{
if (renderer is MeshRenderer mr)
if (RGLObjectHelper.TryCreateRGLObject(renderer, out IRGLObject rglObject))
{
yield return new RGLMeshRendererObject(mr);
}

if (renderer is SkinnedMeshRenderer smr)
{
if (smr.sharedMesh == null)
{
Debug.LogWarning($"Shared mesh of {smr.gameObject} is null, skipping");
continue;
}

yield return new RGLSkinnedMeshRendererObject(smr);
yield return rglObject;
}
}
}
Expand All @@ -342,7 +340,10 @@ private static IEnumerable<IRGLObject> IntoRGLTerrainObjects(IEnumerable<GameObj
{
if (gameObject.TryGetComponent<Terrain>(out var terrain))
{
yield return new RGLTerrainObject(terrain);
if (RGLObjectHelper.TryCreateRGLObject(terrain, out IRGLObject rglObject))
{
yield return rglObject;
}
}
}
}
Expand Down

0 comments on commit 3b4a144

Please sign in to comment.