Search code examples
c#xnashadermonogame

Shader doesnt work in Monogame (With shader it looks like without) | C#


So i have following problem :

I wanna have a nice bloom effect in my game. But when i try to use my shader and the shader.draw(); method is called it looks unchanged ... It looks like i wouldnt use a shader. Also when i change shader parameters nothing changes . I can set BloomThreshold to 10000f and it looks like 0f or 1f ... Im a total noob to shaders so i would be really glad for some help !

Left with shader, right without shader:

enter image description here

This is my draw loop:

protected override void Draw (GameTime gameTime) {  
    bloom.BeginDraw();
    bloom.Draw(gameTime);

    spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.Additive);
    spriteBatch.Draw(texture, new Rectangle(300, 300, 50, 50), Color.White);
    spriteBatch.End();

    base.Draw(gameTime);
}

This is an excerpt of the called methods of my bloom class:

    /// <summary>
    /// This should be called at the very start of the scene rendering. The bloom
    /// component uses it to redirect drawing into its custom rendertarget, so it
    /// can capture the scene image in preparation for applying the bloom filter.
    /// </summary>
    public void BeginDraw()
    {
        if (Visible)
        {
           GraphicsDevice.SetRenderTarget(sceneRenderTarget);
        }

    }


    /// <summary>
    /// This is where it all happens. Grabs a scene that has already been rendered,
    /// and uses postprocess magic to add a glowing bloom effect over the top of it.
    /// </summary>
    public override void Draw(GameTime gameTime)
    {

        GraphicsDevice.SamplerStates[1] = SamplerState.LinearClamp;

        // Pass 1: draw the scene into rendertarget 1, using a
        // shader that extracts only the brightest parts of the image.
        bloomExtractEffect.Parameters["BloomThreshold"].SetValue(
            Settings.BloomThreshold);

        DrawFullscreenQuad(sceneRenderTarget, renderTarget1,
                           bloomExtractEffect,
                           IntermediateBuffer.PreBloom);

        // Pass 2: draw from rendertarget 1 into rendertarget 2,
        // using a shader to apply a horizontal gaussian blur filter.
        SetBlurEffectParameters(1.0f / (float)renderTarget1.Width, 0);

        DrawFullscreenQuad(renderTarget1, renderTarget2,
                           gaussianBlurEffect,
                           IntermediateBuffer.BlurredHorizontally);


        // Pass 3: draw from rendertarget 2 back into rendertarget 1,
        // using a shader to apply a vertical gaussian blur filter.
        SetBlurEffectParameters(0, 1.0f / (float)renderTarget1.Height);

        DrawFullscreenQuad(renderTarget2, renderTarget1,
                           gaussianBlurEffect,
                           IntermediateBuffer.BlurredBothWays);

        // Pass 4: draw both rendertarget 1 and the original scene
        // image back into the main backbuffer, using a shader that
        // combines them to produce the final bloomed result.
        GraphicsDevice.SetRenderTarget(null);

        EffectParameterCollection parameters = bloomCombineEffect.Parameters;

        parameters["BloomIntensity"].SetValue(Settings.BloomIntensity);
        parameters["BaseIntensity"].SetValue(Settings.BaseIntensity);
        parameters["BloomSaturation"].SetValue(Settings.BloomSaturation);
        parameters["BaseSaturation"].SetValue(Settings.BaseSaturation);

        //GraphicsDevice.Textures[1] = sceneRenderTarget; 

        bloomCombineEffect.Parameters["BaseTexture"].SetValue(sceneRenderTarget);

        Viewport viewport = GraphicsDevice.Viewport;

        DrawFullscreenQuad(renderTarget1,
                           viewport.Width, viewport.Height,
                           bloomCombineEffect,
                           IntermediateBuffer.FinalResult);

                           System.Console.WriteLine("Draw Called");

    }

The full classes and shader code can be found here


Solution

  • Your sprite batch rendering should be between bloom.BeginDraw and bloom.Draw.

    bloom.BeginDraw binds the sceneRenderTarget you're supposed to render you scene to. This way the rendered data is accessible for further processing done in bloom.Draw.