九月份一篇博都没更新,这段时间一直在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. Linux 链接详解----静态链接实例分析

    由Linux链接详解(1)中我们简单的分析了静态库的引用解析和重定位的内容, 下面我们结合实例来看一下静态链接重定位过程. /* * a.c */ ; void add(int c); int mai ...

  2. 2016普及组t3海港

    好的,说说这道题的思路,爆搜队列嘛: 用一个结构体队列存每个人来的时间和他的国籍,用一个vis数组存每个人来的次数,是第一次来sum便加一. 然后从前面第一个人开始扔(原谅我用这个词,因为我找不到更好 ...

  3. yii2.0框架debug模式

  4. Mysql服务器SQL模式 (官方精译)

    MySQL服务器可以在不同的SQL模式下运行,并且可以根据sql_mode系统变量的值对不同的客户端应用不同的模式.DBA可以设置全局SQL模式以匹配站点服务器操作需求,并且每个应用程序可以将其会话S ...

  5. YiShop_网上商城系统多少钱

    电子商务的发展,网上商城系统的开发也变得越来越热门.商城系统开发对于企业来说是非常有必要的,一个好的网上商城系统对于企业来说是非常重要.那么到底网上商城系统多少钱呢?下面YiShop带大家去了解. 网 ...

  6. 深度解剖sesssion运行原理

    已经大半年没有更新博客了,一方面有比博客更重要的事情要做,另外一方面也没有时间来整理知识,所以希望在接下来的日子里面能够多多的写博客来与大家交流 什么是session session的官方定义是:Se ...

  7. 基于 HTML5 Canvas 的 3D 压力器反序列化

    在实际应用中,我觉得能够通过操作 JSON 文件来操作 3D 上的场景变化是非常方便的一件事,尤其是在做编辑器进行拖拽图元并且在图元上产生的一系列变化的时候,都能将数据很直观地反应给我们,这边我们简单 ...

  8. python3.6 urllib.request库实现简单的网络爬虫、下载图片

    #更新日志:#0418 爬取页面商品URL#0421 更新 添加爬取下载页面图片功能#0423 更新 添加发送邮件功能# 优化 爬虫异常处理.错误页面及空页面处理# 优化 爬虫关键字黑名单.白名单,提 ...

  9. JaveScript简单数据类型(JS知识点归纳二)

    JS中的简单数据类型有五种 : --> string --> number -->boolean --> null -->undefined 数据类型的检测 :typeo ...

  10. iOS日历中给一个事件加入多个提醒

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 假设认为写的不好请多提意见,假设认为不错请多多支持点赞.谢谢! hopy ;) iOS自带的日历应用中,我们最多仅仅能给一个事件设置2个提醒, ...