赞
踩
Unity2021版本,VR插件为XRIT,Canvas设置为ScreenSpace,渲染相机选了XR的主相机
UI在移动到某些位置还是会被某些3D物体挡住
网上看到有人说调小这个可以解决,不过我这里是没有解决的,我觉得应该用shader来解决
1、创建以下shader:
- Shader "UI/Overlay"
- {
- Properties
- {
- [PerRendererData] _MainTex ("Font 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
- {
- LOD 100
-
- Tags
- {
- "Queue" = "Transparent"
- "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 Always
- Offset -1, -1
- Blend SrcAlpha OneMinusSrcAlpha
- ColorMask [_ColorMask]
-
- Pass
- {
- CGPROGRAM
- #pragma vertex vert
- #pragma fragment frag
- #include "UnityCG.cginc"
- #include "UnityUI.cginc"
-
- struct appdata_t
- {
- float4 vertex : POSITION;
- float2 texcoord : TEXCOORD0;
- float4 color : COLOR;
- };
-
- struct v2f
- {
- float4 vertex : SV_POSITION;
- half2 texcoord : TEXCOORD0;
- fixed4 color : COLOR;
- };
-
- sampler2D _MainTex;
- float4 _MainTex_ST;
- fixed4 _Color;
- fixed4 _TextureSampleAdd;
-
- v2f vert (appdata_t v)
- {
- v2f o;
- o.vertex = UnityObjectToClipPos(v.vertex);
- o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
- o.color = v.color * _Color;
- #ifdef UNITY_HALF_TEXEL_OFFSET
- o.vertex.xy += (_ScreenParams.zw-1.0)*float2(-1,1);
- #endif
-
- return o;
- }
-
- fixed4 frag (v2f i) : SV_Target
- {
- fixed4 col = (tex2D(_MainTex, i.texcoord) + _TextureSampleAdd) * i.color;
- clip (col.a - 0.01);
- return col;
- }
- ENDCG
- }
- }
- }

2、创建一个材质球,选择这个Shader
3、挂载在需要全局显示的UI(这里指的是Text,Button之类的,带有Material属性的)上
即可完成UI不被3D物体遮挡的效果
部分人反应用了这个方法之后,在VR设备里只有一只眼睛能渲染出来
解决方法:
把渲染方式改成双眼(mult-pass),single那个是单眼渲染
转载链接如下,本人仅做了部分优化补充:
解决VR中UGUI world space UI会被其他物体遮挡的问题_unity 世界ui不会被模型遮挡-CSDN博客
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。