Skip to content

Commit

Permalink
Review changes
Browse files Browse the repository at this point in the history
  • Loading branch information
msz-rai authored and Jakub-Krakowiak committed Aug 24, 2023
1 parent 5537cf3 commit 972606e
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 74 deletions.
113 changes: 57 additions & 56 deletions Assets/RGLUnityPlugin/Scripts/LowLevelWrappers/RGLMeshObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,25 +40,29 @@ internal interface IRGLObject
public abstract class RGLObject<T> : IRGLObject
{
private readonly string identifier;
private RGLMesh rglMesh;
private RGLTexture rglTexture;

private IntPtr rglEntityPtr;

protected RGLMesh rglMesh;

public GameObject RepresentedGO { get; }
public int? CategoryId { get; private set; }
public string CategoryName { get; private set; }

private IntPtr rglEntityPtr;

// There are different stratiegies for obtaining a RGLMesh so we have to also destroy it differently.
protected abstract RGLMesh GetRGLMeshFrom(T meshSource);

protected abstract void DestroyRGLMesh();

protected abstract Matrix4x4 GetLocalToWorld();

protected RGLObject(string identifier, GameObject representedGO, T meshSource)
{
this.identifier = identifier;
RepresentedGO = representedGO;
rglMesh = GetRGLMeshFrom(meshSource);
if (rglMesh == null)
{
return;
throw new RGLException($"Could not create RGLMesh from gameobject '{representedGO.name}'.");
}
UploadToRGL();
SetIntensityTexture();
Expand All @@ -76,24 +80,22 @@ protected RGLObject(string identifier, GameObject representedGO, T meshSource)
DestroyInRGL();
}

protected abstract void DestroyRGLMesh(RGLMesh rglMesh);

public virtual void DestroyInRGL()
{
if (rglEntityPtr == IntPtr.Zero)
{
return;
}

RGLNativeAPI.CheckErr(RGLNativeAPI.rgl_entity_destroy(rglEntityPtr));
rglEntityPtr = IntPtr.Zero;

DestroyRGLMesh(rglMesh);
DestroyRGLMesh();
rglMesh = null;

if (rglTexture != null)
{
RGLTextureManager.UnregisterRGLTextureInstance(rglTexture);
RGLTextureSharingManager.UnregisterRGLTextureInstance(rglTexture);
rglTexture = null;
}
}
Expand All @@ -107,15 +109,10 @@ public void Update()
}
}

protected abstract Matrix4x4 GetLocalToWorld();

protected virtual void UpdateTransform()
{
if (rglEntityPtr == IntPtr.Zero)
{
return;
}

Assert.IsFalse(rglEntityPtr == IntPtr.Zero);

Matrix4x4 m = GetLocalToWorld();
float[] matrix3x4 =
{
Expand Down Expand Up @@ -184,7 +181,7 @@ private void SetIntensityTexture()
return;
}

rglTexture = RGLTextureManager.RegisterRGLTextureInstance(intensityTextureComponent.texture);
rglTexture = RGLTextureSharingManager.RegisterRGLTextureInstance(intensityTextureComponent.texture);
try
{
RGLNativeAPI.CheckErr(
Expand All @@ -205,72 +202,71 @@ private void SetIntensityTexture()

public class RGLMeshRendererObject : RGLObject<MeshRenderer>
{
private readonly Transform rendererTransform;
private readonly Func<Matrix4x4> getLocalToWorld;

public RGLMeshRendererObject(MeshRenderer meshRenderer, Func<Matrix4x4> getLocalToWorld = null):
// By default localToWorld is taken from MeshRenderer, but developer can override it by passing overrideGetLocalToWorld.
public RGLMeshRendererObject(MeshRenderer meshRenderer, Func<Matrix4x4> overrideGetLocalToWorld = null):
base(
$"{meshRenderer.gameObject.name}#{meshRenderer.gameObject.GetInstanceID()}",
meshRenderer.gameObject,
meshRenderer
)
{
rendererTransform = meshRenderer.transform;
this.getLocalToWorld = getLocalToWorld;
var rendererTransfrom = meshRenderer.transform;
getLocalToWorld = overrideGetLocalToWorld != null ? overrideGetLocalToWorld : () => rendererTransfrom.localToWorldMatrix;
}

protected override RGLMesh GetRGLMeshFrom(MeshRenderer meshRenderer)
{
var meshFilter = meshRenderer.GetComponent<MeshFilter>();
if (meshFilter.sharedMesh == null)
{
Debug.LogWarning($"Shared mesh of {meshRenderer.gameObject} is null, skipping");
Debug.LogWarning($"Shared mesh of {meshRenderer.gameObject.name} is null, skipping");
return null;
}

return RGLMeshManager.RegisterRGLMeshInstance(meshFilter.sharedMesh);
return RGLMeshSharingManager.RegisterRGLMeshInstance(meshFilter.sharedMesh);
}

protected override Matrix4x4 GetLocalToWorld()
{
return getLocalToWorld != null ? getLocalToWorld() : rendererTransform.localToWorldMatrix;
return getLocalToWorld();
}

protected override void DestroyRGLMesh(RGLMesh rglMesh)
protected override void DestroyRGLMesh()
{
RGLMeshManager.UnregisterRGLMeshInstance(rglMesh);
RGLMeshSharingManager.UnregisterRGLMeshInstance(rglMesh);
}
}

public class RGLSkinnedMeshRendererObject : RGLObject<SkinnedMeshRenderer>
{
private readonly Transform skinnedMeshRendererTransform;
private readonly Func<Matrix4x4> getLocalToWorld;

public RGLSkinnedMeshRendererObject(SkinnedMeshRenderer skinnedMeshRenderer, Func<Matrix4x4> getLocalToWorld = null) :
public RGLSkinnedMeshRendererObject(SkinnedMeshRenderer skinnedMeshRenderer) :
base(
$"{skinnedMeshRenderer.gameObject.name}#{skinnedMeshRenderer.gameObject.GetInstanceID()}",
skinnedMeshRenderer.gameObject,
skinnedMeshRenderer
)
{
skinnedMeshRendererTransform = skinnedMeshRenderer.transform;
this.getLocalToWorld = getLocalToWorld;
}

protected override RGLMesh GetRGLMeshFrom(SkinnedMeshRenderer skinnedMeshRenderer)
{
// Skinned meshes cannot be shared by using RGLMeshSharingManager
return new RGLSkinnedMesh(skinnedMeshRenderer.gameObject.GetInstanceID(), skinnedMeshRenderer);
}

protected override Matrix4x4 GetLocalToWorld()
{
return getLocalToWorld != null ? getLocalToWorld() : skinnedMeshRendererTransform.localToWorldMatrix;
return skinnedMeshRendererTransform.localToWorldMatrix;
}

protected override void DestroyRGLMesh(RGLMesh rglMesh)
protected override void DestroyRGLMesh()
{
rglMesh.DestroyFromRGL();
rglMesh.DestroyInRGL();
}
}

Expand All @@ -289,17 +285,17 @@ public RGLColliderObject(Collider collider) :

protected override RGLMesh GetRGLMeshFrom(Collider collider)
{
return RGLMeshManager.RegisterRGLMeshInstance(ColliderUtilities.GetMeshForCollider(collider));
return RGLMeshSharingManager.RegisterRGLMeshInstance(ColliderUtilities.GetMeshForCollider(collider));
}

protected override Matrix4x4 GetLocalToWorld()
{
return collider.transform.localToWorldMatrix * ColliderUtilities.GetColliderTransformMatrix(collider);
}

protected override void DestroyRGLMesh(RGLMesh rglMesh)
protected override void DestroyRGLMesh()
{
RGLMeshManager.UnregisterRGLMeshInstance(rglMesh);
RGLMeshSharingManager.UnregisterRGLMeshInstance(rglMesh);
}
}

Expand All @@ -324,6 +320,11 @@ public RGLTerrainObject(Terrain terrain) :
var treePrefab = treePrototypes[i].prefab;
if (treePrefab.TryGetComponent<LODGroup>(out var lodGroup))
{
if (lodGroup.GetLODs().Length == 0)
{
Debug.LogWarning($"No LOD levels in LODGroup of tree prototype {treePrefab.name}");
continue;
}
var lod = lodGroup.GetLODs()[0];
foreach (var renderer in lod.renderers)
{
Expand All @@ -340,12 +341,12 @@ public RGLTerrainObject(Terrain terrain) :
}
}

for (var treeIdx = 0; treeIdx < terrain.terrainData.treeInstanceCount; treeIdx++)
for (var treeIndex = 0; treeIndex < terrain.terrainData.treeInstanceCount; treeIndex++)
{
int prototypeIdx = terrain.terrainData.GetTreeInstance(treeIdx).prototypeIndex;
foreach (var renderer in treePrototypesRenderes[prototypeIdx])
int prototypeIndex = terrain.terrainData.GetTreeInstance(treeIndex).prototypeIndex;
foreach (var renderer in treePrototypesRenderes[prototypeIndex])
{
var treePose = TerrainUtilities.GetTreePose(terrain, treeIdx, treePrototypesHasLODGroup[prototypeIdx]);
var treePose = TerrainUtilities.GetTreePose(terrain, treeIndex, treePrototypesHasLODGroup[prototypeIndex]);
if (renderer is MeshRenderer mr)
{
terrainSubObjects.Add(new RGLMeshRendererObject(mr,() =>
Expand Down Expand Up @@ -376,7 +377,7 @@ protected override void UpdateTransform()
{
terrainSubObject.Update();
}

base.UpdateTransform();
}

Expand All @@ -385,9 +386,9 @@ protected override RGLMesh GetRGLMeshFrom(Terrain terrain)
return new RGLMesh(terrain.gameObject.GetInstanceID(), TerrainUtilities.GetTerrainMesh(terrain));
}

protected override void DestroyRGLMesh(RGLMesh rglMesh)
protected override void DestroyRGLMesh()
{
rglMesh.DestroyFromRGL();
rglMesh.DestroyInRGL();
}
}

Expand All @@ -413,10 +414,10 @@ public RGLMesh(int identifier, Mesh mesh)

~RGLMesh()
{
DestroyFromRGL();
DestroyInRGL();
}

public void DestroyFromRGL()
public void DestroyInRGL()
{
if (rglMeshPtr != IntPtr.Zero)
{
Expand All @@ -438,7 +439,7 @@ protected void UploadToRGL()
if (!verticesOK || !indicesOK)
{
throw new NotSupportedException(
$"Could not get mesh data with mesh identifier {Identifier}. The mesh may be not readable or empty.");
$"Could not get mesh data from Mesh '{Mesh.name}'. The mesh may be not readable or empty.");
}

unsafe
Expand Down Expand Up @@ -471,7 +472,7 @@ public void UploadUVs()

if(!uvOK)
{
Debug.LogWarning($"Could not assign UVs to mesh: {Identifier}. Mash has no UV, or UV are empty.");
Debug.LogWarning($"Could not assign UVs to mesh: '{Mesh.name}'. Mesh has no UV, or UV are empty.");
}
else
{
Expand All @@ -488,7 +489,7 @@ public void UploadUVs()
}
catch (RGLException)
{
Debug.LogWarning($"Could not assign UVs to mesh: {Identifier}.");
Debug.LogWarning($"Could not assign UVs to mesh: '{Mesh.name}'.");
throw;
}
}
Expand Down Expand Up @@ -553,10 +554,10 @@ public RGLTexture(Texture2D texture, int identifier)

~RGLTexture()
{
DestroyFromRGL();
DestroyInRGL();
}

public void DestroyFromRGL()
public void DestroyInRGL()
{
if (rglTexturePtr != IntPtr.Zero)
{
Expand All @@ -573,13 +574,13 @@ protected void UploadToRGL()
if (!resolutionOK)
{
throw new NotSupportedException(
$"Could not get texture data. Resolution seems to be broken.");
$"Could not get texture data from Texture '{Texture.name}'. Resolution seems to be broken.");
}

if (!graphicsFormatOK)
{
throw new NotSupportedException(
$"Could not get texture data. Texture format has to be equal to R8_UNorm.");
$"Could not get texture data from Texture '{Texture.name}'. Texture format has to be equal to R8_UNorm.");
}

unsafe
Expand All @@ -602,10 +603,10 @@ protected void UploadToRGL()
RGLNativeAPI.rgl_texture_destroy(rglTexturePtr);
rglTexturePtr = IntPtr.Zero;
}
throw;
throw;
}
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace RGLUnityPlugin
{
public class RGLMeshManager
public class RGLMeshSharingManager
{
private static Dictionary<int, RGLMesh> sharedMeshes = new Dictionary<int, RGLMesh>(); // <Identifier, RGLMesh>
private static Dictionary<int, int> sharedMeshesUsageCount = new Dictionary<int, int>(); // <RGLMesh Identifier, count>
Expand All @@ -30,24 +30,24 @@ public static void UnregisterRGLMeshInstance(RGLMesh rglMesh)
var meshId = rglMesh.Identifier;
if (sharedMeshes[meshId] is null)
{
Debug.LogWarning($"Trying to unregister absent in RGLMeshManager mesh of id: {meshId}, ignoring request");
Debug.LogWarning($"Trying to unregister absent in RGLMeshSharingManager mesh of id: {meshId}, ignoring request");
return;
}

sharedMeshesUsageCount[meshId]--;
if (sharedMeshesUsageCount[meshId] == 0)
{
sharedMeshes[meshId].DestroyFromRGL();
sharedMeshes[meshId].DestroyInRGL();
sharedMeshes.Remove(meshId);
sharedMeshesUsageCount.Remove(meshId);
}
}

public static void ClearAllMeshes()
public static void Clear()
{
foreach (var mesh in sharedMeshes)
{
mesh.Value.DestroyFromRGL();
mesh.Value.DestroyInRGL();
}
}
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 972606e

Please sign in to comment.