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);
//
// }
}