protected override void OnUpdate()
        {
            // create kdtree
            PerfMarker1.Begin();

            var ballEntities = _ballQuery.ToEntityArray(Allocator.TempJob);
            var balls        = GetComponentDataFromEntity <BallData>(true);
            var kdRoot       = new KdRoot();

            Job.WithCode(() => {
                var ballBounds = new NativeArray <Aabb>(ballEntities.Length, Allocator.Temp);
                for (var i = 0; i < ballEntities.Length; i++)
                {
                    ballBounds[i] = balls[ballEntities[i]].GetAabb(ballEntities[i]);
                }
                kdRoot.Init(ballBounds, Allocator.TempJob);
            }).Run();

            ballEntities.Dispose();
            PerfMarker1.End();

            var overlappingEntities = GetBufferFromEntity <OverlappingDynamicBufferElement>();
            var marker = PerfMarker2;

            Entities
            .WithName("StaticBroadPhaseJob")
            .WithNativeDisableParallelForRestriction(overlappingEntities)
            .ForEach((Entity entity, in BallData ball) => {
                // don't play with frozen balls
                if (ball.IsFrozen)
                {
                    return;
                }

                marker.Begin();

                var colliderEntities = overlappingEntities[entity];
                colliderEntities.Clear();
                kdRoot.GetAabbOverlaps(in entity, in ball, ref colliderEntities);

                marker.End();
            }).Run();

            kdRoot.Dispose();
        }
Example #2
0
        protected override void OnUpdate()
        {
            // create kdtree
            PerfMarker1.Begin();

            var ballBounds = new NativeArray <Aabb>(_ballQuery.CalculateEntityCount(), Allocator.TempJob);

            Entities.ForEach((Entity ballEntity, int entityInQueryIndex, in BallData ballData) => {
                ballBounds[entityInQueryIndex] = ballData.GetAabb(ballEntity);
            }).Run();

            var kdRoot = new KdRoot();

            Job.WithCode(() => kdRoot.Init(ballBounds, Allocator.TempJob)).Run();

            PerfMarker1.End();

            var overlappingEntities = GetBufferFromEntity <OverlappingDynamicBufferElement>();
            var marker = PerfMarker2;

            Entities
            .WithName("DynamicBroadPhaseJob")
            .WithDisposeOnCompletion(kdRoot)
            .WithNativeDisableParallelForRestriction(overlappingEntities)
            .ForEach((Entity entity, in BallData ball) => {
                // don't play with frozen balls
                if (ball.IsFrozen)
                {
                    return;
                }

                marker.Begin();

                var colliderEntities = overlappingEntities[entity];
                colliderEntities.Clear();
                kdRoot.GetAabbOverlaps(in entity, in ball, ref colliderEntities);

                marker.End();
            }).Run();
        }