Exemplo n.º 1
0
        public IntersectResult Intersect(Ray3 ray, double maxDistance)
        {
            if (!BoundingBox.Intersect(ray, maxDistance))
            {
                return(IntersectResult.NoHit());
            }
            double          minDistance = maxDistance;
            IntersectResult minResult   = IntersectResult.NoHit();

            if (nodeTriangles != null)
            {
                foreach (var triangle in nodeTriangles)
                {
                    IntersectResult result = triangle.Intersect(ray, minDistance);
                    if (result.Geometry != null && result.Distance < minDistance)
                    {
                        minDistance = result.Distance;
                        minResult   = result;
                    }
                }
            }
            if (children != null)
            {
                foreach (var child in children)
                {
                    IntersectResult result = child.Intersect(ray, minDistance);
                    if (result.Geometry != null && result.Distance < minDistance)
                    {
                        minDistance = result.Distance;
                        minResult   = result;
                    }
                }
            }
            return(minResult);
        }
Exemplo n.º 2
0
        public IntersectResult Intersect(Ray3 ray, double maxDistance)
        {
            if (!BoundingBox.Intersect(ray, maxDistance))
            {
                return(IntersectResult.NoHit());
            }
            if (normal.SqrLength() == 0)
            {
                return(IntersectResult.NoHit());
            }
            Vector3 w0       = ray.Origin - vertices[0];
            double  x        = -normal ^ w0;
            double  y        = normal ^ ray.Direction;
            double  distance = x / y;

            if (y == 0 || distance < 0)
            {
                return(IntersectResult.NoHit());
            }
            Vector3 position = ray.GetPoint(distance);
            double  uu       = edgeAB ^ edgeAB;
            double  uv       = edgeAB ^ edgeAC;
            double  vv       = edgeAC ^ edgeAC;
            Vector3 w        = position - vertices[0];
            double  wu       = w ^ (edgeAB);
            double  wv       = w ^ (edgeAC);
            double  D        = uv * uv - uu * vv;
            double  beta     = (uv * wv - vv * wu) / D;

            if (beta < 0 || beta > 1)
            {
                return(IntersectResult.NoHit());
            }
            double gamma = (uv * wu - uu * wv) / D;

            if (gamma < 0 || beta + gamma > 1)
            {
                return(IntersectResult.NoHit());
            }
            double  alpha     = 1 - beta - gamma;
            Vector3 newNormal = normal;

            if (distance > 0 && normals != null)
            {
                Vector3 n1Interpolated = normals[0] * alpha;
                Vector3 n2Interpolated = normals[1] * beta;
                Vector3 n3Interpolated = normals[2] * gamma;
                newNormal = n1Interpolated + n2Interpolated + n3Interpolated;
            }
            IntersectResult result = new IntersectResult(this, distance, position, newNormal);

            if (textures != null)
            {
                Vector2 t1Interpolated = textures[0] * alpha;
                Vector2 t2Interpolated = textures[1] * beta;
                Vector2 t3Interpolated = textures[2] * gamma;
                result.TextureCoordinates = t1Interpolated + t2Interpolated + t3Interpolated;
            }
            return(result);
        }
Exemplo n.º 3
0
        public override IntersectResult Intersect(Ray3 ray)
        {
            double a = ray.Direction ^ normal;

            if (a >= 0)
            {
                return(IntersectResult.NoHit());
            }
            double b        = normal ^ (ray.Origin - position);
            double distance = -b / a;

            return(new IntersectResult(this, distance, ray.GetPoint(distance), normal));
        }
Exemplo n.º 4
0
        public override IntersectResult Intersect(Ray3 ray)
        {
            double          minDistance = Constant.Infinity;
            IntersectResult minResult   = IntersectResult.NoHit();

            foreach (Geometry geometry in geometries)
            {
                IntersectResult result = geometry.Intersect(ray);
                if (result.Geometry != null && result.Distance < minDistance)
                {
                    minDistance = result.Distance;
                    minResult   = result;
                }
            }
            return(minResult);
        }
Exemplo n.º 5
0
        public override IntersectResult Intersect(Ray3 ray)
        {
            Vector3 v     = ray.Origin - center;
            double  a0    = v.SqrLength() - radius * radius;
            double  DdotV = ray.Direction ^ v;

            if (DdotV <= 0)
            {
                double discr = DdotV * DdotV - a0;
                if (discr >= 0)
                {
                    double          distance = -DdotV - Math.Sqrt(discr);
                    Vector3         position = ray.GetPoint(distance);
                    Vector3         normal   = (position - center).Normalize();
                    IntersectResult result   = new IntersectResult(this, distance, position, normal);
                    result.TextureCoordinates = new Vector2(
                        (Math.PI + Math.Atan2(position.Z - center.Z, position.X - center.X)) / (Math.PI * 2),
                        1.0 - ((Math.PI / 2) + Math.Asin((position.Y - center.Y) / radius)) / Math.PI);
                    return(result);
                }
            }
            return(IntersectResult.NoHit());
        }