How to use build in Unity mask with the shader (Rect Mask etc).
dirindon opened this issue · 3 comments
I was playing around with the shader in Unity, and I am wondering how could I apply mask to it (I do understand that shader would probably need to be edited for that to work but I couldn't find out how to do it).
My test scenario (2d Scene) :
I have UI object 1080x1080, and inside another object thath ave camera feed casted on the rawImage on it (1080x1920), I wish for that object to be masked, and only have 1080x1080 visible on the screen while still having the green screen efect working. Would it be possible to get your help with it?
I managed to work around this with one of the ugliest code you might have seen, but it satisify my needs (this is just a fragment of the code) :
if (_UseClipRect && UnityGet2DClipping(IN.worldPosition.xy, _ClipRect) == 0) { half4 color = (tex2D(_MainTex, IN.uv) + _TextureSampleAdd); color.a *= UnityGet2DClipping(IN.worldPosition.xy, _ClipRect); #ifdef UNITY_UI_ALPHACLIP clip (color.a - 0.001); #endif return color; }
- I can't seem to be able to paste code here, to make it look at least quite decent this was best I could achieve in my short break time.
Basically I added if statement to detect if this pixel should be visible and make it invisible, if it should be visible I let your code do all other calculations, that was done inside " float4 frag(v2f IN) : SV_Target" as well as couple of other minor things to make this work.
Glad that this approach worked for you!