I am quite new to shaders in unity and have a very basic understanding.
I have a foreground image on the UI Layer that covers the whole scene. How can I cut through this foreground image and show a small part of the 3D worldspace behind? I need to make multiple holes dynamically and not just a simple static hole.
I have read that I need two shaders, one for a mask (x-ray) and one for the mask target. I thought it would be a good idea to take a lightweight shader for a base, so I took an Unlit Transparent shader.
Shader "Custom/HoleShader"
{
Properties
{
_MainTex ("Base (RGB), Alpha (A)", 2D) = "white" {}
_Color ("Tint", Color) = (1,1,1,1)
_StencilComp ("Stencil Comparison", Float) = 8
_Stencil ("Stencil ID", Float) = 0
_StencilOp ("Stencil Operation", Float) = 0
_StencilWriteMask ("Stencil Write Mask", Float) = 255
_StencilReadMask ("Stencil Read Mask", Float) = 255
_ColorMask ("Color Mask", Float) = 15
[Toggle(UNITY_UI_ALPHACLIP)] _UseUIAlphaClip ("Use Alpha Clip", Float) = 0
}
SubShader
{
Tags { "Queue"="Geometry-1" }
ColorMask 0
ZWrite Off
Stencil {
Ref 1
Comp always
Pass replace
}
}
FallBack "UI/Default"
}
Then I created another shader for the mask target (in my case for the UI image on the UI Layer and for 3D elements in the worldspace space that are not supposed to be seen through the hole/window).
Shader "Custom/HoleTargetShader"
{
Properties
{
_MainTex ("Base (RGB), Alpha (A)", 2D) = "white" {}
_Color ("Tint", Color) = (1,1,1,1)
_StencilComp ("Stencil Comparison", Float) = 8
_Stencil ("Stencil ID", Float) = 0
_StencilOp ("Stencil Operation", Float) = 0
_StencilWriteMask ("Stencil Write Mask", Float) = 255
_StencilReadMask ("Stencil Read Mask", Float) = 255
_ColorMask ("Color Mask", Float) = 15
[Toggle(UNITY_UI_ALPHACLIP)] _UseUIAlphaClip ("Use Alpha Clip", Float) = 0
}
SubShader
{
Tags { "Queue"="Geometry" }
Stencil {
Ref 1
Comp notequal
Pass keep
}
}
FallBack "UI/Default"
}
Would be very thankful for any help!