Search Unity

How to change the color of the line based on object position in it?

Discussion in 'Scripting' started by kaitekinafune, Apr 12, 2021.

  1. kaitekinafune

    kaitekinafune

    Joined:
    Jan 28, 2021
    Posts:
    2
    I'm trying to recreate this effect:
    . I've tried a couple of things, but failed miserably. I always get the feeling that I'm overcomplicating things that can be done easily, so I thought I'd ask if there's a simpler solution, if there isn't one, what direction should I go?
    I used Sebastian Lague's Path Creator Tool
    to create curved mesh and set its points to draw a line with a line renderer. I want to move object along the path and change the color of the "road" beneath him.

    What I have so far is the line renderer, which looks horrible imo.
    Code (CSharp):
    1.             Vector3[] points = pathCreator.path.localPoints;
    2.             lineRenderer.positionCount = points.Length;
    3.  
    4.             lineRenderer.startColor = player.GetComponent<MeshRenderer>().material.color;
    5.             lineRenderer.endColor = player.GetComponent<MeshRenderer>().material.color;
    6.  
    7.             lineRenderer.SetPositions(points);

    I don't really like the looks of it and I was thinking if I could swap line renderer for a different mesh and change the color of it by splitting two textures at some point on the mesh itself(based on the object position in it).
     
  2. exiguous

    exiguous

    Joined:
    Nov 21, 2010
    Posts:
    1,749
    There is the Vectrosity (paid) Asset in the Asset Store which is better suited for line drawing than built in LineRenderer. There is also a support thread in the Asset subforum. There you could ask the author (Eric Haines) wether it can achieve that effect.

    If you want to go the "do it yourself" route, the mesh should be fine grained enough that the distance between two vertices along the route is smaller than the cube so you can hide the color switching behind it. You could either use a shader with vertex colors and switch them as you travel along, or you could have a texture with several colors and switch the uvs of the vertices. If you build the mesh in a certain pattern you just need to "increment" the vertices which you need to change.

    There is probably also a (compute) shader solution but I don't have too much experience with them. You could calcuate the traveled path as a fraction of the whole track (0-1) and pass that into the shader.

    Or you could have 2 meshes and the grey one get its vertices gradually removed.

    So the possibilities are plenty.

    When in doubt ask the author of the video how he/she has done it.
     
    kaitekinafune likes this.
  3. kaitekinafune

    kaitekinafune

    Joined:
    Jan 28, 2021
    Posts:
    2
    Thank you for the answer.
    The tool that I'm using lets me get the world point at some distance on the path, can I use this point to set the UV's?
    Also I will definately try to remove vertices of the grey mesh.