- Home /
Change Z offset of lines
I am trying to change the z offset of lines in unity so it is possible to cover one line with another in 3D space. For a simple case I am generating a 1x1 square on a 2x2 quad.
The code to generate the square is
using UnityEngine;
using System.Collections;
public class Test : MonoBehaviour {
void Start () {
MeshFilter filter=gameObject.AddComponent<MeshFilter>();
Mesh mesh=new Mesh();
mesh.vertices=new Vector3[] {
new Vector3(-0.5f, -0.5f, 0),
new Vector3(-0.5f, 0.5f, 0),
new Vector3(0.5f, -0.5f, 0),
new Vector3(0.5f, 0.5f, 0),
};
int[] lines=new int[] {
0, 1, 0, 2, 2, 3, 1, 3,
};
mesh.SetIndices(lines, MeshTopology.Lines, 0);
mesh.normals=new Vector3[] {
new Vector3(-0.5f, -0.5f, 0),
new Vector3(-0.5f, 0.5f, 0),
new Vector3(0.5f, -0.5f, 0),
new Vector3(0.5f, 0.5f, 0),
};
filter.mesh=mesh;
MeshRenderer renderer=gameObject.AddComponent<MeshRenderer>();
renderer.sharedMaterial=Resources.Load<Material>("Materials/Flat Offset");
}
}
where the flat offset shader is
Shader "Custom/Flat Offset"{
Properties {
_Color ("Color", Color) = (0.0, 0.0, 0.0, 0.0)
}
SubShader {
Pass {
Offset -1, -1
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
uniform float4 _Color;
float4 vert (float4 position: POSITION) : SV_POSITION
{
return mul(UNITY_MATRIX_MVP, position);
}
float4 frag () : COLOR
{
return _Color;
}
ENDCG
}
}
FallBack "Diffuse"
}
The shader works to fix z fighting between two quads
However this does not fix z fighting between the quad/line.
Is there a way to change the depth offset of lines? I want to make it so one group of lines has a different depth offset from another group of lines (in addition to having a different depth offset than quads).
Answer by zach-r-d · Aug 10, 2015 at 03:09 AM
I can't say for certain without seeing Unity's source code, but I suspect that Offset determines values passed to glPolygonOffset, which does not work with line primitives. It does work with polygons with the draw mode set to lines instead of fill, if there is a way to set that in Unity and the lines being drawn will always be polygons. If not, the solution is to just alter the vertex positions, which wouldn't be too bad in the vertex shader; just subtract a tiny amount in the z axis after the MVP multiplication.
@zach.r.d Thanks, I ended up adding 0.0003 to the z value of SV_POSITION for now. However setting the draw mode to lines ins$$anonymous$$d of fill actually seems like a better solution for me. Are there any resources you would recommend to learn how to make a shader do that?
Thanks, glad that's working out so far. I'm not sure if switching the polygon fill mode is actually possible at all, since it's an OpenGL thing and Unity would have to support it in a cross-platform way. It's done in OpenGL with glPolygon$$anonymous$$ode, but there doesn't seem to be an equivalent Pass setting in ShaderLab.
Your answer
Follow this Question
Related Questions
How to determine shader center? 0 Answers
Depth issues with a shader 1 Answer
How to force the compilation of a shader in Unity? 5 Answers
ZBuffer and Object Depth 1 Answer
Thickness Surface Shader 0 Answers