Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

LineRenderer gradient not working properly

Discussion in 'General Graphics' started by jraami, Aug 14, 2018.

  1. jraami

    jraami

    Joined:
    Jun 27, 2017
    Posts:
    2
    Hello folks,
    I have a strange problem with gradient in a LineRenderer component. Independent of what I enter as the color and alpha keys, only 0.0f and 1.0f times seem to matter. With a multicolor, multi-alpha gradient, all I get is a weird off-set mix of the values of these two. With for example white, blue and red, mixed or blended, different shaders - nothing changes and the result is a white bar with some red in the other end.
    Line length - no effect.
    Sprite or particle shaders - no effect.
    Different Unity versions - no effect.
    Unity installs on a different laptop - no effect.
    Init from script or in editor - no effect.
    Build or in editor - no effect.

    In the example images, what is expected is gradient from full transparent to semi-opaque red to full transparent, and what you see in the Scene view is what I get.

    upload_2018-8-14_22-5-38.png

    upload_2018-8-14_22-14-11.png

    Please help if you can, this is driving me crazy. AND I am aware that it's very likely to be a stupid little thing that I am missing.
    Hours spent,
    sanity waning,
    Jaakko from Finland
     
  2. richardkettlewell

    richardkettlewell

    Unity Technologies

    Joined:
    Sep 9, 2015
    Posts:
    2,240
    It only samples the gradient at each vertex, not per pixel. Add more positions to the line to get a better approximation.
     
    SamuelE, samochreno and McGravity like this.
  3. jraami

    jraami

    Joined:
    Jun 27, 2017
    Posts:
    2
    Thank you Richard, this certainly clarified one crucial thing about the Unity line renderer!

    But a problem still remains with my gradient lines. The colors are blended even if I set the mode to Fixed. This is true with both sprite and particle shaders.

    See below:
    upload_2018-8-14_23-12-25.png

    Appreciate your help,
    J
     
  4. richardkettlewell

    richardkettlewell

    Unity Technologies

    Joined:
    Sep 9, 2015
    Posts:
    2,240
    Unfortunately the colors are blended between the vertices, regardless of whether you used fixed mode.

    I think the only way to get the behaviour you want is to make a texture, rather than use the gradient.
     
  5. highpockets

    highpockets

    Joined:
    Jan 17, 2013
    Posts:
    67
    I was struggling to get sudden color changes instead of blending with the LineRenderer recently and I found myself on this thread while trying to find answers, so I thought I would post my solution for getting the colors to have pretty much 0 blending (vertical ramp). My solution is for opaque lines, but can easily be adapted to include transparency. So this isn't an exact solution to the initial issue, but rather just the gradient/blending issue.

    Anyhow, I find it's quite limiting to not have the ability to tighten up the gradient blending. So I created a shader that samples a vertex color map texture at each vertex. This color map is created at runtime for dynamic control. I posted my solution here on stack overflow.

    Hopefully someone finds it useful.
     
    Last edited: Mar 13, 2023
    Sluggy likes this.