Example #1
0
        public void CommonHitsTest([Values(2, 3, 4)] int maxHits)
        {
            var rayStart          = new Vector3(0f, 4f, 0f);
            var cubeCommand       = new RaycastCommand(cubeCollider.transform.position + rayStart, Vector3.down);
            var meshCubeCommand   = new RaycastCommand(meshCubeCollider.transform.position + rayStart, Vector3.down);
            var meshConvexCommand = new RaycastCommand(meshConvexCollider.transform.position + rayStart, Vector3.down);
            var emptyCommand      = new RaycastCommand(new Vector3(30f, 0f, 0f) + rayStart, Vector3.down);

            var commandsArray = new RaycastCommand[] { cubeCommand, meshCubeCommand, emptyCommand, meshConvexCommand };
            var commands      = new NativeArray <RaycastCommand>(commandsArray, Allocator.TempJob);
            var commandHits   = new NativeArray <RaycastHit>(commands.Length * maxHits, Allocator.TempJob);

            var raycastAllCommand = new RaycastAllCommand(commands, commandHits, maxHits);

            raycastAllCommand.Schedule(default(JobHandle)).Complete();

            Assert.AreEqual(cubeCollider, commandHits[maxHits * 0].collider);
            Assert.AreEqual(meshCubeCollider, commandHits[maxHits * 1].collider);
            Assert.AreEqual(null, commandHits[maxHits * 2].collider);
            Assert.AreEqual(meshConvexCollider, commandHits[maxHits * 3].collider);

            var physicsHits = new RaycastHit[maxHits];

            for (int i = 0; i < commands.Length; i++)
            {
                var physicsHitsCount = Physics.RaycastNonAlloc(commands[i].from, commands[i].direction, physicsHits);

                SortHits(physicsHits, physicsHitsCount);

                for (int j = 0; j < physicsHitsCount; j++)
                {
                    var physicsHit = physicsHits[j];
                    var commandHit = commandHits[i * maxHits + j];
                    RaycastHitEquality.AssertEqual(physicsHit, commandHit);
                }
                if (physicsHitsCount < maxHits)
                {
                    Assert.AreEqual(null, commandHits[i * maxHits + physicsHitsCount].collider);
                }
            }

            commandHits.Dispose();
            commands.Dispose();
            raycastAllCommand.Dispose();
        }
        public void SingleHitsTest()
        {
            var maxHits           = 4;
            var rayStart          = new Vector3(0f, 2f, 0f);
            var cubeCommand       = new SpherecastCommand(cubeCollider.transform.position + rayStart, 1f, Vector3.down);
            var meshCubeCommand   = new SpherecastCommand(meshCubeCollider.transform.position + rayStart, 1f, Vector3.down);
            var meshConvexCommand = new SpherecastCommand(meshConvexCollider.transform.position + rayStart, 1f, Vector3.down);
            var emptyCommand      = new SpherecastCommand(new Vector3(30f, 0f, 0f) + rayStart, 1f, Vector3.down);

            var commandsArray = new SpherecastCommand[] { cubeCommand, meshCubeCommand, emptyCommand, meshConvexCommand };

            commands    = new NativeArray <SpherecastCommand>(commandsArray, Allocator.TempJob);
            commandHits = new NativeArray <RaycastHit>(commands.Length * maxHits, Allocator.TempJob);

            raycastAllCommand = new SpherecastAllCommand(commands, commandHits, maxHits);

            raycastAllCommand.Schedule(default(JobHandle)).Complete();

            Assert.AreEqual(cubeCollider, commandHits[maxHits * 0].collider);
            Assert.AreEqual(meshCubeCollider, commandHits[maxHits * 1].collider);
            Assert.AreEqual(null, commandHits[maxHits * 2].collider);

            var physicsHits = new RaycastHit[maxHits];

            for (int i = 0; i < commands.Length; i++)
            {
                var unityHitsCount = Physics.SphereCastNonAlloc(commands[i].origin, commands[i].radius, commands[i].direction, physicsHits);

                for (int j = 0; j < unityHitsCount; j++)
                {
                    var physicsHit = physicsHits[j];
                    var commandHit = commandHits[i * maxHits + j];
                    RaycastHitEquality.AssertEqual(physicsHit, commandHit);
                }
                if (unityHitsCount < maxHits)
                {
                    Assert.AreEqual(null, commandHits[i * maxHits + unityHitsCount].collider);
                }
            }
        }
        public void NeedForMinStepTest()
        {
            var maxHits           = 4;
            var rayStart          = new Vector3(0f, 4f, 0f);
            var meshConvexCommand = new SpherecastCommand(meshConvexCollider.transform.position + rayStart, 0.1f, Vector3.down);

            var commandsArray = new SpherecastCommand[] { meshConvexCommand };

            commands    = new NativeArray <SpherecastCommand>(commandsArray, Allocator.TempJob);
            commandHits = new NativeArray <RaycastHit>(commands.Length * maxHits, Allocator.TempJob);

            //setting minStep to 0f
            raycastAllCommand = new SpherecastAllCommand(commands, commandHits, maxHits);

            raycastAllCommand.Schedule(default(JobHandle)).Complete();

            Assert.AreEqual(meshConvexCollider, commandHits[0].collider);

            var physicsHits = new RaycastHit[maxHits];

            for (int i = 0; i < commands.Length; i++)
            {
                var unityHitsCount = Physics.SphereCastNonAlloc(commands[i].origin, commands[i].radius, commands[i].direction, physicsHits);

                for (int j = 0; j < unityHitsCount; j++)
                {
                    var physicsHit = physicsHits[j];
                    var commandHit = commandHits[i * maxHits + j];
                    RaycastHitEquality.AssertEqual(physicsHit, commandHit);
                }
                if (unityHitsCount < maxHits)
                {
                    Assert.IsNull(commandHits[i * maxHits + unityHitsCount].collider, "RaycastHit in corner point behaviour changed, minStep may be required");
                }
            }
        }