I have a multipass shadow rendering effect that broke transitioning from 5.6 to 2017.4 that worked like this:
-
Render a quad with a nebula graphic to RT_background rendertexture
-
Render the objects into RT_final_shadows rendertextures and process to make shadows
-
Combine the two, drawing RT_final_shadows over RT_background
-
Render the game on top of this background quad
In 2017.4, the background was rendered black.
I’ve finally identified the problem as an issue with reading+writing to the same rendertexture. Having rendered the background and shadows successfully, the composition code that fails is this:
// Composition shader pass _0_BLEND_SHADOWS
sampler2D _MainTex;
sampler2D _BackgroundTex;
sampler2D _FinalShadowTex;
float _ShadowIntensity;
fixed4 frag (v2f i) : SV_Target{
fixed4 output = tex2D(_MainTex, i.uv);
fixed4 secondary = tex2D(_FinalShadowTex, i.uv);
output *= 1-(secondary.a*_ShadowIntensity);
return output;
}
// Main code
Graphics.Blit(RT_final_shadows, RT_background, compositionMaterial, (int)COMPOSITION_PASS._0_BLEND_SHADOWS);
The background in this case is drawn black in 2017, whether I sample _MainTex (background passed in as source) or _BackgroundTex.
However, if I output to another RT and then copy that to the background, it works…
// Composition shader pass _0_BLEND_SHADOWS
sampler2D _BackgroundTex;
sampler2D _FinalShadowTex;
float _ShadowIntensity;
fixed4 frag (v2f i) : SV_Target{
fixed4 output = tex2D(_BackgroundTex, i.uv);
fixed4 secondary = tex2D(_FinalShadowTex, i.uv);
output *= 1-(secondary.a*_ShadowIntensity);
return output;
}
// Main code
Graphics.Blit(RT_final_shadows, RT_shadowII, compositionMaterial, (int)COMPOSITION_PASS._0_BLEND_SHADOWS);
Graphics.Blit(RT_shadowII, RT_background);
So where in Unity 5.6 I could pass my rendertexture into a Graphics.Blit and draw onto it directly, in 2017 I need an interim texture. This obviously adds a little overhead which is the last thing one wants in a mobile game.
Does anyone know any workaround for this, to be able to read and write to the same rendertexture in a Graphics.Blit?