Ejemplo n.º 1
0
		public override Color Calculate(Vector point, Vector normal)
		{
            // Use dotproduct for diffuse lighting. Add point functionality as this now is a directional light.
            // Ambient light
            var ambient = ColorAmbient.ToVector() * Strength;

            // Diffuse light
            var lightDir = Direction;
            lightDir.Normalize();
            normal.Normalize();
            var dfDot = lightDir.Dot(normal);
            MathHelper.Saturate(ref dfDot);
            var diffuse = ColorDiffuse.ToVector() * dfDot * Strength;

            // Specular highlight
            var Reflection = 2 * dfDot * normal - lightDir;
            Reflection.Normalize();
            var view = Camera.Position - point;
            view.Normalize();
            var spDot = Reflection.Dot(view);
            MathHelper.Saturate(ref spDot);
            var specular = ColorSpecular.ToVector() * spDot * Strength;

            // Compute self shadowing
            var shadow = 4.0f * lightDir.Dot(normal);
            MathHelper.Saturate(ref shadow);


            // Final result
            var colorVector = ambient + shadow * (diffuse + specular);
            //var colorVector = ambient + diffuse;

			return colorVector.ToColorWithClamp();
		}
Ejemplo n.º 2
0
        public override Color Calculate(Viewport viewport, Vector point, Vector normal)
        {
            var actualAmbient = Ambient;
            var actualDiffuse = Diffuse;
            var actualSpecular = Specular;

            // Use dotproduct for diffuse lighting. Add point functionality as this now is a directional light.
            // Ambient light
            var ambient = actualAmbient  * Strength;

            // Diffuse light
            var lightDir = Position-point;
            lightDir.Normalize();
            normal.Normalize();
            var dfDot = lightDir.Dot(normal);
            dfDot = MathHelper.Saturate(dfDot);
            var diffuse = actualDiffuse * dfDot * Strength;

            // Specular highlight
            var reflection = 2f * dfDot * normal - lightDir;
            reflection.Normalize();
            var view = viewport.View.Position - point;
            view.Normalize();
            var spDot = reflection.Dot(view);
            spDot = MathHelper.Saturate(spDot);
            var specular = actualSpecular * spDot * Strength;

            // Compute self shadowing
            var shadow = 4.0f * lightDir.Dot(normal);
            shadow = MathHelper.Saturate(shadow);

            // Compute range for the light
            var attenuation = ((lightDir / Range).Dot(lightDir / Range));
            attenuation = MathHelper.Saturate(attenuation);
            attenuation = 1f - attenuation;

            // Final result
            var colorVector = ambient + shadow * (diffuse + specular) * attenuation;
            //var colorVector = ambient + diffuse;

            return colorVector;
        }