VideoEffectProcessorクラスの実装
このページは映像エフェクトプラグイン作成入門 の一部です。
/icons/-.icon
この記事ではVideoEffectProcessorクラスを実装していきます。
VideoEffectProcessorクラスでは映像エフェクトの動作を設定します。
ファイルの作成
前回実装したSimpleEffect.csを見てください。
20行目辺りのreturn new SimpleEffectProcessor(devices, this);と記述した箇所がエラーになっているはずです。
赤線にマウスを合わせると考えられる修正内容を表示すると出てくるのでクリックします。
https://gyazo.com/49c2ef30f0b65dbba968ddee7311545e
新しいファイルに class 'SimpleEffectProcessor' を生成するをクリックしてください。
https://gyazo.com/8637ea2524d65c15c9026fb55a03c86d
ソリューションエクスプローラーにSimpleEffectProcessor.csが表示されるのでクリックします。
https://gyazo.com/f9fa8c9a1d44f155ccb2d47dee739c11
フィールドとコンストラクターの追加
SimpleEffectクラスの情報やエフェクトの入力画像をこのクラス内で使えるようにします。
このようなコードが入力されている入力されているはずです。
code:SimpleEffectProcessor.cs
using YukkuriMovieMaker.Player.Video;
namespace VideoEffectPluginTutorial
{
internal class SimpleEffectProcessor : IVideoEffectProcessor
{
}
}
次のように書き換えてください。
code:SimpleEffectProcessor.cs
using Vortice.Direct2D1;
using YukkuriMovieMaker.Commons;
using YukkuriMovieMaker.Player.Video;
namespace VideoEffectPluginTutorial
{
class SimpleEffectProcessor : IVideoEffectProcessor
{
readonly SimpleEffect item;
ID2D1Image? input;
public SimpleEffectProcessor(IGraphicsDevicesAndContext devices, SimpleEffect item)
{
this.item = item;
}
}
}
インターフェイスの実装
前回と同じようにVideoEffectProcessorクラス用にも枠組みが用意されています。
IVideoEffectProcessorの部分にエラーを示す赤線が表示されています。
赤線部にマウスを合わせて考えられる修正内容を表示するをクリックします。
https://gyazo.com/ec9ad9a954e49f7e05bb730bfed3e2dd
インターフェイスを実装しますをクリックしてください。
https://gyazo.com/fa2df5b585cede3a37e62d246368db68
次のようなコードが補完されます。
code:SimpleEffectProcessor.cs
using Vortice.Direct2D1;
using YukkuriMovieMaker.Commons;
using YukkuriMovieMaker.Player.Video;
namespace VideoEffectPluginTutorial
{
class SimpleEffectProcessor : IVideoEffectProcessor
{
readonly SimpleEffect item;
ID2D1Image? input;
public SimpleEffectProcessor(IGraphicsDevicesAndContext devices, SimpleEffect item)
{
this.item = item;
}
public ID2D1Image Output => throw new NotImplementedException();
public void ClearInput()
{
throw new NotImplementedException();
}
public void Dispose()
{
throw new NotImplementedException();
}
public void SetInput(ID2D1Image? input)
{
throw new NotImplementedException();
}
public DrawDescription Update(EffectDescription effectDescription)
{
throw new NotImplementedException();
}
}
}
前回のVideoEffectクラスの実装のときと同じようにthrow new NotImplementedException()の部分に必要な実装を行っていきます。
/icons/-.icon
Output
code:実装前.cs
public ID2D1Image Output => throw new NotImplementedException();
code:実装後.cs
public ID2D1Image Output => input ?? throw new NullReferenceException(nameof(input) + " is null");
ここでは映像エフェクトの出力画像を設定しています。
今回はinput(入力画像)をそのまま渡しています。
?? throw new NullReferenceException(nameof(input) + " is null")はinputがnull(空っぽ)だった場合に「inputがnullです」という意味の例外を出すように設定しています。
/icons/-.icon
ClearInput
code:実装前.cs
public void ClearInput()
{
throw new NotImplementedException();
}
code:実装後.cs
public void ClearInput()
{
input = null;
}
ここではinputをnullにしています。
/icons/-.icon
Dispose
code:実装前.cs
public void Dispose()
{
throw new NotImplementedException();
}
code:実装後.cs
public void Dispose()
{
}
一部のオブジェクトはリソースの開放という操作が必要になることがあります。
Disposeメソッドではそれらのオブジェクトのリソースを開放します。
今回はリソースの開放が必要なオブジェクトがないので処理を記述しません。
/icons/-.icon
SetInput
code:実装前.cs
public void SetInput(ID2D1Image? input)
{
throw new NotImplementedException();
}
code:実装後.cs
public void SetInput(ID2D1Image? input)
{
this.input = input;
}
ここではエフェクトの入力画像を設定します。
/icons/-.icon
Update
code:実装前.cs
public DrawDescription Update(EffectDescription effectDescription)
{
throw new NotImplementedException();
}
code:実装後.cs
public DrawDescription Update(EffectDescription effectDescription)
{
return effectDescription.DrawDescription;
}
Updateメソッドはプレビュー画面を更新したときに実行されます。
描画のたびに更新が必要な処理をここに記述します。
EffectDescription
3行目のeffectDescription.DrawDescriptionの値を変えることでアイテムの描画位置などを変更できます。
/icons/-.icon
これでSimpleEffectProcessorクラスの実装は完了です。
ここまで2つのクラスを実装しました。お疲れ様でした。これで作ったエフェクトをYMM4上で映像エフェクトとして使えるようになりました。
次回はこのプロジェクトをビルドしてYMM4上でエフェクトを確認します。
参考
最終的なSimpleEffectProcessor.csは次のようになります。
code:SimpleEffectProcessor.cs
using Vortice.Direct2D1;
using YukkuriMovieMaker.Commons;
using YukkuriMovieMaker.Player.Video;
namespace VideoEffectPluginTutorial
{
class SimpleEffectProcessor : IVideoEffectProcessor
{
readonly SimpleEffect item;
ID2D1Image? input;
public SimpleEffectProcessor(IGraphicsDevicesAndContext devices, SimpleEffect item)
{
this.item = item;
}
public ID2D1Image Output => input ?? throw new NullReferenceException(nameof(input) + " is null");
public void ClearInput()
{
input = null;
}
public void Dispose()
{
}
public void SetInput(ID2D1Image? input)
{
this.input = input;
}
public DrawDescription Update(EffectDescription effectDescription)
{
return effectDescription.DrawDescription;
}
}
}
/icons/-.icon