How can give the depth texture to other pass use?

Hi, I want to make a volume deth test in object shader.not post render.
the step is:
pass 1:. Cull back to render depth give to depthTex1;
pass2: Cull front to render depth give to depthTex2;
pass3: get the object volume v=depthTex2-depthTex1;

so, how to get it? Need your help,thanks~

half4 frag (v2f i) : COLOR
	
	{
	  float4 color =0;
	 _DepthTex1=_CameraDepthTexture;
	  return color; 
	
	}

this not work.

Well color is a float4, so you are declaring it wrong. float4 color = float4(0,0,0,0);

how can draw the depth when cull front?

You must write custom replacement shader to render depths per rendertype 1st one to cull front, 2nd one to cull back and pass the outcomes as rendertextures to your final shader.

Thanks aubergine! now i want to draw the depth when cull front ,but it not work…

Shader "Custom/ShowNormals" {
Properties {
    _MainTex ("Base (RGB)", 2D) = "white" {}
    _DepthNormal ("Depth and Normals", 2D) = "white" {}
}

// Shader code pasted into all further CGPROGRAM blocks
CGINCLUDE
#include "UnityCG.cginc"
struct v2f {
    float4 pos : POSITION;
    float2 uv : TEXCOORD0;
};

uniform sampler2D _MainTex;
uniform sampler2D _CameraDepthNormalsTexture;
float4 _CameraDepthNormalsTexture_ST;
sampler2D _DepthNormal;

v2f vert( appdata_img v ){
    v2f o;
    o.pos = mul(UNITY_MATRIX_MVP, v.vertex);

    float2 uv = v.texcoord.xy;
    o.uv = uv;

    return o;
}

half4 frag(v2f i) : COLOR {
   
    half4 color=0;
    float outlineDepth;
    float3 norm;
    DecodeDepthNormal(tex2D(_CameraDepthNormalsTexture, i.uv.xy), outlineDepth, norm);
	outlineDepth = LinearEyeDepth(outlineDepth);

	norm=-1*norm;
	return half4(norm.x, norm.y, norm.z, outlineDepth);
//	return half4(0.2,0.2,0.2,outlineDepth);
//    if (outlineDepth>0)
//        return half4(norm.x, norm.y, norm.z, outlineDepth);
//    else
//       return half4(tex2D(_MainTex, i.uv).rgb,1);
//        return color;
     
}
ENDCG

SubShader {

    Pass {
        ZTest Always
        Cull Front
      //  ZWrite On
        Fog { Mode off }
     //   Blend SrcAlpha OneMinusSrcAlpha 

        CGPROGRAM
        #pragma vertex vert
        #pragma fragment frag
        ENDCG
    }
}
FallBack off
}

hi,I found this link, this is not work, can not render ,cam.renderWithShader.It only work with DecodeDepthNormal(tex2D(_CameraDepthNormalsTexture, i.uv.xy), outlineDepth, norm)http://www.danielkiedrowski.com/2012/02/unity3d-how-to-render-depth-values-and-normals-to-a-rendertexture/,someBody make it sense?

Why can not render target object. here is the c# code:

using UnityEngine;

using System.Collections;

 [ExecuteInEditMode]
public class VolumeFogObject: PostEffectsBase {

    protected MeshRenderer FogObject;

    public float Radius = 10f;

    public Material VolumFogMaterial;

    public Color FogColor = Color.white;
	public float SoftScale=0.01f;
	public float Power=0.2f;
	private RenderTexture renderTXT1;
	private RenderTexture renderTXT2;
	private GameObject shaderCamera  ;
	private Camera cam;
	public Shader renderTypeShader;
	private Material renderTypeMt;
	//public Shader VolumtricObj;
	

 

   bool CheckResources ()  {	
		CheckSupport (true);
		
//		VolumFogMaterial = CheckShaderAndCreateMaterial (VolumtricObj, VolumFogMaterial);
	//	depthFetchMaterial = CheckShaderAndCreateMaterial (depthFetchShader, depthFetchMaterial);
	
		
		if(!isSupported)
			ReportAutoDisable ();
		return isSupported;			
	}
 
  
 
  void OnPreRender()
	//void Awake()

    {
		//if(CheckResources()==false)return;
		renderTXT1 = RenderTexture.GetTemporary ((int)Camera.main.pixelWidth  , (int)Camera.main.pixelHeight , 16);

		if (!shaderCamera) {
		shaderCamera = new GameObject("ShaderCamera", typeof(Camera));
		shaderCamera.camera.enabled = false;
		shaderCamera.hideFlags = HideFlags.HideAndDontSave;
		}
	
		cam = shaderCamera.camera;
		cam.CopyFrom (Camera.main);
		cam.backgroundColor = new Color(0,0,0,0);
		cam.clearFlags = CameraClearFlags.SolidColor;
		
		cam.RenderWithShader(renderTypeShader, "RenderType");	
		cam.depthTextureMode|=DepthTextureMode.Depth;
		cam.targetTexture=renderTXT1;
	//	renderTypeMt=new Material(renderTypeShader);
		
    }

//// 
	void OnRenderImage (RenderTexture source  , RenderTexture destination )
	{	
		
		if(CheckResources()==false)
		{
			Graphics.Blit (source, destination);
			return;
		}
		
		RenderTexture hrTex   = RenderTexture.GetTemporary (source.width, source.height, 0);
		RenderTexture lrTex1   = RenderTexture.GetTemporary (source.width / 2, source.height / 2, 0);
		RenderTexture lrTex2  = RenderTexture.GetTemporary (source.width / 2, source.height / 2, 0);
		//Debug.Log("dd");
				
		cam.targetTexture=renderTXT1;
		RenderTexture.active=lrTex1;
	   //  renderTXT1.SetGlobalShaderProperty ("__RenderTex");
	  ////  renderTypeMt.SetPass(0);
		Graphics.Blit(renderTXT1,lrTex1);
		VolumFogMaterial.SetTexture("_DepthTex1", lrTex1);
		Graphics.Blit (source,destination, VolumFogMaterial);	
//		cam.targetTexture=renderTXT2;
//		VolumFogMaterial.SetTexture("_DepthTex2", lrTex2);
//		Graphics.Blit(renderTXT2,lrTex2,VolumFogMaterial,1);
//	
//		VolumFogMaterial.SetTexture("_DepthTex1", lrTex1);
//		VolumFogMaterial.SetTexture("_DepthTex2", lrTex2);
//		Graphics.Blit (source,destination, VolumFogMaterial,2);	
		
		RenderTexture.ReleaseTemporary (hrTex);
		RenderTexture.ReleaseTemporary (lrTex1);
		RenderTexture.ReleaseTemporary (lrTex2);
		
	}	
//	void OnEnable()
//
//    {
//
//        //Note: In forward lightning path, the depth texture is not automatically generated.
//
//        if (cam.depthTextureMode == DepthTextureMode.None)
//
//            cam.depthTextureMode = DepthTextureMode.Depth;
//
//    }

//    void Update () 
//
//    {
//		cam.targetTexture=renderTXT1;
//		RenderTexture.active=renderTXT1;
//	
//		this.renderer.sharedMaterial.SetPass(0); 
//		this.renderer.sharedMaterial.SetTexture("_DepthTex1",renderTXT1);
//		cam.targetTexture=renderTXT2;
//		RenderTexture.active=renderTXT2;
//		this.renderer.sharedMaterial.SetPass(1); 
//		this.renderer.sharedMaterial.SetTexture("_DepthTex2",renderTXT2);
//////		
//		this.renderer.sharedMaterial.SetPass(2);
//		this.renderer.sharedMaterial.SetColor("_FogColor", FogColor);
//		this.renderer.sharedMaterial.SetTexture("_DepthTex1",renderTXT1);
//		this.renderer.sharedMaterial.SetTexture("_DepthTex2",renderTXT2);
// 		this.renderer.sharedMaterial.SetVector("FogParam", new Vector4(transform.position.x, transform.position.y, transform.position.z, Radius));
//		this.renderer.sharedMaterial.SetFloat("scale",SoftScale);
//		this.renderer.sharedMaterial.SetFloat("power",Power);
//
//    }

}

shader:

Shader "fireBird/volumtricFog" {

Properties {

  _MainTex ("Base (RGBA)", 2D) = "white" {}
  _DepthTex1 ("_DepthTex1 (RGB)", 2D) = "white" {}
  _DepthTex2 ("_DepthTex2 (RGB)", 2D) = "white" {}
}

 

Category {

// Tags { "Queue"="Transparent+99" "IgnoreProjector"="True" "RenderType"="Transparent" }

 //  Blend SrcAlpha OneMinusSrcAlpha
 

SubShader {

 
// 0 
  Pass{
   
     	
	CGPROGRAM
	
	#pragma target 2.0
	
	#pragma vertex vert
	
	#pragma fragment frag
	
	#include "UnityCG.cginc"
    sampler2D _CameraDepthNormalsTexture;
	sampler2D _DepthTex1;
	sampler2D _MainTex;
	struct v2f {
	
	    float4 pos : SV_POSITION;
	    float4 projPos : TEXCOORD1;
	    float2 uv:TEXCOORD0;
	
	};
	
		
	v2f vert (appdata_base v)
	
	{
	
	    v2f o;
	
	  
	
	    o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
	
	   
	
	    o.projPos = ComputeScreenPos (o.pos);
	    //float2 uv = MultiplyUV( UNITY_MATRIX_TEXTURE0, v.texcoord);
	    o.uv = v.texcoord.xy;
	 
	
	    float inFrontOf = ( o.pos.z / o.pos.w ) > 0;
	
	    o.pos.z *= inFrontOf; 
//	
	    return o;
	
	}
	half4 frag (v2f i) : COLOR
	
	{
	   half4 color=0;
    float outlineDepth;
    float3 norm;
    DecodeDepthNormal(tex2D(_DepthTex1, i.uv.xy), outlineDepth, norm);
	outlineDepth = LinearEyeDepth(outlineDepth);

    color=tex2D(_DepthTex1, i.uv.xy);// color=tex2D(_CameraDepthNormalsTexture, i.uv.xy);   WHY THIS CAN SHOW???
    return color;
	//norm=-1*norm;
	//norm=(0.5,0.5,0);
	//return half4(norm.x, norm.y, norm.z, 0.5);
	
    if (outlineDepth>0)
        return half4(norm.x, norm.y, norm.z, outlineDepth);
    else
       return half4(tex2D(_MainTex, i.uv).rgb,1);
        return color;
	
	}
	
	ENDCG
  
  
  }
  
	
}

}

Fallback "VertexLit"

}

need render object’ s shader: Now ,use cam.renderWithShader,can not see the renderTexture,it is empty,need your help,thanks.

Shader "VertexLit Funky" {
Properties {
	_Color ("Main Color", Color) = (1,1,1,1)
	_MainTex ("Base (RGB)", 2D) = "white" {}
}

SubShader {
	Tags { "RenderType"="Funky" }
	
	Pass {
		Material {
			Diffuse [_Color]
			Ambient [_Color]
		} 
		Lighting On
		SetTexture [_MainTex] {
			Combine texture * primary DOUBLE, texture * primary
		} 
	}
}
}

One is _CameraDepthNormalsTexture,another is _DepthTex1

Any help? I saw someBody else also can not get the camera depth texture,by cam.targer texture. this is a bug??

BUMP.

:),bump~

:?BUMP

BUMP

Hi,aubergine~,i according your advice make this.I make 3 replaceShader and 3 cam.Still don’t know how give the depth texture to the final cam shader to use. it only can get the lastest depth texture.can not get two depth texture…

using UnityEngine;
using System.Collections;

[ExecuteInEditMode]
[RequireComponent(typeof(Camera))]
[AddComponentMenu("Image Effects/Object Glow2")]

public class volumtricObj : MonoBehaviour {
  
  // public Shader replaceFront;
    private GameObject shaderCamera;
	public Material depthMt;
	public RenderTexture bufferBack;
	public RenderTexture bufferFront;
	Camera cam ;
    void Start()
    {
//        if (!replaceFront)
//        {
//            Debug.LogError("Shader not assigned");
//            enabled = false;
//            return;
//        }
    }
	void OnPreRender()
	{
		bufferFront=GetComponent<frontDepth>().bufferFront;
		bufferBack=GetComponent<backDepth>().bufferBack;
		 if (!shaderCamera)
        {
            shaderCamera = new GameObject("ShaderCamera", typeof(Camera));
            shaderCamera.camera.enabled = false;
            shaderCamera.hideFlags = HideFlags.HideAndDontSave;
        }
        cam = shaderCamera.camera;
        cam.CopyFrom(camera);
        cam.backgroundColor = new Color(0, 0, 0, 0);
        cam.clearFlags = CameraClearFlags.SolidColor;
      //  cam.targetTexture = bufferBack;
  //     cam.RenderWithShader(replaceFront, "RenderType");
		cam.depthTextureMode|=DepthTextureMode.DepthNormals;
		
	}
	void Update()
	{
		bufferFront=GetComponent<frontDepth>().bufferFront;
		bufferBack=GetComponent<backDepth>().bufferBack;
	}
    void OnRenderImage(RenderTexture source, RenderTexture destination)
    {
        
		depthMt.SetTexture("_repalceDepthTextureFront",bufferFront);
		depthMt.SetTexture("_repalceDepthTextureBack",bufferBack);

        Graphics.Blit(source, destination,depthMt);
		bufferBack.Release();
    }
}
Shader "Custom/depthDif" {
	Properties {
		_MainTex ("Base (RGB)", 2D) = "" {}
	}
	
	// Shader code pasted into all further CGPROGRAM blocks
	CGINCLUDE
		
	#include "UnityCG.cginc"
	
	struct v2f {
		float4 pos : POSITION;
		float2 uv : TEXCOORD0;
	};
		
	sampler2D _MainTex;
	sampler2D _CameraDepthTexture;
	sampler2D _repalceDepthTextureBack;
	sampler2D _repalceDepthTextureFront;
		
	v2f vert( appdata_img v ) 
	{
		v2f o;
		o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
		o.uv =  v.texcoord.xy;
		return o;
	}
	
	half4 fragFront(v2f i) : COLOR 
	{
		
		float d = UNITY_SAMPLE_DEPTH( tex2D(_repalceDepthTextureFront, i.uv.xy) );
		d = Linear01Depth(d);

		if(d>0.99999)
			return half4(1,1,1,1);
		else
			return EncodeFloatRGBA(d); 
	}
	
	
		half4 fragBack(v2f i) : COLOR 
	{
		float d = UNITY_SAMPLE_DEPTH( tex2D(_repalceDepthTextureBack, i.uv.xy) );
		d = Linear01Depth(d);
//		
//		float d2 = UNITY_SAMPLE_DEPTH( tex2D(_CameraDepthTexture, i.uv.xy) );
//		d2 = Linear01Depth(d2);
//			 
		//return tex2D(_CameraDepthTexture, i.uv.xy);
		//return EncodeFloatRGBA(d); 
		return EncodeFloatRGBA(d); 
	}
	
	
	
		half4 fragDif(v2f i) : COLOR 
	{
		float df= UNITY_SAMPLE_DEPTH( tex2D(_repalceDepthTextureFront, i.uv.xy) );
		df = Linear01Depth(df);
		
//		
		half4 backDepthColor=tex2D(_repalceDepthTextureBack, i.uv.xy);
		//half4 groundColor=tex2D(_CameraDepthTexture, i.uv.xy);
		//backDepthColor=backDepthColor+groundColor;
		float d2 = UNITY_SAMPLE_DEPTH( tex2D(_repalceDepthTextureBack, i.uv.xy) );
		d2 = Linear01Depth(d2);
		
     	return EncodeFloatRGBA(d2); 
	}

	ENDCG
	
Subshader {
	
	//back depth
// Pass {
// 	 // Blend one one
//	  ZTest Always Cull off ZWrite Off
//	  Fog { Mode off }      
//
//      CGPROGRAM
//      #pragma fragmentoption ARB_precision_hint_fastest
//      #pragma vertex vert
//      #pragma fragment fragBack
//      ENDCG
//  }
//  //front depth
//   Pass {
//	  ZTest Always Cull off ZWrite Off
//	  Fog { Mode off }      
//
//      CGPROGRAM
//      #pragma fragmentoption ARB_precision_hint_fastest
//      #pragma vertex vert
//      #pragma fragment fragFront
//      ENDCG
//  }
//  // dis depth
  Pass {
  
	  ZTest Always Cull off ZWrite Off
	  Fog { Mode off }      

      CGPROGRAM
      #pragma fragmentoption ARB_precision_hint_fastest
      #pragma vertex vert
      #pragma fragment fragDif
      ENDCG
  }
}

Fallback off
	
} // shader




1179935–45495–$volumtricObj.unitypackage (63.7 KB)

I have a confused with the rendertexture for depth ,it look not the same as the _CameraDepthTexture. WHY? _CameraDepthTexture look have many color stripes, but the rendertexture form script,it only color plane, so the depth is not correct???

Any help?

anybody help