Exemplo n.º 1
0
        public (Vec3 color, Ray ray) Scatter(Ray rIn, HitRecord hitRec)
        {
            var col           = new Vec3(1, 1, 1);
            var nOverNP       = hitRec.IsFrontFace ? (1 / RefIndex) : RefIndex;
            var unitDirection = rIn.Direction.Normalize();
            var cosAlpha      = Math.Min(MathR.Dot(unitDirection * -1, hitRec.N), 1.0F);
            var sinAlpha      = MathF.Sqrt(1.0F - (cosAlpha * cosAlpha));

            // TIR
            if (nOverNP * sinAlpha > 1)
            {
                var reflect    = MathR.Reflect(unitDirection, hitRec.N);
                var scatterRay = new Ray(hitRec.P, reflect);
                return(col, scatterRay);
            }

            //Reflection
            var reflectionProp = MathR.Schlick(cosAlpha, RefIndex);

            if (Rand.Rand01() < reflectionProp)
            {
                var reflect    = MathR.Reflect(unitDirection, hitRec.N);
                var scatterRay = new Ray(hitRec.P, reflect);
                return(col, scatterRay);
            }

            //Refraction
            var refract     = MathR.Refract(unitDirection, hitRec.N, nOverNP);
            var scatterRay2 = new Ray(hitRec.P, refract);

            return(col, scatterRay2);
        }
Exemplo n.º 2
0
 public HitRecord(float t, Vec3 p, Vec3 n, Ray r, IMaterial material) : this()
 {
     (T, P, R, N, Material) = (t, p, r, n, material);
     IsFrontFace            = MathR.Dot(r.Direction, N) < 0;
     N = IsFrontFace ? n : n * -1;
 }