如何为除一个对象之外的所有场景创建模糊效果(专注于该对象)? [英] How to create blur effect to all the scene except for one object (focus on that object)?
问题描述
我想创建一个类似于下图的模糊效果:
图片来自本站:
确保在 All Packages
视图中,搜索 post
,找到 Post Processing
包并点击 Install
现在首先转到图层设置(图层 → 编辑图层)
并添加两个附加层:例如PostProcessing
和 Focused
现在是相机.Afaik 无论您是否在 VR 中都没有区别,通常您有一个 MainCamera
与您的耳机一起移动.如果您的项目设置中应该有两个,只需对第二个相机重复相同的步骤.
- 确保
MainCamera
不会渲染两个添加的图层 →从Culling Mask
中删除它们
添加一个新的相机
FocusCamera
作为现有MainCamera
的子级.这样它就会随着主相机自动移动.RightClick on
MainCamera
&rightarrow Camera它应该具有与
MainCamera
相同的所有设置 除了:清除标志
: 不清除
如果将其设置为Depth Only
,则聚焦对象将始终呈现在所有内容之上,即使它实际上位于 3D 空间中的其他对象之后.你决定你想要的效果在这里;)CullingMask
: only FocusedDepth
: 任何高于MainCamera
的东西,所以这个相机在它上面渲染- 确保删除
AudioListener
组件.
最后添加一个新的
PostProcessingVolume
到场景中.我会将它作为子项添加到FocusCamera
!为什么?- 因为这样它会与FocusCamera
一起自动禁用!RightClick on
FocusCamera
→3D 物体 →后期处理量将其
Layer
设置为添加的PostProcessing
启用
Is Global
以便与音量的距离无关紧要,并通过点击 new → 添加一个新的配置文件.统一 →景深在您的情况下,您想覆盖
Focus Distance
,因此请选中左侧的框并设置一个靠近相机的值,例如0.5
到目前为止,您的场景中没有任何真正改变.
现在转到 MainCamera
和组件 PostProcessingLayer
并将 Layer
设置为我们添加的图层 PostProcessing
现在场景中的一切都应该模糊了!
几乎准备好了!现在禁用 FocusCamera
并将此脚本添加到其中
使用UnityEngine;公共类 FocusSwitcher:MonoBehaviour{公共字符串 FocusedLayer = "Focused";当前关注的私有游戏对象;私有 int 前一层;公共无效SetFocused(游戏对象对象){//启用此相机和作为子项的 postProcessingVolumegameObject.SetActive(true);//如果在重置之前有其他东西被聚焦if (currentlyFocused) currentFocused.layer = previousLayer;//存储并聚焦新对象当前聚焦 = obj;如果(当前焦点){previousLayer = currentFocused.layer;currentFocused.layer = LayerMask.NameToLayer(FocusedLayer);}别的{//如果没有对象被聚焦,则禁用 FocusCamera//和 PostProcessingVolume 不浪费渲染资源gameObject.SetActive(false);}}//在禁用时确保重置当前对象私有无效 OnDisable(){if (currentlyFocused) currentFocused.layer =previousLayer;当前聚焦 = 空;}}
这将允许您通过将某个 GameObject 的层更改为我们添加的 Focused
层来将其聚焦在运行时,这是唯一由 FocusCamera
渲染的层.所以这个物体会被渲染在图像之上,没有任何模糊效果!
为了演示,我只是将这个简单的脚本添加到每个立方体对象中,以便在鼠标进入时启用焦点并在鼠标退出时禁用它:
使用UnityEngine;公共类 FocusMe:MonoBehaviour{[SerializeField] 私有 FocusSwitcher 焦点;私有无效 OnMouseEnter(){focus.SetFocused(gameObject);}私有无效 OnMouseExit(){//重置焦点//将来你可能应该先检查一下//如果这个对象实际上是当前的焦点对象focus.SetFocused(null);}}
这是它的样子
<小时>如前所述,我不知道您的 VR 设置究竟是什么样子.如果您必须使用 MainCameras,只需向其中添加两个子摄像机即可.您仍然只需要一个 PostProcessingVolume 和一个 FocusSwitcher
,因此您可能会将它们移动到另一个对象并以不同的方式处理相机禁用等,但我希望这个想法足够清晰.
I want to create a blur effect similar to the picture below:
the picture is taken from this site:
https://forum.unity.com/threads/how-to-blur-specific-layers-only.555520/
I tried the post-processing profile and played around with the depth of field in the post-processing volume but it blurs all the scene.
I came across a YouTube Video that explains how to implement similar results to what I am looking for but things have changed drastically in the settings. For example, at 1:57 (minute and 57 seconds) he clicks on Add Additional Camera Data
which I am struggling to find in the latest versions of LWRP.
I am using Unity version 2019.2.9f1
How can I achieve the result in the picture above? (Blurring all the scene except for one object)
Your guidance will be appreciated.
NOTE: My project is in VR using SteamVR and VRTK
Though your question is a bit broad I took some time to show you how it can be done.
First of all you will need to import the Post Processing
package via the PackageManager
Window → PackageManager
Make sure to be in the All Packages
view, search for post
, find the Post Processing
package and hit Install
Now first of all go to the Layer settings (Layers → Edit Layers)
and add two additional Layers: e.g. PostProcessing
and Focused
Now to the cameras. Afaik it makes no difference whether you are in VR or not, usually you have one MainCamera
that is moved along with your headset. If there should be two of them in your project setup just repeat the same steps for the second camera.
- Make sure the
MainCamera
doesn't render the two added Layers → remove them from theCulling Mask
Add a new Camera
FocusCamera
as child to the existingMainCamera
. This way it is automatically moved along with the main Camera.RightClick on
MainCamera
&rightarrow CameraIt should have all the settings equal to the
MainCamera
except:Clear Flags
: Don't Clear
If you set it toDepth Only
the focused object will always be rendered on top of everything, even if it is actually behind other objects in 3D space. You decide which effect you want here ;)CullingMask
: only FocusedDepth
: Anything higher than theMainCamera
so this camera is rendered on top of it- Make sure to remove the
AudioListener
component.
Finally add a new
PostProcessingVolume
to the scene. I would add it as child to theFocusCamera
! Why? - Because this way it is automatically disabled together with theFocusCamera
!RightClick on
FocusCamera
→ 3D Object → Post Processing VolumeSet its
Layer
to the addedPostProcessing
enable
Is Global
so the distance to the volume doesn't matter and add a new profile by hitting new → Unity → Depth of fieldIn your case you want to overwrite the
Focus Distance
so check the box on the left and set a value close to the camera like e.g.0.5
Until now nothing has really changed in your scene.
Now go to the MainCamera
and, a component PostProcessingLayer
and set the Layer
to our added layer PostProcessing
Now everything should be blurred in your scene!
Almost ready to go! Now Disable the FocusCamera
and add this script to it
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;
}
}
This will allow you to focus a certain GameObject on runtime by changing its layer to the Focused
layer we added, the only one that is rendered by the FocusCamera
. So this object will be rendered on top of the image without any blur effect!
For demonstration I just added this simple script to every cube object in order to enable focus on mouse enter and disable it on mouse exit:
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);
}
}
And here is what it looks like
as said I don't know exactly what your VR setup looks like. If you have to MainCameras simply add two child cameras to them. You still will need only one PostProcessingVolume and only one FocusSwitcher
so you would probably move them to another object and handle the camera disabling etc differently but I hope the idea gets clear enough.
这篇关于如何为除一个对象之外的所有场景创建模糊效果(专注于该对象)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!