- Home /
Geometry shader for thick lines in clip space
Hi,
For non continuous line segments, LineRenderer requires a GO for each and so its slow with alot of them.
So, I'm attempting to make a line segment renderer. I'm passing the verts for all lines to a geometry shader, the plan being to transfer the verts into clip space and build thick 2D line segments. Like as described in this post RE: Smooth thick lines using geometry shader
I'm not concerned with the case where to segments are joined since joins are hidden behind other objects in my scene.
My attempt is shown below. However the Lines rotate strangely at the edges of the screen and are thicker at one end (which wasn't expected since I'm changing x and y in clip space).
I think I am doing something fundamentally wrong in my shader, hopefully it will be obvious to someone. I'm hoping the approach is valid.
Thankyou for any assistance!
See updated shader in my followup post below
Answer by gibber10 · Jan 07, 2017 at 01:55 PM
I've made some progress with this shader.
I had to divide the x and y by w then set it to 1 for both points. I'm also setting their z to make sure its consistent.
Its working correctly now except that it behaves strangely when lines are very close to the camera. And very strangely when you turn 180 degrees there is a copy of all lines behind the camera. I'm nearly there, I hope to get this working as it can currently render 10k lines at 500fps on my machine.
I've added the test script underneath the shader so others can test the shader without writing any code. Note that the camera probably starts inside all the lines so move it somewhere sensible or run it and use the scene view.
Shader "Custom/GeometryShaderTest1"
{
Properties
{
_Color("Color", Color) = (1,1,1,1)
_Size("Size", float) = 0.5
}
SubShader
{
Tags { "Queue" = "Transparent" "RenderType" = "Transparent" }
LOD 100
Blend SrcAlpha OneMinusSrcAlpha
ZWrite Off
Cull Off
Pass
{
CGPROGRAM
#pragma target 5.0
#pragma vertex vert
#pragma geometry geom
#pragma fragment frag
#include "UnityCG.cginc"
// Vars
float4 _Color;
float _Size;
float3 _worldPos;
struct data
{
float4 pos0;
};
StructuredBuffer<data> buf_Points;
struct input
{
float4 pos : SV_POSITION;
float4 color: COLOR;
};
input vert(uint id : SV_VertexID)
{
input o;
o.pos = float4(buf_Points[id].pos0 + _worldPos, 1.0f);
return o;
}
[maxvertexcount(8)]
void geom(line input p[2], inout TriangleStream<input> triStream)
{
float4 s[2];
s[0] = mul(UNITY_MATRIX_VP, p[0].pos);
s[1] = mul(UNITY_MATRIX_VP, p[1].pos);
s[0].x /= s[0].w;
s[0].y /= s[0].w;
s[1].x /= s[1].w;
s[1].y /= s[1].w;
s[0].z = s[1].z = 0.1;
s[0].w = s[1].w = 1;
float4 ab = s[1] - s[0];
float4 normal = float4(-ab.y, ab.x, 0, 0);
normal = normalize(normal);
normal.x /= (_ScreenParams.x / _ScreenParams.y);
input pIn;
pIn.pos = s[0] - normal * _Size;
pIn.color = float4(1.0, 0.0, 0.0, 1.0);
triStream.Append(pIn);
pIn.pos = s[0] + normal * _Size;
pIn.color = float4(1.0, 0.0, 0.0, 1.0);
triStream.Append(pIn);
pIn.pos = s[1] - normal * _Size;
pIn.color = float4(0.0, 1.0, 0.0, 1.0);
triStream.Append(pIn);
pIn.pos = s[1] + normal * _Size;
pIn.color = float4(0.0, 1.0, 0.0, 1.0);
triStream.Append(pIn);
}
float4 frag(input i) : COLOR
{
return i.color;
}
ENDCG
}
}
FallBack "Diffuse"
}
Script Below
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
struct data
{
public Vector3 pos;
}
public class LineSegmentRenderer : MonoBehaviour
{
public Shader geomShader;
public float Size = 0.05f;
Material material;
ComputeBuffer outputBuffer;
List<Vector3> points;
// Use this for initialization
void Start ()
{
material = new Material(geomShader);
points = new List<Vector3>();
SetupTest();
}
void SetupTest()
{
for (int i = 0; i < 10; i++)
{
AddLineSegmentGS(
new Vector3(Random.Range(-10f, 10f), Random.Range(-10f, 10f), Random.Range(-1f, 1f)),
new Vector3(Random.Range(-10f, 10f), Random.Range(-10f, 10f), Random.Range(-1f, 1f))
);
}
Apply();
}
// Update is called once per frame
void Update ()
{
}
public void AddLineSegmentGS(Vector3 a, Vector3 b)
{
points.Add(a);
points.Add(b);
}
public void Apply()
{
outputBuffer = new ComputeBuffer(points.Count, 24);
outputBuffer.SetData(points.ToArray());
}
public void OnRenderObject()
{
material.SetPass(0);
material.SetBuffer("buf_Points", outputBuffer);
material.SetFloat("_Size", Size);
Graphics.DrawProcedural(MeshTopology.Lines, outputBuffer.count);
}
public void OnDestroy()
{
outputBuffer.Release();
}
}
Answer by VStheijs · Nov 28, 2017 at 02:10 PM
I have written a very similar geometry shader and I have the same effect when I move the camera too close. Did you by any chance found a solution for this problem? ,I have written a very similar geometry shader and I also have the strange effect when the camera is close. Did you by any chance found a fix for this problem?