Beispiel #1
0
        /// <summary>
        /// Function to initialize the effects and the effect compositor.
        /// </summary>
        private static void InitializeEffects()
        {
            // The blur effect.
            _blurEffect = new Gorgon2DGaussBlurEffect(_renderer)
            {
                PreserveAlpha = true
            };
            _blurEffect.Precache();

            // The grayscale effect.
            _grayScaleEffect = new Gorgon2DGrayScaleEffect(_renderer);

            // The posterize effect.
            _posterizeEffect = new Gorgon2DPosterizedEffect(_renderer)
            {
                Bits = 10
            };

            // The 1 bit effect.
            _1BitEffect = new Gorgon2D1BitEffect(_renderer)
            {
                Threshold           = new GorgonRangeF(0.5f, 1.0f),
                ConvertAlphaChannel = false
            };

            // The burn effect.
            _burnEffect = new Gorgon2DBurnDodgeEffect(_renderer)
            {
                UseDodge = false
            };

            // The dodge effect.
            _dodgeEffect = new Gorgon2DBurnDodgeEffect(_renderer)
            {
                UseDodge = true
            };

            // The invert effect.
            _invertEffect = new Gorgon2DInvertEffect(_renderer);

            // The sharpen effect.
            _sharpenEffect = new Gorgon2DSharpenEmbossEffect(_renderer)
            {
                UseEmbossing = false,
                Amount       = 1.0f
            };
            // The emboss effect.
            _embossEffect = new Gorgon2DSharpenEmbossEffect(_renderer)
            {
                UseEmbossing = true,
                Amount       = 1.0f
            };

            // The sobel edge detection effect.
            _sobelEffect = new Gorgon2DSobelEdgeDetectEffect(_renderer)
            {
                LineThickness = 2.5f,
                EdgeThreshold = 0.80f
            };

            // An old film effect.
            _oldFilmEffect = new Gorgon2DOldFilmEffect(_renderer)
            {
                ScrollSpeed = 0.05f
            };
            // A HDR bloom effect.
            _bloomEffect = new Gorgon2DBloomEffect(_renderer)
            {
                Threshold      = 1.11f,
                BloomIntensity = 35.0f,
                BlurAmount     = 6.0f,
                ColorIntensity = 1.15f
            };
            // A chromatic aberration effect.
            _chromatic = new Gorgon2DChromaticAberrationEffect(_renderer)
            {
                Intensity = 0.25f
            };

            _compositor = new Gorgon2DCompositor(_renderer);
            // Set up each pass for the compositor.
            // As you can see, we're not strictly limited to using our 2D effect objects, we can define custom passes as well.
            // And, we can also define how existing effects are rendered for things like animation and such.
            _compositor.EffectPass("Chromatic Aberration", _chromatic)
            .EffectPass("1-Bit Color", _1BitEffect)
            .EffectPass("Blur", _blurEffect)
            .EffectPass("Grayscale", _grayScaleEffect)
            .EffectPass("Posterize", _posterizeEffect)
            .EffectPass("Burn", _burnEffect)
            .EffectPass("Dodge", _dodgeEffect)
            .EffectPass("Invert", _invertEffect)
            .EffectPass("Sharpen", _sharpenEffect)
            .EffectPass("Emboss", _embossEffect)
            .EffectPass("Bloom", _bloomEffect)
            .Pass(new Gorgon2DCompositionPass("Sobel Edge Detection", _sobelEffect)
            {
                BlendOverride = GorgonBlendState.Default,
                ClearColor    = GorgonColor.White
            })
            .RenderPass("Sobel Blend Pass",
                        (sobelTexture, pass, passCount, size) =>
            {
                // This is a custom pass that does nothing but rendering.  No effect is applied here, just straight rendering to
                // the currently active render target.
                var rectPosition = new DX.RectangleF(0, 0, size.Width, size.Height);
                var texCoords    = new DX.RectangleF(0, 0, 1, 1);
                _renderer.DrawFilledRectangle(rectPosition, GorgonColor.White, _images[_currentImage], texCoords);
                _renderer.DrawFilledRectangle(rectPosition, GorgonColor.White, sobelTexture, texCoords);
            })
            .Pass(new Gorgon2DCompositionPass("Olde Film", _oldFilmEffect)
            {
                BlendOverride = GorgonBlendState.Additive,
                RenderMethod  = (prevEffect, passIndex, passCount, size) =>
                {
                    // Here we can override the method used to render to the effect.
                    // In this case, we're animating our old film content to shake and darken at defined intervals.
                    // If we do not override this, the compositor would just blit the previous texture to the
                    // current render target.
                    var rectPosition  = new DX.RectangleF(0, 0, size.Width, size.Height);
                    var texCoords     = new DX.RectangleF(0, 0, 1, 1);
                    GorgonColor color = GorgonColor.White;

                    if ((GorgonTiming.SecondsSinceStart % 10) >= 4)
                    {
                        rectPosition.Inflate(GorgonRandom.RandomSingle(1, 5), GorgonRandom.RandomSingle(1, 5));
                        float value = GorgonRandom.RandomSingle(0.5f, 0.89f);
                        color       = new GorgonColor(value, value, value, 1.0f);
                    }

                    _renderer.DrawFilledRectangle(rectPosition, color, prevEffect, texCoords);
                }
            })
            .InitialClearColor(GorgonColor.White)
            .FinalClearColor(GorgonColor.White);

            _compositor.Passes["Chromatic Aberration"].Enabled = false;
            _compositor.Passes["Bloom"].Enabled       = false;
            _compositor.Passes["Posterize"].Enabled   = false;
            _compositor.Passes["Grayscale"].Enabled   = false;
            _compositor.Passes["1-Bit Color"].Enabled = false;
            _compositor.Passes["Emboss"].Enabled      = false;
            _compositor.Passes["Dodge"].Enabled       = false;
            _compositor.Passes["Olde Film"].Enabled   = false;
        }
Beispiel #2
0
        /// <summary>
        /// Function to release the shaders and the resources allocated to them.
        /// </summary>
        public void FreeEffects()
        {
            if (_posterizeEffect != null)
            {
                _posterizeEffect.Dispose();
                _posterizeEffect = null;
            }

            if (_grayScaleEffect != null)
            {
                _grayScaleEffect.Dispose();
                _grayScaleEffect = null;
            }

            if (_waveEffect != null)
            {
                _waveEffect.Dispose();
                _waveEffect = null;
            }

            if (_1BitEffect != null)
            {
                _1BitEffect.Dispose();
                _1BitEffect = null;
            }

            if (_sharpenEmbossEffect != null)
            {
                _sharpenEmbossEffect.Dispose();
                _sharpenEmbossEffect = null;
            }

            if (_invertEffect != null)
            {
                _invertEffect.Dispose();
                _invertEffect = null;
            }

            if (_gaussBlurEffect != null)
            {
                _gaussBlurEffect.Dispose();
                _gaussBlurEffect = null;
            }

            if (_sobelEdgeDetectEffect != null)
            {
                _sobelEdgeDetectEffect.Dispose();
                _sobelEdgeDetectEffect = null;
            }

            if (_burnDodgeEffect != null)
            {
                _burnDodgeEffect.Dispose();
                _burnDodgeEffect = null;
            }

            if (_displacementEffect == null)
            {
                return;
            }

            _displacementEffect.Dispose();
            _displacementEffect = null;
        }