Skip to content

Commit

Permalink
feat: sdk7 debug panel scene metrics (#5980)
Browse files Browse the repository at this point in the history
Implemented debug scene panel metrics for SDK7's MeshRenderer, Material and GLTFcontainer.
  • Loading branch information
pravusjif authored Nov 29, 2023
1 parent 879152e commit 1d06975
Show file tree
Hide file tree
Showing 18 changed files with 193 additions and 97 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
"GUID:1e6b57fe78f7b724e9567f29f6a40c2c",
"GUID:2995626b54c60644988f134a69a77450",
"GUID:ac62e852826a4b36aeb22931dad73edb",
"GUID:46951180f4d84b25a1619640aa317e5b"
"GUID:46951180f4d84b25a1619640aa317e5b",
"GUID:0b3e983b6c2fed54ebecf9d146c251ba"
],
"includePlatforms": [],
"excludePlatforms": [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public IReadOnlyList<Collider> GetVisibleMeshesColliders()
return visibleMeshesColliders;
}

public IReadOnlyList<Collider> GetInVisibleMeshesColliders()
public IReadOnlyList<Collider> GetInvisibleMeshesColliders()
{
return invisibleMeshesColliders;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,22 @@ private class ActiveCollidersData

private readonly DataStore_ECS7 dataStoreEcs7;
private readonly DataStore_FeatureFlag featureFlags;
private readonly DataStore_WorldObjects dataStoreWorldObjects;

private readonly ActiveCollidersData visibleActiveColliders = new ActiveCollidersData();
private readonly ActiveCollidersData invisibleActiveColliders = new ActiveCollidersData();
private readonly bool isDebugMode = false;

public RendereableAssetLoadHelper gltfLoader;
internal GameObject gameObject;

private IReadOnlyCollection<Renderer> renderers;
private Rendereable currentRendereable;

internal GltfContainerCollidersHandler collidersHandler;
private PBGltfContainer previousModel = null;
private IParcelScene scene;
private IDCLEntity entity;

public GltfContainerHandler(IInternalECSComponent<InternalColliders> pointerColliderComponent,
IInternalECSComponent<InternalColliders> physicColliderComponent,
Expand All @@ -53,7 +58,9 @@ public GltfContainerHandler(IInternalECSComponent<InternalColliders> pointerColl
IInternalECSComponent<InternalGltfContainerLoadingState> gltfContainerLoadingStateComponent,
IInternalECSComponent<InternalAnimation> animationComponent,
DataStore_ECS7 dataStoreEcs7,
DataStore_FeatureFlag featureFlags)
DataStore_FeatureFlag featureFlags,
DataStore_WorldObjects dataStoreWorldObjects,
DebugConfig debugConfig)
{
this.featureFlags = featureFlags;
this.pointerColliderComponent = pointerColliderComponent;
Expand All @@ -63,10 +70,14 @@ public GltfContainerHandler(IInternalECSComponent<InternalColliders> pointerColl
this.gltfContainerLoadingStateComponent = gltfContainerLoadingStateComponent;
this.animationComponent = animationComponent;
this.dataStoreEcs7 = dataStoreEcs7;
this.dataStoreWorldObjects = dataStoreWorldObjects;
this.isDebugMode = debugConfig.isDebugMode.Get();
}

public void OnComponentCreated(IParcelScene scene, IDCLEntity entity)
{
this.scene = scene;
this.entity = entity;
gameObject = new GameObject("GLTF mesh");

Transform transform = gameObject.transform;
Expand All @@ -84,7 +95,7 @@ public void OnComponentCreated(IParcelScene scene, IDCLEntity entity)

public void OnComponentRemoved(IParcelScene scene, IDCLEntity entity)
{
UnloadGltf(scene, entity, previousModel?.Src);
UnloadGltf(entity, previousModel?.Src);

gltfContainerLoadingStateComponent.RemoveFor(scene, entity,
new InternalGltfContainerLoadingState() { GltfContainerRemoved = true });
Expand All @@ -100,21 +111,16 @@ public void OnComponentModelUpdated(IParcelScene scene, IDCLEntity entity, PBGlt

if (srcChanged)
{
OnGltfSrcChanged(scene, entity, previousModel, model);
OnGltfSrcChanged(previousModel, model);
}
else
{
if (visibleMeshColliderMaskChanged)
{
SetUpColliders(
scene,
entity,
previousModel?.GetVisibleMeshesCollisionMask() ?? 0,
model.GetVisibleMeshesCollisionMask(),
collidersHandler.GetVisibleMeshesColliders(),
pointerColliderComponent,
physicColliderComponent,
customLayerColliderComponent,
visibleActiveColliders.PointerColliders,
visibleActiveColliders.PhysicColliders,
visibleActiveColliders.CustomLayerColliders);
Expand All @@ -123,14 +129,9 @@ public void OnComponentModelUpdated(IParcelScene scene, IDCLEntity entity, PBGlt
if (invisibleMeshColliderMaskChanged)
{
SetUpColliders(
scene,
entity,
previousModel?.GetInvisibleMeshesCollisionMask() ?? 0,
model.GetInvisibleMeshesCollisionMask(),
collidersHandler.GetInVisibleMeshesColliders(),
pointerColliderComponent,
physicColliderComponent,
customLayerColliderComponent,
collidersHandler.GetInvisibleMeshesColliders(),
invisibleActiveColliders.PointerColliders,
invisibleActiveColliders.PhysicColliders,
invisibleActiveColliders.CustomLayerColliders);
Expand All @@ -140,11 +141,11 @@ public void OnComponentModelUpdated(IParcelScene scene, IDCLEntity entity, PBGlt
previousModel = model;
}

private void OnGltfSrcChanged(IParcelScene scene, IDCLEntity entity, PBGltfContainer prevModel, PBGltfContainer model)
private void OnGltfSrcChanged(PBGltfContainer prevModel, PBGltfContainer model)
{
if (!string.IsNullOrEmpty(prevModel?.Src))
{
UnloadGltf(scene, entity, prevModel.Src);
UnloadGltf(entity, prevModel.Src);
}

string newGltfSrc = model.Src;
Expand All @@ -154,8 +155,7 @@ private void OnGltfSrcChanged(IParcelScene scene, IDCLEntity entity, PBGltfConta
gltfContainerLoadingStateComponent.PutFor(scene, entity,
new InternalGltfContainerLoadingState() { LoadingState = LoadingState.Loading });

gltfLoader.OnSuccessEvent += rendereable => OnLoadSuccess(scene,
entity, rendereable.renderers, model);
gltfLoader.OnSuccessEvent += rendereable => OnLoadSuccess(rendereable, model);

gltfLoader.OnFailEvent += exception => OnLoadFail(scene, entity, newGltfSrc, exception,
dataStoreEcs7, gltfContainerLoadingStateComponent);
Expand All @@ -166,53 +166,41 @@ private void OnGltfSrcChanged(IParcelScene scene, IDCLEntity entity, PBGltfConta
}

private void OnLoadSuccess(
IParcelScene scene,
IDCLEntity entity,
HashSet<Renderer> rendererHashSet,
Rendereable rendereable,
PBGltfContainer model)
{
renderers = rendererHashSet;
renderers = rendereable.renderers;

InitColliders(
rendererHashSet,
rendereable.renderers,
gameObject,
collidersHandler,
model.GetVisibleMeshesCollisionMask() != 0);

SetUpRenderers(scene, entity, rendererHashSet, renderersComponent);
SetUpRenderers(scene, entity, rendereable.renderers, renderersComponent);

// setup colliders for visible meshes
SetUpColliders(
scene,
entity,
previousModel?.GetVisibleMeshesCollisionMask() ?? 0,
model.GetVisibleMeshesCollisionMask(),
model.GetVisibleMeshesCollisionMask() != 0 ? collidersHandler.GetVisibleMeshesColliders() : null,
pointerColliderComponent,
physicColliderComponent,
customLayerColliderComponent,
visibleActiveColliders.PointerColliders,
visibleActiveColliders.PhysicColliders,
visibleActiveColliders.CustomLayerColliders);

// setup colliders for invisible meshes
SetUpColliders(
scene,
entity,
previousModel?.GetInvisibleMeshesCollisionMask() ?? 0,
model.GetInvisibleMeshesCollisionMask(),
collidersHandler.GetInVisibleMeshesColliders(),
pointerColliderComponent,
physicColliderComponent,
customLayerColliderComponent,
collidersHandler.GetInvisibleMeshesColliders(),
invisibleActiveColliders.PointerColliders,
invisibleActiveColliders.PhysicColliders,
invisibleActiveColliders.CustomLayerColliders);

SetGltfLoaded(scene, entity, gameObject, model.Src, gltfContainerLoadingStateComponent, animationComponent, dataStoreEcs7);
SetGltfLoaded(gameObject, model.Src, rendereable);
}

private void UnloadGltf(IParcelScene scene, IDCLEntity entity, string gltfSrc)
private void UnloadGltf(IDCLEntity entity, string gltfSrc)
{
void RemoveActiveColliders(IList<Collider> colliders, IInternalECSComponent<InternalColliders> colliderComponent)
{
Expand Down Expand Up @@ -244,6 +232,9 @@ void RemoveActiveColliders(IList<Collider> colliders, IInternalECSComponent<Inte

gltfLoader.ClearEvents();
gltfLoader.Unload();

if (isDebugMode)
RemoveCurrentRendereableFromSceneMetrics();
}

private static void InitColliders(
Expand All @@ -257,41 +248,29 @@ private static void InitColliders(
collidersHandler.InitVisibleMeshesColliders(rendererHashSet, createVisibleMeshColliders);
}

private static void SetUpColliders(
IParcelScene scene,
IDCLEntity entity,
private void SetUpColliders(
uint prevColliderLayer,
uint colliderLayer,
IReadOnlyList<Collider> gltfColliders,
IInternalECSComponent<InternalColliders> pointerColliderComponent,
IInternalECSComponent<InternalColliders> physicColliderComponent,
IInternalECSComponent<InternalColliders> customLayerColliderComponent,
IList<Collider> currentPointerColliders,
IList<Collider> currentPhysicColliders,
IList<Collider> currentCustomLayerColliders)
{
if (prevColliderLayer != 0)
{
RemoveColliders(scene, entity, prevColliderLayer, pointerColliderComponent,
physicColliderComponent, customLayerColliderComponent, currentPointerColliders,
RemoveColliders(prevColliderLayer, currentPointerColliders,
currentPhysicColliders, currentCustomLayerColliders);
}

if (colliderLayer != 0)
{
SetColliders(scene, entity, colliderLayer, gltfColliders, pointerColliderComponent,
physicColliderComponent, customLayerColliderComponent, currentPointerColliders,
SetColliders(colliderLayer, gltfColliders, currentPointerColliders,
currentPhysicColliders, currentCustomLayerColliders);
}
}

private static void RemoveColliders(
IParcelScene scene,
IDCLEntity entity,
private void RemoveColliders(
uint colliderLayer,
IInternalECSComponent<InternalColliders> pointerColliderComponent,
IInternalECSComponent<InternalColliders> physicColliderComponent,
IInternalECSComponent<InternalColliders> customLayerColliderComponent,
IList<Collider> currentPointerColliders,
IList<Collider> currentPhysicColliders,
IList<Collider> currentCustomLayerColliders)
Expand Down Expand Up @@ -332,14 +311,9 @@ void LocalRemoveColliders(
}
}

private static void SetColliders(
IParcelScene scene,
IDCLEntity entity,
private void SetColliders(
uint colliderLayer,
IReadOnlyList<Collider> gltfColliders,
IInternalECSComponent<InternalColliders> pointerColliderComponent,
IInternalECSComponent<InternalColliders> physicColliderComponent,
IInternalECSComponent<InternalColliders> customLayerColliderComponent,
IList<Collider> currentPointerColliders,
IList<Collider> currentPhysicColliders,
IList<Collider> currentCustomLayerColliders)
Expand Down Expand Up @@ -426,14 +400,10 @@ private static void SetUpRenderers(
renderersComponent.PutFor(scene, entity, model);
}

private static void SetGltfLoaded(
IParcelScene scene,
IDCLEntity entity,
private void SetGltfLoaded(
GameObject rootGameObject,
string prevLoadedGltf,
IInternalECSComponent<InternalGltfContainerLoadingState> gltfContainerLoadingStateComponent,
IInternalECSComponent<InternalAnimation> animationComponent,
DataStore_ECS7 dataStoreEcs7)
Rendereable rendereable)
{
gltfContainerLoadingStateComponent.PutFor(scene, entity,
new InternalGltfContainerLoadingState() { LoadingState = LoadingState.Finished });
Expand All @@ -449,6 +419,12 @@ private static void SetGltfLoaded(
{
dataStoreEcs7.RemovePendingResource(scene.sceneData.sceneNumber, prevLoadedGltf);
}

if (isDebugMode)
{
currentRendereable = rendereable;
AddCurrentRendereableToSceneMetrics();
}
}

private static void OnLoadFail(
Expand All @@ -464,5 +440,31 @@ private static void OnLoadFail(

dataStoreEcs7.RemovePendingResource(scene.sceneData.sceneNumber, gltfSrc);
}

private void AddCurrentRendereableToSceneMetrics()
{
dataStoreWorldObjects.AddRendereable(entity.scene.sceneData.sceneNumber, currentRendereable);

foreach (Renderer renderer in renderers)
{
foreach (Material material in renderer.sharedMaterials)
{
dataStoreWorldObjects.AddMaterial(entity.scene.sceneData.sceneNumber, entity.entityId, material);
}
}
}

private void RemoveCurrentRendereableFromSceneMetrics()
{
foreach (Renderer renderer in currentRendereable.renderers)
{
foreach (Material material in renderer.sharedMaterials)
{
dataStoreWorldObjects.RemoveMaterial(entity.scene.sceneData.sceneNumber, entity.entityId, material);
}
}

dataStoreWorldObjects.RemoveRendereable(scene.sceneData.sceneNumber, currentRendereable);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ public class GltfContainerRegister : IDisposable
public GltfContainerRegister(int componentId, ECSComponentsFactory factory, IECSComponentWriter componentWriter,
IInternalECSComponents internalComponents)
{
var dataStoreEcs7 = DataStore.i.ecs7;
var featureFlags = DataStore.i.featureFlags;

factory.AddOrReplaceComponent(componentId, ProtoSerialization.Deserialize<PBGltfContainer>,
() => new GltfContainerHandler(
internalComponents.onPointerColliderComponent,
Expand All @@ -23,8 +20,10 @@ public GltfContainerRegister(int componentId, ECSComponentsFactory factory, IECS
internalComponents.renderersComponent,
internalComponents.GltfContainerLoadingStateComponent,
internalComponents.Animation,
dataStoreEcs7,
featureFlags));
DataStore.i.ecs7,
DataStore.i.featureFlags,
DataStore.i.sceneWorldObjects,
DataStore.i.debugConfig));

componentWriter.AddOrReplaceComponentSerializer<PBGltfContainer>(componentId, ProtoSerialization.Serialize);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ public void SetUp()
internalEcsComponents.GltfContainerLoadingStateComponent,
animationComponent,
new DataStore_ECS7(),
new DataStore_FeatureFlag());
new DataStore_FeatureFlag(),
new DataStore_WorldObjects(),
new DebugConfig());

handler.OnComponentCreated(scene, entity);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ public void SetUp()
internalEcsComponents.GltfContainerLoadingStateComponent,
internalEcsComponents.Animation,
new DataStore_ECS7(),
new DataStore_FeatureFlag());
new DataStore_FeatureFlag(),
new DataStore_WorldObjects(),
new DebugConfig());

handler.OnComponentCreated(scene, entity);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ public void SetUp()
gltfContainerLoadingStateComponent,
internalEcsComponents.Animation,
dataStoreEcs7,
new DataStore_FeatureFlag());
new DataStore_FeatureFlag(),
new DataStore_WorldObjects(),
new DebugConfig());

handler.OnComponentCreated(scene, entity);
}
Expand Down
Loading

0 comments on commit 1d06975

Please sign in to comment.