Пример #1
0
        public void Scale(RenderTarget2D source, RenderTarget2D destination)
        {
            effect.CurrentTechnique = source.Format.IsFloatingPoint() ? effect.Techniques["Software"] : effect.Techniques["Hardware"];

            Vector2 resolution  = new Vector2(source.Width, source.Height);
            float   ScaleFactor = (destination.Width > source.Width) ? 2 : 0.5f;

            RenderTarget2D input  = source;
            RenderTarget2D output = null;

            while (IntermediateNeeded(resolution, destination, ScaleFactor))
            {
                resolution *= ScaleFactor;

                output = RenderTargetManager.GetTarget(device, (int)resolution.X, (int)resolution.Y, source.Format, name: "scaled");
                Draw(input, output);

                if (input != source)
                {
                    RenderTargetManager.RecycleTarget(input);
                }
                input = output;
            }

            Draw(input, destination);

            if (input != source)
            {
                RenderTargetManager.RecycleTarget(input);
            }
        }
Пример #2
0
        public void Blur(RenderTarget2D source, RenderTarget2D destination, float sigma)
        {
            if (width != source.Width || height != source.Height || this.sigma != sigma)
            {
                CalculateWeights(source.Width, source.Height, sigma);
            }

            effect.Parameters["Resolution"].SetValue(new Vector2(width, height));

            var intermediate = RenderTargetManager.GetTarget(device, width, height, destination.Format, name: "gaussian intermediate");

            device.SetRenderTarget(intermediate);

            effect.Parameters["Texture"].SetValue(source);
            effect.CurrentTechnique = effect.Techniques["BlurHorizontal"];
            quad.Draw(effect);

            device.SetRenderTarget(destination);

            effect.Parameters["Texture"].SetValue(intermediate);
            effect.CurrentTechnique = effect.Techniques["BlurVertical"];
            quad.Draw(effect);

            RenderTargetManager.RecycleTarget(intermediate);
        }
Пример #3
0
        public override void Draw(Renderer renderer)
        {
            var resolution = renderer.Data.Get <Vector2>("resolution").Value;

            //if (renderer.Data.Get<float>("ssao_radiosityintensity").Value > 0)
            //    ssaoMaterial.CurrentTechnique = ssaoMaterial.Techniques["SSGI"];
            //else
            //{
            if (renderer.Data.Get <bool>("ssao_highquality").Value)
            {
                ssaoMaterial.CurrentTechnique = ssaoMaterial.Techniques["HQ_SSAO"];
            }
            else
            {
                ssaoMaterial.CurrentTechnique = ssaoMaterial.Techniques["LQ_SSAO"];
            }
            //}

            var unblured = RenderTargetManager.GetTarget(renderer.Device, (int)resolution.X, (int)resolution.Y, surfaceFormat: SurfaceFormat.HalfVector4, name: "ssao unblurred");//, SurfaceFormat.HalfVector4);

            renderer.Device.SetRenderTarget(unblured);
            renderer.Device.Clear(Color.Transparent);
            renderer.Device.BlendState = BlendState.Opaque;
            quad.Draw(ssaoMaterial, renderer.Data);

            ssao = RenderTargetManager.GetTarget(renderer.Device, (int)resolution.X, (int)resolution.Y, SurfaceFormat.HalfVector4, name: "ssao");
            renderer.Device.SetRenderTarget(ssao);
            renderer.Device.Clear(Color.Transparent);
            ssaoBlurMaterial.Parameters["SSAO"].SetValue(unblured);
            quad.Draw(ssaoBlurMaterial, renderer.Data);
            RenderTargetManager.RecycleTarget(unblured);

            Output("ssao", ssao);
        }
Пример #4
0
        private void Bloom(Renderer renderer, Box <Vector2> resolution, GraphicsDevice device, Texture2D lightBuffer)
        {
            var screenResolution  = resolution.Value;
            var halfResolution    = screenResolution / 2;
            var quarterResolution = halfResolution / 2;

            // downsample the light buffer to half resolution, and threshold at the same time
            var thresholded = RenderTargetManager.GetTarget(device, (int)halfResolution.X, (int)halfResolution.Y, SurfaceFormat.Rgba64, name: "bloom thresholded");

            device.SetRenderTarget(thresholded);
            bloom.Parameters["Resolution"].SetValue(halfResolution);
            bloom.Parameters["Threshold"].SetValue(renderer.Data.Get <float>("hdr_bloomthreshold").Value);
            bloom.Parameters["MinExposure"].SetValue(renderer.Data.Get <float>("hdr_minexposure").Value);
            bloom.Parameters["MaxExposure"].SetValue(renderer.Data.Get <float>("hdr_maxexposure").Value);
            bloom.Parameters["Texture"].SetValue(lightBuffer);
            bloom.Parameters["Luminance"].SetValue(adaptedLuminance[current]);
            bloom.CurrentTechnique = bloom.Techniques["ThresholdDownsample2X"];
            quad.Draw(bloom);

            // downsample again to quarter resolution
            var downsample = RenderTargetManager.GetTarget(device, (int)quarterResolution.X, (int)quarterResolution.Y, SurfaceFormat.Rgba64, name: "bloom downsampled");

            device.SetRenderTarget(downsample);
            bloom.Parameters["Resolution"].SetValue(quarterResolution);
            bloom.Parameters["Texture"].SetValue(thresholded);
            bloom.CurrentTechnique = bloom.Techniques["Scale"];
            quad.Draw(bloom);

            // blur the target
            var blurred = RenderTargetManager.GetTarget(device, (int)quarterResolution.X, (int)quarterResolution.Y, SurfaceFormat.Rgba64, name: "bloom blurred");

            gaussian.Blur(downsample, blurred, renderer.Data.Get <float>("hdr_bloomblurammount").Value);

            // upscale back to half resolution
            device.SetRenderTarget(thresholded);
            bloom.Parameters["Resolution"].SetValue(halfResolution);
            bloom.Parameters["Texture"].SetValue(blurred);
            quad.Draw(bloom);

            // output result
            Output("bloom", thresholded);

            // cleanup temp render targets
            RenderTargetManager.RecycleTarget(downsample);
            RenderTargetManager.RecycleTarget(blurred);
        }
Пример #5
0
        public void Prepare(Renderer renderer)
        {
            renderer.Data.Get <BoundingFrustum>("viewfrustum").Value.GetCorners(frustumCornersWS);

            for (int i = 0; i < lights.Count; i++)
            {
                var data  = lights[i];
                var light = data.Light;

                light.Direction = Vector3.Normalize(light.Direction);

                if (data.ShadowMap != null)
                {
                    RenderTargetManager.RecycleTarget(data.ShadowMap);
                    data.ShadowMap = null;
                }

                if (light.ShadowResolution != 0)
                {
                    CalculateShadowMatrices(renderer, data);
                    DrawShadowMap(renderer, data);
                }
            }
        }
Пример #6
0
        public void Prepare(Renderer renderer)
        {
            touchesNearPlane.Clear();
            touchesFarPlane.Clear();
            touchesBothPlanes.Clear();
            touchesNeitherPlane.Clear();

            var frustum = renderer.Data.Get <BoundingFrustum>("viewfrustum").Value;

            float falloffFactor = renderer.Data.Get("lighting_attenuationscale", 100).Value;

            geometryLightingMaterial.Parameters["LightFalloffFactor"].SetValue(falloffFactor);
            quadLightingMaterial.Parameters["LightFalloffFactor"].SetValue(falloffFactor);

            //float threshold = renderer.Data.Get("lighting_threshold", 1f / 100f).Value;
            //float adaptedLuminance = renderer.Data.Get<float>("adaptedluminance", 1).Value;

            //threshold = adaptedLuminance * threshold;

            foreach (var light in lights)
            {
                light.Light.Direction = Vector3.Normalize(light.Light.Direction);

                //var luminance = Math.Max(light.Colour.X, Math.Max(light.Colour.Y, light.Colour.Z));
                //light.range = (float)Math.Sqrt(luminance * falloffFactor / threshold);

                var bounds = new BoundingSphere(light.Light.Position, light.Light.Range);
                if (!bounds.Intersects(frustum))
                {
                    continue;
                }

                var near = bounds.Intersects(frustum.Near) == PlaneIntersectionType.Intersecting;
                var far  = bounds.Intersects(frustum.Far) == PlaneIntersectionType.Intersecting;

                if (near && far)
                {
                    touchesBothPlanes.Add(light);
                }
                else if (near)
                {
                    touchesNearPlane.Add(light);
                }
                else if (far)
                {
                    touchesFarPlane.Add(light);
                }
                else
                {
                    touchesNeitherPlane.Add(light);
                }

                if (light.ShadowMap != null)
                {
                    RenderTargetManager.RecycleTarget(light.ShadowMap);
                    light.ShadowMap = null;
                }

                if (light.Light.ShadowResolution > 0)
                {
                    DrawShadowMap(renderer, light);
                }
            }
        }