コード例 #1
0
ファイル: Program.cs プロジェクト: ishkang/Gorgon
        /// <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;
        }
コード例 #2
0
        /// <summary>
        /// Function to initialize the application.
        /// </summary>
        /// <returns>The main window for the application.</returns>
        private static FormMain Initialize()
        {
            GorgonExample.ResourceBaseDirectory = new DirectoryInfo(Settings.Default.ResourceLocation);
            FormMain window = GorgonExample.Initialize(new DX.Size2(Settings.Default.Resolution.Width, Settings.Default.Resolution.Height), "Effects");

            try
            {
                IReadOnlyList <IGorgonVideoAdapterInfo> videoDevices = GorgonGraphics.EnumerateAdapters(log: GorgonApplication.Log);

                if (videoDevices.Count == 0)
                {
                    throw new GorgonException(GorgonResult.CannotCreate,
                                              "Gorgon requires at least a Direct3D 11.4 capable video device.\nThere is no suitable device installed on the system.");
                }

                // Find the best video device.
                _graphics = new GorgonGraphics(videoDevices.OrderByDescending(item => item.FeatureSet).First());

                _screen = new GorgonSwapChain(_graphics,
                                              window,
                                              new GorgonSwapChainInfo("Gorgon2D Effects Example Swap Chain")
                {
                    Width  = Settings.Default.Resolution.Width,
                    Height = Settings.Default.Resolution.Height,
                    Format = BufferFormat.R8G8B8A8_UNorm
                });
                _screen.BeforeSwapChainResized += Screen_BeforeSwapChainResized;
                _screen.AfterSwapChainResized  += Screen_AfterSwapChainResized;
                // Tell the graphics API that we want to render to the "screen" swap chain.
                _graphics.SetRenderTarget(_screen.RenderTargetView);

                // Initialize the renderer so that we are able to draw stuff.
                _renderer = new Gorgon2D(_graphics);

                // Create the displacement effect used for the "cloaking" effect.
                _displacement = new Gorgon2DDisplacementEffect(_renderer)
                {
                    Strength = 0
                };

                // Create the old film effect for that old timey look.
                _oldFilm = new Gorgon2DOldFilmEffect(_renderer)
                {
                    ScrollSpeed = 0.05f
                };

                // Create the gaussian blur effect for that "soft" look.
                _gaussBlur = new Gorgon2DGaussBlurEffect(_renderer, 9)
                {
                    BlurRenderTargetsSize = new DX.Size2(_screen.Width / 2, _screen.Height / 2),
                    BlurRadius            = 1
                };
                // The higher # of taps on the blur shader will introduce a stutter on first render, so precache its setup data.
                _gaussBlur.Precache();

                // Load our texture with our space background in it.
                _background = GorgonTexture2DView.FromFile(_graphics,
                                                           Path.Combine(GorgonExample.GetResourcePath(@"Textures\").FullName, "HotPocket.dds"),
                                                           new GorgonCodecDds(),
                                                           new GorgonTexture2DLoadOptions
                {
                    Usage   = ResourceUsage.Immutable,
                    Binding = TextureBinding.ShaderResource
                });

                // Load up our super space ship image.
                _spaceShipTexture = GorgonTexture2DView.FromFile(_graphics,
                                                                 Path.Combine(GorgonExample.GetResourcePath(@"Textures\Effects\").FullName, "ship.png"),
                                                                 new GorgonCodecPng(),
                                                                 new GorgonTexture2DLoadOptions
                {
                    Usage   = ResourceUsage.Immutable,
                    Binding = TextureBinding.ShaderResource
                });
                _shipSprite = new GorgonSprite
                {
                    Texture       = _spaceShipTexture,
                    TextureRegion = new DX.RectangleF(0, 0, 1, 1),
                    Anchor        = new DX.Vector2(0.5f, 0.5f),
                    Position      = new DX.Vector2(_screen.Width / 2.0f, _screen.Height / 2.0f),
                    Size          = new DX.Size2F(_spaceShipTexture.Width, _spaceShipTexture.Height)
                };

                BuildRenderTargets();
                InitializeBackgroundTexturePositioning();

                GorgonExample.LoadResources(_graphics);

                window.MouseMove  += Window_MouseMove;
                window.MouseWheel += Window_MouseWheel;
                window.KeyUp      += Window_KeyUp;

                return(window);
            }
            finally
            {
                GorgonExample.EndInit();
            }
        }