您当前的位置: 首页 > 

果冻喜之郎

暂无认证

  • 6浏览

    0关注

    79博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Dof景深基础

果冻喜之郎 发布时间:2022-07-18 16:05:19 ,浏览量:6

景深在后处理阶段完成:利用一张景深mask,将模糊处理的正常图片与原图做差值

景深mask制作:

1. 获取摄像机深度纹理,在unity中深度图挂载到实时光源上,如果使用烘焙灯则需要强制获取,添加代码:

private Camera currentCamera = null;
void Awake(){
    currentCamera = GetComponent();
}

void OnEnable(){
    currentCamera.depthTextureMode |= DepthTextureMode.Depth;
}

void OnDisable(){
    currentCamera.depthTextureMode &= -DepthTextureMode.Depth;
}

2. 制作mask

_ProjectionParams.z:摄像机远裁面,由于在实际项目中景深效果一直跟随角色,不应该受到远裁面的影响,所以在获取深度信息时要乘以这个值。

定义景深值和焦距,焦距-景深 = 前焦,焦距+景深 = 远焦。当深度值小于前焦,景深用前焦-深度值,当深度值大于远焦,景深用深度值-远焦,加绝对值。

Pass{
    CGPROGRAM
    #pragma vertex vert
    #pragma fragment frag                                                       // 顶点着色器和片源着色器声明

    #include "UnityCG.cginc"

    struct appdata{                                                             // 顶点着色器输入结构体,位置和UV
        float4 vertex : POSITION;
        float2 uv : TEXCOORD0;
    };

    struct v2f{                                                                 // 顶点着色器输出结构体,位置和UV
        float2 uv : TEXCOORD0;
        float4 vertex : SV_POSITION;
    };

    sampler2D _MainTex;
    sampler2D _BlurTex;                                                         // 模糊后的纹理
    sampler2D _CameraDepthTexture;                                              // 相机深度纹理
    float4 _BlurOffset;                                                         // 模糊范围偏移
    float _FocusDistance, _DepthOfField, _DofSmoothRange;                       // 焦点距离,景深,光滑过度  
    float _Step;               

    v2f vert (appdata v){
        v2f o;
        o.vertex = UnityObjectToClipPos(v.vertex);                              // 对象空间转换到裁剪空间
        o.uv = v.uv;
        return o;
    }       

    half4 frag (v2f i) : SV_Target{
        half4 col = tex2D(_MainTex, i.uv);                                      // 原图颜色
        half4 blur_col = tex2D(_BlurTex, i.uv);                                 // 模糊后的颜色
        
        
        // half depth = Linear01Depth(tex2D(_CameraDepthTexture, i.uv));        // 避免远裁面的值对景深效果的影响
        half depth = Linear01Depth(tex2D(_CameraDepthTexture, i.uv)).r * _ProjectionParams.z * _Step;//避免远裁面的值对景深效果的影响
        float focusNear = _FocusDistance - _DepthOfField;                       // 近焦距点
        float focusFar = _FocusDistance + _DepthOfField;                        // 远焦距点

        half final_depth = 0;
        if((depth>=focusNear)&&(depth            
关注
打赏
1655963140
查看更多评论
0.0483s