/// <summary> /// Search which is the nearby light for the current object. /// </summary> /// <returns>The nearby light.</returns> private LightProperties SearchForNearbyLight() { LightProperties nearbyLight = null; float maxIntensity = 0; Vector3 objectPosition = this.Matrices.World.Translation; if (this.renderManager != null) { foreach (var light in this.renderManager.Lights) { if (light.IsLightEnabled && light.Intensity > 0) { float intensity = 0; if (light is DirectionalLightProperties) { intensity = light.Intensity; } else { Vector3 lightPosition = Vector3.Zero; float lightRange = 0; if (light is PointLightProperties) { PointLightProperties pointLight = light as PointLightProperties; lightPosition = pointLight.Position; lightRange = pointLight.LightRange; } else if (light is SpotLightProperties) { SpotLightProperties spotLight = light as SpotLightProperties; lightPosition = spotLight.Position; lightRange = spotLight.LightRange; } float distance = (lightPosition - objectPosition).Length(); if (distance < lightRange) { intensity = light.Intensity * (1 - (distance / lightRange)); } } intensity *= light.Color.Luminance; if (intensity > maxIntensity) { maxIntensity = intensity; nearbyLight = light; } } } } return(nearbyLight); }
/// <summary> /// Sets the parameters. /// </summary> /// <param name="cached">if set to <c>true</c> [cached].</param> public override void SetParameters(bool cached) { base.SetParameters(cached); if (!cached) { Camera camera = this.renderManager.CurrentDrawingCamera; this.shaderParameters.CameraPosition = camera.Position; this.shaderParameters.ReferenceAlpha = this.ReferenceAlpha; this.shaderParameters.DiffuseColor = this.diffuseColor; this.shaderParameters.Alpha = this.Alpha; this.shaderParameters.AmbientColor = this.ambientColor; this.shaderParameters.SpecularPower = this.SpecularPower; this.shaderParameters.SpecularIntensity = this.SpecularIntensity; this.shaderParameters.TextureOffset = this.TexcoordOffset; if (this.LightingEnabled) { if (this.nearbyLight != null) { // Common parameters this.Light.Color = this.nearbyLight.Color.ToVector3(); this.Light.Intensity = this.nearbyLight.Intensity; if (this.nearbyLight is DirectionalLightProperties) { DirectionalLightProperties directional = this.nearbyLight as DirectionalLightProperties; this.Light.Direction = directional.Direction; } else if (this.nearbyLight is PointLightProperties) { PointLightProperties point = this.nearbyLight as PointLightProperties; this.Light.Position = point.Position; this.Light.LightRange = point.LightRange; } else { SpotLightProperties spot = this.nearbyLight as SpotLightProperties; this.Light.Direction = spot.Direction; this.Light.LightRange = spot.LightRange; this.Light.Position = spot.Position; this.Light.ConeAngle = (float)Math.Cos(spot.LightConeAngle / 2); } this.shaderParameters.Light = this.Light; } } this.Parameters = this.shaderParameters; if (this.diffuse != null) { this.graphicsDevice.SetTexture(this.diffuse, 0); } if (this.ambient != null) { this.graphicsDevice.SetTexture(this.ambient, 1); } } }