VR的左右眼渲染方法

VR的左右眼视频渲染shader

unity_StereoEyeIndex 结点可以判断当前渲染的时候左眼还是右眼,所以可以通过着色器来更根据当前眼睛使用不同的渲染方式达到左右眼渲染不同。

c 复制代码
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
        }
    }
}
相关推荐
sensen_kiss6 小时前
CPT306 Principles of Computer Games Design 电脑游戏设计原理 Pt.5 VR 游戏
学习·游戏·vr
大囚长7 小时前
AI技术对VR发展的核心促进作用
人工智能·vr
星幻元宇VR8 小时前
VR摩托车|科技赋能交通安全教育新模式
科技·学习·安全·vr·虚拟现实
星幻元宇VR1 天前
VR航空航天学习机|一场触手可及的太空之旅
科技·学习·安全·vr·虚拟现实
星幻元宇VR1 天前
VR自行车|开启沉浸式交通安全新体验
科技·学习·安全·vr·虚拟现实
星幻元宇VR2 天前
VR科普蛋椅|打造沉浸式科普教育新体验
科技·安全·vr
星幻元宇VR2 天前
VR航空航天学习机|让航天梦想在虚拟现实中起航
科技·学习·安全·生活·vr
CV实验室4 天前
Meta引爆3D革命!SAM 3D 发布:单张图秒建3D模型,AR/VR、游戏圈炸锅!
计算机视觉·3d·meta·ar·vr
星幻元宇VR5 天前
VR安全带防坠落体验平台|强化高空作业安全教育的新途径
科技·学习·安全·生活·vr