Local vertex position in opacity shader chunk

I have done some shaders in PC using the shader chunk system and i think it’s really easy to add just one or two features without rewriting the whole code.
but i find myself searching for a detailed overview of variables over and over again. And today i am again at the point where i simply want to use the model vertex postion in the opacity chunk.
I have a cylinder-like object and i want to “open” it by a certain degree. So the basic idea is to use a parameter for the minimum angle to be displayed. And then calculate the angle by the atan() in the shader and handle the opacity accordingly.

But i cannot find a variable which makes the local position available in the opacity chunk. I only found the vPositionW. It would be no problem to override a vertex chunk as well to pass it in my own var, if only i could find the correct vertex chunk :wink: So, any hints in that direction would be great.

Find the project here:
https://playcanvas.com/project/901361

So far i use the global position, which of course only works, as long as the model stays at 0,0,0 and is not tilted :slight_smile: but you can get the idea.

thx in advance

Hi @ynt_holger,

What I would do is as you say override the following vertex chunk (transformVS):

And add the localPos variable to a varying attribute. That varying attribute will be passed then to the pixel shader, where you can intercept and use it.

2 Likes

thanks @Leonidas

i knew it must be that easy somehow :grinning:

I added the varying vec3 vPositionL and voilà…

1 Like

Nice! Though now that I think of it may be too much to override such a big chunk since local position it’s just the base attribute vertex_position (no processing is done to it).

You could do it easily in this chunk:

// example code not tested
varying vec3 vPositionL;

void main(void) {
    vPositionL = vertex_position;
    gl_Position = getPosition();
1 Like

Thx, I will try that too.
I just copied the whole transformVS code and only added the same two lines you mentioned and that did the trick. Would there be a performance benefit when adding this in the startVS?

Thinking about it, it’s more likely that the transformVS will change over time than the startVS, i guess, so to be safer for future engine compability, i would prefer adding it to the smallest chunk possible.

few minutes later…
Ok, works fine, too. I’d prefer that way.

Thx again!

2 Likes

Yes, exactly not a performance benefit, but makes maintenance much easier.

1 Like