Unity 自定义Post Processing 后期效果

测试环境

Unity2018.4.6

Post Processing2.1.7

实现后期效果需要三个文件 ,以GrayScale举例:

 

1.GrayScaleEditor.cs

    此文件主要做面板上控制属性显示

    

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


namespace UnityEditor.Rendering.PostProcessing
{
    [PostProcessEditor(typeof(GrayScale))]
    public sealed class GrayScaleEditor : PostProcessEffectEditor
    {
        SerializedParameterOverride m_Blend;
        public override void OnEnable()
        {
            m_Blend = FindParameterOverride(x => x.m_Blend);
        }
        public override void OnInspectorGUI()
        {
            EditorUtilities.DrawHeaderLabel("GrayScale");
            PropertyField(m_Blend);
        }
    }
}

 

 

2.GrayScale.cs

  GrayScale类需要继承 PostProcessEffectSettings,这里主要是提供给用户的调参属性结构。

  GrayScaleRenderer类继承PostProcessEffectRenderer,重载Render方法,此方法可以通过CommandBuff做效果。

using System;
using UnityEngine.Serialization;
namespace UnityEngine.Rendering.PostProcessing
{
    [Serializable]
    [PostProcess(typeof(GrayScaleRenderer), UnityEngine.Rendering.PostProcessing.PostProcessEvent.AfterStack, "Unity/GrayScale", true)]
    public class GrayScale : PostProcessEffectSettings
    {
        [Range(0f, 1f), Tooltip("Grayscale effect intensity.")]
        public FloatParameter m_Blend = new FloatParameter { value = 0.5f };
        public override bool IsEnabledAndSupported(PostProcessRenderContext context)
        {
            return enabled.value
                     && m_Blend.value > 0f;
        }
    }

#if UNITY_2017_1_OR_NEWER
    [UnityEngine.Scripting.Preserve]
#endif
    internal sealed class GrayScaleRenderer : PostProcessEffectRenderer
    {
        public override void Render(PostProcessRenderContext context)
        {
            var sheet = context.propertySheets.Get(Shader.Find("Test/Grayscale"));
            sheet.properties.SetFloat("_Blend", settings.m_Blend);
            context.command.BlitFullscreenTriangle(context.source, context.destination, sheet, 0);
        }
    }
}

 

注意点:

MaterialPropertyBlock(MPB) 

MaterialPropertyBlock is used by Graphics.DrawMesh and Renderer.SetPropertyBlock. Use it in situations where you want to draw multiple objects with the same material, but slightly different properties. For example, if you want to slightly change the color of each mesh drawn. Changing the render state is not supported.

 

  这是文档的一段说明,Unity建议用MPB的API去替换掉传统的Material.setXXX,有用户测试过,提升还是蛮大的。猜测Unity应该是对材质管理和图形API底层Shader赋值的结构上进行了优化,降低了不必要的查找,拷贝等。

 

3.GrayScale.shader

   shader需要使用HLSL来编写,必须引入文件 StdLib.hlsl

Shader "Test/Grayscale"
{
    HLSLINCLUDE
        #include "Packages/com.unity.postprocessing/PostProcessing/Shaders/StdLib.hlsl"
        TEXTURE2D_SAMPLER2D(_MainTex, sampler_MainTex);
        float _Blend;

        float4 Frag(VaryingsDefault i) : SV_Target
        {
            float4 color = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.texcoord);
            float luminance = dot(color.rgb, float3(0.2126729, 0.7151522, 0.0721750));
            color.rgb = lerp(color.rgb, luminance.xxx, _Blend.xxx);
            return color;
        }
    ENDHLSL

    SubShader
    {
        Cull Off ZWrite Off ZTest Always
        Pass
        {
            HLSLPROGRAM
                #pragma vertex VertDefault
                #pragma fragment Frag
            ENDHLSL
        }
    }
}

 

你可能感兴趣的:(unity)