Ejemplo n.º 1
0
    public bool HitSphere(jRay r, float tMin, float tMax, jCommonMaterial mat, out jRay or, out float t)
    {
        or = new jRay()
        {
            bAlive = 0,
            color  = r.color * JobGlobal.envColor
        };
        t = tMax;
        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;

        if (discriminant <= 0)
        {
            return(false);
        }

        float temp = (-b - Mathf.Sqrt(b * b - a * c)) / a;

        if (temp < tMax && temp > tMin)
        {
            Vector3 p    = r.origin + temp * r.direction;
            Vector3 n    = (p - center) / radius;
            Vector3 refl = JobGlobal.emit(r.direction, n, mat.distance);
            or.bAlive    = 1;
            or.origin    = p;
            or.direction = refl;
            or.color     = r.color * mat.albedo;
            t            = temp;
            return(true);
        }

        temp = (-b + Mathf.Sqrt(b * b - a * c)) / a;
        if (temp < tMax && temp > tMin)
        {
            Vector3 p    = r.origin + temp * r.direction;
            Vector3 n    = (p - center) / radius;
            Vector3 refl = JobGlobal.emit(r.direction, n, mat.distance);
            or.bAlive    = 1;
            or.origin    = p;
            or.direction = refl;
            or.color     = r.color * mat.albedo;
            t            = temp;
            return(true);
        }

        return(false);
    }
Ejemplo n.º 2
0
    public jSphere toJobSphere()
    {
        jCommonMaterial mat = new jCommonMaterial()
        {
            albedo   = material.albedo,
            distance = 0f
        };

        return(new jSphere()
        {
            center = transform.position,
            radius = transform.lossyScale.x / 2f,
            mat = mat
        });
    }
Ejemplo n.º 3
0
    public void Execute(int index)
    {
        if (sourceRays[index].bAlive == 0)
        {
            destRays[index] = new jRay()
            {
                bAlive = 0,
                color  = sourceRays[index].color
            };
            return;
        }

        float closestSoFar = t_max;
        bool  hitAnything  = false;

        for (int i = 0; i < objs.Length; i++)
        {
            jRay            r        = sourceRays[index];
            jCommonMaterial material = objs[i].mat;
            float           t        = t_max;
            jRay            or       = new jRay();
            if (objs[i].HitSphere(r, t_min, closestSoFar, material, out or, out t))
            {
                hitAnything     = true;
                closestSoFar    = t;
                destRays[index] = or;
            }
        }

        if (!hitAnything)
        {
            destRays[index] = new jRay()
            {
                bAlive = 0,
                color  = sourceRays[index].color * JobGlobal.envColor
            };
        }
    }