当前位置:   article > 正文

UnityVR UI被3D物体遮挡_unity 3dui 遮挡

unity 3dui 遮挡

问题描述:

Unity2021版本,VR插件为XRIT,Canvas设置为ScreenSpace,渲染相机选了XR的主相机

UI在移动到某些位置还是会被某些3D物体挡住

网上看到有人说调小这个可以解决,不过我这里是没有解决的,我觉得应该用shader来解决

解决办法:

1、创建以下shader:

  1. Shader "UI/Overlay"
  2. {
  3. Properties
  4. {
  5. [PerRendererData] _MainTex ("Font Texture", 2D) = "white" {}
  6. _Color("Tint", Color) = (1,1,1,1)
  7. _StencilComp ("Stencil Comparison", Float) = 8
  8. _Stencil ("Stencil ID", Float) = 0
  9. _StencilOp ("Stencil Operation", Float) = 0
  10. _StencilWriteMask ("Stencil Write Mask", Float) = 255
  11. _StencilReadMask ("Stencil Read Mask", Float) = 255
  12. _ColorMask ("Color Mask", Float) = 15
  13. }
  14. SubShader
  15. {
  16. LOD 100
  17. Tags
  18. {
  19. "Queue" = "Transparent"
  20. "IgnoreProjector" = "True"
  21. "RenderType" = "Transparent"
  22. "PreviewType"="Plane"
  23. "CanUseSpriteAtlas" = "True"
  24. }
  25. Stencil
  26. {
  27. Ref [_Stencil]
  28. Comp [_StencilComp]
  29. Pass [_StencilOp]
  30. ReadMask [_StencilReadMask]
  31. WriteMask [_StencilWriteMask]
  32. }
  33. Cull Off
  34. Lighting Off
  35. ZWrite Off
  36. ZTest Always
  37. Offset -1, -1
  38. Blend SrcAlpha OneMinusSrcAlpha
  39. ColorMask [_ColorMask]
  40. Pass
  41. {
  42. CGPROGRAM
  43. #pragma vertex vert
  44. #pragma fragment frag
  45. #include "UnityCG.cginc"
  46. #include "UnityUI.cginc"
  47. struct appdata_t
  48. {
  49. float4 vertex : POSITION;
  50. float2 texcoord : TEXCOORD0;
  51. float4 color : COLOR;
  52. };
  53. struct v2f
  54. {
  55. float4 vertex : SV_POSITION;
  56. half2 texcoord : TEXCOORD0;
  57. fixed4 color : COLOR;
  58. };
  59. sampler2D _MainTex;
  60. float4 _MainTex_ST;
  61. fixed4 _Color;
  62. fixed4 _TextureSampleAdd;
  63. v2f vert (appdata_t v)
  64. {
  65. v2f o;
  66. o.vertex = UnityObjectToClipPos(v.vertex);
  67. o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
  68. o.color = v.color * _Color;
  69. #ifdef UNITY_HALF_TEXEL_OFFSET
  70. o.vertex.xy += (_ScreenParams.zw-1.0)*float2(-1,1);
  71. #endif
  72. return o;
  73. }
  74. fixed4 frag (v2f i) : SV_Target
  75. {
  76. fixed4 col = (tex2D(_MainTex, i.texcoord) + _TextureSampleAdd) * i.color;
  77. clip (col.a - 0.01);
  78. return col;
  79. }
  80. ENDCG
  81. }
  82. }
  83. }

2、创建一个材质球,选择这个Shader

3、挂载在需要全局显示的UI(这里指的是Text,Button之类的,带有Material属性的)上

即可完成UI不被3D物体遮挡的效果

后续优化:

部分人反应用了这个方法之后,在VR设备里只有一只眼睛能渲染出来

解决方法:

把渲染方式改成双眼(mult-pass),single那个是单眼渲染

转载链接如下,本人仅做了部分优化补充:

解决VR中UGUI world space UI会被其他物体遮挡的问题_unity 世界ui不会被模型遮挡-CSDN博客

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/708481
推荐阅读
相关标签
  

闽ICP备14008679号