#Visual Compositor カスタムノードクラスの作成
ノードクラスの作成
カスタムノードクラスは,ベースクラスであるUnity.VisualCompositor.CompositorNodeを継承する必要があります.このクラスは抽象ベースのクラスで,実装すべきメソッドは1つだけです。
public override void Render() { }
このメソッドは Visual Compositor の想定したタイミングで呼び出されます。
UI コントロールの作成
カスタムノードの UI 作成には[ExposeField] アトリビュートをお使いください。
例:
[ExposeField] public Shader shader;
この例ではシェーダーオブジェクト選択するためのドロップダウンフィールドを作成します。シリアライズ可能で、Unity Editor UI 対応の型が使えます。
入力
他のノードからカスタムノードに入力を得るために、[InputPort]が利用できます。カスタムノード上でRenderメソッドが呼ばれると、このフィールドで利用可能な値が入ります。
例:
[InputPort] public RenderTexture inputA;
この例では inputA という名前のフィールドを宣言してます。レンダーテクスチャーを受け取る入力ポートです。
あらゆるタイプで、多数のインプットノードを宣言することができます。例えば、Selection Group ノードは HashSet<GameObject>の値を出力します。
出力
通常、Renderメソッドが完了すると、カスタムノードの出力フィールドに接続されたノードにいくつかの出力値を送られます。これは、[OutputPort]アトリビュートを持つフィールドです。メソッドから戻ると、すべての出力フィールドが接続されているノードにコピーされます。
例:
[OutputPort] public RenderTexture output;
この例では接続されたノードにRenderTextureを送る出力ポートを作成しています。
完成例
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.VisualCompositor;
// 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);
}
}