예제 #1
0
        public override bool DistanceToRay(Ray ray, HitResult result)
        {
            var pos = Transform.Translation;

            float a = VMath.Dot(ray.vec, pos - ray.pos);
            float e = (pos - ray.pos).LengthSquare();

            float f = Range * Range - e + a * a;

            if (f < 0)
            {
                return(false);
            }

            float distance = a - (float)Math.Sqrt(f);

            if (result.distance > distance && distance > 0.05)
            {
                result.normal   = VMath.Normalize((ray.pos + ray.vec * distance) - pos);
                result.material = Material;
                result.distance = distance;
                return(true);
            }
            else
            {
                return(false);
            }
        }
예제 #2
0
        public Vector3 RandomCosineDirection()
        {
            double r1  = Rand.NextDouble();
            double r2  = Rand.NextDouble();
            float  z   = (float)Math.Sqrt(1 - r2);
            double phi = Math.PI * 2 * r1;
            float  x   = (float)(Math.Cos(phi) * Math.Sqrt(r2));
            float  y   = (float)(Math.Sin(phi) * Math.Sqrt(r2));

            return(VMath.Normalize(new Vector3(x, y, z)));
        }
예제 #3
0
        protected Vector3 CreateReflectedVector(Ray ray, Vector3 normal, Vector3 pos)
        {
            if (Rand.NextDouble() < Roughness)
            {
                var vec = new Vector3((float)Rand.NextDouble() * 0.5F - 0.25F, 3, (float)Rand.NextDouble() * 0.5F - 0.25F) - pos;
                if (VMath.Dot(vec, normal) > 0)
                {
                    return(VMath.Normalize(vec));
                }
            }

            return(Vector3.Interpolate(Reflect(ray.vec, normal), RandomCosineDirection() * Matrix3.LookAt(normal, RandomCosineDirection()), Roughness));
        }
예제 #4
0
        public bool IsIntersectWithRay(Ray ray)
        {
            var   target = Pos - ray.pos;
            float time   = VMath.Dot(VMath.Normalize(ray.vec), target);

            if (time <= 0)
            {
                return(false);
            }

            float distance = target.LengthSquare() - time * time;

            return(distance <= Range * Range);
        }
예제 #5
0
 public Vector3 Reflect(Vector3 vec, Vector3 nornal)
 {
     return(VMath.Normalize(vec - 2 * VMath.Dot(vec, nornal) * nornal));
 }