Ejemplo n.º 1
0
        public bool Raycast(Ray ray, out Surfel surfel)
        {
            Surfel closestSurfel = null;

            foreach (var renderableObject in objects)
            {
                if (renderableObject.mesh.Intersect(ray, out var hitSurfel))
                {
                    if (hitSurfel.t > closestSurfel?.t)
                    {
                        continue;
                    }
                    hitSurfel.material = renderableObject.material;
                    hitSurfel.ray      = ray.direction;
                    closestSurfel      = hitSurfel;
                }
            }

            if (closestSurfel == null)
            {
                surfel = new Surfel()
                {
                    ray = ray.direction
                };
                return(false);
            }
            surfel = closestSurfel;
            return(true);
        }
Ejemplo n.º 2
0
        private float TraceShadowRay(Surfel surfel, Vector3 direction, float distance)
        {
            var position = surfel.point + surfel.normal * RenderConfig.bias;
            var isHit    = Raycaster.Raycast(new Ray(position, direction), out var hit);

            return(isHit && hit.t < distance ? 0 : 1);
        }
Ejemplo n.º 3
0
 public Color GetColor(Scene scene, Surfel surfel)
 {
     if (surfel.material == null)
     {
         return(backgroundColor);
     }
     return(TraceRecursive(scene, surfel, 0));
 }
Ejemplo n.º 4
0
        public Color GetColor(Scene scene, Surfel surfel)
        {
            if (surfel.material == null)
            {
                return(backgroundColor);
            }

            return(directIntegrator.GetColor(scene, surfel) + indirectIntegrator.GetColor(scene, surfel));
        }
Ejemplo n.º 5
0
 public override ShadingInfo GetShadingInfo(Surfel surfel)
 {
     return(new ShadingInfo()
     {
         direction = transform.forward,
         distance = float.PositiveInfinity,
         color = color * intensity
     });
 }
Ejemplo n.º 6
0
        public Color GetColor(Scene scene, Surfel surfel)
        {
            if (surfel.material == null)
            {
                return(negativeColor);
            }

            return(ColorExtensions.LerpUnclamped(minColor, maxColor, (surfel.t - minDistance) / (maxDistance - minDistance)));
        }
Ejemplo n.º 7
0
 public override ShadingInfo GetShadingInfo(Surfel surfel)
 {
     return(new ShadingInfo()
     {
         direction = Mathf.CosineSampleHemisphere(surfel.normal, Mathf.CreateSample()),
         distance = float.PositiveInfinity,
         color = color * intensity
     });
 }
Ejemplo n.º 8
0
        private Color GetDirectLighting(Surfel surfel, Light light)
        {
            var shading = light.GetShadingInfo(surfel);
            var color   = surfel.material.GetColor(surfel.ray, shading.direction);
            var dot     = Vector3.Dot(surfel.normal, shading.direction);

            dot = Math.Max(dot, 0);
            var multiplier = TraceShadowRay(surfel, shading.direction, shading.distance);

            return(multiplier * color * shading.color * dot);
        }
Ejemplo n.º 9
0
 public bool Raycast(Ray ray, out Surfel surfel)
 {
     if (tree.Intersect(ray, out surfel))
     {
         return(true);
     }
     surfel = new Surfel()
     {
         ray = ray.direction
     };
     return(false);
 }
Ejemplo n.º 10
0
        public Color GetColor(Scene scene, Surfel surfel)
        {
            if (surfel.material == null)
            {
                return(negativeColor);
            }

            var normal = surfel.normal;

            normal = (normal + Vector3.One) * 0.5f;
            return(new Color(normal.X, normal.Y, normal.Z));
        }
Ejemplo n.º 11
0
        public override ShadingInfo GetShadingInfo(Surfel surfel)
        {
            var direction    = transform.position - surfel.point;
            var sqrtDistance = direction.LengthSquared();
            var distance     = (float)Math.Sqrt(sqrtDistance);

            return(new ShadingInfo
            {
                direction = direction / distance,
                distance = distance,
                color = color * (intensity / (4 * Math.PI * sqrtDistance))
            });
        }
Ejemplo n.º 12
0
        private Surfel[] Raycast(Camera camera, Vector2 point)
        {
            var numberOfRay = RenderConfig.numberOfRayPerPixel;
            var surfels     = new Surfel[numberOfRay];
            var rays        = camera.Sample(point, numberOfRay);

            for (var i = 0; i < numberOfRay; i++)
            {
                Raycaster.Raycast(rays[i], out var surfel);
                surfels[i] = surfel;
            }

            return(surfels);
        }
Ejemplo n.º 13
0
        private Color GetIndirectLighting(Surfel surfel, Light light, int depth)
        {
            var result = Color.Black;
            var n      = RenderConfig.numberOfRayPerLight;

            for (var i = 0; i < n; i++)
            {
                var f = surfel.material.Sample(surfel, out var wi, out var pdf);
                if (pdf > 0 && f > 0)
                {
                    result += f * pdf * Trace(surfel, light, wi, depth);
                }
            }
            return(result / RenderConfig.numberOfRayPerLight);
        }
Ejemplo n.º 14
0
        public Color GetColor(Scene scene, Surfel surfel)
        {
            if (surfel.material == null)
            {
                var environment = scene.lights.FirstOrDefault(l => l is EnvironmentLight);
                return(environment?.Sample(surfel.ray) ?? Color.Black);
            }

            var result = Color.Black;

            foreach (var light in scene.lights)
            {
                result += GetLighting(surfel, light, 0);
            }
            return(result);
        }
Ejemplo n.º 15
0
        protected Surfel[,] GetPixelsRaycastSurfels(Camera camera)
        {
            var w       = (int)(to.X - from.X);
            var h       = (int)(to.Y - from.Y);
            var surfels = new Surfel[h, w];

            for (var y = 0; y < h; y++)
            {
                for (var x = 0; x < w; x++)
                {
                    var cameraRay = camera.ScreenPointToRay(new Vector2(x + from.X + 0.5f, y + from.Y + 0.5f));
                    Raycaster.Raycast(cameraRay, out var surfel);
                    surfels[y, x] = surfel;
                }
            }
            return(surfels);
        }
Ejemplo n.º 16
0
        private Color TraceRecursive(Scene scene, Surfel surfel, int depth)
        {
            if (depth >= RenderConfig.rayDepth)
            {
                return(new Color(0f));
            }
            var dir = surfel.ray.Reflect(surfel.normal);
            var p   = surfel.point + surfel.normal * RenderConfig.bias;
            var ray = new Ray(p, dir);

            if (Raycaster.Raycast(ray, out surfel))
            {
                var dot = Vector3.Dot(surfel.normal, dir);
                return(dot * (directIntegrator.GetColor(scene, surfel) + TraceRecursive(scene, surfel, depth + 1)));
            }
            return(new Color(0f));
        }
Ejemplo n.º 17
0
        public Color GetColor(Scene scene, Surfel surfel)
        {
            if (surfel.material == null)
            {
                return(backgroundColor);
            }

            var baseColor = surfel.material.color;
            var result    = new Color(0, 0, 0);

            foreach (var light in scene.lights)
            {
                var shading = light.GetShadingInfo(surfel);
                var dot     = Vector3.Dot(shading.direction, surfel.normal);
                dot     = Math.Max(dot, 0);
                result += baseColor * shading.color * dot;
            }
            return(result);
        }
Ejemplo n.º 18
0
        private Color Trace(Surfel surfel, Light light, Vector3 direction, int depth)
        {
            if (depth >= RenderConfig.rayDepth)
            {
                return(Color.Black);
            }
            var sign     = Math.Sign(Vector3.Dot(surfel.normal, direction));
            var position = surfel.point + sign * surfel.normal * RenderConfig.bias;

            if (Raycaster.Raycast(new Ray(position, direction), out var hit))
            {
                return(GetLighting(hit, light, depth + 1));
            }
            var lightning = light.Sample(direction);
            var dot       = Vector3.Dot(surfel.normal, direction);

            dot = Math.Max(dot, 0);
            return(surfel.material.color * lightning * dot);
        }
Ejemplo n.º 19
0
        private Color TraceRecursive(Scene scene, Surfel surfel, int depth)
        {
            if (depth >= RenderConfig.rayDepth)
            {
                return(new Color(0f));
            }
            var p     = surfel.point + surfel.normal * RenderConfig.bias;
            var color = new Color(0f);

            for (var i = 0; i < RenderConfig.numberOfRayPerLight; i++)
            {
                var dir = Mathf.CosineSampleHemisphere(surfel.normal, Mathf.CreateSample());
                var ray = new Ray(p, dir);
                if (Raycaster.Raycast(ray, out var hit))
                {
                    var dot = Vector3.Dot(surfel.normal, dir);
                    color += dot * (directIntegrator.GetColor(scene, hit) + TraceRecursive(scene, hit, depth + 1));
                }
            }
            return(color / RenderConfig.numberOfRayPerLight);
        }
Ejemplo n.º 20
0
 public abstract ShadingInfo GetShadingInfo(Surfel surfel);
Ejemplo n.º 21
0
 public Color GetColor(Scene scene, Surfel surfel)
 {
     return(surfel.material == null ? bColor : wColor);
 }
Ejemplo n.º 22
0
 private Color GetLighting(Surfel surfel, Light light, int depth)
 {
     return(GetDirectLighting(surfel, light) + GetIndirectLighting(surfel, light, depth));
 }