docs.unity3d.com
    目次を表示する/隠す

    Visual Compositor カスタムノード

    ノードクラスの作成

    カスタムノードクラスは Unity.Composition.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メソッドの処理が完了すると、通常、カスタムノードの出力フィールドに接続されたノード群に結果が送られます。これらには [OuputPort] アトリビュートが使われます。Renderメソッドが終了すると出力フィールドを通じて、接続ノードにコピーされます。

    例:

    [OutputPort] public RenderTexture output;
    

    この例では接続されたノードにRenderTextureを送る出力ポートを作成しています。

    完成例

    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);
        }
    }
    
    トップに戻る
    Copyright © 2023 Unity Technologies — 商標と利用規約
    • 法律関連
    • プライバシーポリシー
    • クッキー
    • 私の個人情報を販売または共有しない
    • Your Privacy Choices (Cookie Settings)