解决VR中UGUI world space UI会被其他物体遮挡的问题

转载自:解决VR中UGUI world space UI会被其他物体遮挡的问题
http://www.taidous.com/thread-41442-1-1.html
(出处: 【泰斗社区】-专注互联网游戏和应用的开发者平台)


在制作VR内容时,通常使用的都是UGUI,一般会将Canvas的render mode设置为world space,但是这样设置过后,因为UI是直接放在了场景里面,很容易被其他物体挡住,Unity官方的一个VR例子中给出了一个shader的解决方案,将如下的shader挂在材质上,并将这个材质赋予需要总是显示在所有物体前面的UI控件上,就可以解决这个问题。

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


===================================================分割===================================================

下面是自己的一点小小的思路,不利用shader

利用双摄像机,通过更改摄像机的层级达到所想要的效果

对于空间中的问题都能够基本实现,但是不知道为什么手柄会比较先进行渲染,导致手柄会遮挡


猜你喜欢

转载自blog.csdn.net/aila852/article/details/78193640
今日推荐