Non static occludee hidden by occluder
Hi,
On a VR project, I had some GUI I wanted to be drawn over everything, no matter what, but since I had to use World Space canvas, the best I came up with was to add a material to all my drawn GUI element using a shader to have it drawn over everything (I don't fully understand the shader, I found it on the internet while looking for means to draw world space GUI over everything).
I've had no problems with this technique, actually, until now, it has worked wonders. But then I tried mixing that with occlusion areas and, somehow, my GUI gets hidden behind occluders.
Of course, neither my GUI nor any of it's parents, nor children are marked as occludee static, so I don't understand why it disappears. I did check that it was an occlusion problem by checking that the item was still in the scene, but when going into the "visualisation" tab of the occlusion window, it is hidden.
Any hint on what I might be doing wrong? Here is the code of the shader :
Shader "UI/MenuOverEverything"
{
Properties
{
[PerRendererData] _MainTex ("Sprite Texture", 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
}
SubShader
{
Tags
{
"Queue"="Overlay+1"
"IgnoreProjector"="True"
"RenderType"="Transparent"
"PreviewType"="Plane"
"CanUseSpriteAtlas"="True"
}
Stencil
{
Ref [_Stencil]
Comp [_StencilComp]
Pass [_StencilOp]
ReadMask [_StencilReadMask]
WriteMask [_StencilWriteMask]
}
Cull Off
Lighting Off
ZWrite Off
ZTest Off
Blend SrcAlpha OneMinusSrcAlpha
ColorMask [_ColorMask]
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata_t
{
float4 vertex : POSITION;
float4 color : COLOR;
float2 texcoord : TEXCOORD0;
};
struct v2f
{
float4 vertex : SV_POSITION;
fixed4 color : COLOR;
half2 texcoord : TEXCOORD0;
};
fixed4 _Color;
fixed4 _TextureSampleAdd; //Added for font color support
v2f vert(appdata_t IN)
{
v2f OUT;
OUT.vertex = mul(UNITY_MATRIX_MVP, IN.vertex);
OUT.texcoord = IN.texcoord;
#ifdef UNITY_HALF_TEXEL_OFFSET
OUT.vertex.xy += (_ScreenParams.zw-1.0)*float2(-1,1);
#endif
OUT.color = IN.color * _Color;
return OUT;
}
sampler2D _MainTex;
fixed4 frag(v2f IN) : SV_Target
{
half4 color = (tex2D(_MainTex, IN.texcoord) + _TextureSampleAdd) * IN.color; //Added for font color support
clip (color.a - 0.01);
return color;
}
ENDCG
}
}
}
Note : I'm currently using Unity 5.4.2f2
Can someone at least confirm that occluders are supposed to hide occludees only?
Answer by hexagonius · Jan 28, 2017 at 01:44 PM
If your UI always stays infront of everything, create a second camera, set it's depth higher than the game cam and let it render the UI only. This way it has nothing ever than can occlude it.
EDIT : never$$anonymous$$d what was written here before (if you read it)
This works great ! Just a heads up for other people that might come around here, this is how you should configure the second camera :
DO NOT make it a child of the first camera (it would apply twice your VR headset rotation to the second camera)
Set "Clear Flags" to "Depth Only"
Set the culling mask to your UI layer
Reduce the clipping planes as much as possible for optimization
Set the "Depth" to a value higher than the Depth of the first camera
Uncheck "occlusion culling" (although you are not culling other layers with this camera, you wouldn't see your UI through occluding elements)
The other parameters should be the same than the parameters of your first camera.
Also, don't forget to remove the scripts attached to this camera other than the Camera script itself unless you are sure it makes sense for this script to be on your two cameras.