Exemple #1
0
        protected override bool RayCheckInternal(int time, Vector3 rayStart, Vector3 rayDirection, out int triangleIndex, out bool hitOpaque, out Vector3 intersection)
        {
            Matrix4x4 mat = GetTransform();

            hitOpaque     = false;
            triangleIndex = -1;
            intersection  = new Vector3(0, 0, 0);
            double  minDistSq            = Double.MaxValue;
            Vector3 triangleIntersection = new Vector3(0, 0, 0);

            int elementCount = GetElementCount(time, false);

            for (int i = 0; i < elementCount; i += 3)
            {
                Vector3 a = mat * opaqueVertexList[i].Position;
                Vector3 b = mat * opaqueVertexList[i + 1].Position;
                Vector3 c = mat * opaqueVertexList[i + 2].Position;
                if (GeometryMath.Intersect(rayStart, rayDirection, a, b, c, out triangleIntersection))
                {
                    double distSq = rayStart.DistanceSquared(triangleIntersection);
                    if (distSq < minDistSq)
                    {
                        minDistSq     = distSq;
                        intersection  = triangleIntersection;
                        triangleIndex = i;
                        hitOpaque     = true;
                    }
                }
            }

            elementCount = GetElementCount(time, true);
            for (int i = 0; i < elementCount; i += 3)
            {
                Vector3 a = mat * transparentVertexList[i].Position;
                Vector3 b = mat * transparentVertexList[i + 1].Position;
                Vector3 c = mat * transparentVertexList[i + 2].Position;
                if (GeometryMath.Intersect(rayStart, rayDirection, a, b, c, out triangleIntersection))
                {
                    double distSq = rayStart.DistanceSquared(triangleIntersection);
                    if (distSq < minDistSq)
                    {
                        minDistSq     = distSq;
                        intersection  = triangleIntersection;
                        triangleIndex = i;
                        hitOpaque     = false;
                    }
                }
            }

            return(triangleIndex != -1);
        }
Exemple #2
0
        public void TestRayTriangleIntersect()
        {
            Vector3 lineStart = new Vector3(0, 0, 0);
            Vector3 lineEnd   = new Vector3(0, 0, 2);

            Vector3 a = new Vector3(0, -0.5, 1);
            Vector3 b = new Vector3(-0.5, 0.5, 1);
            Vector3 c = new Vector3(0.5, 0.5, 1);

            // Check that line hits triangle (should pass through center)
            Assert.IsTrue(GeometryMath.Intersect(lineStart, lineEnd, a, b, c), "Expected to hit triangle but did not (line should go through the center)");

            lineEnd = new Vector3(0, 0, 1);
            // Check that line hits triangle (should just hit the triangle at the end)
            Assert.IsTrue(GeometryMath.Intersect(lineStart, lineEnd, a, b, c), "Expected to hit triangle but did not (line should hit in the very end)");

            lineStart = new Vector3(0, 0, 1);
            lineEnd   = new Vector3(0, 0, 2);
            // Check that line hits triangle (should just hit the triangle in the beginning)
            Assert.IsTrue(GeometryMath.Intersect(lineStart, lineEnd, a, b, c), "Expected to hit triangle but did not (line should hit in the very beginning)");

            lineStart = new Vector3(-0.4, 0, 0);
            lineEnd   = new Vector3(-0.4, 0, 2);
            // Check that line does not hit triangle (should intersect plane but not triangle)
            Assert.IsFalse(GeometryMath.Intersect(lineStart, lineEnd, a, b, c), "Expected to miss triangle but hit anyway (line intersects plane but not triangle)");

            lineStart = new Vector3(0, 0, 1);
            lineEnd   = new Vector3(1, 0, 1);
            // Check that line does not hit triangle (line is co-linear with triangle)
            Assert.IsFalse(GeometryMath.Intersect(lineStart, lineEnd, a, b, c), "Expected to miss triangle but hit anyway (line is co-linear with triangle)");

            lineStart = new Vector3(0, 0, 0);
            lineEnd   = new Vector3(0, 0, 0.9);
            // Check that line does not hit triangle (not long enough)
            Assert.IsFalse(GeometryMath.Intersect(lineStart, lineEnd, a, b, c), "Expected to miss triangle but hit anyway (line not be long enough)");

            lineStart = new Vector3(0, 0, 1.1);
            lineEnd   = new Vector3(0, 0, 2);
            // Check that line does not hit triangle (goes wrong way)
            Assert.IsFalse(GeometryMath.Intersect(lineStart, lineEnd, a, b, c), "Expected to miss triangle, but hit anyway (line goes away from triangle)");
        }