I’m just wondering if there is any difference in performance when scripts create render textures as opposed to having them already created in your project? Also other scripts that create things on the fly, this is the best example, but say things like building shaders embedded in code, some scripts even seem to create the shader every frame (there’s one for doing line rendering I think that does this)
So in various example scripts for effects, like in this edge detection example, RenderTextures are created and released on the fly, clearly it takes a small amount more effort to code and a lot more knowledge to understand what is going on in this script than one written that would just use already RenderTextures etc. so is there any benefit speed wise in doing so, or is it simply good practice in order to keep your projects clear of clutter and perhaps save on memory?
class EdgeDetectEffectNormals extends ImageEffectBase
{
var renderSceneShader : Shader;
private var renderTexture : RenderTexture;
private var shaderCamera : GameObject;
function OnDisable() {
super.OnDisable();
DestroyImmediate (shaderCamera);
if (renderTexture != null) {
RenderTexture.ReleaseTemporary (renderTexture);
renderTexture = null;
}
}
function OnPreRender()
{
if (!enabled || !gameObject.active)
return;
if (renderTexture != null) {
RenderTexture.ReleaseTemporary (renderTexture);
renderTexture = null;
}
renderTexture = RenderTexture.GetTemporary (camera.pixelWidth, camera.pixelHeight, 16);
if (!shaderCamera) {
shaderCamera = new GameObject("ShaderCamera", Camera);
shaderCamera.camera.enabled = false;
shaderCamera.hideFlags = HideFlags.HideAndDontSave;
}
var cam = shaderCamera.camera;
cam.CopyFrom (camera);
cam.backgroundColor = Color(1,1,1,1);
cam.clearFlags = CameraClearFlags.SolidColor;
cam.targetTexture = renderTexture;
cam.RenderWithShader (renderSceneShader, "RenderType");
}
function OnRenderImage (source : RenderTexture, destination : RenderTexture)
{
var mat = material;
mat.SetTexture("_NormalsTexture", renderTexture);
ImageEffects.BlitWithMaterial (mat, source, destination);
if (renderTexture != null) {
RenderTexture.ReleaseTemporary (renderTexture);
renderTexture = null;
}
}
}