コード例 #1
0
ファイル: Intersect.cs プロジェクト: mzandvliet/BurstRender
        public static float3 TraceRecursive(Ray3f ray, Scene scene, ref Random rng, NativeArray <float3> fibs, int depth, int maxDepth, ref ushort rayCount)
        {
            HitRecord hit;

            const float tMin = 0f;
            const float tMax = 1000f;

            bool hitSomething = Intersect.Scene(scene, ray, tMin, tMax, out hit);

            ++rayCount;

            float3 light = new float3(0);

            if (hitSomething)
            {
                Ray3f nextRay;
                bool  scattered = Scatter(ray, hit, ref rng, fibs, out nextRay);
                if (scattered && depth < maxDepth)
                {
                    light = TraceRecursive(nextRay, scene, ref rng, fibs, depth + 1, maxDepth, ref rayCount);
                }
                light = BRDF(hit) * light;
            }
            else
            {
                float t = 0.5f * (ray.direction.y + 1f);
                light = (1f - t) * new float3(1f) + t * scene.LightColor;
            }

            return(light);
        }
コード例 #2
0
ファイル: Intersect.cs プロジェクト: mzandvliet/BurstRender
        public static bool TraceStep(ref Ray3f ray, Scene scene, ref Random rng, NativeArray <float3> fibs, ref float3 color)
        {
            HitRecord hit;

            const float tMin = 0f;
            const float tMax = 1000f;

            bool hitSomething = Intersect.Scene(scene, ray, tMin, tMax, out hit);

            if (hitSomething)
            {
                color *= BRDF(hit);

                Ray3f nextRay;
                bool  scattered = Scatter(ray, hit, ref rng, fibs, out nextRay);
                if (scattered)
                {
                    ray = nextRay;
                    return(true);
                }
                return(false);
            }
            else
            {
                float  t     = 0.5f * (ray.direction.y + 1f);
                float3 light = (1f - t) * new float3(1f) + t * scene.LightColor;
                color = light * color;
                return(false);
            }
        }
コード例 #3
0
ファイル: Intersect.cs プロジェクト: mzandvliet/BurstRender
        public static bool Scene(Scene s, Ray3f r, float tMin, float tMax, out HitRecord finalHit)
        {
            bool      hitAnything = false;
            HitRecord closestHit  = new HitRecord();

            closestHit.t = tMax;

            // Note: this is the most naive brute force scene intersection you could ever do :P

            HitRecord hit;

            // Hit planes
            for (int i = 0; i < s.Planes.Length; i++)
            {
                if (Intersect.Plane(s.Planes[i], r, tMin, tMax, out hit))
                {
                    if (hit.t < closestHit.t)
                    {
                        hit.material = s.Planes[i].Material;
                        hitAnything  = true;
                        closestHit   = hit;
                    }
                }
            }

            // Hit disks
            for (int i = 0; i < s.Disks.Length; i++)
            {
                if (Intersect.Disk(s.Disks[i], r, tMin, tMax, out hit))
                {
                    if (hit.t < closestHit.t)
                    {
                        hit.material = s.Disks[i].Material;
                        hitAnything  = true;
                        closestHit   = hit;
                    }
                }
            }

            // // Hit spheres
            for (int i = 0; i < s.Spheres.Length; i++)
            {
                if (Intersect.Sphere(s.Spheres[i], r, tMin, tMax, out hit))
                {
                    if (hit.t < closestHit.t)
                    {
                        hit.material = s.Spheres[i].Material;
                        hitAnything  = true;
                        closestHit   = hit;
                    }
                }
            }

            finalHit = closestHit;

            return(hitAnything);
        }
コード例 #4
0
        private void OnDrawGizmos()
        {
            if (!Application.isPlaying || !_drawDebugRays)
            {
                return;
            }

            var rng = new Unity.Mathematics.Random(14387);

            var    screenPos = new float2(0);
            float3 pixel     = new float3(0f);

            Gizmos.color = new Color(1f, 1f, 1f, 0.5f);
            for (int s = 0; s < _scene.Spheres.Length; s++)
            {
                Gizmos.DrawSphere(_scene.Spheres[s].Center, _scene.Spheres[s].Radius);
            }

            for (int r = 0; r < 16; r++)
            {
                Gizmos.color = Color.HSVToRGB(r / 8f, 0.7f, 0.5f);
                float2 jitter = new float2(rng.NextFloat(), rng.NextFloat() / 16f);
                float2 p      = (screenPos + jitter);

                var ray = _camera.GetRay(p, ref rng);

                for (int t = 0; t < 8; t++)
                {
                    const float tMin = 0f;
                    const float tMax = 1000f;

                    Gizmos.DrawSphere(ray.origin, 0.01f);

                    HitRecord hit;
                    bool      hitSomething = Intersect.Scene(_scene, ray, tMin, tMax, out hit);
                    if (hitSomething)
                    {
                        Gizmos.DrawLine(ray.origin, hit.point);
                        Ray3f subRay;
                        if (!Trace.Scatter(ray, hit, ref rng, _fibs, out subRay))
                        {
                            break;
                        }
                        ray = subRay;
                    }
                    else
                    {
                        Gizmos.DrawRay(ray.origin, math.normalize(ray.direction));
                        break;
                    }
                }
            }
        }