Visual Compositorのためのカスタムノード
Create the node class.
カスタムノードクラスは Unity.Composition.CompositorNode
ベースクラスを継承する必要があります。このクラスは抽象クラスで、メソッドが1つ実装されなければなりません。
public override void Render() { }
このメソッドは Visual Compsitorの想定したタイミングで呼び出されます。
UI Controls
カスタムノードのUI作成には[ExposeField]
アトリビュートをお使いください。
例:
[ExposeField] public Shader shader;
この例ではシェーダーオブジェクト選択するためのドロップダウンフィールドを作成します。シリアライズ可能で、Unity Editor UI対応の型が使えます。
Inputs
他のノードからカスタムノードに入力を得るために、[InputPort]
が利用できます。カスタムノード上でRender
メソッドが呼ばれると、このフィールドで利用可能な値が入ります。
例:
[InputPort] public RenderTexture inputA;
この例では inputA という名前のフィールドを宣言してます。レンダーテクスチャーを受け取る入力ポートです。
あらゆるタイプで、多数のインプットノードを宣言することができます。例えば、Selection Group ノードは HashSet<GameObject>
の値を出力します。
Outputs
Render
メソッドの処理が完了すると、通常、カスタムノードの出力フィールドに接続されたノード群に結果が送られます。これらには [OuputPort]
アトリビュートが使われます。Render
メソッド終了すると出力フィールドを通じて、接続ノードにコピーされます。
例:
[OutputPort] public RenderTexture output;
この例では接続されたノードにRenderTextureを送る出力ポートを作成しています。
Complete Example
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.Composition;
// Any class that inherits Compositor Node will be available to use in the Compositor.
public class MyCustomNode : CompositorNode
{
// Fields that have an InputPort attribute will become input ports in the Compositor Editor.
[InputPort] public RenderTexture inputA;
// Fields that have an OutputPort attribute will become output ports in the Compositor Editor.
[OutputPort] public RenderTexture output;
// Fields that have an ExposeField attribute will have the appropriate ui controls created in
// the node editor. Any serializable field types can be used.
[ExposeField] public Shader shader;
Material mat;
// This method implements the functionality of the node. You can read from input port fields
// and write to output port fields, the Compositor takes care of moving those values around
// the graph.
public override void Render() {
if(inputA == null || shader is null) return;
if(output == null) {
output = new RenderTexture(inputA);
}
if(mat == null) {
mat = new Material(shader);
}
Graphics.Blit(inputA, output, mat);
}
}