- Home /
Graphics.Blit() results in empty RenderTexture
So I'm trying to use Graphics.Blit as cross-platform alternative to using Compute Shaders, specifically to merge a bunch of different images together and store them into a Texture2D. I've set up a shader that does this successfully, and a utility function as follows:
public void LayerMerge(Texture2D destination, Texture2D over, Texture2D region = null, int regionIndex = 0, Texture2D under = null)
{
avatarLayerMergeMaterial.SetTexture("_MainTex", destination);
avatarLayerMergeMaterial.SetTexture("_Over", over);
avatarLayerMergeMaterial.SetTexture("_Region",region);
avatarLayerMergeMaterial.SetInt("_RegionIndex",regionIndex);
avatarLayerMergeMaterial.SetTexture("_Under", under == null ? nullTexture : under);
Graphics.Blit(destination,avatarLayerMergeRenderTexture,avatarLayerMergeMaterial,-1);
Graphics.CopyTexture(avatarLayerMergeRenderTexture,destination);
}
The region/region index stuff is unimportant, and for now under is as well. My function call looks like this, using only the first two parameters:
foreach (AvatarLayer layer in layers)
{
textureUtility.LayerMerge(displayTexture,layer.Texture);
}
The problem is that the render texture is completely empty and seems to be completely unaffected by the Blit call. I'm able to look at the actual material instance (avatarLayerMergeMaterial) and it's got a little preview window in the inspector at runtime that displays the correct results. For some reason it's simply not being copied over into the render texture.
Thanks.
Looks like my question is still awaiting moderation for whatever reason, but good news! I solved the issue myself, and unfortunately the problem was related to something I did not mention in the original question: I was using ShaderForge to create the shader in question.
ShaderForge included a "Forward" and a "$$anonymous$$eta" pass. Now my understanding of shaders is fairly limited (hence my using ShaderForge) but removing the meta pass outright seemed to solve the problem completely. At least, as far as I can tell.
This is all based on a test I was running during my lunch break at work, where I was able to reproduce the problem with the same shader, and then solve it - so I have every confidence that it will fix the problem in the actual project in question.
Probably.
Answer by irshad0karim · Mar 04, 2017 at 08:39 AM
(Pasted from the comment I posted earlier while the post was still awaiting moderator approval so the answer is not missed by those struggling with the same problem)
Looks like my question is still awaiting moderation for whatever reason, but good news! I solved the issue myself, and unfortunately the problem was related to something I did not mention in the original question: I was using ShaderForge to create the shader in question.
ShaderForge included a "Forward" and a "Meta" pass. Now my understanding of shaders is fairly limited (hence my using ShaderForge) but removing the meta pass outright seemed to solve the problem completely. At least, as far as I can tell.
This is all based on a test I was running during my lunch break at work, where I was able to reproduce the problem with the same shader, and then solve it - so I have every confidence that it will fix the problem in the actual project in question.
Probably.
Edit:
So this definitely did solve my problem. Instead of just deleting that pass from the shader however, I ended up using the fourth parameter in the Blit() call (pass). It defaults to -1 which means that all passes in the shader should be used. By setting it to 0, only the first pass is used, which had the same effect in fixing my problem. This is beneficial because now I can edit my shader in ShaderForge without having to go back in and manually remove the Meta pass that would inevitably be added every time the shader is compiled.
In case you don't want to use the -1 parameter and you want to specifically give numbers to run several passes, you can copy the source texture parameters (so that they are the same in a new RenderTexture) and pass it. Otherwise, it will pass "destination" which is generally NULL.
For example, run just pass 0 and then 5 and then return to destination the result.
RenderTexture intermediate = new RenderTexture(source);
Graphics.Blit(source, intermediate, bloom$$anonymous$$aterial, 0);
Graphics.Blit(intermediate, destination, bloom$$anonymous$$aterial, 5);
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Using Graphics.Blit with the same source and destination 1 Answer
Graphics.DrawTexture on RenderTexture not working...? 2 Answers
Blur Effect 0 Answers