Hello everyone!
Sorry for reaching out about a topic that was discussed many times. I’m obviously not great with shaders, but I’ve studied all the major threads throughout, I promise. My problem is slightly specific though… I’m not gonna bother you with the broader problem I’m trying to solve and will present just the basic roadblock I’ve bumped into.
In the most simple words possible, I’m attempting to display only the intersection of an object and a plane, not including half of the cut object. There I’m all set with no problems:
I use this sort of simple, rather primitive fragment shader:
half4 frag(v2f i, float facing : VFACE) : SV_Target
{
half4 customColour = half4(0,0,0,1);
float dotProd = dot(i.worldPos.xyz - _PlanePosition.xyz, _PlaneNormal.xyz);
clip(-dotProd);
if(facing < 0){
customColour = _InnerColour;
}
return customColour;
}
Now, what I’m trying to do is to get rid of the black parts (front faces) and only leave the pink bits (back faces). When I try to set the front face colour to fully transparent (in the video below I keep the alpha to 0.35, just for reference), some of the back faces are fully visible:
half4 frag(v2f i, float facing : VFACE) : SV_Target
{
half4 customColour = half4(0,0,0,0.35);
float dotProd = dot(i.worldPos.xyz - _PlanePosition.xyz, _PlaneNormal.xyz);
clip(-dotProd);
if(facing < 0){
customColour = _InnerColour;
}
return customColour;
}
As you can see in the attached video, weirdly at some rotations it works as it should (ignoring the 0.35 alpha for reference). For most of you pros this behaviour is probably expected, but I really couldn’t understand why that happens. I do understand that all backfaces are drawn as I’ve set the Cull to Off, however I don’t understand why some back faces are drawn and some are not. So my questions are:
- Why does this happen?
- Is it possible to bypass it? Could it be just a wrong set of shader commands?
- Is there a way to achieve such effect in one shader (working in URP so also in a single pass)? I plan to later use this effects in render features for multiple objects at once so achieving this effect in a single shader would be brilliant.
- If such solution is not available, is it possible to use the front faces to generate a stencil, later applying it on the back faces?
Any tips or references for similar problems would be much appreciated. I’ve read and tried many things how to solve the more complex effect I’m working towards, this is a dumb first roadblock I’ve hit… If necessary, I’ll of course happily depict the larger goal of mine, but I don’t want to bother you with it for now.
Thank you very much for any help! Have a great day!
Cheers,
Vojta