Created
November 4, 2015 04:10
-
-
Save sugi-cho/e0c874ebcd196be06010 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
using System.Collections.Generic; | |
using System.Linq; | |
public class MultiRenderTexture : MonoBehaviour | |
{ | |
public RenderTextureUtil util; | |
public Material updateMat; | |
public string[] propNames; | |
List<RenderTexture[]> rtsList; | |
RenderTexture dRt; | |
// Use this for initialization | |
void Start () | |
{ | |
rtsList = new List<RenderTexture[]> (); | |
for (var i=0; i<rtsList.Count; i++) { | |
rtsList.Add (CreateRenderTextures (propNames [i])); | |
} | |
dRt = new RenderTexture (util.texWidth, util.texHeight, 24, RenderTextureFormat.Depth); | |
dRt.filterMode = util.filterMode; | |
dRt.wrapMode = util.wrapMode; | |
} | |
void OnDestroy () | |
{ | |
foreach (var rts in rtsList) { | |
for (var i = 0; i < 2; i++) { | |
if (rts [i] != null) { | |
rts [i].Release (); | |
Destroy (rts [i]); | |
} | |
} | |
} | |
} | |
RenderTexture[] CreateRenderTextures (string name="output") | |
{ | |
var rts = new RenderTexture[2]; | |
for (var i = 0; i<2; i++) { | |
rts [i] = new RenderTexture (util.texWidth, util.texHeight, 0, util.format); | |
rts [i].filterMode = util.filterMode; | |
rts [i].wrapMode = util.wrapMode; | |
rts [i].name = name; | |
} | |
return rts; | |
} | |
void Render (int pass = 0) | |
{ | |
var cBuffers = rtsList.Select (b => b [0].colorBuffer).ToArray (); | |
var dBuffer = dRt.depthBuffer; | |
Graphics.SetRenderTarget (cBuffers, dBuffer); | |
updateMat.DrawFullscreenQuad (pass); | |
SetProps (); | |
SwapRts (); | |
Graphics.SetRenderTarget (null); | |
} | |
void SwapRts () | |
{ | |
foreach (var rts in rtsList) { | |
var tmp = rts [0]; | |
rts [0] = rts [1]; | |
rts [1] = tmp; | |
} | |
} | |
void SetProps () | |
{ | |
Shader.SetGlobalInt ("_MRT_TexWidth", util.texWidth); | |
Shader.SetGlobalInt ("_MRT_TexHeight", util.texHeight); | |
for (var i = 0; i < rtsList.Count; i++) | |
Shader.SetGlobalTexture (rtsList [i] [0].name, rtsList [i] [0]); | |
} | |
[System.Serializable] | |
public class RenderTextureUtil | |
{ | |
public int texWidth = 64; | |
public int texHeight = 64; | |
public RenderTextureFormat format; | |
public FilterMode filterMode; | |
public TextureWrapMode wrapMode; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment