Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by gibber10 · Jan 03, 2017 at 07:33 PM · linerendererlinesgeometry shader

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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();
     }
 }
 

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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?

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Issue drawing lines between buttons - Lines overlap the buttons 2 Answers

What is the best way to draw a border around a viewport? 1 Answer

Line Renderer in World Space 2 Answers

Rendering large amount of lines 2 Answers

[Not Solved]Line renderer ugly lines, how to fix? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges