Search Unity

Replacement Shaders don't work in 4.5? Depth map into RT

Discussion in 'Scripting' started by varfare, Oct 1, 2014.

  1. varfare

    varfare

    Joined:
    Feb 12, 2013
    Posts:
    227
    I had my script working in Unity 3.5.7 and after migrating into 4.5 it stopped. There are no errors but replacement shader functionality seems to be broken or usage is different. I was basically creating another camera with RenderWithShader with depthTexture shader added. Whole screen was then copyied into RT.

    Code (CSharp):
    1. public class GenerateDepthMap : MonoBehaviour {
    2.     [HideInInspector]
    3.     public RenderTexture depthTex = null;
    4.     private GameObject depthCam = null;
    5.    
    6.     void CleanUpTextures() {
    7.         if (depthTex) {
    8.             RenderTexture.ReleaseTemporary(depthTex);
    9.             depthTex = null;
    10.         }
    11.     }
    12.    
    13.    
    14.     void OnRenderImage(RenderTexture src, RenderTexture dst) {
    15.         CleanUpTextures();
    16.        
    17.         depthTex = RenderTexture.GetTemporary(src.width, src.height, 16, RenderTextureFormat.Depth);
    18.        
    19.         if (!depthCam) {
    20.             depthCam = new GameObject("DepthMapCamera");
    21.             depthCam.transform.parent = transform;
    22.            
    23.             depthCam.AddComponent<Camera>();
    24.             depthCam.camera.enabled = true;
    25.             depthCam.hideFlags = HideFlags.DontSave;
    26.             Debug.Log("camera created");
    27.         }
    28.        
    29.         depthCam.camera.CopyFrom(this.camera);
    30.         depthCam.camera.backgroundColor = new Color(0, 0, 0, 0);
    31.         depthCam.camera.clearFlags = CameraClearFlags.Depth;
    32.         depthCam.camera.cullingMask = ~(0);
    33.         depthCam.camera.targetTexture = depthTex;
    34.         depthCam.camera.RenderWithShader((Shader.Find("Hidden/Camera-DepthTexture")), null);
    35.  
    36.         Graphics.Blit(src, dst);  
    37.     }
    38.    
    39.    
    40.     void OnDisable() {
    41.         DestroyImmediate(depthTex);
    42.         DestroyImmediate(depthCam);
    43.         depthCam = null;
    44.         depthTex = null;
    45.     }
    46. }
    The output of this script in unity 4.5 is black render texture with no depth information at all. I've tried different approaches but I can't seem to find a proper way to copy depth map into a Render Texture.
     
  2. vexe

    vexe

    Joined:
    May 18, 2013
    Posts:
    644
    Find what you were looking for? - Anyways, I wanted to do something similar, render scene depth and output to an image for use as a depth map later on. Here's what I did, maybe it'll help.
     
  3. varfare

    varfare

    Joined:
    Feb 12, 2013
    Posts:
    227
    Since the project that I'm working on switched to deferred renderer and I wanted to just pass depth texture from first camera to the second cam I developed a workaround . Although I didn't find the reason for depth replacement shader not working in 4.5...

    Basically, deferred renderer is always generating depth, color and normal buffers and getting them is quite cheap relative to forward rendering. So I decided to copy all buffers from cam1 to cam2. There is no clear documentation on the subject. The solution was simply setting second's cam "clear flags" to "none" and setting culling mask to "nothing" so that second camera is only receiving all buffors from camera1.