Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Trail Renderer without Vertex Overlap

Discussion in 'General Graphics' started by Zilby, Mar 24, 2019.

  1. Zilby

    Zilby

    Joined:
    Jan 25, 2017
    Posts:
    10
    So I've been having a hell of a time trying to get a trail renderer for a nebula I've been working on looking right.

    The general effect is supposed to look like this:
    Screen Shot 2019-03-24 at 7.18.28 PM.png
    Which for the full effect is designed to have overlapping trails like so:
    Screen Shot 2019-03-24 at 7.18.02 PM.png
    However this ends up producing artifacts like these where the vertices overlap if the camera looks directly at points where the nebula is straight on:
    Screen Shot 2019-03-24 at 7.18.56 PM.png
    This is of course caused by the fact that trails generated by Unity's trail renderer always face the camera.

    I've seen this post using z-buffers and this one using stencils but can't get a good result using either. My current shader code is here:

    Code (CSharp):
    1. Shader "Aurora" {
    2.     Properties {
    3.         _MainTex ("Particle Texture", 2D) = "white" {}
    4.     }
    5.  
    6.     Category {
    7.         Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" "PreviewType"="Plane" }
    8.         Blend SrcAlpha OneMinusSrcAlpha
    9.         Cull Back Lighting Off ZWrite Off
    10.  
    11.         SubShader {
    12.          
    13.             LOD 100
    14.          
    15.             Pass {
    16.              
    17.                 //Stencil {
    18.                 //    Ref 0
    19.                 //    Comp Equal
    20.                 //    Pass IncrWrap
    21.                 //    Fail IncrWrap
    22.                 //}
    23.              
    24.                 CGPROGRAM
    25.                 #pragma vertex vert
    26.                 #pragma fragment frag
    27.  
    28.                 #include "UnityCG.cginc"
    29.              
    30.                 sampler2D _MainTex;
    31.                 float4 _MainTex_ST;
    32.              
    33.                 struct appdata
    34.                 {
    35.                     float4 vertex : POSITION;
    36.                 };
    37.  
    38.                 struct v2f
    39.                 {
    40.                     float4 vertex : SV_POSITION;
    41.                     float2 uv : TEXCOORD0;
    42.                     half4 color : COLOR0;
    43.                 };
    44.  
    45.                 v2f vert (appdata_full v)
    46.                 {
    47.                     v2f o;
    48.                     o.color = v.color;
    49.                     o.vertex = UnityObjectToClipPos(v.vertex);
    50.                     o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
    51.                     return o;
    52.                 }
    53.  
    54.                 fixed4 frag (v2f i) : SV_Target
    55.                 {
    56.                     fixed4 texcol = tex2D(_MainTex, i.uv) * i.color;
    57.                     return texcol;
    58.                 }
    59.              
    60.                 ENDCG
    61.             }
    62.         }
    63.     }
    64. }
    Any suggestions at all is appreciated, I'm not even sure if there is a shader solution that would allow overlap while getting rid of the artifacts produced by the trail. I've looked into other trail rendering solutions as well but haven't found much.
     
  2. richardkettlewell

    richardkettlewell

    Unity Technologies

    Joined:
    Sep 9, 2015
    Posts:
    2,281
    This is indeed a known problem with our line/trail renderers. We haven't found a robust solution to it yet.

    Disabling the "smooth corners" feature by setting the corner vertex count to zero can help, if you are using it.
    Increasing the minimum vertex distance can also help, but generally reduces, rather than eliminates the problem.

    Alternatively, you could perhaps render the trails as opaque geometry, to a render target with depth, and then compose the result onto your final target with a constant alpha value.

    The stencil approach of only writing the first transparent value might work too. I've not tried any of these workarounds, so I may only be of limited help.
     
    pranta15 likes this.
  3. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    There are usually two main solutions to this. The first one is to fade out the geometry when you're viewing it edge on, which works with Unity's built in trails, but may hide portions you want to keep visible. The other solution that some other engines use by default is to scale the width of the line down when viewed from edge on, but this is harder to do with Unity's trail renderer. Both solutions are basically the same idea though, hide the line when viewed from the edge to reduce situations where you get the "pinwheels" of geometry.
     
    richardkettlewell likes this.