九月份一篇博都没更新,这段时间一直在unity的坑里爬不起来,感觉真的很绝望啊,仿佛对生活都失去了信心。

渲染问题并没有解决,目前方案只是减轻视觉冲突,降低违和感。项目AR产品也做的越来越艰难,开始经常想一个问题,我从哪里来,我该到哪里去。。。

好吧,唠叨这么多,言归正传,今天说说unity的Post-Processing后期处理的景深 Depth Of Field

官方文档 https://docs.unity3d.com/Manual/PostProcessingOverview.html

先贴完整代码,下载PostProcessing插件并import,将下面脚本挂在camera上,运行

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.PostProcessing; public class DepthOfFieldTest : MonoBehaviour { public Transform nearBlurTarget;
public Transform farBlurTarget; private PostProcessingProfile profile; // Use this for initialization
void Start () {
AddPostProcessing ();
BlurEffect();
} /// <summary>
/// Adds the post processing 后期处理特效.
/// </summary>
public void AddPostProcessing ()
{
PostProcessingBehaviour postProcessingBehaviour = gameObject.AddComponent<PostProcessingBehaviour> ();
this.profile = new PostProcessingProfile ();
// this.profile = Resources.Load ("GreenArchPOST") as PostProcessingProfile;
postProcessingBehaviour.profile = profile; profile.depthOfField.enabled = false;
profile.antialiasing.enabled = true;
profile.ambientOcclusion.enabled = true; // AO参数设置,主要是要勾选ambientOnly选项
AmbientOcclusionModel.Settings aoSettings = new AmbientOcclusionModel.Settings () {
intensity = 1f,
radius = 0.3f,
sampleCount = AmbientOcclusionModel.SampleCount.Medium,
downsampling = true,
forceForwardCompatibility = false,
ambientOnly = true,
highPrecision = false
};
profile.ambientOcclusion.settings = aoSettings;
} /// <summary>
/// Blurs the effect景深模糊效果.
/// </summary>
/// <param name="jo">Jo.</param>
public void BlurEffect()
{
DepthOfFieldModel depthOfField = this.profile.depthOfField; Vector3 focusPosition = (1.5f * nearBlurTarget.position + farBlurTarget.position) / 2.5f; Plane cameraPlane = new Plane(gameObject.transform.forward, gameObject.transform.position);
float focusDistance = cameraPlane.GetDistanceToPoint (focusPosition);
float depthOfFeild = Mathf.Abs (cameraPlane.GetDistanceToPoint (farBlurTarget.position) - cameraPlane.GetDistanceToPoint (nearBlurTarget.position)); // 根据公式计算 景深ΔL=ΔL1+ΔL2=(2f^2FδL^2)/(f^4-F^2δ^2L^2)
// 容许弥散圆直径 δ=0.035mm
float ap = 5.6f * 0.035f / 1000f;
float focusDistancex2 = Mathf.Pow (focusDistance, 2f);
float lengthx2 = ap * focusDistancex2 + ap * focusDistance * Mathf.Sqrt(focusDistancex2 + Mathf.Pow (depthOfFeild, 2f));
float focalLengthByMath = Mathf.Sqrt (lengthx2 / depthOfFeild) * 1000f; Debug.Log ("清晰点距相机距离:" + focusDistance);
Debug.Log ("景深:" + depthOfFeild);
Debug.Log ("相机焦距 focalLengthByMath (mm):" + focalLengthByMath); depthOfField.enabled = true;
UnityEngine.PostProcessing.DepthOfFieldModel.Settings depthOfFieldSetting = new UnityEngine.PostProcessing.DepthOfFieldModel.Settings {
focusDistance = focusDistance,
aperture = 5.6f,
focalLength = focalLengthByMath,
useCameraFov = false,
kernelSize = DepthOfFieldModel.KernelSize.Medium
};
depthOfField.settings = depthOfFieldSetting;
}
}

采用PostProcessing摄像机后期处理特效,Depth Of Field模块,根据前端给的三个参数计算Focus Distance 焦点距离 和 focalLength镜头焦距。

1.Focus Distance具体含义是距离摄像机多远的“距离”拍摄最高清,这个距离是目标点与相机平面的垂直距离,相机平面可以这样确定,1.与nearPlane平面平行,过相机位置点。所以焦距Focus Distance可以通过以下代码求得并设置:

//相机辅助平面

Plane cameraPlane = new Plane(camera.transform.forward,camera.transform.position);

//计算目标距离相机的距离,焦点距离 focusDistance

float focusDistance = cameraPlane.GetDistanceToPoint (focusPosition);

2.focalLength 镜头焦距,通过景深 nearBlurPosition 和 farBlurPosition计算得到。景深计算方式如下:

看成景深、镜头焦距、光圈值的一个方程,将镜头焦距作为要求的跟,根据一元二次方程的解:

得到 :

随便说说 post-processing的更多相关文章

  1. 转债---Pregel: A System for Large-Scale Graph Processing(译)

    转载:http://duanple.blog.163.com/blog/static/70971767201281610126277/   作者:Grzegorz Malewicz, Matthew ...

  2. struts2与struts1整合,java.lang.InstantiationException, Exception occurred during processing request: null

    做了2个action,其中一个运行没有问题,另一个报错,看下面的报错信息,再看了看struts.xml,因为没有给GetBooks这个action配置actionform,所以就导致报null.下面是 ...

  3. Pregel: A System for Large-Scale Graph Processing(译)

    [说明:Pregel这篇是发表在2010年的SIGMOD上,Pregel这个名称是为了纪念欧拉,在他提出的格尼斯堡七桥问题中,那些桥所在的河就叫Pregel.最初是为了解决PageRank计算问题,由 ...

  4. Processing 中玩增强现实 Argument Reality

    其实2009年Processing就能做AR了,只是我不知道而已~ 需要以下几个东西: 1.JMyron 2.GSVideo 3.nyar4psg 4.Picking 5.OBJLoader 或者大伙 ...

  5. 随便记录下系列 - node->express

    随便记录下系列 - node->express 文章用啥写?VsCode. 代码用啥写?VsCode. 编辑器下载:VsCode 一.windows下安装node.js环境: 下载地址 相比以前 ...

  6. OLTP(on-line transaction processing)与OLAP(On-Line Analytical Processing)

    OLTP与OLAP的介绍 数据处理大致可以分成两大类:联机事务处理OLTP(on-line transaction processing).联机分析处理OLAP(On-Line Analytical ...

  7. 新书到手 TRANSACTION PROCESSING:CONCEPTS AND TECHNIQUES

    新书到手 TRANSACTION PROCESSING:CONCEPTS AND TECHNIQUES Jim Gray大神的著作 本文版权归作者所有,未经作者同意不得转载.

  8. Report processing of Microsoft Dynamic AX

    Report processing of Microsoft Dynamic AX 版权声明:本文为博主原创文章,未经博主允许不得转载. The implementation of a general ...

  9. Java基础-多线程编程-1.随便选择两个城市作为预选旅游目标。实现两个独立的线程分别显示10次城市名,每次显示后休眠一段随机时间(1000ms以内),哪个先显示完毕,就决定去哪个城市。分别用Runnable接口和Thread类实现。

    1.随便选择两个城市作为预选旅游目标.实现两个独立的线程分别显示10次城市名,每次显示后休眠一段随机时间(1000ms以内),哪个先显示完毕,就决定去哪个城市.分别用Runnable接口和Thread ...

  10. ANNOTATION PROCESSING 101 by Hannes Dorfmann — 10 Jan 2015

    原文地址:http://hannesdorfmann.com/annotation-processing/annotationprocessing101 In this blog entry I wo ...

随机推荐

  1. UITextField的使用小技巧

    [tf setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"];//修改placeHolder ...

  2. VS2015远程调试

    原文链接 VS2015远程调试   在PayPal支付时,PayPal回调函数一直报错,本地没有外网IP,没有办法在本地调试,需要远程调试: 1.找到远程调试的文件夹: 找到对应的服务器的型号:64位 ...

  3. Smarty基础用法

    一.Smarty基础用法: 1.基础用法如下 include './smarty/Smarty.class.php';//引入smarty类 $smarty = new Smarty();//实例化s ...

  4. (翻译)使用Api分析器与Windows兼容包来编写智能的跨平台.NET Core应用

    本文翻译自Scott Hanselman博客: https://www.hanselman.com/blog/WritingSmarterCrossplatformNETCoreAppsWithThe ...

  5. mysql基础知识点

    /* 启动MySQL */net start mysql /* 连接与断开服务器 */mysql -h 地址 -P 端口 -u 用户名 -p 密码 /* 跳过权限验证登录MySQL */mysqld ...

  6. WinForm程序,实现只启动一个实例

    前言:在我们做的软件中,当点击图标运行时,正常的需求是只需要启动一个软件的实例,这是非常重要的一点,不然就显得我们的软件非常的山寨,笔者在工作中经常遇到同事没有注意这一点,看是不重要,实则非常的重要, ...

  7. 室内地图1:从CAD到Map(画图,发布,路径导航)

    首先这个教程整理,比较偏细节. 因为我本身不是做GIS,所以可能有点流水账,当然错漏难免,恳请指正. 当我们做定位的时候,想要可视化展示,室外当然可以直接使用google,百度的底图.对于室内,有两种 ...

  8. 使用.Net Core+EF7 完成CodeFirst

    emmm,本来想着用Core做一个小项目玩玩的,然后肯定是要用到数据库的, 然后想,啊,要不用CodeFirst,感觉很腻害的样子,于是,一脸天真无邪的我就踏入了一个深不见底的天坑... 本来想着,应 ...

  9. Java二分法

    public class Dichotomy {        //定义查找次数    static int count = 0;        public static void main(Str ...

  10. java 邮件发送的公共方法

    protected static String host = "true"; protected static String auth = "smtp.163.com&q ...