P-ratt
P-ratt

Reputation: 401

Unity LineRenderer Gradient doesn't work with custom colors

There are quite a few results of "LineRenderer color not working", but my issue isn't with it coming out purple like the rest of the world.

Creating a line from scratch I have the following code:

private void AddLineRendererColors(LineRenderer renderer)
{
    renderer.material = lineMaterial;
    renderer.useWorldSpace = true;
    renderer.startColor = new Color(172, 242, 245, 255);
    renderer.endColor = new Color(255, 0, 0, 70);
}

It's worth noting that I created an empty game object from scratch in code and added the LineRenderer script to it before passing it to the function.

This displays the red as the end color, but without the defined alpha. "startColor" just turns out white. If I use a color like "Color.green" or something it works fine.

I've also attempted to use this code, but this just makes the entire line white:

    Gradient gradient = new Gradient();
    GradientColorKey startGradient = new GradientColorKey(new Color(172, 242, 245, 255), 0);
    GradientColorKey endGradient = new GradientColorKey(new Color(255, 0, 0, 70), 1);
    gradient.colorKeys = new GradientColorKey[] { startGradient, endGradient };
    renderer.colorGradient = gradient;

I tried various different types of googling to see if I could get different results, but with no success. If someone else finds a result that resolves my problem you have my permission to call me a googling noob and berate me for insubordination.

Upvotes: 0

Views: 1983

Answers (1)

TEEBQNE
TEEBQNE

Reputation: 6275

The issue you are facing is that the struct Color expects a float value ranging from 0.0 -> 1.0 which is a quotient generally of some number 0 -> 255 over the denominator 255. If you instead swap your code color to be new Color(172/255f, 242/255f, 245/255f) it should work. I am uncertain if you are able to set both color and alpha in one go with LineRenderers. In my attached snippet, I removed the chance of an issue like this happening by serializing the colors and setting them myself in the inspector.

[RequireComponent(typeof(LineRenderer))]
public class TestScript : MonoBehaviour
{
    [SerializeField] private LineRenderer lr = null;
    [SerializeField] private Color start;
    [SerializeField] private Color end;

    private void Awake()
    {
        if (lr == null)
            lr.GetComponent<LineRenderer>();

        lr.material = new Material(Shader.Find("Sprites/Default"));
        Vector3[] positions = new Vector3[3];
        positions[0] = new Vector3(-2.0f, -2.0f, 0.0f);
        positions[1] = new Vector3(0.0f, 2.0f, 0.0f);
        positions[2] = new Vector3(2.0f, -2.0f, 0.0f);
        lr.positionCount = positions.Length;
        lr.SetPositions(positions);

        AddLineRendererColors(lr);
    }

    private void AddLineRendererColors(LineRenderer renderer)
    {
        Gradient newGradient = new Gradient();
        newGradient.SetKeys(
            new GradientColorKey[] { new GradientColorKey(start, 0.0f), new GradientColorKey(end,1.0f) },
            new GradientAlphaKey[] { new GradientAlphaKey(1.0f, 0.0f), new GradientAlphaKey(70/255f, 1.0f) });

        renderer.useWorldSpace = true;
        renderer.colorGradient = newGradient; 
    }
}

Here's an example of the above snippet working. I also decided to add some points along the line renderer to display the gradient in a more obvious way.

The reason the 255 works for red is the color red is denoted by (1.0, 0.0, 0.0) and I believe the Color struct clamps your value between 0.0 and 1.0, resulting in 1.0, or the expected red color. Example

Edit: Also for completeness, I tested both of your provided snippets with the corrected color values, and neither produce an alpha in the gradient. You will need to use a gradient and set the GradientAlphaKey as I have in my example.

Upvotes: 1

Related Questions