Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Pre-rendered backgrounds with zDepth baked

Discussion in 'General Graphics' started by Emil_FAWorks, Feb 25, 2015.

  1. Emil_FAWorks

    Emil_FAWorks

    Joined:
    Feb 9, 2015
    Posts:
    14
    I'm using a fixed camera in unity and realised I could cheat a bit by pre-rendering all my static objects (Background), using a collision mesh for interaction and a zDepth image from max (Some parts need to be infront of others etc).

    I found some old discussions from people wanting to recreate this effect (mainly from old games like Final Fantasy and Resident Evil).

    But I figured some of you people might have a better understanding on how to practically do this?

    1 - I would like to hear your ideas regarding the ZBuffer. How could I alter it in a simple and easy way?
    2 - Is there a way to do shadows on top of this? More specifically I want to have shadows generated from my interactive objects ontop of the prerendered background.

    Thanks in advance! I'm new to unity but so far I really dig it!
     
  2. Emil_FAWorks

    Emil_FAWorks

    Joined:
    Feb 9, 2015
    Posts:
    14
    Ok, so I got it working, kinda. I'm using this shader:
    Code (CSharp):
    1. Shader "Custom/DioramaBG" {
    2.     Properties {
    3.         _RGB ("Base (RGB)", 2D) = "white" {}
    4.         _DEPTH ("Base (RGB)", 2D) = "white" {}
    5.     }
    6.     SubShader {
    7.         Pass {
    8.             CGPROGRAM
    9.             #pragma target 4.0
    10.             #pragma vertex vert
    11.             #pragma fragment frag
    12.             #include "UnityCG.cginc"
    13.              
    14.             sampler2D _RGB;
    15.             sampler2D _DEPTH;
    16.          
    17.             struct v2f
    18.             {
    19.                 float4 position : POSITION;
    20.                 float2 uv : TEXCOORD0;
    21.             };
    22.          
    23.             struct fragOut
    24.             {
    25.                 half4 color : COLOR;
    26.                 float depth : DEPTH;
    27.             };
    28.                  
    29.             v2f vert(appdata_base v)
    30.             {
    31.                 v2f o;
    32.                 o.position = mul(UNITY_MATRIX_MVP, v.vertex);
    33.                 o.uv = v.texcoord;
    34.                 return o;
    35.             }
    36.  
    37.             fragOut frag(in v2f i)
    38.             {
    39.                 fragOut o;
    40.                 o.color = tex2D(_RGB, i.uv);
    41.                 o.depth = tex2D(_DEPTH, i.uv);
    42.                 return o;
    43.             }
    44.             ENDCG
    45.         }
    46.     }
    47. }
    48.  
    I'm placing a quad in front of the camera with this shader on top with a depth texture and colors assigned to it.
    It works fine except for some rounding errors on the depth.., I'm guessing the zbuffer texture needs to have a higher bit but it could also just be that it reads from my texture badly.

    I'll explore more and see what is the problem (I get jaggies around objects in my prerendered scene when placing "real" objects halfway into them.., maybe I'll post a pic later).
     
    lassade likes this.
  3. Emil_FAWorks

    Emil_FAWorks

    Joined:
    Feb 9, 2015
    Posts:
    14
    So a small update.., The jaggies are gone, turned off mip-mapping and changed filter mode to point and I render to a format that matches either 2048 or 4096 right now. looks really smooth!

    One thing though.., I can get SSAO (screen space ambient occlusion) to work with my realtime objects.., but nothing happens with my pre-rendered background.., anybody have any ideas about that?.., I'll probably attach some image later to gain interest in this thread. (can't just upload the original since client secrecy.., yeah)
     
  4. Deleted User

    Deleted User

    Guest

    sounds very cool, hope you release this script.
     
  5. Louis-N-D

    Louis-N-D

    Joined:
    Apr 17, 2013
    Posts:
    224
    You'll have to look into what buffers are needed for SSAO. I think you'll need screen space normals and depth.
     
  6. Emil_FAWorks

    Emil_FAWorks

    Joined:
    Feb 9, 2015
    Posts:
    14
    Just wanted to update with the finished result
     
    lassade and marmito like this.