Can I bring some attention to this 2015 shader question?

http://answers.unity3d.com/questions/1093244/simple-shader-question-on-finding-vertex-relative.html

It is almost exactly what I need but it has no answers…

Some possible solutions I thought of:

  1. Pass the height of the object to the material via C#. I’d like to avoid this…

  2. Have two shader passes, the first one for the sole purpose of storing somewhere the highest vertex.y passed to the vert function (vertex being POSITION). Then actually paint the object in the seond pass, and use the previously stored value as the height. Could work but apparently it’s not easy to share data between passes.

It’d be fairly ridiculous if there is no easy way to achieve this…

Any help is appreciated.

  1. Totally works, but you’d have to deal with per object scale and possibly orientation.

  2. Not possible, you can’t pass data between shader passes like that.

  3. Use an additional UV channel to store the relative position. In the program you use to model your objects just apply a planar UV projection that’s aligned to your objects bounds and set it to be UV2 (or UV4 if you need lightmaps). Then access that in the shader.

Hey @bgolus , thanks for your answer.

  1. Yep.

  2. This thread claims otherwise, but I don’t know how to render to a texture in a shader and some of the links are broken. Nevertheless, I’d rather not do that as it’d be a rather hacky and probably slow solution.

  3. Yeeeah, here’s the thing though…: I want to do this for a sprite. And I guess I could pull like a gradient? kind of thing? and use that, but… I’d rather not.

That thread is talking about a fast more complex process than you’re likely thinking. Some people do abuse the grab pass for this, but that’s really slow. Plus if you’re doing this with a sprite you’d need to know the relative position to begin with to output that, and if you have it you wouldn’t need two passes.

Unfortunately the way Unity’s sprites work there really is no way to do what you want, unless you don’t use the sprite system at all, or use option #1.

Yeah, that seems to be the case. I guess I’ll be sticking to option 1 then.