Search Unity

Mask intersection between Line Renderer and Sprite

Discussion in 'Shaders' started by Ennaxor, Sep 25, 2018.

  1. Ennaxor

    Ennaxor

    Joined:
    Sep 2, 2013
    Posts:
    5
    Hi there!

    I'm having an issue I'm not quite sure how to resolve, so I decided to post it over here.

    I'm working with Line Renderers and Sprites (using tk2d), each one of them having their own materials and colliders. What I need is to find the way to hide the parts of the Line Renderer that aren't in contact with the Sprite, meaning that the Sprite has to stay just like it is and the LineRenderer must show its material only on the segments that are colliding with the Sprite (the remaining parts must have their alpha set to 0).

    In the following images you'll see what kind of materials they currently have:


    Sprite

    Line Renderer​

    My case applied in images:
    The red cone is the sprite that is a trigger and the line renderer is the gray spline that is created between the two green dots.


    The collider of the sprite occupies the entire red area that I show in the following image. The clearest blue on the line renderer is the segment that MUST have the desired material visible, while the darkest blue is the part which should be transparent.



    Just like this:



    I haven't found many answers to a situation like mine. I think it's likely that the solution is going to be Shaders, but I have almost no knowledge on them. I'm guessing I must have two different shaders, one of them acting as a mask but... How do I apply this with Line Renderers?

    Thanks a lot for your help in advance! I hope I've explained enough my case.
     
  2. Pakillottk

    Pakillottk

    Joined:
    Dec 30, 2012
    Posts:
    11
    Hi, I've just answered to a post with a similar problem. My suggestion is that you draw the red cone sprite in a RenderTexture behind the scenes with a flat color (for example pure red). Then you'll have to write a shader that sample that RenderTexture and checks if the current pixel it's marked with the cone color. If it is then you have your intersection.