Manipulating the Stack 操作后期栈 后期处理系列8

Manipulating the Stack 操作后期栈

本文档主要是对Unity官方手册的个人理解与总结(其实以翻译记录为主:>)
仅作为个人学习使用,不得作为商业用途,欢迎转载,并请注明出处。
文章中涉及到的操作都是基于Unity2018.4版本
参考链接:https://docs.unity3d.com/Packages/com.unity.postprocessing@2.1/manual/Manipulating-the-Stack.html

Quick Volumes 快速体

While working on a game you’ll often need to push effect overrides on the stack for time-based events or temporary states. You could dynamically create a global volume on the scene, create a profile, create a few overrides, put them into the profile and assign the profile to the volume but that’s not very practical.
在开发游戏时,您经常需要为基于时间的事件或临时状态下在栈上加入或重写效果。您可以在场景中动态地创建一个全局体,创建一个配置文件,创建一些重写,将它们放到配置文件中,并将概要文件分配给卷,但这并不十分实用。

We provide a QuickVolume method to quickly spawn new volumes in the scene:
们提供了一个QuickVolume方法用于在场景中快速生成新的体积:

public PostProcessVolume QuickVolume(int layer, float priority, params PostProcessEffectSettings[] settings)

First two parameters are self-explanatory. The last parameter takes an array or a list of effects you want to override in this volume.
前两个参数是不言自明的。最后一个参数接受要在此体中重写的效果数组或列表。

Instancing a new effect is fairly straightforward. For instance, to create a Vignette effect and override its enabled & intensity fields:
举例说明一个新的效果是比较直接的。例如,要创建一个Vignette效果并覆盖它的enabled & intensity字段:

var vignette = ScriptableObject.CreateInstance<Vignette>();
vignette.enabled.Override(true);
vignette.intensity.Override(1f);

Now let’s look at a slightly more complex effect. We want to create a pulsating vignette effect entirely from script:
现在让我们看一个稍微复杂一点的效果。我们想创建一个颤动的晕映效果完全从脚本:

using UnityEngine;
using UnityEngine.Rendering.PostProcessing;

public class VignettePulse : MonoBehaviour
{
    PostProcessVolume m_Volume;
    Vignette m_Vignette;

    void Start()
    {
        m_Vignette = ScriptableObject.CreateInstance<Vignette>();
        m_Vignette.enabled.Override(true);
        m_Vignette.intensity.Override(1f);

        m_Volume = PostProcessManager.instance.QuickVolume(gameObject.layer, 100f, m_Vignette);
    }

    void Update()
    {
        m_Vignette.intensity.value = Mathf.Sin(Time.realtimeSinceStartup);
    }

    void OnDestroy()
    {
        RuntimeUtilities.DestroyVolume(m_Volume, true, true);
    }
}

This code creates a new vignette and assign it to a newly spawned volume with a priority of 100. Then, on every frame, it changes the vignette intensity using a sinus curve.
这段代码创建一个新的晕映,并将其分配给一个新生成的体积,该体积的优先级为100。然后,在每一帧,它改变了晕映强度使用正弦曲线。

Important: Don’t forget to destroy the volume and the attached profile when you don’t need them anymore!
重要提示:当您不再需要它们时,不要忘记销毁体积和附带的配置文件!

Fading Volumes 淡出体

Distance-based volume blending is great for most level design use-cases, but once in a while you’ll want to trigger a fade in and/or out effect based on a gameplay event. You could do it manually in an Update method as described in the previous section or you could use a tweening library to do all the hard work for you. A few of these are available for Unity for free, like DOTween, iTween or LeanTween.
基于距离的体积混合对于大多数关卡设计用例来说都是非常棒的,但偶尔你会想要根据游戏玩法事件触发淡入和/或淡出效果。您可以使用上一节描述的更新方法手动完成,也可以使用tweening库为您完成所有繁重的工作。其中一些可以免费用于Unity,比如DOTween、iTween或LeanTween。

Let’s use DOTween for this example. We won’t go into details about it (it already has a good documentation) but this should get you started:
让我们在这个例子中使用DOTween。我们不会详细讨论它(它已经有一个很好的文档),但这应该让你开始:

using UnityEngine;
using UnityEngine.Rendering.PostProcessing;
using DG.Tweening;

public class VignettePulse : MonoBehaviour
{
    void Start()
    {
        var vignette = ScriptableObject.CreateInstance<Vignette>();
        vignette.enabled.Override(true);
        vignette.intensity.Override(1f);

        var volume = PostProcessManager.instance.QuickVolume(gameObject.layer, 100f, vignette);
        volume.weight = 0f;

        DOTween.Sequence()
            .Append(DOTween.To(() => volume.weight, x => volume.weight = x, 1f, 1f))
            .AppendInterval(1f)
            .Append(DOTween.To(() => volume.weight, x => volume.weight = x, 0f, 1f))
            .OnComplete(() =>
            {
                RuntimeUtilities.DestroyVolume(volume, true, true);
                Destroy(this);
            });
    }
}

In this example, like the previous one, we spawn a quick volume with a vignette. We set its weightproperty to 0 as we don’t want it to have any contribution just yet.
在本例中,与前面的示例一样,我们使用一个晕映生成一个快速体。我们把它的权值设为0因为我们不想让它有任何影响。

Then we use the sequencing feature of DOTween to chain a set of tweening events: fade in, pause for a second, fade out and finally destroy the volume and the component itself once it’s done.
然后,我们使用DOTween的排序特性来链接一组渐变事件:淡入、暂停一秒钟、淡出并最终销毁体积和组件本身。

And that’s it. Of course you can also tween individual effect properties instead of the volume as a whole, it’s up to you.
就是这样。当然,你也可以在单独的效果属性之间进行渐变,而不是将体积作为一个整体,这取决于你。

Profile Editing 配置文件编辑

You can also manually edit an existing profile on a volume. It’s very similar to how material scripting works in Unity. There are two ways of doing that: either by modifying the shared profile directly or by requesting a clone of the shared profile that will only be used for this volume.
您还可以手动编辑体积上的现有配置文件。这非常类似于在Unity中编写材质脚本。有两种方法可以做到这一点:直接修改共享配置文件,或者请求只用于体积的共享配置文件的克隆体。

Each method comes with a a few advantages and downsides:
每种方法都有一些优点和缺点:

  • Shared profile editing:
  1. Changes will be applied to all volumes using the same profile 更改将应用于使用相同配置文件的所有体积
  2. Modifies the actual asset and won’t be reset when you exit play mode 修改实际资产,当您退出播放模式时不会重置
  3. Field name: sharedProfile 字段名:sharedProfile
  • Owned profile editing:
  1. Changes will only be applied to the specified volume 更改将只应用于指定的体积
  2. Resets when you exit play mode 退出播放模式时重置
  3. It is your responsibility to destroy the profile when you don’t need it anymore 当你不再需要它的时候,销毁它是你的责任
  4. Field name: profile 字段名:profile

The PostProcessProfile class has a few utility methods to help you manage assigned effects. Notable ones include:
PostProcessProfile 类有一些通用的方法来帮助您管理分配的效果。值得注意的包括:

  • T AddSettings(): creates, adds and returns a new effect or type T to the profile. Will throw an exception if it already exists.
  • PostProcessEffectSettings AddSettings(PostProcessEffectSettings effect): adds and returns an effect you created yourself to the profile.
  • void RemoveSettings(): removes an effect from the profile. Will throw an exception if it doesn’t exist.
  • bool TryGetSettings(out T outSetting): gets an effect from the profile, returns true if one was found, false otherwise.

You’ll find more methods by browsing the /PostProcessing/Runtime/PostProcessProfile.cs source file.

Important: Don’t forget to destroy any manually created profiles or effects.
重要提示:不要忘记销毁任何手动创建的配置文件或效果。

Additional notes 补充说明

If you need to instantiate PostProcessLayer at runtime you’ll need to make sure resources are properly bound to it. After the component has been added, don’t forget to call Init() on it with a reference to the PostProcessResources file as a parameter.
如果需要在运行时实例化PostProcessLayer,则需要确保将资源正确地绑定到它。添加组件之后,不要忘记对其调用Init(),并将对PostProcessResources文件的引用作为参数。

var postProcessLayer = gameObject.AddComponent<PostProcessLayer>();
postProcessLayer.Init(resources);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值