Skip to content

Commit 0efdc6e

Browse files
committed
refactor(avatar-rendering): drop camera param from IsVisibleInCamera, pass CameraComponent as query data
1 parent 36c528f commit 0efdc6e

2 files changed

Lines changed: 11 additions & 48 deletions

File tree

Explorer/Assets/DCL/AvatarRendering/AvatarShape/Systems/AvatarShapeVisibilitySystem.cs

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,6 @@ public partial class AvatarShapeVisibilitySystem : BaseUnityLoopSystem
3232

3333
private SingleInstanceEntity camera;
3434
private GameObject? playerCamera;
35-
// Camera the cached frustum planes were built for. IsVisibleInCamera validates against this so a
36-
// caller cannot read planes computed for a different camera (or before any were computed).
37-
private Camera? planesCamera;
3835

3936
public AvatarShapeVisibilitySystem(World world, IUserBlockingCache userBlockingCache, IRendererFeaturesCache outlineFeature, float startFadeDithering, float endFadeDithering, bool includeBannedUsersFromScene) : base(world)
4037
{
@@ -68,27 +65,21 @@ protected override void Update(float t)
6865

6966
if (outlineFeature != null && outlineFeature.isActive)
7067
{
71-
Camera cam = camera.GetCameraComponent(World).Camera;
72-
CalculateFrustumPlanes(cam);
73-
GetAvatarsVisibleWithOutlineQuery(World, cam);
68+
CameraComponent cameraComponent = camera.GetCameraComponent(World);
69+
CalculateFrustumPlanes(cameraComponent.Camera);
70+
GetAvatarsVisibleWithOutlineQuery(World, cameraComponent);
7471
}
7572
}
7673

77-
public void CalculateFrustumPlanes(Camera camera)
74+
internal void CalculateFrustumPlanes(Camera camera)
7875
{
7976
GeometryUtility.CalculateFrustumPlanes(camera, planes);
80-
planesCamera = camera;
8177
}
8278

8379
// Tests the AABB against the frustum planes cached by the most recent CalculateFrustumPlanes call.
84-
// Extraction runs once per tick in Update (not per avatar), so this does not recompute the planes;
85-
// the camera argument is validated to match the camera those cached planes were built for.
86-
public bool IsVisibleInCamera(Camera camera, Bounds bounds)
87-
{
88-
UnityEngine.Assertions.Assert.IsTrue(ReferenceEquals(planesCamera, camera),
89-
"IsVisibleInCamera reads planes cached by CalculateFrustumPlanes; call it for this camera in the current tick first.");
90-
return GeometryUtility.TestPlanesAABB(planes, bounds);
91-
}
80+
// Extraction runs once per tick in Update (not per avatar), so this does not recompute the planes.
81+
internal bool IsVisibleInCamera(Bounds bounds) =>
82+
GeometryUtility.TestPlanesAABB(planes, bounds);
9283

9384
public bool IsWithinCameraDistance(Camera camera, Transform objectTransform, float maxDistancesquared)
9485
{
@@ -98,9 +89,9 @@ public bool IsWithinCameraDistance(Camera camera, Transform objectTransform, flo
9889
}
9990

10091
[Query]
101-
private void GetAvatarsVisibleWithOutline([Data] Camera cam, in AvatarBase avatarBase, ref AvatarShapeComponent avatarShape)
92+
private void GetAvatarsVisibleWithOutline([Data] in CameraComponent cameraComponent, in AvatarBase avatarBase, ref AvatarShapeComponent avatarShape)
10293
{
103-
if (avatarShape.IsPreview || (IsWithinCameraDistance(cam, avatarBase.HeadAnchorPoint, 64.0f) && IsVisibleInCamera(cam, avatarBase.AvatarSkinnedMeshRenderer.bounds)))
94+
if (avatarShape.IsPreview || (IsWithinCameraDistance(cameraComponent.Camera, avatarBase.HeadAnchorPoint, 64.0f) && IsVisibleInCamera(avatarBase.AvatarSkinnedMeshRenderer.bounds)))
10495
{
10596
RendererFeature_AvatarOutline.m_AvatarOutlineRenderers.AddRange(avatarShape.OutlineCompatibleRenderers);
10697
}

Explorer/Assets/DCL/AvatarRendering/AvatarShape/Tests/EditMode/AvatarShapeVisibilitySystemShould.cs

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -131,40 +131,12 @@ public void ReturnTrueWhenObjectIsVisibleInCamera()
131131

132132
// Act
133133
system!.CalculateFrustumPlanes(testCamera);
134-
bool isVisible = system.IsVisibleInCamera(testCamera, bounds);
134+
bool isVisible = system.IsVisibleInCamera(bounds);
135135

136136
// Assert
137137
Assert.IsTrue(isVisible);
138138
}
139139

140-
[Test]
141-
public void IsVisibleInCameraRejectsPlanesFromDifferentCamera()
142-
{
143-
// The frustum-plane extraction is hoisted out of the per-avatar loop into CalculateFrustumPlanes,
144-
// so IsVisibleInCamera reads cached planes. Querying it with a camera whose planes were not the
145-
// ones just computed must be caught, not silently answered against another camera's planes.
146-
var bounds = new Bounds(new Vector3(0, 1, 5), Vector3.one);
147-
148-
var otherCameraGo = new GameObject("OtherCamera");
149-
createdGameObjects.Add(otherCameraGo);
150-
Camera otherCamera = otherCameraGo.AddComponent<Camera>();
151-
152-
system.CalculateFrustumPlanes(testCamera);
153-
154-
bool previousRaiseExceptions = UnityEngine.Assertions.Assert.raiseExceptions;
155-
UnityEngine.Assertions.Assert.raiseExceptions = true;
156-
157-
try
158-
{
159-
Assert.Throws<UnityEngine.Assertions.AssertionException>(
160-
() => system.IsVisibleInCamera(otherCamera, bounds));
161-
}
162-
finally
163-
{
164-
UnityEngine.Assertions.Assert.raiseExceptions = previousRaiseExceptions;
165-
}
166-
}
167-
168140
[Test]
169141
public void ReturnFalseWhenObjectIsNotVisibleInCamera()
170142
{
@@ -174,7 +146,7 @@ public void ReturnFalseWhenObjectIsNotVisibleInCamera()
174146

175147
// Act
176148
system!.CalculateFrustumPlanes(testCamera);
177-
bool isVisible = system.IsVisibleInCamera(testCamera, bounds);
149+
bool isVisible = system.IsVisibleInCamera(bounds);
178150

179151
// Assert
180152
Assert.IsFalse(isVisible);

0 commit comments

Comments
 (0)