RenderTextureを使いレンダリング結果をTextureに保存する
レンダリング結果をテクスチャでほしい事ってありますよね
例えばレンダリング結果を鏡にうつしたり G-Bufferをファイルに保存したり
ってことで、RenderTextureを作り カメラにAttachし、レンダリング結果を取得します
まず RenderTextureをCreateします MultiRenderTarget機能があるので、最大8個まで取得できます
newでRenderTextureを作成しますが、幅、高さをピクセルサイズで指定し、ビット深度を指定します 幅、高さは2のn乗にしておきましょう
そして カメラにたいして SetTargetBuffersでアタッチします
なんとこれだけ。簡単ですね
private RenderTexture[] _rt;
public Camera cam;
// Use this for initialization
void Start() {
_rt = new RenderTexture[8];
for (int i=0; i< _rt.Length; i++)
{
_rt[i] = new RenderTexture(2048, 2048, 32);
}
cam.SetTargetBuffers(new RenderBuffer[8] { _rt[0].colorBuffer, _rt[1].colorBuffer, _rt[2].colorBuffer, _rt[3].colorBuffer
, _rt[4].colorBuffer, _rt[5].colorBuffer , _rt[6].colorBuffer, _rt[7].colorBuffer}
, _rt[0].depthBuffer);
}