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

标签 unity3d blur

我想创建类似于下图的模糊效果:
enter image description here

图片来自这个网站:

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

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

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

我使用的是 Unity 版本 2019.2.9f1

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

您的指导将不胜感激。

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

最佳答案

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

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

窗口 → 包管理器

enter image description here

确保在 All Packages查看、搜索 post ,找到 Post Processing打包并点击安装

enter image description here

现在首先转到图层设置(图层→编辑图层)

enter image description here

并添加 两个附加层:例如PostProcessingFocused
enter image description here

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

  • 确保 MainCamera不渲染添加的两个图层 → 从 Culling Mask 中删除它们

  • enter image description here
  • 添加新相机 FocusCamera作为现有 MainCamera 的 child .这样,它会自动与主相机一起移动。

    右键单击 MainCamera &rightarrow 相机

    enter image description here

    它应该具有等于 MainCamera 的所有设置除了 :
  • Clear Flags : 不清除
    如果您将其设置为 Depth Only聚焦对象将始终呈现在所有对象之上,即使它实际上位于 3D 空间中的其他对象之后。你决定你想要的效果在这里;)
  • CullingMask :只有专注
  • Depth : 任何高于 MainCamera所以这个相机是在它上面渲染的
  • 确保删除 AudioListener成分。

  • enter image description here
    enter image description here
  • 最后添加一个新的PostProcessingVolume到现场。我会把它作为 child 添加到 FocusCamera !为什么? - 因为这样它会与 FocusCamera 一起自动禁用!

    右键单击 FocusCamera → 3D 对象 → 后处理体积

    enter image description here

    设置其Layer添加到 PostProcessing
    enter image description here

    启用 Is Global所以与体积的距离无关紧要,并通过点击 new → Unity → Depth of field 添加新的配置文件

    在您的情况下,您想覆盖 Focus Distance所以选中左边的框并设置一个靠近相机的值,例如0.5
    enter image description here

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

    现在转到 MainCamera和,一个组件 PostProcessingLayer并设置 Layer到我们添加的层后处理

    enter image description here

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

    几乎准备好了!现在禁用 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;
        }
    }
    

    这将允许您通过将某个游戏对象的层更改为 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);
        }
    }
    

    这是它的样子

    enter image description here

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

    关于unity3d - 如何为除一个对象之外的所有场景创建模糊效果(专注于该对象)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59188747/

    相关文章:

    audio - Unity只播放少数游戏对象的声音

    c# - Unity 输入系统按钮多次触发

    c# - 我怎样才能在播放模式下沿特定轴获取播放器的位置?

    image - 如何使用 react-native 制作模糊效果?

    unity3d - Unity- Photosphere Photo Viewer for Google Cardboard

    arrays - 如何将 Json 数组作为统一字段传递给 WWWform

    html - 如何在背景上制作模糊效果

    javascript - 当 child 获得焦点时触发模糊事件

    unity-game-engine - Unity 着色器将具有相同 Material 的对象渲染到后续 GrabPasses

    javascript - JQuery 模糊事件在 IE 中未触发