CommandBufferで簡単なポストエフェクトを作る
前回のおさらい
前回はレンダリングターゲットの切り替えについて学びました。 こちらを先に見ておくと理解が捗ります。 ny-program.hatenablog.com
CommandBufferとは?
CommandBufferとは描画命令の塊を任意のタイミングで行える機能です。
CommandBufferでポストエフェクトを実装してみる
シェーダーは色を反転する簡単なシェーダーです。 これを画面に対して適応させます。
Shader "InversionColor" { Properties { _MainTex ("Texture", 2D) = "white" {} } SubShader { // No culling or depth Cull Off ZWrite Off ZTest Always Pass { CGPROGRAM #pragma vertex vert #pragma fragment frag #include "UnityCG.cginc" struct appdata { float4 vertex : POSITION; float2 uv : TEXCOORD0; }; struct v2f { float2 uv : TEXCOORD0; float4 vertex : SV_POSITION; }; v2f vert (appdata v) { v2f o; o.vertex = UnityObjectToClipPos(v.vertex); o.uv = v.uv; return o; } sampler2D _MainTex; fixed4 frag (v2f i) : SV_Target { fixed4 col = tex2D(_MainTex, i.uv); return 1.0 - col; } ENDCG } } }
これをCommandBufferを使って画面に対して適応させます。
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Rendering; public class CommandBufferDrawMesh : MonoBehaviour { private void Awake() { Initialize(); } private void Initialize() { var camera = GetComponent<Camera>(); if (camera.allowMSAA) { // MSAAがONになっていると正常に動作しない return; } // シェーダー名からシェーダーを取得してマテリアル作成 var material = new Material(Shader.Find("InversionColor")); var commandBuffer = new CommandBuffer(); // ① // // テクスチャのIDを取得するにはShader.PropertyToIDを使う int tempTextureIdentifier = Shader.PropertyToID("_PostEffectTempTexture"); // 一時テクスチャを取得する,サイズは画面と同じ大きさ commandBuffer.GetTemporaryRT(tempTextureIdentifier, -1, -1); // ② // // 現在のレンダーターゲットを一時テクスチャにコピー commandBuffer.Blit(BuiltinRenderTextureType.CurrentActive, tempTextureIdentifier); // ③ // // 一時テクスチャからレンダーターゲットにポストエフェクトを掛けつつ描画 commandBuffer.Blit(tempTextureIdentifier, BuiltinRenderTextureType.CurrentActive, material); // ④ // // 一時テクスチャを解放 commandBuffer.ReleaseTemporaryRT(tempTextureIdentifier); // ⑤ // camera.AddCommandBuffer(CameraEvent.AfterEverything, commandBuffer); } }
処理の流れ
①一時テクスチャを作成
↓
②現在の画面を一時テクスチャにコピー
↓
③一時テクスチャにポストエフェクトをかけて現在の画面にコピー
↓
④一時テクスチャを解放
↓
⑤カメラの描画が全て終わったタイミングで実行するように設定
今回は以上となります。 ここまでご視聴ありがとうございました。