Unity自定义SRP(十六):内嵌VFX Graph

参考:https://forum.unity.com/threads/state-of-vfx-graph-for-custom-srps.951059/

VFX是Unity基于GPU的全新的粒子系统,但只支持内置的管线(传统,URP,HDRP)。我们自己的RP要想使用VFX的话,需要一点技巧。

如果我们查看Packages/Visual Effect Graph/Editor/PackageInfo.cs的话,我们可以看到:

using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("Unity.VisualEffectGraph.EditorTests")]
[assembly: InternalsVisibleTo("Unity.VisualEffectGraph.EditorTests-testable")]
[assembly: InternalsVisibleTo("Unity.VisualEffectGraph.RuntimeTests")]
[assembly: InternalsVisibleTo("Unity.VisualEffectGraph.RuntimeTests-testable")]
[assembly: InternalsVisibleTo("Unity.Testing.VisualEffectGraph.Tests")]
[assembly: InternalsVisibleTo("Unity.Testing.VisualEffectGraph.Tests-testable")]
[assembly: InternalsVisibleTo("Unity.Testing.VisualEffectGraph.EditorTests")]
[assembly: InternalsVisibleTo("Unity.Testing.VisualEffectGraph.EditorTests-testable")]
[assembly: InternalsVisibleTo("Unity.RenderPipelines.HighDefinition.Editor")]
[assembly: InternalsVisibleTo("Unity.RenderPipelines.HighDefinition.Editor-testable")]

以上这些定义了Unity.VisualEffectGraph.Editor这一assembly对哪些assembly内部可见,即可以使用VFX Graph。我们可以看到HDRP是单独定义的,因此可以模仿其构建一个assembly definition。

assembly definition的文档参考:https://docs.unity3d.com/Manual/ScriptCompilationAssemblyDefinitionFiles.html

创建一个VFXGraph文件夹,并加入一个Unity.RenderPipelines.HighDefinition.Editor.asmdef文件,内容如下:

{
    "name": "Unity.RenderPipelines.HighDefinition.Editor",
    "references" : [
        "Unity.VisualEffectGraph.Editor"
    ],
    "includePatforms":[
        "Editor"
    ],
    "excludePlatforms": [],
    "allowUnsafeCode": true,
    "overrideReferences": false,
    "precompiledReferences": [],
    "autoReferenced": true,
    "defineConstraints": []
}

创建好之后,VFXGraph文件夹下就可以访问所需的内容了。接着创建一个自定义的VFX Binder:

using System;

namespace UnityEditor.VFX.HDRP
{
    class CustomVFXBinder : VFXSRPBinder
    {
        public override string templatePath { get { return "Assets/Custom RP/VFXGraph/Editor/Shaders"; } }
        public override string runtimePath { get { return "Assets/Custom RP/VFXGraph/Runtime/Shaders"; } }

        public override string SRPAssetTypeStr { get { return "CustomRenderPipelineAsset"; } }

        public override Type SRPOutputDataType
        {
            get { return null; }
        }
    }
}

注意命名空间,这个VFX Binder与HDRP的类似,主要是告知模板等着色器的路径等。

接下来要做的就是将模板加入到我们的SRP中。参考Packages/Visual Effect Graph/Shaders/RenderPipeline/Universal来设置模板即可,也可以参考HDRP的模板来编写或直接使用。

VFXPasses里面主要是SRP中使用的LightModestag的定义,以及一些函数,放于VFXGraph/Editor/Shaders下:

${VFXBegin:VFXPassForward}"CustomLit"${VFXEnd}
${VFXBegin:VFXPassShadow}"ShadowCaster"${VFXEnd}
${VFXBegin:VFXPassVelocity}"MotionVectors"${VFXEnd}
${VFXBegin:VFXPassForwardAdditionalPragma}#pragma multi_compile_fog${VFXEnd}

${VFXBegin:VFXShaderGraphFunctionsInclude}
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Common.hlsl"
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Packing.hlsl"
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl"
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/EntityLighting.hlsl"
${VFXEnd}

注意VFXCommon.hlslVFXDefines也要加上,其中包含生成的计算着色器所需要的方法和定义,可以直接复制,当然我们也可以修改为适合自己管线的方法。

最基础的是模板VFXParticlePlanarPrimitive.template

{
    SubShader
    {   
        Cull Off
        
        ${VFXInclude("Shaders/VFXParticleHeader.template")}
        ${VFXInclude("Shaders/ParticlePlanarPrimitives/PassSelection.template")}
        ${VFXInclude("Shaders/ParticlePlanarPrimitives/PassForward.template")}
        ${VFXInclude("Shaders/ParticlePlanarPrimitives/PassShadowCaster.template"),USE_CAST_SHADOWS_PASS}
    }
}

这个直接复制就行,该模板对应Shaders下的许多预制Pass。改文件放于VFXGraph/Editor/Shaders/Templates下。

最后记得在管线的遍历摄像机渲染的方法中加上:

UnityEngine.VFX.VFXManager.ProcessCamera(camera);

这样,基本的内嵌就完成了。新建一个Visual Effect组件和一个VFX Graph的效果:


你可能感兴趣的:(Unity自定义SRP(十六):内嵌VFX Graph)