Пример #1
0
        public override bool Scatter(Scene scene, Ray ray, HitRecord rec, out Attenuation attenuation, out Ray scattered)
        {
            Vector3 outward_normal;
            float   ni_over_nt;

            attenuation = Attenuation.Multiply(Vector3.One);
            float reflect_prob;
            float cosine;

            if (Vector3.Dot(ray.direction, rec.normal) > 0)//内部射出
            {
                outward_normal = -rec.normal;
                ni_over_nt     = refIndex;
                cosine         = refIndex * Vector3.Dot(ray.direction, rec.normal) / ray.direction.Length();
            }
            else //外部射入
            {
                outward_normal = rec.normal;
                ni_over_nt     = 1.0f / refIndex;
                //ni_over_nt = this.ri_out / this.ri_in;
                cosine = -Vector3.Dot(ray.direction, rec.normal) / ray.direction.Length();
            }

            if (Refract(ref ray.direction, ref outward_normal, ni_over_nt, out Vector3 refracted))
            {
                reflect_prob = Schlick(cosine, refIndex);
            }
Пример #2
0
        public static Attenuation None(Vector3 color)
        {
            var attenuation = new Attenuation();

            attenuation.attenuationType = AttenuationType.None;
            attenuation.color           = color;
            return(attenuation);
        }
Пример #3
0
        public static Attenuation Multiply(Vector3 color)
        {
            var attenuation = new Attenuation();

            attenuation.attenuationType = AttenuationType.Multiply;
            attenuation.color           = color;
            return(attenuation);
        }
Пример #4
0
        public override bool Scatter(Scene scene, Ray ray, HitRecord hit, out Attenuation attenuation, out Ray scattered)
        {
            Vector3 dir = Vector3.Normalize(ray.direction);

            attenuation = Attenuation.None(GetTextureColor(GetUV(dir)));
            scattered   = null;
            return(true);
        }
Пример #5
0
        public static Attenuation Add(Vector3 color, Vector3 attenuationColor)
        {
            var attenuation = new Attenuation();

            attenuation.attenuationType  = AttenuationType.Add;
            attenuation.attenuationColor = attenuationColor;
            attenuation.color            = color;
            return(attenuation);
        }
Пример #6
0
        public override bool Scatter(Scene scene, Ray ray, HitRecord hit, out Attenuation attenuation, out Ray scattered)
        {
            Vector3 target = hit.position + hit.normal + Sphere.RandomInUnitSphere();

            scattered   = new Ray(hit.position, target - hit.position);
            attenuation = Attenuation.Multiply(albedo);

            return(true);
        }
Пример #7
0
        public static Attenuation Blend(Vector3 color, float percent, Vector3 attenuationColor)
        {
            var attenuation = new Attenuation();

            attenuation.attenuationType  = AttenuationType.Blend;
            attenuation.color            = color;
            attenuation.blendPercent     = percent;
            attenuation.attenuationColor = attenuationColor;
            return(attenuation);
        }
Пример #8
0
        public override bool Scatter(Scene scene, Ray ray, HitRecord hit, out Attenuation attenuation, out Ray scattered)
        {
            scattered = new Ray(hit.position, ray.direction);

            var textureColor = GetTextureColor(hit.uv);

            attenuation = Attenuation.Blend(albedo * textureColor, 1 - transparency, albedo * textureColor);
            attenuation.forceToDoAnotherPass = true;
            return(true);
        }
Пример #9
0
        public override bool Scatter(Scene scene, Ray ray, HitRecord hit, out Attenuation attenuation, out Ray scattered)
        {
            Vector3 unitDirection = Vector3.Normalize(ray.direction);
            float   t             = (0.5f * unitDirection.Y) + 1.0f;
            Vector3 colorValue    = ((1.0f - t) * new Vector3(1f, 1f, 1f)) + (t * skyColor);

            attenuation = Attenuation.None(colorValue);
            scattered   = null;
            return(true);
        }
Пример #10
0
        public override bool Scatter(Scene scene, Ray ray, HitRecord hit, out Attenuation attenuation, out Ray scattered)
        {
            scattered = null;

            var textureColor = GetTextureColor(hit.uv);

            attenuation = Attenuation.None(color * textureColor * intensity);

            return(true);
        }
Пример #11
0
        public override bool Scatter(Scene scene, Ray ray, HitRecord hit, out Attenuation attenuation, out Ray scattered)
        {
            var     direction = Vector3.Normalize(ray.direction);
            Vector3 reflected = Vector3.Reflect(direction, hit.normal);

            scattered   = new Ray(hit.position, reflected + fuzz * Sphere.RandomInUnitSphere());
            attenuation = Attenuation.Multiply(albedo);

            //return true;
            return(Vector3.Dot(scattered.direction, hit.normal) > 0);
        }
Пример #12
0
        public override bool Scatter(Scene scene, Ray ray,HitRecord hit, out Attenuation attenuation, out Ray scattered)
        {
            //skyMat
            if(hit==null)
            {
                attenuation = Attenuation.None(albedo);
                scattered = null;
                return true;
            }

            scattered = new Ray(hit.position, ray.direction);
            var textureColor = GetTextureColor(hit.uv);
            //attenuation = Attenuation.None(new Vector3(hit.uv.X, hit.uv.Y,0));
            attenuation = Attenuation.None(albedo * textureColor);

            return true;
        }
Пример #13
0
        public override bool Scatter(Scene scene, Ray ray, HitRecord hit, out Attenuation attenuation, out Ray scattered)
        {
            var     direction = Vector3.Normalize(ray.direction);
            Vector3 reflected = Vector3.Reflect(direction, hit.normal);

            scattered = new Ray(hit.position, reflected + fuzz * Sphere.RandomInUnitSphere());

            Vector3 diffuseColor  = Vector3.Zero;
            Vector3 SpecularColor = Vector3.Zero;

            foreach (var light in scene.lights)
            {
                if (CastShadow(scene, light, hit))
                {
                    continue;
                }

                Vector3 lightDir   = -Vector3.Normalize(hit.position - light.position);
                Vector3 reflectDir = Vector3.Reflect(-lightDir, hit.normal);

                diffuseColor  += light.color * light.insensity * Vector3.Dot(hit.normal, lightDir);
                SpecularColor += light.color * light.insensity * specularIndex *
                                 MathF.Pow(Math.Clamp(Vector3.Dot(reflectDir, -direction), 0, 1), glossIndex);
            }

            //环境光 + 光源
            var textureColor = GetTextureColor(hit.uv);
            var allColor     = (scene.ambientColor + diffuseColor) * albedo * textureColor + SpecularColor;

            attenuation = Attenuation.Blend(allColor, blendPercent, albedo * textureColor);
            attenuation.attenuationType = attenuationType;

            //兼容easyPipeline
            if (fuzz == 0 && specularIndex == 1)
            {
                attenuation.forceToDoAnotherPass = true;
            }

            //attenuation = Attenuation.None(new Vector3(hit.uv.X, hit.uv.Y,0));

            return(true);//(Vector3.Dot(scattered.direction, hit.normal) > 0);
        }
Пример #14
0
 public abstract bool Scatter(Scene scene, Ray ray, HitRecord hit, out Attenuation attenuation, out Ray scattered);