Beispiel #1
0
            public void Execute(int i_arrayIndex)
            {
                Entity octreeRootNodeEntity = a_collisionChecksEntities [i_arrayIndex];


                // Its value should be 0, if no collision is detected.
                // And >= 1, if instance collision is detected, or there is more than one collision,
                // indicating number of collisions.
                IsCollidingData isCollidingData = a_isCollidingData [octreeRootNodeEntity];

                isCollidingData.i_collisionsCount = 0;                    // Reset colliding instances counter.
                //isCollidingData.i_nearestInstanceCollisionIndex   = 0 ; // Unused
                //isCollidingData.f_nearestDistance                 = float.PositiveInfinity ; // Unused


                RootNodeData octreeRootNodeData = a_octreeRootNodeData [octreeRootNodeEntity];

                DynamicBuffer <NodeBufferElement> a_nodesBuffer = nodeBufferElement [octreeRootNodeEntity];
                DynamicBuffer <NodeInstancesIndexBufferElement> a_nodeInstancesIndexBuffer = nodeInstancesIndexBufferElement [octreeRootNodeEntity];
                DynamicBuffer <NodeChildrenBufferElement>       a_nodeChildrenBuffer       = nodeChildrenBufferElement [octreeRootNodeEntity];
                DynamicBuffer <InstanceBufferElement>           a_instanceBuffer           = instanceBufferElement [octreeRootNodeEntity];


                RayEntityPair4CollisionData rayEntityPair4CollisionData = a_rayEntityPair4CollisionData [octreeRootNodeEntity];

                // Ray entity pair, for collision checks

                Entity ray2CheckEntity = rayEntityPair4CollisionData.ray2CheckEntity;

                // Is target octree active
                if (a_isActiveTag.Exists(ray2CheckEntity))
                {
                    RayData            rayData            = a_rayData [ray2CheckEntity];
                    RayMaxDistanceData rayMaxDistanceData = a_rayMaxDistanceData [ray2CheckEntity];


                    // To even allow instances collision checks, octree must have at least one instance.
                    if (octreeRootNodeData.i_totalInstancesCountInTree > 0)
                    {
                        if (IsRayColliding_Common._IsNodeColliding(octreeRootNodeData, octreeRootNodeData.i_rootNodeIndex, rayData.ray, ref isCollidingData, a_nodesBuffer, a_nodeChildrenBuffer, a_nodeInstancesIndexBuffer, a_instanceBuffer, rayMaxDistanceData.f))
                        {
                            /*
                             * // Debug
                             * Debug.Log ( "Is colliding." ) ;
                             */
                        }
                    }
                }

                a_isCollidingData [octreeRootNodeEntity] = isCollidingData;  // Set back.
            }
            public void Execute(int i_arrayIndex)
            {
                Entity octreeEntity = a_collisionChecksEntities [i_arrayIndex];

                RayEntityPair4CollisionData rayEntityPair4CollisionData = a_rayEntityPair4CollisionData [octreeEntity];
                Entity octreeRayEntity = rayEntityPair4CollisionData.ray2CheckEntity;

                RayData rayData = new RayData()
                {
                    ray = ray
                };

                a_rayData [octreeRayEntity] = rayData;
            }
        static public void _DebugRays(EntityArray a_collisionChecksEntities, ComponentDataFromEntity <RayData> a_rayData, ComponentDataFromEntity <RayMaxDistanceData> a_rayMaxDistanceData, ComponentDataFromEntity <IsCollidingData> a_isCollidingData, ComponentDataFromEntity <RayEntityPair4CollisionData> a_rayEntityPair4CollisionData, bool canDebugAllChecks, bool canDebugAllrays)
        {
            // Debug all, or only one check
            int i_debugCollisionChecksCount = canDebugAllChecks ? a_collisionChecksEntities.Length : 1;


            // Debug
            // ! Ensure test this only with single, or at most few ray entiities.
            for (int i_collisionChecksIndex = 0; i_collisionChecksIndex < i_debugCollisionChecksCount; i_collisionChecksIndex++)
            {
                Entity octreeRayEntity = a_collisionChecksEntities [i_collisionChecksIndex];
                Entity octreeRayEntity2;

                if (!a_rayData.Exists(octreeRayEntity))
                {
                    RayEntityPair4CollisionData rayEntityPair4CollisionData = a_rayEntityPair4CollisionData [octreeRayEntity];
                    octreeRayEntity2 = rayEntityPair4CollisionData.ray2CheckEntity;
                }
                else
                {
                    octreeRayEntity2 = octreeRayEntity;
                }

                // Draw all available rays, or signle ray
                if (canDebugAllrays)
                {
                    RayData            rayData            = a_rayData [octreeRayEntity2];
                    RayMaxDistanceData rayMaxDistanceData = a_rayMaxDistanceData [octreeRayEntity2];

                    Debug.DrawLine(rayData.ray.origin, rayData.ray.origin + rayData.ray.direction * rayMaxDistanceData.f, Color.red);
                }
                else if (i_collisionChecksIndex == 0)
                {
                    RayData            rayData            = a_rayData [octreeRayEntity2];
                    RayMaxDistanceData rayMaxDistanceData = a_rayMaxDistanceData [octreeRayEntity2];

                    Debug.DrawLine(rayData.ray.origin, rayData.ray.origin + rayData.ray.direction * rayMaxDistanceData.f, Color.red);
                }


                IsCollidingData isCollidingData = a_isCollidingData [octreeRayEntity];

                if (isCollidingData.i_collisionsCount > 0)
                {
                    Debug.Log("Is colliding.");
                }
            }
        }
            public void Execute(int i_arrayIndex)
            {
                Entity octreeRootNodeEntity = a_collisionChecksEntities [i_arrayIndex];


                // Its value should be 0, if no collision is detected.
                // And >= 1, if instance collision is detected, or there is more than one collision,
                // indicating number of collisions.
                IsCollidingData isCollidingData = a_isCollidingData [octreeRootNodeEntity];
                // Stores reference to detected colliding instance.
                DynamicBuffer <CollisionInstancesBufferElement> a_collisionInstancesBuffer = collisionInstancesBufferElement [octreeRootNodeEntity];


                isCollidingData.i_nearestInstanceCollisionIndex = 0;
                isCollidingData.f_nearestDistance = float.PositiveInfinity;

                isCollidingData.i_collisionsCount = 0;                // Reset colliding instances counter.


                RootNodeData octreeRootNodeData = a_octreeRootNodeData [octreeRootNodeEntity];

                DynamicBuffer <NodeBufferElement> a_nodesBuffer = nodeBufferElement [octreeRootNodeEntity];
                DynamicBuffer <NodeInstancesIndexBufferElement> a_nodeInstancesIndexBuffer = nodeInstancesIndexBufferElement [octreeRootNodeEntity];
                DynamicBuffer <NodeChildrenBufferElement>       a_nodeChildrenBuffer       = nodeChildrenBufferElement [octreeRootNodeEntity];
                DynamicBuffer <InstanceBufferElement>           a_instanceBuffer           = instanceBufferElement [octreeRootNodeEntity];


                RayEntityPair4CollisionData rayEntityPair4CollisionData = a_rayEntityPair4CollisionData [octreeRootNodeEntity];

                // Ray entity pair, for collision checks

                Entity ray2CheckEntity = rayEntityPair4CollisionData.ray2CheckEntity;

                // Is target octree active
                if (a_isActiveTag.Exists(ray2CheckEntity))
                {
                    RayData            rayData            = a_rayData [ray2CheckEntity];
                    RayMaxDistanceData rayMaxDistanceData = a_rayMaxDistanceData [ray2CheckEntity];


                    // To even allow instances collision checks, octree must have at least one instance.
                    if (octreeRootNodeData.i_totalInstancesCountInTree > 0)
                    {
                        if (GetCollidingRayInstances_Common._GetNodeColliding(octreeRootNodeData, octreeRootNodeData.i_rootNodeIndex, rayData.ray, ref a_collisionInstancesBuffer, ref isCollidingData, a_nodesBuffer, a_nodeChildrenBuffer, a_nodeInstancesIndexBuffer, a_instanceBuffer, rayMaxDistanceData.f))
                        {
                            /*
                             * // Debug
                             * string s_collidingIDs = "" ;
                             * int i_collisionsCount = isCollidingData.i_collisionsCount ;
                             *
                             * for ( int i = 0; i < i_collisionsCount; i ++ )
                             * {
                             *  CollisionInstancesBufferElement collisionInstancesBuffer = a_collisionInstancesBuffer [i] ;
                             *  s_collidingIDs += collisionInstancesBuffer.i_ID + ", " ;
                             * }
                             *
                             * Debug.Log ( "Is colliding with #" + i_collisionsCount + " instances of IDs: " + s_collidingIDs + "; Nearest collided instance is at " + isCollidingData.f_nearestDistance + "m, with index #" + isCollidingData.i_nearestInstanceCollisionIndex ) ;
                             */
                        }
                    }
                }

                a_isCollidingData [octreeRootNodeEntity] = isCollidingData;  // Set back.
            }
        /// <summary>
        ///
        /// </summary>
        /// <param name="a_collisionChecksEntities"></param>
        /// <param name="a_rayData"></param>
        /// <param name="a_rayMaxDistanceData"></param>
        /// <param name="a_isCollidingData"></param>
        /// <param name="collisionInstancesBufferElement"></param>
        /// <param name="canDebugAllChecks">Debug Log all checks, or only one (first one)</param>
        /// <param name="canDebugAllrays">Draw all, or only single ray (first one).</param>
        static public void _DebugRays(EntityCommandBuffer ecb, EntityArray a_collisionChecksEntities, ComponentDataFromEntity <RayData> a_rayData, ComponentDataFromEntity <RayMaxDistanceData> a_rayMaxDistanceData, ComponentDataFromEntity <IsCollidingData> a_isCollidingData, BufferFromEntity <CollisionInstancesBufferElement> collisionInstancesBufferElement, ComponentDataFromEntity <RayEntityPair4CollisionData> a_rayEntityPair4CollisionData, bool canDebugAllChecks, bool canDebugAllrays)
        {
            // Debug
            // ! Ensure test this only with single, or at most few ray entiities.


            // if ( !canDebugAllrays )

            // Debug all, or only one check
            int i_debugCollisionChecksCount = canDebugAllChecks ? a_collisionChecksEntities.Length : 1;

            for (int i_collisionChecksIndex = 0; i_collisionChecksIndex < i_debugCollisionChecksCount; i_collisionChecksIndex++)
            {
                Entity octreeRayEntity = a_collisionChecksEntities [i_collisionChecksIndex];
                Entity octreeRayEntity2;

                if (!a_rayData.Exists(octreeRayEntity))
                {
                    RayEntityPair4CollisionData rayEntityPair4CollisionData = a_rayEntityPair4CollisionData [octreeRayEntity];
                    octreeRayEntity2 = rayEntityPair4CollisionData.ray2CheckEntity;
                }
                else
                {
                    octreeRayEntity2 = octreeRayEntity;
                }

                // Draw all available rays, or signle ray
                if (canDebugAllrays)
                {
                    RayData            rayData            = a_rayData [octreeRayEntity2];
                    RayMaxDistanceData rayMaxDistanceData = a_rayMaxDistanceData [octreeRayEntity2];

                    Debug.DrawLine(rayData.ray.origin, rayData.ray.origin + rayData.ray.direction * rayMaxDistanceData.f, Color.red);
                }
                else if (i_collisionChecksIndex == 0)
                {
                    RayData            rayData            = a_rayData [octreeRayEntity2];
                    RayMaxDistanceData rayMaxDistanceData = a_rayMaxDistanceData [octreeRayEntity2];

                    Debug.DrawLine(rayData.ray.origin, rayData.ray.origin + rayData.ray.direction * rayMaxDistanceData.f, Color.red);
                }
                //}


                // Last known instances collisions count.
                IsCollidingData isCollidingData = a_isCollidingData [octreeRayEntity];

                if (isCollidingData.i_collisionsCount > 0)
                {
                    // Debug.Log ( "Octree: Last known instances collisions count #" + isCollidingData.i_collisionsCount ) ;

                    // Stores reference to detected colliding instance.
                    DynamicBuffer <CollisionInstancesBufferElement> a_collisionInstancesBuffer = collisionInstancesBufferElement [octreeRayEntity];


                    string s_collidingIDs = "";

                    CollisionInstancesBufferElement collisionInstancesBuffer;

                    for (int i = 0; i < isCollidingData.i_collisionsCount; i++)
                    {
                        collisionInstancesBuffer = a_collisionInstancesBuffer [i];
                        s_collidingIDs          += collisionInstancesBuffer.i_ID + ", ";
                    }

                    CollisionInstancesBufferElement closestCollisionInstance = a_collisionInstancesBuffer [isCollidingData.i_nearestInstanceCollisionIndex];
                    Entity closestInstanceEntity = new Entity()
                    {
                        Index   = closestCollisionInstance.i_ID,
                        Version = closestCollisionInstance.i_version
                    };

                    // Test highlight
                    Highlight.SwitchMethods._Switch(ecb, closestInstanceEntity);

                    Debug.Log("Is colliding with #" + isCollidingData.i_collisionsCount + " instances of IDs: " + s_collidingIDs + "; Nearest collided instance is at " + isCollidingData.f_nearestDistance + "m, with ID #" + a_collisionInstancesBuffer [isCollidingData.i_nearestInstanceCollisionIndex].i_ID);
                }
            } // for
        }