Is there an equivalent for OnBecameVisible in DOTS, for the life of me I am unable to find it?
Been trying to wrap my head around DOTS and it’s lil things like this that seem lacking or not obvious, any help would be much appreciated!
Is there an equivalent for OnBecameVisible in DOTS, for the life of me I am unable to find it?
Been trying to wrap my head around DOTS and it’s lil things like this that seem lacking or not obvious, any help would be much appreciated!
It doesn’t exist in vanilla Entities Graphics. I did add it to my framework though: Latios-Framework/Core/Framework/SuperSystem.cs at master · Dreaming381/Latios-Framework · GitHub
Thanks, I managed to figure a solution using the GeometryUtils, I’m going to Jobify this for a enablable component and use that as the “Flag”
partial struct OnScreenSystem : ISystem
{
public void OnUpdate(ref SystemState state)
{
Plane[] planes = GeometryUtility.CalculateFrustumPlanes(CameraHelper.Instance.Camera);
foreach ((RefRO<LocalTransform> localTransform, RefRO<WorldRenderBounds> worldRenderBounds) in SystemAPI.Query<RefRO<LocalTransform>, RefRO<WorldRenderBounds>>().WithAll<OnScreen>())
{
Bounds bounds = worldRenderBounds.ValueRO.Value.ToBounds();
bool isVisible = GeometryUtility.TestPlanesAABB(planes, bounds);
}
}
}
Once it’s jobified with the Frustum planes being passed in so they’re only calculated once I think it will work quite well.
Here’s what I came up with with help from this DOTS Job compatible TestPlanesAABB thread.
Adding it here for anyone else that may need this and struggled to find a solution, if anyone has any advice on how it could be improved or done better I’m all ears!
OnScreenAuthoring & Component
public class OnScreenAuthoring : MonoBehaviour
{
public class Baker : Baker<OnScreenAuthoring>
{
public override void Bake(OnScreenAuthoring authoring)
{
Entity entity = GetEntity(TransformUsageFlags.Dynamic);
AddComponent(entity, new OnScreen());
}
}
}
public struct OnScreen : IComponentData, IEnableableComponent
{
}
OnScreenSystem
partial struct OnScreenSystem : ISystem
{
public void OnUpdate(ref SystemState state)
{
Plane[] planes = GeometryUtility.CalculateFrustumPlanes(CameraHelper.Instance.Camera);
NativeArray<Plane> planesArray = new NativeArray<Plane>(planes, Allocator.TempJob);
OnScreenJob onScreenJob = new OnScreenJob
{
planes = planesArray
};
state.Dependency = onScreenJob.ScheduleParallel(state.Dependency);
state.Dependency = planesArray.Dispose(state.Dependency);
}
[BurstCompile]
[WithOptions(EntityQueryOptions.IgnoreComponentEnabledState)]
public partial struct OnScreenJob : IJobEntity
{
[ReadOnly] public NativeArray<Plane> planes;
public void Execute(EnabledRefRW<OnScreen> onScreen, RefRO<WorldRenderBounds> bounds)
{
onScreen.ValueRW = DOTSGeometryUtils.TestPlanesAABB(planes, bounds.ValueRO.Value.ToBounds());
}
}
}
DOTSGeometryUtil class for TestPlanesAABB
public static class DOTSGeometryUtils
{
public static bool TestPlanesAABB(NativeArray<Plane> planes, Bounds bounds)
{
for (int x = 0; x < planes.Length; x++)
{
Plane plane = planes[x];
float3 normalSign = math.sign(plane.normal);
float3 testPoint = (float3)bounds.center + (bounds.extents * normalSign);
float dot = math.dot(testPoint, plane.normal);
if (dot + plane.distance < 0f)
{
return false;
}
}
return true;
}
}