Unity自定義SRP(十六):內(nèi)嵌VFX Graph

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

VFX是Unity基于GPU的全新的粒子系統(tǒng),但只支持內(nèi)置的管線(傳統(tǒng),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內(nèi)部可見,即可以使用VFX Graph。我們可以看到HDRP是單獨定義的,因此可以模仿其構(gòu)建一個assembly definition。

assembly definition的文檔參考:https://docs.unity3d.com/Manual/ScriptCompilationAssemblyDefinitionFiles.html

創(chuàng)建一個VFXGraph文件夾,并加入一個Unity.RenderPipelines.HighDefinition.Editor.asmdef文件,內(nèi)容如下:

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

創(chuàng)建好之后,VFXGraph文件夾下就可以訪問所需的內(nèi)容了。接著創(chuàng)建一個自定義的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來設(shè)置模板即可,也可以參考HDRP的模板來編寫或直接使用。

VFXPasses里面主要是SRP中使用的LightModestag的定義,以及一些函數(shù),放于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也要加上,其中包含生成的計算著色器所需要的方法和定義,可以直接復制,當然我們也可以修改為適合自己管線的方法。

最基礎(chǔ)的是模板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);

這樣,基本的內(nèi)嵌就完成了。新建一個Visual Effect組件和一個VFX Graph的效果:


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容