- Home /
Decal shader: z-fighting when camera axis is parallel to normal axis
I've created a basic decal shader (see below; it's just the default new shader with a couple modifications). It usually works well, but if the camera axis is parallel to the normal axis of the polygon(s) the material using the decal shader is applied to, z-fighting occurs when the camera moves.
Example (z-fighting when camera axis is parallel to normal axis of plane; fine otherwise):
Here's a simple project (used to record the GIF above) that demonstrates the issue:
Unity project demonstrating issue (press "E" to toggle camera angle)
I'm wondering if this might be related to my video card rather than the decal shader itself or Unity, so attempts to replicate using the demo project would be appreciated! (For what it's worth, my video card's drivers are up-to-date, but the card itself is a few years old at this point.)
Decal shader for reference:
Shader "Custom/DecalShader" {
Properties {
_Color ("Color", Color) = (1,1,1,1)
_MainTex ("Albedo (RGB)", 2D) = "white" {}
_Glossiness ("Smoothness", Range(0,1)) = 0.5
_Metallic ("Metallic", Range(0,1)) = 0.0
}
SubShader {
Tags { "RenderType"="Transparent" "ForceNoShadowCasting"="True"}
LOD 200
Offset -1, -1
CGPROGRAM
// Physically based Standard lighting model, and enable shadows on all light types (added decal:blend)
#pragma surface surf Standard fullforwardshadows decal:blend
// Use shader model 3.0 target, to get nicer looking lighting
#pragma target 3.0
sampler2D _MainTex;
struct Input {
float2 uv_MainTex;
};
half _Glossiness;
half _Metallic;
fixed4 _Color;
void surf (Input IN, inout SurfaceOutputStandard o) {
// Albedo comes from a texture tinted by color
fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
o.Albedo = c.rgb;
// Metallic and smoothness come from slider variables
o.Metallic = _Metallic;
o.Smoothness = _Glossiness;
o.Alpha = c.a;
}
ENDCG
}
FallBack "Diffuse"
}
Answer by Bunny83 · Jul 08, 2017 at 03:52 PM
What is your depth buffer range? (what's your near and far clipping values?)
The Offset parameter can only help to avoid z-fighting but can't prevent it when the numbers are to high.
So try to increase your near-clipping plane or decrease your far-clipping plane. Alternatively you can increase your Offset in your shader, mainly the second parameter:
Offset -1, -2
Reducing the depth buffer range (old near/far clipping planes: [0.3, 1000], new: [5, 20]) didn't remove the z-fighting, but increasing the offset worked ("Offset -1, -5" gave good results). Thanks for the tip!
Your answer
Follow this Question
Related Questions
2.5D lighting looks wrong when rotating camera 0 Answers
having a camera render an object only if it appears after a plane 1 Answer
Semi transparent Object behind walls? 1 Answer
Unity decal shader 0 Answers
Shader to a Camera 1 Answer