Exemplo n.º 1
0
        public void ComputeLightMap(SpriteBatch batch, Map map, string Path)
        {
            Camera cam = new Camera(Vector2.Zero, map.MapBoundaries);
            cam.Zoom(-1);
            Vector2 viewFieldDiagonal = new Vector2(cam.ViewSpace.Width *0.5f, cam.ViewSpace.Height *0.5f);
            cam.SetPos(viewFieldDiagonal);
            lightMapTarget = new RenderTarget2D(device, map.MapBoundaries.Width, map.MapBoundaries.Height,false, SurfaceFormat.Color, DepthFormat.Depth24Stencil8);
            Matrix baseProjection = cam.ProjectionMatrix;
            map.SetUpLightMapPrecomputeLights();
            device.SetRenderTarget(lightMapTarget);

            //Clear Target first

            device.DepthStencilState = clearStencil;
            device.RasterizerState = RasterizerState.CullNone;
            device.BlendState = BlendState.Opaque;
            shadowEffect.Parameters["View"].SetValue(cam.ViewMatrix);
            shadowEffect.Parameters["Projection"].SetValue(Matrix.Identity);
            shadowEffect.Parameters["shadowDarkness"].SetValue(0.9f - 1);
            shadowEffect.Parameters["viewDistance"].SetValue((float)(1 / (0.001f + 0.999 )));
            shadowEffect.CurrentTechnique = shadowEffect.Techniques["Clear"];
            device.SetVertexBuffer(clearVBuffer);
            device.Indices = clearIBuffer;
            shadowEffect.CurrentTechnique.Passes[0].Apply();
            device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, 4, 0, 2);

            //Matrix UpperLeftShift = Matrix.CreateTranslation(-1+(float)cam.ViewSpace.Width / (float)map.MapBoundaries.Width, 1-(float)cam.ViewSpace.Height / (float)map.MapBoundaries.Height, 0);
            //cam.ProjectionMatrix = baseProjection * Matrix.CreateScale((float)cam.ViewSpace.Width / map.MapBoundaries.Width, (float)cam.ViewSpace.Height / map.MapBoundaries.Height, 1) * UpperLeftShift * Matrix.CreateTranslation(cam.ViewSpace.Left / map.MapBoundaries.Width, cam.ViewSpace.Top / map.MapBoundaries.Height, 0);
            DrawLights(1, cam, batch, lightMapTarget);
            //cam.Move(new Vector2(0, viewFieldDiagonal.Y * 2));

            RenderTarget2D blurTarget = new RenderTarget2D(device,lightMapTarget.Width,lightMapTarget.Height);

            shadowEffect.Parameters["xTexelDist"].SetValue(1.0f / (blurTarget.Width / lightMapDownSample));
            shadowEffect.Parameters["yTexelDist"].SetValue(1.0f / (blurTarget.Height / lightMapDownSample));
            BlurShadowTarget(batch, lightMapTarget, blurTarget);
            BlurShadowTarget(batch, lightMapTarget, blurTarget);
            shadowEffect.Parameters["xTexelDist"].SetValue(1.0f / (device.PresentationParameters.BackBufferWidth / lightMapDownSample));
            shadowEffect.Parameters["yTexelDist"].SetValue(1.0f / (device.PresentationParameters.BackBufferHeight / lightMapDownSample));

            device.SetRenderTarget(null);
            Clear();

            System.IO.FileStream fs = System.IO.File.Open(Path, System.IO.FileMode.Create);
            lightMapTarget.SaveAsPng(fs, lightMapTarget.Width, lightMapTarget.Height);
            fs.Close();

            lightMap = lightMapTarget;
            _hasLightmap = true;
        }