does anyone know if there is a way to modify what is being written to the z-buffer in a shader ?
for example adding noise or using a texture or alpha channel to modify the z-depth ?
thx in advance
does anyone know if there is a way to modify what is being written to the z-buffer in a shader ?
for example adding noise or using a texture or alpha channel to modify the z-depth ?
thx in advance
Yes, this is called offset. Here is an explanation in the Unity Documentation:
But I am not sure if this works with textures since it is a parameter.
Thanks, I didn’t know about offset, that may be useful.
But I guess I should have posted more info… I was thinking is it possible to do on a per pixel bases, in fragment shader ?
there is an entire tutorial somewhere about z clashing and offset tricks here:
It should be possible in latest version of Unity, but I haven’t tried it yet.
Try googling unity/cg depth output or something like that.
IRC it need at least Shader Model 4, so DX11 in Unity, but I may be wrong.
In the vertex shader, does modifying the Z coord write to depth, or does depth get written before that?
hrmmm shader model 4 ? I guess not possible for mobile then
?
still … worth researching though. Thanks for the replies.
It works on iOS. did you see
Unity iOS ShaderLab - Tutorial 10-1 (Z-Fighting) - YouTube
Unity iOS ShaderLab - Tutorial 10-2 (Offset) - YouTube
Depth is written per pixel (as a pixel could still be discarded in fragment shader e.g. alpha cutoff)
It is also possible in DX9 to write depth directly, however mobile seems to be a no-go according to Aras:
http://forum.unity3d.com/threads/66153-Writing-depth-value-in-fragment-program
Shader "Custom/WriteDepth" {
Properties {
_MainTex ("Base (RGB)", 2D) = "white" {}
}
SubShader {
Tags { "RenderType"="Opaque" }
Pass {
CGPROGRAM
#pragma exclude_renderers gles flash
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct v2f {
float4 pos : POSITION;
float2 uv : TEXCOORD0;
};
struct fout {
half4 color : COLOR;
float depth : DEPTH;
};
uniform sampler2D _MainTex;
v2f vert (appdata_base v) {
v2f vo;
vo.pos = mul( UNITY_MATRIX_MVP, v.vertex );
vo.uv = v.texcoord.xy;
return vo;
}
fout frag( v2f i ) {
fout fo;
fo.color = tex2D(_MainTex, i.uv);
fo.depth = 0.99;
return fo;
}
ENDCG
}
}
FallBack "Diffuse"
}
thanks Big Bug.
shame its not possible on mobile… yet
OK but how would I modify the Depth? The following example forces to hide the texture. Also I get very strange behaviour like adding float2(0) vs. float2(0,0), which should be the same, right?
struct v2f {
float4 pos : POSITION;
float2 uv : TEXCOORD0;
float2 depth : TEXCOORD1;
};
v2f vert (appdata_base v) {
v2f o;
o.pos = mul( UNITY_MATRIX_MVP, v.vertex );
o.uv = v.texcoord.xy;
UNITY_TRANSFER_DEPTH(o.depth);
return o;
}
half4 frag( v2f i ) : COLOR {
fixed4 c = tex2D(_MainTex, i.uv);
float2 depth = i.depth;
UNITY_OUTPUT_DEPTH(depth);
return float4(c.rgb * _Color.rgb, c.a);
}