- Home /
How can I take depth into account when rendering to texture?
Okay so basically I have an object with particles around it. I wanna apply a special image effect only on my particles (metaballs), so I put the particles on a separate layer, and render to texture from a separate camera that only sees this layer. I create a material that uses this render texture, and I apply it to a plane that rotates with my main camera. Everything works as expected, except for the fact that since I'm rendering to a plane, and the plane is closer to my camera than the actual scene, it gets drawn over my scene. How can I take into account the depth when I render to my texture so that my geometry is drawn over the particles? Or is my setup wrong? Any ideas?
What my scene looks like: http://i.imgur.com/mRCSC3U.png
What the game view looks like: http://i.imgur.com/McojxnE.png
Main camera settings: http://i.imgur.com/SEX5KtU.png
Blob camera settings: http://i.imgur.com/m36YL1D.png
I'm doing it with OnRenderImage now, ins$$anonymous$$d of the plane. The problem I have now is that the cameras have different depth, so either I just get the blobs, or I just get the object with the particles. If I set the blob camera to "Don't Clear" I get this http://imgur.com/dB$$anonymous$$r$$anonymous$$XS which is correct except for the fact that I just want the postprocessing effect on the particles but not the object.
Answer by Naphier · Mar 06, 2016 at 09:10 PM
You might need a third camera that is the same as your main camera. Have the new camera be the one that renders the render texture for the blobs and alter the depth of this new camera accordingly. You might be able to do this with the blob camera if the effect isn't fullscreen.
As suggested I'm not rendering to texture anymore and I'm just using OnRenderImage to post process the particles into blobs. The problem is that since the depth of the two cameras is different I either get this http://i.imgur.com/Jk$$anonymous$$ftem.png or this http://i.imgur.com/Unc9qVc.png depending on the relative depth. Would a third camera help in this case?
Answer by tanoshimi · Mar 06, 2016 at 09:14 PM
Why do you draw the rendertexture to a plane in the scene? If the particles are on a separate layer with a separate camera, it would probably make more sense to apply the metaball material as an image effect in the OnRenderImage of that camera:
void OnRenderImage (RenderTexture source, RenderTexture destination)
{
Graphics.Blit (source, destination, material);
}
You're right, I'm doing it with OnRenderImage now. I still have a problem with the depth though: with my main camera's depth is set to 0, and the blob camera is set to -1 I see this http://i.imgur.com/Jk$$anonymous$$ftem.png so no effect is visible, and when I change the blob camera's depth to something higher than zero I get this http://i.imgur.com/Unc9qVc.png so the effect is the only thing I see. I don't know how to blend the two.
You should draw the effect on top (i.e. your metaball camera should have a higher depth), but it looks like the blend mode on your metaball material is wrong. Are you using alpha blending and, if so, are you setting transparent pixels to all parts of the screen not covered by the metaball particles?
Shader "Custom/Blobs"
{
Properties
{
_$$anonymous$$ainTex("Texture", 2D) = "white" { }
_Threshold1("Threshold 1", Range(0.001,1.0)) = 0.05
_Threshold2("Threshold 2", Range(0.001,1.0)) = 0.5
_Threshold3("Threshold 3", Range(0.001,1.0)) = 0.3
}
SubShader
{
Pass
{
Blend SrcAlpha One$$anonymous$$inusSrcAlpha
CGPROGRA$$anonymous$$
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
float4 _Color;
sampler2D _$$anonymous$$ainTex;
struct v2f
{
float4 pos : SV_POSITION;
float2 uv : TEXCOORD0;
};
float4 _$$anonymous$$ainTex_ST;
v2f vert(appdata_base v)
{
v2f o;
o.pos = mul(UNITY_$$anonymous$$ATRIX_$$anonymous$$VP, v.vertex);
o.uv = TRANSFOR$$anonymous$$_TEX(v.texcoord, _$$anonymous$$ainTex);
return o;
}
float _Threshold1;
float _Threshold2;
float _Threshold3;
half4 frag(v2f i) : COLOR
{
half4 texcol = tex2D(_$$anonymous$$ainTex, i.uv);
half4 finalColor = texcol;
if(texcol.r > _Threshold3)
{
finalColor = half4(texcol.r, texcol.g, texcol.b, texcol.a);
finalColor = floor(finalColor / _Threshold1)*_Threshold2;
}
return finalColor;
}
ENDCG
}
}
Fallback "VertexLit"
}
This is what my shader looks like, you mean change the line with SrcAlpha?