/// <summary> /// return a depth buffer texture from given camera, only render "Opaque" renderType objects /// </summary> public static Texture2D CreateDepthBuffer(Camera cam) { RenderTexture rt = RenderTexture.GetTemporary(Screen.width, Screen.height, 24, RenderTextureFormat.ARGB32); if (cam == null) { cam = Camera.main; } var oldClearFlags = cam.clearFlags; cam.clearFlags = CameraClearFlags.SolidColor; var oldClearColor = cam.backgroundColor; cam.backgroundColor = new Color32(254, 254, 254, 254); Shader shader = GetRenderDepthShader(); cam.targetTexture = rt; cam.RenderWithShader(shader, ReplacementTag); cam.targetTexture = null; Texture2D tex = RTUtil.GetRTPixels(rt); RenderTexture.ReleaseTemporary(rt); cam.clearFlags = oldClearFlags; cam.backgroundColor = oldClearColor; //byte[] img = tex.EncodeToJPG(); //System.IO.File.WriteAllBytes("Assets/depthBuffer.jpg", img); //Dbg.Log("write to depthBuffer: {0}, {1}x{2}", tex.GetInstanceID(), tex.width, tex.height); return(tex); }