VR的左右眼渲染方法
VR的左右眼视频渲染shader
unity_StereoEyeIndex 结点可以判断当前渲染的时候左眼还是右眼,所以可以通过着色器来更根据当前眼睛使用不同的渲染方式达到左右眼渲染不同。
Shader "Unlit/VRVideoPlay"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
[KeywordEnum(None, Top_Bottom, Left_Right, Custom_UV)] Stereo ("Stereo Mode", Float) = 0
[KeywordEnum(None, Left, Right)] ForceEye ("Force Eye Mode", Float) = 0
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile MONOSCOPIC STEREO_TOP_BOTTOM STEREO_LEFT_RIGHT STEREO_CUSTOM_UV
#pragma multi_compile_local FORCEEYE_NONE FORCEEYE_LEFT FORCEEYE_RIGHT
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
sampler2D _MainTex;
float4 _MainTex_ST;
float2 SetVR_UV(float2 UV)
{
#if FORCEEYE_NONE
// 左右采样
#if STEREO_LEFT_RIGHT
if (unity_StereoEyeIndex == 0)
{
return float2(UV.x / 2, UV.y);
}
else
{
return float2(UV.x / 2 + 0.5, UV.y);
}
#endif
// 上下采样
#if STEREO_TOP_BOTTOM
if (unity_StereoEyeIndex == 0)
{
return float2(UV.x, UV.y / 2);
}
else
{
return float2(UV.x, UV.y / 2 + 0.5);
}
#endif
#elif FORCEEYE_LEFT
// 左右采样
#if STEREO_LEFT_RIGHT
return float2(UV.x / 2, UV.y);
#endif
// 上下采样
#if STEREO_TOP_BOTTOM
return float2(UV.x, UV.y / 2);
#endif
#elif FORCEEYE_RIGHT
// 左右采样
#if STEREO_LEFT_RIGHT
return float2(UV.x / 2 + 0.5, UV.y);
#endif
// 上下采样
#if STEREO_TOP_BOTTOM
return float2(UV.x, UV.y / 2 + 0.5);
#endif
#endif
return UV;
}
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
float2 uv = TRANSFORM_TEX(v.uv, _MainTex);
o.uv=SetVR_UV(uv);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
// sample the texture
fixed4 col = tex2D(_MainTex, i.uv);
return col;
}
ENDCG
}
}
}