如何为除一个对象之外的所有场景创建模糊效果(专注于该对象)?

maz*_*zin 4 blur unity-game-engine

我想创建类似于下图的模糊效果: 在此处输入图片说明

图片来自这个网站:

https://forum.unity.com/threads/how-to-blur-specific-layers-only.555520/

我尝试了后处理配置文件并在后处理体积中使用了景深,但它模糊了所有场景。

我遇到了一个YouTube 视频,它解释了如何实现与我正在寻找的类似的结果,但设置中的情况发生了巨大变化。例如,在 1:57(分 57 秒),他点击了Add Additional Camera Data我在最新版本的 LWRP 中很难找到的内容。

我使用的是 Unity 版本 2019.2.9f1

我怎样才能达到上图中的结果?(模糊除一个物体外的所有场景)

您的指导将不胜感激。

注意:我的项目是在 VR 中使用 SteamVR 和 VRTK

der*_*ugo 14

虽然你的问题有点宽泛,但我花了一些时间向你展示如何做到这一点。


首先,您需要Post Processing通过PackageManager导入包

WindowPackageManager

在此处输入图片说明

确保在All Packages视图中,搜索post,找到Post Processing包并点击Install

在此处输入图片说明


现在首先转到图层设置(LayersEdit Layers

在此处输入图片说明

并添加两个额外的图层:例如PostProcessingFocused

在此处输入图片说明

现在到相机。Afaik 无论你是否在 VR 中都没有区别,通常你有一个MainCamera与你的耳机一起移动的。如果您的项目设置中应该有两个,只需对第二个相机重复相同的步骤。

  1. 确保MainCamera不会渲染两个添加的图层 → 从Culling Mask

在此处输入图片说明

  1. 将一个新的 CameraFocusCamera作为子项添加到现有的MainCamera. 这样,它会自动与主相机一起移动。

    RightClick on MainCamera &右箭头 Camera

    在此处输入图片说明

    它应该具有与MainCamera except相同的所有设置:

    • Clear FlagsDon't Clear
      如果将其设置为Depth Only聚焦对象将始终呈现在所有内容之上,即使它实际上位于 3D 空间中的其他对象之后。你决定你想要的效果在这里;)
    • CullingMask:只有 Focused
    • Depth: 任何高于 的东西,MainCamera所以这个相机会在它上面渲染
    • 确保卸下AudioListener组件。

    在此处输入图片说明 在此处输入图片说明

  2. 最后添加一个新PostProcessingVolume的场景。我会把它作为孩子添加到FocusCamera! 为什么?- 因为这样它会自动与FocusCamera!

    RightClickFocusCamera3D ObjectPost Processing Volume

    在此处输入图片说明

    将其设置Layer为添加的PostProcessing

    在此处输入图片说明

    启用Is Global以便与音量的距离无关紧要并通过点击newUnity→添加新的配置文件Depth of field

    在您的情况下,您想覆盖Focus Distance因此选中左侧的框并设置一个靠近相机的值,例如0.5

    在此处输入图片说明

到目前为止,您的场景中没有任何真正改变。

现在转到MainCamera和,一个组件PostProcessingLayer并将其设置Layer为我们添加的图层PostProcessing

在此处输入图片说明

现在场景中的一切都应该模糊了!

几乎准备好了!现在禁用FocusCamera并将此脚本添加到它

using UnityEngine;

public class FocusSwitcher : MonoBehaviour
{
    public string FocusedLayer = "Focused";

    private GameObject currentlyFocused;
    private int previousLayer;

    public void SetFocused(GameObject obj)
    {
        // enables this camera and the postProcessingVolume which is the child
        gameObject.SetActive(true);

        // if something else was focused before reset it
        if (currentlyFocused) currentlyFocused.layer = previousLayer;

        // store and focus the new object
        currentlyFocused = obj;

        if (currentlyFocused)
        {
            previousLayer = currentlyFocused.layer;
            currentlyFocused.layer = LayerMask.NameToLayer(FocusedLayer);
        }
        else
        {
            // if no object is focused disable the FocusCamera
            // and PostProcessingVolume for not wasting rendering resources
            gameObject.SetActive(false);
        }
    }

    // On disable make sure to reset the current object
    private void OnDisable()
    {
        if (currentlyFocused) currentlyFocused.layer =previousLayer;

        currentlyFocused = null;
    }
}
Run Code Online (Sandbox Code Playgroud)

这将允许您通过将某个游戏对象的Focused层更改为我们添加的层来将某个游戏对象集中在运行时,这是唯一由FocusCamera. 所以这个对象将被渲染在图像之上,没有任何模糊效果!


为了演示,我只是将这个简单的脚本添加到每个立方体对象中,以便在鼠标进入时启用焦点并在鼠标退出时禁用它:

using UnityEngine;

public class FocusMe : MonoBehaviour
{
    [SerializeField] private FocusSwitcher focus;

    private void OnMouseEnter()
    {
        focus.SetFocused(gameObject);
    }

    private void OnMouseExit()
    {
        // reset the focus
        // in the future you should maybe check first
        // if this object is actually the focused one currently
        focus.SetFocused(null);
    }
}
Run Code Online (Sandbox Code Playgroud)

这是它的样子

在此处输入图片说明


如前所述,我不确切知道您的 VR 设置是什么样的。如果您必须使用 MainCameras,只需向其中添加两个子摄像机即可。您仍然只需要一个 PostProcessingVolume 并且只需要一个,FocusSwitcher因此您可能会将它们移动到另一个对象并以不同的方式处理相机禁用等,但我希望这个想法足够清晰。