Search Unity

Question Why are Trail Renderer UVs distorted?

Discussion in 'General Graphics' started by GeriB, Sep 23, 2021.

  1. GeriB

    GeriB

    Joined:
    Apr 26, 2017
    Posts:
    193
    I've always had a lot of trouble making trails look good. What's going on with the distorted wavy UV's?
    Why is the mesh resolution so low no matter what parameters you touch?

    Is there any way of having accurate UVs and more mesh resolution?

    This are my current results:
    upload_2021-9-23_1-36-38.png

    It looks absolutely terrible in every possible way :(
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    The issue is GPUs render triangles, not quads.

    Why this matters is the mesh of a trail is made up of a bunch of triangles that are strung together, and when the UVs are interpolated over the surface of the triangle, it only knows about the 3 vertices of the current triangle and linearly interpolate the UVs across that.

    Search for affine texture mapping and you'll find lots of examples. You'll also probably find lots of stuff saying the fix is perspective correction. And it can be, in very specific situations ... ones GPUs already handle. It's why textures on walls / floors / etc don't look weird on anything more recent than the original PlayStation, which is basically the last hardware triangle renderer to not use perspective correction by default. The real solution is quadrilateral interpolation, and that's much, much harder and expensive to do. Basically you can't do it with Unity's built in trails.

    The other more realistic solution is to never scale the trail. Instead you scale the UVs in the fragment shader. This won't fix it 100%, especially for trails that are going towards or away from you and you can see the "spin", there's no solution to that problem apart from hiding the trail. But it will fix about 90% of the rest of it.
     
  3. richardkettlewell

    richardkettlewell

    Unity Technologies

    Joined:
    Sep 9, 2015
    Posts:
    2,285
    I see quite a few posts about this, and I wonder if doubling the triangle count across the width of the trail would fix this.
     
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    Increasing the number of vertices reduces artifacts from UV interpolation. But it increases artifacts caused by overlapping triangles when the emitter isn’t moving in straight lines. Any “bends” will cause fan artifacts.

    It’s kind of the unending problem with trails that the solution for one artifact makes another, equally bad artifact worse.
     
  5. richardkettlewell

    richardkettlewell

    Unity Technologies

    Joined:
    Sep 9, 2015
    Posts:
    2,285
    But I mean increasing the vert count across the width of the trail. That shouldn’t affect overlapping artifacts should it? And it means there will be a vertex on the 0.5 u coordinate, which should help with the mapping quality when the width varies.
     
    bgolus and AcidArrow like this.
  6. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    Ah, yes. That is something that can help. Similar to how higher tessellation was used to help the lack of perspective correction on the PS1. I do know some other engines use at least an extra center line of vertices to try to help. But until you get down to 1 triangle per pixel there'll always be some artifacts.
     
    richardkettlewell likes this.
  7. Torbach78

    Torbach78

    Joined:
    Aug 10, 2013
    Posts:
    296
    I wonder a variable on line/trail renderers to assign #spans would be enough to help 90% of this. user discretion about vertex count.

    I still imagine tight arc's create problems without a proxy catmul cage interpolator - having used procedural splines in the past they are very much a missed feature for artists ; spriteshape renderer has features to enjoy in this regard but it is still not enough for 3D
     
    Last edited: Sep 27, 2021
  8. richardkettlewell

    richardkettlewell

    Unity Technologies

    Joined:
    Sep 9, 2015
    Posts:
    2,285
    It's an interesting idea. I was just thinking of adding a centre line of vertices, (and maybe only if the width wasn't constant).

    Do you think spans for width *and* length would be useful? Or maybe just my idea (centre line) for the width, and your idea (spans setting) for the length?
     
  9. Torbach78

    Torbach78

    Joined:
    Aug 10, 2013
    Posts:
    296
    I'm not 100% on the tech, but tessellation reduces common egregious mapping issues; VFX artists tessellate curves/arcs to ensure something like lightning or energy-beams bend smooth-'enough' (adding 2...3 even 4 length spans is common. It helps noticeably)
     
    richardkettlewell likes this.
  10. GeriB

    GeriB

    Joined:
    Apr 26, 2017
    Posts:
    193
    Thank you very much Ben. Super useful and concise information as always. All your knowledge spread around the forums really makes a difference, you have no idea how much me and many others have learnt from it! :D

    Quadrilateral Interpolation being out of reach I went for UV scaling. With a custom gradient shader property drawer I made it so that a greyscale value decides the trail width. It's like the Unity built in width graph but useful.

    The fan shape trail is still a problem but it can be improved by setting the trail to have a fixed rotation. As you say you can't solve all the problems, you need to settle and compromise depending on the project needs. Trails artifacts are like the greek mythology hydra heads, cut one head off and two more appear

    Will leave the hydra alone for now and settle on the smooth uv scaled trails. Thanks again!
     
  11. richardkettlewell

    richardkettlewell

    Unity Technologies

    Joined:
    Sep 9, 2015
    Posts:
    2,285
    Hey all. I prototyped adding a center line of verts along the line, but it didn’t really help. Skinny triangles still mess up the overall mapping when the line width varies.

    Really, it needs something like this: https://www.reedbeta.com/blog/quadrilateral-interpolation-part-2/ but we don’t require bespoke shaders, so can’t really offer shader-based solutions out of the box.
     
  12. Torbach78

    Torbach78

    Joined:
    Aug 10, 2013
    Posts:
    296
    I think most of us know it's not a 100% fix for procedural geo

    • We continue to use software to author an FBX for a simple tapering line or Arc because artists struggle with the Trail** Line renderer as-is
    • Artists are tech-blocked by our limited maths skillset without software that executes it with a check box
    • A variable dividing N spans along the Line & Trails is still immensely helpful for existing and new users

    **(and the bilinear implementation still wouldn't address UV construction desires; Leaving something like tire tracks in worldSpace)
     
    richardkettlewell likes this.