Ejemplo n.º 1
0
        /// <summary>
        /// Calculates all coordinate offsets for
        /// both the X and Y blur pass in a single
        /// pass and returns an object that allows
        /// each pass to be handed to the render cycle
        /// individually.
        /// </summary>
        /// <param name="textureWidth">The width of the texture to be blurred</param>
        /// <param name="textureHeight">The height of the texture to be blurred</param>
        /// <param name="radius">The blur radius</param>
        /// <returns></returns>
        private LinearOffsets ComputeOffsets(float textureWidth, float textureHeight, int radius)
        {
            var linearOffsets = new LinearOffsets()
            {
                X = new Vector2[radius * 2 + 1],
                Y = new Vector2[radius * 2 + 1]
            };

            var   index   = 0;
            float xOffset = 1.0f / textureWidth;
            float yOffset = 1.0f / textureHeight;

            for (int i = -radius; i <= radius; i++)
            {
                index = i + radius;
                linearOffsets.X[index] = new Vector2(i * xOffset, 0f);
                linearOffsets.Y[index] = new Vector2(0f, i * yOffset);
            }

            return(linearOffsets);
        }
Ejemplo n.º 2
0
        public void Initialize(GraphicsDevice device, Camera camera)
        {
            Width       = device.PresentationParameters.BackBufferWidth;
            Height      = device.PresentationParameters.BackBufferHeight;
            this.device = device;
            spriteBatch = new SpriteBatch(this.device);

            // load our fx
            blur = FlatRedBallServices.Load <Effect>(@"Content\Shaders\Blur");

            // calculate the effect blur radius using the array length
            var radius = (blur.Parameters["Weights"].Elements.Count - 1) / 2;

            // calculate kernal and offsets
            kernal  = ComputeKernal(radius, BlurAmount);
            offsets = ComputeOffsets(Width, Height, radius);

            blur.Parameters["Weights"].SetValue(kernal);

            target2 = new RenderTarget2D(device, Width, Height);

            initialized = true;
        }