Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7738955
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T08:24:01+00:00 2026-06-01T08:24:01+00:00

I’m trying to create a depth of field post process, but have no idea

  • 0

I’m trying to create a depth of field post process, but have no idea where to start (except render depth map, which I’m currently at). All the tutorials for it are either for XNA3.1, don’t actually give you an explanation, or part of a book.

So, can you go through a detailed, step-by-step process on how DOF is rendered?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T08:24:02+00:00Added an answer on June 1, 2026 at 8:24 am

    Here’s a description on how to achieve a basic approximation of it using the “out of the box” features provided by XNA within the Reach profile.

    Once you’re across how to do it in C# using the inbuilt stuff, achieving it in HLSL will hopefully be a little more obvious.

    Also, should you ever wish to produce a game for Windows Phone 7, you’ll where to start (as Windows Phone 7 doesn’t support custom shaders at this point in time).

    First we’ll define some instance level variable to hold the bits and pieces we need to produce the look:

    BasicEffect effect;
    List<Matrix> projections;
    List<RenderTarget2D> renderTargets;
    SpriteBatch spriteBatch;
    

    Next, in the LoadContent() method, we’ll start loading them up. Starting with a SpriteBatch that’ll we’ll use to render the final scene:

    spriteBatch = new SpriteBatch(GraphicsDevice);
    

    Followed by an instance of BasicEffect:

    effect = new BasicEffect(GraphicsDevice);
    effect.EnableDefaultLighting();
    effect.DiffuseColor = Color.White.ToVector3();
    effect.View = Matrix.CreateLookAt(
                Vector3.Backward * 9 + Vector3.Up * 9,
                Vector3.Zero,
                Vector3.Up);
    effect.World = Matrix.Identity;
    
    effect.Texture = Content.Load<Texture2D>("block");
    effect.TextureEnabled = true;
    effect.EnableDefaultLighting();
    

    The specifics of how the Basic Effect are configured aren’t important here. Merely that we have an effect to render with.

    Next up we’re going to need a few projection Matrices:

    projections = new List<Matrix>() {
            Matrix.CreatePerspectiveFieldOfView(
            MathHelper.ToRadians(60f),
            GraphicsDevice.Viewport.AspectRatio,
            9f,
            200f),
        Matrix.CreatePerspectiveFieldOfView(
            MathHelper.ToRadians(60f),
            GraphicsDevice.Viewport.AspectRatio,
            7f,
            10f),
        Matrix.CreatePerspectiveFieldOfView(
            MathHelper.ToRadians(60f),
            GraphicsDevice.Viewport.AspectRatio,
            0.2f,
            8f)};
    

    If you examine the last two parameters of each projection, you’ll notice what we’re effectively doing here is splitting the world up into “chunks” with each chunk covering a different range of distances from the camera.

    e.g. everything from 9 units beyond, anything between 7 units and 10 units from the camera and finally anything closer then 8 units.

    (You’ll need to tweak these distances depending on your scene. Please note the small amount of overlap)

    Next we’ll create some render targets:

    var pp = GraphicsDevice.PresentationParameters;
    
    renderTargets = new List<RenderTarget2D>()
    {
        new RenderTarget2D(GraphicsDevice,
            GraphicsDevice.Viewport.Width / 8,
            GraphicsDevice.Viewport.Height / 8,
            false, pp.BackBufferFormat, pp.DepthStencilFormat),
    
            new RenderTarget2D(GraphicsDevice,
            GraphicsDevice.Viewport.Width / 4,
            GraphicsDevice.Viewport.Height / 4,
            false, pp.BackBufferFormat, pp.DepthStencilFormat),
    
        new RenderTarget2D(GraphicsDevice,
            GraphicsDevice.Viewport.Width,
            GraphicsDevice.Viewport.Height,
            false, pp.BackBufferFormat, pp.DepthStencilFormat),
    };
    

    Each render target corresponds to an aforementioned “chunk”. To achieve a really simplistic blur effect, each render target is set to a different resolution with the “furthest” chunk being a low resolution and the closest chunk being a high resolution.

    Jumping across to the Draw() method, we can render our scene chunks:
    (Being sure not to render the background in each chunk)

            effect.Projection = projections[0];
            GraphicsDevice.SetRenderTarget(renderTargets[0]);
            GraphicsDevice.Clear(Color.Transparent);
            // render scene here
    
            effect.Projection = projections[1];
            GraphicsDevice.SetRenderTarget(renderTargets[1]);
            GraphicsDevice.Clear(Color.Transparent);
            // render scene here
    
            effect.Projection = projections[2];
            GraphicsDevice.SetRenderTarget(renderTargets[2]);
            GraphicsDevice.Clear(Color.Transparent);
            // render scene here
    
            GraphicsDevice.SetRenderTarget(null);
    

    So now we’ve got our scene, broken up and blurred by distance, all that’s left is to recombine it back together for our final image.

    First step, render the (awesome) background:

        GraphicsDevice.Clear(Color.CornflowerBlue);
    

    Next render each chunk, from further to closest:

        spriteBatch.Begin(
            SpriteSortMode.Deferred, 
            BlendState.AlphaBlend, 
            SamplerState.AnisotropicClamp, 
            null, null);
    
        spriteBatch.Draw(renderTargets[0], GraphicsDevice.Viewport.Bounds, Color.White);
        spriteBatch.Draw(renderTargets[1], GraphicsDevice.Viewport.Bounds, Color.White);
        spriteBatch.Draw(renderTargets[2], GraphicsDevice.Viewport.Bounds, Color.White);
    
        spriteBatch.End();
    

    And viola! We have a, albeit a little rough around the proverbial edges, approximation of Depth Of Field.

    Now if you’re planning to stay within the confines of the Reach profile, you can improve the blur effect by rendering each chunk at multiple resolutions and combining the resulting images together using something like the Additive BlendState.

    If, on the other hand, you’re planning to branch out into writing custom shaders in the HiDef profile, the concepts are roughly the same, just the method of execution changes.

    For example, swapping the low resolution rendering for a more authentic Gaussian style blur… or… ditching the course grained idea of chunks and moving to the relatively fine grained method of blurring based off a depth map.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create an if statement in PHP that prevents a single post
Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I have a French site that I want to parse, but am running into
I have a text area in my form which accepts all possible characters from
I have thousands of HTML files to process using Groovy/Java and I need to
I am trying to loop through a bunch of documents I have to put
link Im having trouble converting the html entites into html characters, (&# 8217;) i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.