Exemple #1
0
 private static Vector3[] PerlinGenerate()
 {
     Vector3[] res = new Vector3[256];
     for (int i = 0; i < 256; i++)
     {
         res[i] = new Vector3(2 * VectorHelpers.RandomFloat() - 1, 2 * VectorHelpers.RandomFloat() - 1, 2 * VectorHelpers.RandomFloat() - 1).GetUnitVector();
     }
     return(res);
 }
Exemple #2
0
 private static int[] PerlinGeneratePerm()
 {
     int[] res = new int[256];
     for (int i = 0; i < 256; i++)
     {
         res[i] = i;
     }
     // Permute the array of 0..255
     for (int i = 0; i < 256; i++)
     {
         int swithedIndex = (int)((i + 1) * VectorHelpers.RandomFloat());
         int temp         = res[i];
         res[i]            = res[swithedIndex];
         res[swithedIndex] = temp;
     }
     return(res);
 }
Exemple #3
0
        public override bool Scatter(Ray r, HitRecord hitRecord, out Vector3 attenuation, out Ray scattered)
        {
            attenuation = Vector3.One;

            Vector3 reflected = VectorHelpers.Reflect(r.Direction, hitRecord.SurfaceNormal);
            Vector3 outwardNormal;
            float   niOverNt;
            float   cosine;

            if (Vector3.Dot(r.Direction, hitRecord.SurfaceNormal) > 0)
            {
                outwardNormal = -hitRecord.SurfaceNormal;
                niOverNt      = RefractiveIndex;
                cosine        = RefractiveIndex * Vector3.Dot(r.Direction, hitRecord.SurfaceNormal) / r.Direction.Length();
            }
            else
            {
                outwardNormal = hitRecord.SurfaceNormal;
                niOverNt      = 1.0f / RefractiveIndex;
                cosine        = -Vector3.Dot(r.Direction, hitRecord.SurfaceNormal) / r.Direction.Length();
            }

            float reflectProb = 1.0f;

            if (Refract(r.Direction, outwardNormal.GetUnitVector(), niOverNt, out Vector3 refracted))
            {
                reflectProb = Schlick(cosine);
            }

            if (VectorHelpers.RandomFloat() < reflectProb)
            {
                scattered = new Ray(hitRecord.HitPoint, reflected);
            }
            else
            {
                scattered = new Ray(hitRecord.HitPoint, refracted);
            }
            return(true);
        }