public void Draw() { float dot = Vector3.Dot(SkyDome.LightDirection, Camera.Forward); if (dot < 0.5f) { foreach (Flare flare in flares) { SpriteDrawer.SetVisible(flare.id, false); } return; } Matrix infiniteView = Camera.ViewMatrix; infiniteView[3, 0] = infiniteView[3, 1] = infiniteView[3, 2] = 0; // Project the light position into 2D screen space. Viewport vp = Game.Device.Rasterizer.GetViewports()[0]; Matrix proj = Matrix.PerspectiveFovLH(Camera.Fovx, Camera.AspectRatio, 1.0f, 1000000.0f); Vector3 projectedPosition = Vector3.Project(-SkyDome.LightDirection, vp.X, vp.Y, vp.Width, vp.Height, vp.MinZ, vp.MaxZ, Matrix.Identity * infiniteView * proj); projectedPosition.X /= WeatherGame.Framework.Rendering.DeferredRenderer.RenderWidth; projectedPosition.Y /= WeatherGame.Framework.Rendering.DeferredRenderer.RenderHeight; Vector2 lightPosition = new Vector2(projectedPosition.X * 2.0f - 1.0f, (projectedPosition.Y * 2.0f - 1.0f) * -1); UpdateOcclusion(); DrawFlares(lightPosition); }
void DrawFlares(Vector2 lightPosition) { // Lensflare sprites are positioned at intervals along a line that // runs from the 2D light position toward the center of the screen. Vector2 screenCenter = Vector2.Zero; Vector2 flareVector = screenCenter - lightPosition; foreach (Flare flare in flares) { SpriteDrawer.SetVisible(flare.id, true); // Compute the position of this flare sprite. Vector2 flarePosition = lightPosition + flareVector * flare.Position; Color4 c = new SlimDX.Color4(flare.Color); c *= new Color4(SkyDome.SunColor); c *= occlusionAlpha; SpriteDrawer.AddInstance(flare.id, flare.Texture, flarePosition, c, flare.Scale / 2, SpriteBlendMode.Additive); } }