Unity 景深Depth Of Field

这篇具有很好参考价值的文章主要介绍了Unity 景深Depth Of Field。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

目录

 文章来源地址https://www.toymoban.com/news/detail-493098.html

介绍

准备

设置

基于Unity Builtin 管线

基于Unity URP

基于Unity HDRP


 

介绍:

景深效果Depth Of Field是摄影界的老常客了,在游戏中也非常多见,它能够大幅提升游戏画面体验和真实度,使得物体看起来更有细节。

Unity 景深Depth Of Field

GTA5中的景深效果

Unity 当然提供了景深支持: Creative Core: Post-processing - Unity Learn

本文我将介绍最简单直接的实现方法,不需要任何复杂的物理学只是,直截了当的就能出效果。

准备

本节将分别介绍Built-in Render,URP,HDRP中的基础景深效果。所有的管线景深都需要用到PostProcessing功能。

首先,三种管线都需要先把简单的前置设置部署好:Built-in Render需要额外安装package,另外两个不用,在package manager中搜索安装 Post Processing,这个插件早晚会用,它提供了全方位的画质颜色和视觉提升。

设置

  • 基于Unity Builtin 管线

更多参考:Post Process Volumes - Unity Learn
基于Unity Builtin 管线, 需要额外安装 PostProcessing package,安装完成后,你需要:

  1. 在Layer设置中增加一个Layer,可以随便命名,但是建议就叫 PostProcessing” ;然后在你想要设置的Camera中增加Post-process Layer组件;接着设置这个组件的Layer为你刚刚新添加的“PostProcessing” ,确组件的Trigger是你想要的相机;
  2. 创建一个空物体,或者你可以继续在Camera中添加,(只要确保和PostProcessing有关的所有对象都有“PostProcessing” Layer!),在这个物体中添加Post-Process Volume
  3. 创建并保存你的Profile,并在Add effect选项中添加Depth Of Field效果,一般来说Apture小一些Focal Length大一些可以得到一个不错的景深效果,之后你就只需要调整焦距Focus Distance就可以了!
  4. Unity 景深Depth Of Field

    Unity 景深Depth Of Field

    Unity 景深Depth Of Field

    Unity 景深Depth Of Field

    Unity 景深Depth Of Field

     

Unity 景深Depth Of Field

如果你需要动态根据距离来调整的话,需要代码接入来更改volume的 Focus Distance,可以动过简单计算相机和焦点物体的距离来更改;另一种方案是通过屏幕中心发射一道射线来判断距离,这里介绍简单的:


using UnityEngine;
using UnityEngine.Rendering.PostProcessing;

public class cameraDist : MonoBehaviour
{
[SerializeField] PostProcessVolume postProcessVolume;
[SerializeField] DepthOfField df;
[SerializeField] Transform player,mainCamera;

void Start()
    {
      
          postProcessVolume.sharedProfile.TryGetSettings<DepthOfField>(out df);

    }
}
void Update()
    {
        df.focusDistance.value = Vector3.Distance(player.position, mainCamera.position);
    }
  • 基于Unity URP

基于Unity URP, PostProcessing 已经默认包含,而且不需要设置Layer。其它设置和Buit-in基本一致,但是确保在相机设置中启用Post Processing功能! 然后直接在相机中添加Volume组件就可以了,其它步骤基本不变,景深设置中可选用Bokeh

 Unity 景深Depth Of FieldUnity 景深Depth Of Field

Unity 景深Depth Of Field

 

Unity 景深Depth Of Field

动态更改焦距,这里和Built-in类似,只是引用的API不一样,此外,这里使用简单的射线检测,从相机发出一条射线,射到谁就用谁到相机的距离,但这个方法不适用在复杂场景和运动条件下


using UnityEngine;


public class cameraDist : MonoBehaviour
{
    [SerializeField] private UnityEngine.Rendering.Volume postProcessVolume;
    [SerializeField] private UnityEngine.Rendering.Universal.DepthOfField dof;

void Start()
    {
      
              postProcessVolume.profile.TryGet<UnityEngine.Rendering.Universal.DepthOfField>(out dof);

    }
}
void Update()
    {
        RaycastHit hit;
            if (Physics.Raycast(mainCamera.transform.position, mainCamera.transform.forward, out hit, Mathf.Infinity))
            {
                dof.focusDistance.value = hit.distance;
            }
    }
  • 基于Unity HDRP

HDRP就更神奇了,由于更高渲染技术的加持,其图像原理看起来很不一样,这里的相机看起来更像显示世界里的相机了,相机直接按如图设置:

Unity 景深Depth Of Field

 Volume和景深效果的添加和URP一样,但是它的参数可以被相机的物理信息直接控制了:

Unity 景深Depth Of Field

Unity 景深Depth Of Field

hHDRP的动态对焦,脚本这时候就需要直接更改相机属性就行了,但是注意,相机必须设置为物理相机!

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering.HighDefinition;

public class CameraFocus : MonoBehaviour
{
    public Transform target;
    [SerializeField] Camera cam;
    // Update is called once per frame
    private void OnValidate()
    {
        cam = GetComponent<Camera>();
        cam.usePhysicalProperties = true;
        cam.GetComponent<HDAdditionalCameraData>().physicalParameters.focusDistance = Vector3.Distance(transform.position, target.position);
    }
    private void Start()
    {
        cam = GetComponent<Camera>();
        cam.usePhysicalProperties = true;
    }
    void Update()
    {
        FocusByRaycast();
    }
    void FocusByTransform(Transform target)
    {
        // change camera focus distance 
        cam.GetComponent<HDAdditionalCameraData>().physicalParameters.focusDistance = Vector3.Distance(transform.position, target.position);
    }
    void FocusByRaycast()
    {
        //raycast to target hit distance
        RaycastHit hit;
        if (Physics.Raycast(transform.position, transform.forward, out hit))
        {
            // change camera focus distance 
            cam.GetComponent<HDAdditionalCameraData>().physicalParameters.focusDistance = hit.distance;
        }

    }
}

 

 

 

 

 

到了这里,关于Unity 景深Depth Of Field的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 【异常】The field file exceeds its maximum permitted size of 1048576 bytes.

    本项目是个Springboot 项目,功能是要做一个文件上传,在测试时发现报错,上传的是一个 word 文件,大小是 1.25MB,报错内容如下: Caused by: org.apache.tomcat.util.http.fileupload.FileUploadBase$FileSizeLimitExceededException: The field file exceeds its maximum permitted size of 1048576 bytes. 详细报错内容如下图

    2024年03月15日
    浏览(31)
  • Field [price] of type [text] is not supported for aggregation [avg]

    原因:text类型不支持求平均值,改为float PUT http://localhost:9201/shopping/_mapping

    2024年02月13日
    浏览(37)
  • Unity UI与粒子 层级问题Camera depth Sorting Layer Order in Layer RenderQueue

    Unity游戏开发中,模型、界面、特效等,需要规划好 layer 的概念,涉及到摄像机(Camera)、画布(Canvas)、Shader等相关内容。 在 Unity 中,渲染顺序是由多个因素共同决定的,大致分为三层优先级: Camera depth、Sorting Layer/Order in Layer 和 RenderQueue 。 一般游戏项目,会创建至少两

    2024年02月08日
    浏览(26)
  • 【Unity】UGUI中Camera Depth,Canvas Sorting Layer、Order in Layer与Particle System渲染层级分析

    目录   前言 一、项目需求 二、Camera 1.Clear Flags 2.Culling Mask  三、Canvas 1.Sorting Layer 2.Order in Layer 四、Particle System 1.Sorting LayerID 与Order in Layer 总结         最近在做项目的过程中,发现项目中的部分3d模型会被粒子特效所遮挡,这并不是笔者想要的效果,于是经过一番面向

    2024年02月05日
    浏览(36)
  • go: Unmarshal error: json: cannot unmarshal string into Go struct field .timestamp of type int64

    在我们作为Go开发工程师的工作中,错误和异常处理无疑是非常重要的一环。今天,我们来讲解一个在Go中进行JSON解析时可能会遇到的具体错误,即: ERR: Unmarshal error: json: cannot unmarshal string into Go struct field .timestamp of type int64 。 在进行服务端或客户端开发时,经常需要通过

    2024年02月03日
    浏览(27)
  • es--基础--9.2--SpringBoot注解--Field--介绍

    字段名称,是name的别名 字段名称 字段类型 默认:自动检测字段的类型 是否建立索引 false: 不能查询 不能得到返回 时间类型的格式化 是否存储原文 false:字段内容 存储到_source中 ture:字段内容 存储到其他地方,不存储到_source中 当你将一个field的store属性设置为true,这个

    2024年02月02日
    浏览(22)
  • 【人工智能】NLP自然语言处理领域发展史 | The History of Development in Natural Language Processing (NLP) Field

    自然语言处理(Natural Language Processing,NLP)是人工智能(AI)领域的重要分支,旨在让计算机能够理解、处理和生成自然语言,如英语、汉语等。本文将介绍NLP领域的发展历史和里程碑事件。

    2024年02月07日
    浏览(50)
  • 第四十一章 Unity 输入框 (Input Field) UI

    本章节我们学习输入框 (Input Field),它可以帮助我们获取用户的输入。我们点击菜单栏“GameObject”-“UI”-“Input Field”,我们调整一下它的位置,效果如下 我们在层次面板中发现,这个InputField UI元素包含两个子元素,一个是Placeholder,另一个是Text。如下所示 同样,我们查看

    2024年02月04日
    浏览(27)
  • 神经辐射场(Neural Radiance Field,NeRF)的简单介绍

    参考文章:https://arxiv.org/abs/2210.00379    神经场 是一种神经网络,其输入为坐标,输出为坐标对应点的某个属性。    神经辐射场 (NeRF)模型是一种新视图合成方法,它使用体积网格渲染,通过MLP进行隐式神经场景表达,以学习3D场景的几何和照明。    应用 :照片编

    2024年02月07日
    浏览(34)
  • 微信小程序入门及开发准备,申请测试号以及小程序开发的两种方式,目录结构说明

    目录 1. 介绍 1.1 优点 1.2 开发方式 2. 开发准备 2.1 申请 2.2 申请测试号 2.2 小程序开发的两种方式 2.3 开发工具 3. 开发一个demo 3.1 创建项目 3.2 配置 3.3 常用框架 3.3 目录结构说明 3.4 新建组件 是一种不需要下载安装即可使用的应用,是一种 触手可及 的应用 可以借助微信的流量

    2024年02月05日
    浏览(33)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包