You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity中如何裁剪相机渲染区域以显示另一相机画面(VR场景)

解决方案:Unity默认管线实现VR场景的圆形区域画面切换效果

一、基础配置(双相机+RenderTexture)

  • 创建RenderTexture:右键菜单Create > Render Texture,设置分辨率为VR设备单眼分辨率(如1920×1080),Anti-aliasing匹配项目设置,Depth Buffer设为24位。
  • 配置主相机:
    • 调整Culling Mask,排除副相机专属层(新建SecondaryLayer,主相机不勾选该层)。
    • 确保项目已启用VR支持(Edit > Project Settings > XR Plug-in Management)。
  • 配置副相机:
    • Culling Mask仅勾选SecondaryLayer,将需要透过圆形显示的物体移至该层。
    • Target Texture设为刚创建的RenderTexture。
    • 将副相机设为主相机子物体,同步位置与旋转,避免画面错位;关闭副相机的Audio Listener(主相机保留一个即可)。

二、自定义Shader实现圆形混合逻辑

以下是适配默认管线的Unlit Shader,可用于后处理或全屏Quad,实现圆形区域显示副相机画面、其余区域显示主相机画面的效果:

Shader "Custom/CircleRenderTextureBlend"
{
    Properties
    {
        _MainTex ("主相机纹理", 2D) = "white" {}
        _SecondaryTex ("副相机纹理", 2D) = "white" {}
        _CircleCenter ("圆形中心(UV)", Vector) = (0.5, 0.5, 0, 0)
        _CircleRadius ("圆形半径", Range(0, 0.5)) = 0.2
        _EdgeSmoothness ("边缘平滑度", Range(0, 0.1)) = 0.02
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" "Queue"="Overlay" }
        LOD 100

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 vertex : SV_POSITION;
            };

            sampler2D _MainTex;
            sampler2D _SecondaryTex;
            float2 _CircleCenter;
            float _CircleRadius;
            float _EdgeSmoothness;

            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = v.uv;
                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
                // 计算当前UV到圆心的距离
                float dist = distance(i.uv, _CircleCenter);
                // 计算平滑混合因子
                float blend = smoothstep(_CircleRadius + _EdgeSmoothness, _CircleRadius - _EdgeSmoothness, dist);
                
                // 采样双纹理并混合
                fixed4 mainCol = tex2D(_MainTex, i.uv);
                fixed4 secondaryCol = tex2D(_SecondaryTex, i.uv);
                fixed4 finalCol = lerp(mainCol, secondaryCol, blend);
                
                return finalCol;
            }
            ENDCG
        }
    }
}

三、Shader挂载与VR适配

方式1:后处理(推荐VR场景)

  1. 导入Post Processing Stack v2(Package Manager搜索安装)。
  2. 主相机添加Post Process Layer组件,设置Layer为新建的PostProcessing层。
  3. 主相机添加Post Process Volume组件,勾选Is Global,并添加自定义后处理效果:
using UnityEngine;
using UnityEngine.Rendering.PostProcessing;

[System.Serializable]
[PostProcess(typeof(CircleBlendRenderer), PostProcessEvent.AfterStack, "Custom/CircleBlend")]
public sealed class CircleBlend : PostProcessEffectSettings
{
    public TextureParameter secondaryTexture = new TextureParameter();
    public Vector2Parameter circleCenter = new Vector2Parameter { value = new Vector2(0.5f, 0.5f) };
    public FloatParameter circleRadius = new FloatParameter { value = 0.2f };
    public FloatParameter edgeSmoothness = new FloatParameter { value = 0.02f };
}

public sealed class CircleBlendRenderer : PostProcessEffectRenderer<CircleBlend>
{
    public override void Render(PostProcessRenderContext context)
    {
        var sheet = context.propertySheets.Get(Shader.Find("Custom/CircleRenderTextureBlend"));
        sheet.properties.SetTexture("_SecondaryTex", settings.secondaryTexture.value);
        sheet.properties.SetVector("_CircleCenter", settings.circleCenter.value);
        sheet.properties.SetFloat("_CircleRadius", settings.circleRadius.value);
        sheet.properties.SetFloat("_EdgeSmoothness", settings.edgeSmoothness.value);
        context.command.BlitFullscreenTriangle(context.source, context.destination, sheet, 0);
    }
}
  1. 在Post Process Volume的Add Effect中选择Custom/CircleBlend,将Secondary Texture设为之前创建的RenderTexture,调整参数即可。

方式2:全屏Quad(固定位置圆形)

  • 创建Quad,缩放至覆盖主相机视野(VR场景需创建左右眼两个Quad,或使用VR专用全屏Mesh)。
  • 创建材质并选择上述Shader,将Secondary Tex设为目标RenderTexture,调整圆心、半径参数。

内容的提问来源于stack exchange,提问作者Pizza Programmer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.27 17:17:52