Esempio n. 1
0
        public static float3 Color(Ray r, HitableArray <Sphere> world, int depth, ref Random rng)
        {
            var rec = new HitRecord();

            if (world.Hit(r, 0.001f, float.MaxValue, ref rec))
            {
                Ray    scattered   = new Ray();
                float3 attenuation = new float3();
                if (depth < 50)
                {
                    if (r.Scatter(rec, ref attenuation, ref scattered, ref rng))
                    {
                        return(attenuation * Color(scattered, world, depth + 1, ref rng));
                    }
                }
                else
                {
                    // this ray has run out of bounces - draw a black pixel
                    return(new float3());
                }
            }

            // the ray didn't hit anything, so draw the background color for this pixel
            return(Utils.BackgroundColor(ref r));
        }
Esempio n. 2
0
            public static float3 Color(Ray r, HitableArray <Sphere> world)
            {
                var rec = new HitRecord();

                if (world.Hit(r, 0f, float.MaxValue, ref rec))
                {
                    var rn = rec.normal;
                    return(0.5f * new float3(rn.x + 1f, rn.y + 1f, rn.z + 1f));
                }

                return(Utils.BackgroundColor(ref r));
            }
Esempio n. 3
0
        public static HitableArray <Sphere> RandomScene(int n, uint seed = default, Allocator allocator = Allocator.Persistent)
        {
            var rng = new Random();

            rng.InitState(seed);
            var list = new HitableArray <Sphere>(n, allocator)
            {
                [0] = new Sphere(new float3(0, -1000, 0), 1000,
                                 new Material(MaterialType.Lambertian, new float3(0.5f, 0.5f, 0.5f)))
            };

            var    i = 1;
            float3 centerComparePoint = new float3(4f, 0.2f, 0f);

            for (int a = -11; a < 11; a++)
            {
                for (int b = -11; b < 11; b++)
                {
                    float  chooseMat = rng.NextFloat();
                    float3 center    = new float3(a + 0.9f * rng.NextFloat(), 0.2f, b + 0.9f * rng.NextFloat());
                    if (!(math.length(center - centerComparePoint) > 0.9f))
                    {
                        continue;
                    }

                    if (chooseMat < 0.8f)        // diffuse
                    {
                        var diffuseMat = new Material(MaterialType.Lambertian, RandomFloat3(ref rng));
                        list[i++] = new Sphere(center, 0.2f, diffuseMat);
                    }
                    else if (chooseMat < 0.95f)        // metal
                    {
                        var metalMat = new Material(MaterialType.Metal, RandomFloat3(ref rng));
                        list[i++] = new Sphere(center, 0.2f, metalMat);
                    }
                    else                         // glass
                    {
                        var metalMat = new Material(MaterialType.Dielectric, RandomFloat3(ref rng));
                        list[i++] = new Sphere(center, 0.2f, metalMat);
                    }
                }
            }

            list[i++] = new Sphere(new float3(0, 1, 0), 1f,
                                   new Material(MaterialType.Dielectric, float3.zero, 0f, 1.5f));
            list[i++] = new Sphere(new float3(-4, 1, 0), 1f,
                                   new Material(MaterialType.Lambertian, new float3(0.4f, 0.2f, 0.1f), 0f, 1.5f));
            list[i] = new Sphere(new float3(4, 1, 0), 1f,
                                 new Material(MaterialType.Metal, new float3(0.7f, 0.6f, 0.5f)));

            return(list);
        }
Esempio n. 4
0
            public float3 Color(Ray r, HitableArray <Sphere> world)
            {
                var rec = new HitRecord();

                if (recursionCounter < maxHits && world.Hit(r, 0.001f, float.MaxValue, ref rec))
                {
                    recursionCounter++;
                    var target = rec.p + rec.normal + Utils.RandomInUnitSphere(random);
                    return(absorbRate * Color(new Ray(rec.p, target - rec.p), world));
                }

                return(Utils.BackgroundColor(ref r));
            }
 void Start()
 {
     m_MainCamera = UnityEngine.Camera.main;
     Spheres      = new HitableArray <Sphere>(0);
     UpdateWorld();
 }