public override bool hit(zRay r, float t_min, float t_max, ref hit_record rec)
    {
        if (rec.lastHit == id)
        {
            return(false);
        }
        Vector3 oc           = r.origin - center;
        float   a            = Vector3.Dot(r.direction, r.direction);
        float   b            = Vector3.Dot(oc, r.direction);
        float   c            = Vector3.Dot(oc, oc) - radius * radius;
        float   discriminant = b * b - a * c;

        //Debug.Log("try hit " + discriminant + " " + r.origin + " " + r.direction);
        if (discriminant > 0)
        {
            rec.mat = material;
            float temp = (-b - Mathf.Sqrt(b * b - a * c)) / a;
            if (temp < t_max && temp > t_min)
            {
                rec.t       = temp;
                rec.p       = r.point_at_parameter(rec.t);
                rec.normal  = (rec.p - center) / radius;
                rec.lastHit = id;
                Debug.Log("hit");
                if (DebugDrawer.isDebug)
                {
                    DebugDrawer.points.Add(rec.p);
                    DebugDrawer.normals.Add(rec.normal);
                }
                return(true);
            }
            temp = (-b + Mathf.Sqrt(b * b - a * c)) / a;
            if (temp < t_max && temp > t_min)
            {
                rec.t       = temp;
                rec.p       = r.point_at_parameter(rec.t);
                rec.normal  = (rec.p - center) / radius;
                rec.lastHit = id;
                Debug.Log("hit");
                if (DebugDrawer.isDebug)
                {
                    DebugDrawer.points.Add(rec.p);
                    DebugDrawer.normals.Add(rec.normal);
                }
                return(true);
            }
        }
        return(false);
    }
    public override bool hit(zRay r, float t_min, float t_max, ref hit_record rec)
    {
        if (rec.lastHit == id)
        {
            return(false);
        }
        Vector3 oc           = r.origin - center(r.time);
        float   a            = Vector3.Dot(r.direction, r.direction);
        float   b            = Vector3.Dot(oc, r.direction);
        float   c            = Vector3.Dot(oc, oc) - radius * radius;
        float   discriminant = b * b - a * c;

        if (discriminant > 0)
        {
            rec.mat = material;
            float temp = (-b - Mathf.Sqrt(discriminant)) / a;
            if (temp < t_max && temp > t_min)
            {
                rec.t       = temp;
                rec.p       = r.point_at_parameter(rec.t);
                rec.normal  = (rec.p - center(r.time)) / radius;
                rec.mat     = material;
                rec.lastHit = id;
                return(true);
            }
            temp = (-b + Mathf.Sqrt(discriminant)) / a;
            if (temp < t_max && temp > t_min)
            {
                rec.t       = temp;
                rec.p       = r.point_at_parameter(rec.t);
                rec.normal  = (rec.p - center(r.time)) / radius;
                rec.mat     = material;
                rec.lastHit = id;
                return(true);
            }
        }
        return(false);
    }
Beispiel #3
0
    public override bool hit(zRay r, float t_min, float t_max, ref hit_record rec)
    {
        if (Vector3.Dot(r.direction, normal) > 0)
        {
            return(false);
        }
        r = new zRay(trans.worldToLocalMatrix.MultiplyPoint(r.origin), trans.worldToLocalMatrix.MultiplyVector(r.direction));
        float t = (k - r.origin.z) / r.direction.z;

        if (t < t_min || t > t_max)
        {
            return(false);
        }
        float x = r.origin.x + t * r.direction.x;
        float y = r.origin.y + t * r.direction.y;

        if (x < x0 || x > x1 || y < y0 || y > y1)
        {
            return(false);
        }
        rec.uv      = new Vector2((x - x0) / (x1 - x0), (y - y0) / (y1 - y0));
        rec.t       = t * trans.lossyScale.x;
        rec.mat     = material;
        rec.p       = trans.localToWorldMatrix.MultiplyPoint(r.point_at_parameter(t));
        rec.normal  = normal;
        rec.lastHit = id;
        if (DebugDrawer.isDebug)
        {
            DebugDrawer.points.Add(rec.p);
            DebugDrawer.normals.Add(rec.normal);
            //Debug.Log(r.origin + " " + r.direction);
            //Debug.Log("rec.p " + rec.p + " local " + r.point_at_parameter(t));
            //Debug.Log("id " + id);
        }
        return(true);
    }
    public override bool hit(zRay r, float t_min, float t_max, ref hit_record rec)
    {
        if (Vector3.Dot(normal, r.direction) > 0)
        {
            return(false);
        }

        Vector3 e1  = b - a;
        Vector3 e2  = c - a;
        Vector3 p   = Vector3.Cross(r.direction, e2);
        float   det = Vector3.Dot(e1, p);
        Vector3 T;

        if (det > 0)
        {
            T = r.origin - a;
        }
        else
        {
            T   = a - r.origin;
            det = -det;
        }

        if (det < 0.0001f)
        {
            return(false);
        }

        float u = Vector3.Dot(T, p);

        if (u < 0f || u > det)
        {
            return(false);
        }

        Vector3 Q = Vector3.Cross(T, e1);
        float   v = Vector3.Dot(r.direction, Q);

        if (v < 0 || u + v > det)
        {
            return(false);
        }

        float t      = Vector3.Dot(e2, Q);
        float invDet = 1 / det;

        t *= invDet;
        u *= invDet;
        v *= invDet;

        rec.mat     = material;
        rec.t       = t;
        rec.p       = r.point_at_parameter(t);
        rec.normal  = normal;
        rec.lastHit = id;
        if (DebugDrawer.isDebug)
        {
            DebugDrawer.points.Add(rec.p);
            DebugDrawer.normals.Add(rec.normal);
        }
        return(true);
    }