Пример #1
0
        public bool IntersectRay(BVHRay r)//, out float hitDist)
        {
            float tXmin, tXmax, tYmin, tYmax, tZmin, tZmax;
            float xA = 1f / r.Direction.x, yA = 1f / r.Direction.y, zA = 1f / r.Direction.z;
            float xE = r.Origin.x, yE = r.Origin.y, zE = r.Origin.z;

            // calculate t interval in x-axis
            if (xA >= 0)
            {
                tXmin = (BMin.x - xE) * xA;
                tXmax = (BMax.x - xE) * xA;
            }
            else
            {
                tXmin = (BMax.x - xE) * xA;
                tXmax = (BMin.x - xE) * xA;
            }

            // calculate t interval in y-axis
            if (yA >= 0)
            {
                tYmin = (BMin.y - yE) * yA;
                tYmax = (BMax.y - yE) * yA;
            }
            else
            {
                tYmin = (BMax.y - yE) * yA;
                tYmax = (BMin.y - yE) * yA;
            }

            // calculate t interval in z-axis
            if (zA >= 0)
            {
                tZmin = (BMin.z - zE) * zA;
                tZmax = (BMax.z - zE) * zA;
            }
            else
            {
                tZmin = (BMax.z - zE) * zA;
                tZmax = (BMin.z - zE) * zA;
            }

            // find if there an intersection among three t intervals

            // float3
            var tMin = math.max(tXmin, math.max(tYmin, tZmin));
            var tMax = math.min(tXmax, math.min(tYmax, tZmax));

            // Vector3
            //t_min = Mathf.Max(t_xmin, Mathf.Max(t_ymin, t_zmin));
            //t_max = Mathf.Min(t_xmax, Mathf.Min(t_ymax, t_zmax));

            //hitDist = t_min;
            return(tMin <= tMax);
        }
Пример #2
0
        public static bool IntersectRay(BVHRay r, float3 min, float3 max, out float hitDist)
        {
            float tXmin, tXmax, tYmin, tYmax, tZmin, tZmax;
            float xA = 1f / r.Direction.x, yA = 1f / r.Direction.y, zA = 1f / r.Direction.z;
            float xE = r.Origin.x, yE = r.Origin.y, zE = r.Origin.z;

            // calculate t interval in x-axis
            if (xA >= 0)
            {
                tXmin = (min.x - xE) * xA;
                tXmax = (max.x - xE) * xA;
            }
            else
            {
                tXmin = (max.x - xE) * xA;
                tXmax = (min.x - xE) * xA;
            }

            // calculate t interval in y-axis
            if (yA >= 0)
            {
                tYmin = (min.y - yE) * yA;
                tYmax = (max.y - yE) * yA;
            }
            else
            {
                tYmin = (max.y - yE) * yA;
                tYmax = (min.y - yE) * yA;
            }

            // calculate t interval in z-axis
            if (zA >= 0)
            {
                tZmin = (min.z - zE) * zA;
                tZmax = (max.z - zE) * zA;
            }
            else
            {
                tZmin = (max.z - zE) * zA;
                tZmax = (min.z - zE) * zA;
            }

            var tMin = math.max(tXmin, math.max(tYmin, tZmin));
            var tMax = math.min(tXmax, math.min(tYmax, tZmax));

            hitDist = tMin;
            return(tMin <= tMax);
        }
Пример #3
0
        public bool IntersectRay(BVHRay ray, out HitInfo hitInfo)
        {
            bool intersect = false;

            hitInfo.HitPoint        = new float3(0f, 0f, 0f);
            hitInfo.HitNormal       = new float3(0f, 1f, 0f);
            hitInfo.HitDistance     = float.MaxValue;
            hitInfo.TerrainSourceID = (byte)TerrainSourceID;

            float3 rayO = ray.Origin, rayD = ray.Direction;

            float3 edge0 = V0 - rayO;
            float3 edge1 = V1 - rayO;
            float3 edge2 = V2 - rayO;

            float3 cross0 = math.normalize(math.cross(edge0, edge1));
            float3 cross1 = math.normalize(math.cross(edge1, edge2));
            float3 cross2 = math.normalize(math.cross(edge2, edge0));

            float angle0 = math.dot(cross0, rayD);
            float angle1 = math.dot(cross1, rayD);
            float angle2 = math.dot(cross2, rayD);

            if (angle0 < 0f && angle1 < 0f && angle2 < 0f)
            {
                float3 w0 = rayO - V0;
                var    a  = -math.dot(N, w0);
                var    b  = math.dot(N, rayD);
                var    r  = a / b;
                float3 I  = rayO + rayD * r;
                if (a < 0f)
                {
                    hitInfo.HitPoint    = I;
                    hitInfo.HitDistance = r;
                    hitInfo.HitNormal   = math.normalize(N);
                    intersect           = true;
                }
            }
            return(intersect);
        }
Пример #4
0
        public bool IntersectRay(BVHRay ray, ref NativeArray <HitInfo> hitInfos, int hitInfoID)
        {
            float3 rayO = ray.Origin, rayD = ray.Direction;

            float3 edge0 = V0 - rayO;
            float3 edge1 = V1 - rayO;
            float3 edge2 = V2 - rayO;

            float3 cross0 = math.normalize(math.cross(edge0, edge1));
            float3 cross1 = math.normalize(math.cross(edge1, edge2));
            float3 cross2 = math.normalize(math.cross(edge2, edge0));

            float angle0 = math.dot(cross0, rayD);
            float angle1 = math.dot(cross1, rayD);
            float angle2 = math.dot(cross2, rayD);

            if (angle0 < 0f && angle1 < 0f && angle2 < 0f)
            {
                float3 w0 = rayO - V0;
                var    a  = -math.dot(N, w0);
                var    b  = math.dot(N, rayD);
                var    r  = a / b;
                float3 I  = rayO + rayD * r;
                if (a < 0f)
                {
                    HitInfo hI = new HitInfo
                    {
                        HitNormal       = math.normalize(N),
                        HitPoint        = I,
                        HitDistance     = r,
                        TerrainSourceID = (byte)TerrainSourceID
                    };
                    hitInfos[hitInfoID] = hI;
                    return(true);
                }
            }
            return(false);
        }