# 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 () {
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;

}
}
``````

where the flat offset shader is

``````Shader "Custom/Flat Offset"{
Properties {
_Color ("Color", Color) = (0.0, 0.0, 0.0, 0.0)
}
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).

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.