Search Unity

Change shader when the line collides

Discussion in 'Shaders' started by MPForte, May 15, 2019.

  1. MPForte

    MPForte

    Joined:
    Aug 28, 2018
    Posts:
    4
    Hello!!

    I would like to be able to do something similar:



    I am drawing a line with the mouse.
    I'd like that only the segment of the line that collides changes colour. I'd prefer to avoid to create different lines to solve this issue but I have no experience with shaders. May someone help me?


    Ps. Already tried:
    - The classical
    "line.startColor = Color.green;
    line.endColor = Color.green;"
    changes the colour of the entire line when the line collides with the cube, while I would like to change only the colour of the segment that is colliding.
    - Also changing shader does not work since the new shader is applied to the entire line as well.
     
    Last edited: May 15, 2019
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    Unity's line renderer doesn't really have a lot of good options for changing the color mid-line. The easiest option really is to make a new line.

    The more complex setup would be to use the color gradient, but you'd have to update this every frame as the gradient is applied to the normalized length. It also means how sharply the color changes depends on your point density, and how closely you can calculate the appropriate gradient key positions compared to how Unity is doing it internally (which is a total black box).

    Shader based setups would likely rely on the line's UVs, which would have a lot of the same problems as updating the gradient would have in that you'd have to make a best guess calculation for where the color should change along the UVs and pass that information to the shader somehow, likely using very similar kinds of data as the gradient method.

    However since I can't see the image you attempted to attach, I can't comment on other options.
     
  3. MPForte

    MPForte

    Joined:
    Aug 28, 2018
    Posts:
    4
    Hi bgolus,

    thank you a lot for your explanation.
    Now you should be able to see the image. If by seeing the image, something new comes to your mind, please let me know. Otherwise, I'll create multiple lines.
    Thanks again for your help!!!
     

    Attached Files:

  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    If you can guarantee the line actually intersects the mesh, or more specifically is further from the camera than the mesh, and only objects that you want the line to change color when "colliding with" will be that close, then you could use a two pass shader on the line. One using the usual settings you get from something like an unlit shader, the other using ZTest Greater (instead of the default ZTest LEqual that's used if it's not specified). That'll mean if the line is further from the camera than another opaque object, it'll draw that pass.
     
  5. MPForte

    MPForte

    Joined:
    Aug 28, 2018
    Posts:
    4
    Thank you!! I can guarantee that the line intersects. I'll look in how to deal with your proposed idea! Thank you again!