Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Blit material color works in editor and certain builds but not others

Discussion in 'General Graphics' started by thawhtaik, Nov 9, 2023.

  1. thawhtaik

    thawhtaik

    Joined:
    Jan 26, 2013
    Posts:
    2
    Hello all, been trying to fix this problem for two days now and thought I'd try asking here. I have a ScriptableRenderPass that takes a SpriteDefault material, and is using that material to blit its color. I'm using this to basically create a fade in / out effect, the kind you might have seen on old NES games. However, while it works in the Unity editor, and the Windows build works on my Windows desktop PC, the Mac build works on one of my Macbooks, but not the other...

    Here is the code for the renderer feature and scriptable render pass below. I left in the comments that I put in to help debug (particularly in the Execute() function), so I can confirm that on the Mac build in question, the renderer handlers, material, shader etc are indeed not null.

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Rendering;
    3. using UnityEngine.Rendering.Universal;
    4.  
    5. namespace Rendering_RendererFeature
    6. {
    7.  
    8.     public class TintColorRenderPassFeature : ScriptableRendererFeature
    9.     {
    10.         public class TintColorRenderPass : ScriptableRenderPass
    11.         {
    12.  
    13.             public RenderTargetIdentifier SourceRendererTarget;
    14.             private Material _TheMaterial;
    15.             private RenderTargetHandle _TempRenderTargetHandle;
    16.             private bool _enabled = false;
    17.  
    18.             private static TintColorRenderPass _Instance;
    19.  
    20.             public TintColorRenderPass(Material NewMaterial)
    21.             {
    22.                 this._TheMaterial = NewMaterial;
    23.  
    24.                 this._TempRenderTargetHandle.Init("TemporaryColorTextureToExample");
    25.  
    26.                 _Instance = this;
    27.             }
    28.  
    29.  
    30.             public static TintColorRenderPass getInstance()
    31.             {
    32.                 return _Instance;
    33.             }
    34.  
    35.  
    36.             public bool isEnabled()
    37.             {
    38.                 return this._enabled;
    39.             }
    40.  
    41.  
    42.             public void enable()
    43.             {
    44.                 this._enabled = true;
    45.                 // Debug.Log("<color=#00FF00> TINT COLOR RENDER PASS ENABLED</color>");
    46.             }
    47.  
    48.  
    49.             public void disable()
    50.             {
    51.                 this._enabled = false;
    52.                 // Debug.Log("<color=#FF0000> TINT COLOR RENDER PASS DISABLED</color>");
    53.             }
    54.  
    55.             // This method is called before executing the render pass.
    56.             // It can be used to configure render targets and their clear state. Also to create temporary render target textures.
    57.             // When empty this render pass will render to the active camera render target.
    58.             // You should never call CommandBuffer.SetRenderTarget. Instead call <c>ConfigureTarget</c> and <c>ConfigureClear</c>.
    59.             // The render pipeline will ensure target setup and clearing happens in a performant manner.
    60.             public override void OnCameraSetup(CommandBuffer cmd, ref RenderingData renderingData)
    61.             {
    62.                 // When a pass is enqueued (in the RenderPassFeature) all the ScriptableRenderer passes run in
    63.                 // a specific order, some calls are consecutive with each pass and some calls are not. You can't
    64.                 // guarantee that the source target you set before the passes are executed exist or will be the same
    65.                 // target by the time your execute is called. You should assign the target every time in Configure or
    66.                 // OnCameraSetup.
    67.                 this.SourceRendererTarget = renderingData.cameraData.renderer.cameraColorTarget;
    68.             }
    69.  
    70.             // Here you can implement the rendering logic.
    71.             // Use <c>ScriptableRenderContext</c> to issue drawing commands or execute command buffers
    72.             // https://docs.unity3d.com/ScriptReference/Rendering.ScriptableRenderContext.html
    73.             // You don't have to call ScriptableRenderContext.submit, the render pipeline will call it at specific points in the pipeline.
    74.             public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
    75.             {
    76.                 Debug.Log("<color=cyan>RENDERING W/ TINT COLOR RENDER PASS</color>");
    77.                 if (this._TheMaterial != null) {
    78.                     Debug.Log("<color=cyan>TINT COLOR RENDER PASS has material!</color>");
    79.                     if (this._TheMaterial.shader != null) {
    80.                         Debug.Log("<color=cyan>TINT COLOR RENDER PASS has material SHADER!</color>");
    81.                     } else {
    82.                         Debug.Log("<color=red>TINT COLOR RENDER PASS is missing material SHADER!</color>");
    83.                     }
    84.                 } else {
    85.                     Debug.Log("<color=red>TINT COLOR RENDER PASS is MISSING material!</color>");
    86.                 }
    87.  
    88.                 if (this._TempRenderTargetHandle != null) {
    89.                     Debug.Log("<color=cyan>Found TempRenderTargetHandle!</color>");
    90.                 } else {
    91.                     Debug.Log("<color=red>DID NOT find TempRenderTargetHandle!</color>");
    92.                 }
    93.  
    94.                 // We can give the command buffer any name we want (easier to see on Frame Debug list)
    95.                 // This command buffer holds the list of rendering commands to execute.
    96.                 CommandBuffer TheCommandBuffer = CommandBufferPool.Get("TintColorRenderPass");
    97.                 TheCommandBuffer.Clear();
    98.  
    99.                 // Can't blit from / to same target, so need to use temporary render target in-between
    100.                 TheCommandBuffer.GetTemporaryRT(this._TempRenderTargetHandle.id, renderingData.cameraData.cameraTargetDescriptor);
    101.  
    102.                 Blit(TheCommandBuffer, this.SourceRendererTarget, this._TempRenderTargetHandle.Identifier(), this._TheMaterial);
    103.                 Blit(TheCommandBuffer, this._TempRenderTargetHandle.Identifier(), this.SourceRendererTarget);
    104.  
    105.                 context.ExecuteCommandBuffer(TheCommandBuffer);
    106.  
    107.                 TheCommandBuffer.Clear();
    108.                 CommandBufferPool.Release(TheCommandBuffer);
    109.             }
    110.  
    111.  
    112.             // called after Execute, use it to clean up anything allocated in Configure
    113.             public override void FrameCleanup(CommandBuffer cmd)
    114.             {
    115.                 cmd.ReleaseTemporaryRT(this._TempRenderTargetHandle.id);
    116.             }
    117.  
    118.             // Cleanup any allocated resources that were created during the execution of this render pass.
    119.             public override void OnCameraCleanup(CommandBuffer cmd)
    120.             {
    121.             }
    122.  
    123.             public void setColor(Color NewColor)
    124.             {
    125.                 // this is for shader
    126.                 // this._TheMaterial.SetColor("_TintColor", NewColor);
    127.  
    128.                 // this for SpriteDefault
    129.                 this._TheMaterial.SetColor("_Color", NewColor);
    130.             }
    131.         } // end TintColorRenderPass
    132.  
    133.         [System.Serializable]
    134.         public class RenderPassSettings {
    135.             public Material MaterialToUse = null;
    136.         }
    137.  
    138.         public RenderPassSettings CustomSettings = new RenderPassSettings();
    139.  
    140.         TintColorRenderPass m_ScriptablePass;
    141.  
    142.         /// <inheritdoc/>
    143.         public override void Create()
    144.         {
    145.             m_ScriptablePass = new TintColorRenderPass(this.CustomSettings.MaterialToUse);
    146.  
    147.             // Configures where the render pass should be injected.
    148.             // m_ScriptablePass.renderPassEvent = RenderPassEvent.AfterRenderingOpaques;
    149.             m_ScriptablePass.renderPassEvent = RenderPassEvent.BeforeRenderingPostProcessing;
    150.         }
    151.  
    152.         // Here you can inject one or multiple render passes in the renderer.
    153.         // This method is called when setting up the renderer once per-camera.
    154.         // Called every frame once per-camera
    155.         public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData)
    156.         {
    157.             if (!this.m_ScriptablePass.isEnabled()) {
    158.                 return;
    159.             }
    160.          
    161.             // m_ScriptablePass.SourceRendererTarget = renderer.cameraColorTarget;
    162.             renderer.EnqueuePass(m_ScriptablePass);
    163.         }
    164.  
    165.  
    166.         public override void SetupRenderPasses(ScriptableRenderer renderer, in RenderingData renderingData)
    167.         {
    168.             m_ScriptablePass.SourceRendererTarget = renderer.cameraColorTarget;
    169.         }
    170.  
    171.     } // end TintColorRenderPassFeature
    172. }
    173.  
    174.  
    To use this, I'm basically enabling / disabling the TintColorRenderPass from another class (UIUtilities, pasted below), and then calling setColor at intervals to get the staggered fading effect I'd like.

    Code (CSharp):
    1.  
    2.  
    3. using System.Collections;
    4. using Player;
    5. using ResourceLoad;
    6. using UnityEngine;
    7. using static Rendering_RendererFeature.TintColorRenderPassFeature;
    8.  
    9.  
    10. namespace GameUI
    11. {
    12.  
    13.     // Monobehavior so we can use Invoke / StartCoroutine etc
    14.     public class UIUtilities : MonoBehaviour
    15.     {
    16.  
    17.         private Color _FadeOutStep1Color = new Color(0.5f, 0.0f, 1.0f);
    18.         private Color _FadeOutStep2Color = new Color(0.0f, 0.0f, 0.75f);
    19.         private Color _FadeOutStep3Color = new Color(0.0f, 0.0f, 0.25f);
    20.         private Color _FadeOutColor = Color.black;
    21.         private Color _FadeInColor = Color.white;
    22.  
    23.         private static float _TIME_BETWEEN_FADE_COLORS = 0.11f;
    24.         private static float _TIME_BETWEEN_FADE_COLORS_QUICK = 0.05f;
    25.  
    26.         private static UIUtilities _Instance;
    27.  
    28.  
    29.         public void Awake()
    30.         {
    31.             _Instance = this;
    32.  
    33.             Cursor.visible = false;
    34.         }
    35.  
    36.  
    37.         public void Start()
    38.         {
    39.          
    40.         }
    41.  
    42.  
    43.         public static UIUtilities getInstance()
    44.         {
    45.             return _Instance;
    46.         }
    47.  
    48.  
    49.         public void OnApplicationQuit()
    50.         {
    51.             this.doResetLogic();
    52.         }
    53.  
    54.  
    55.         public float fadeOutWithDelay(float delay)
    56.         {
    57.             return this._fadeOutWithDelay(delay, _TIME_BETWEEN_FADE_COLORS);
    58.         }
    59.  
    60.  
    61.         public float fadeInWithDelay(float delay)
    62.         {
    63.             return this._fadeInWithDelay(delay, _TIME_BETWEEN_FADE_COLORS);
    64.         }
    65.  
    66.  
    67.         public float fadeOutWithDelayQuick(float delay)
    68.         {
    69.             return this._fadeOutWithDelay(delay, _TIME_BETWEEN_FADE_COLORS_QUICK);
    70.         }
    71.  
    72.  
    73.         public float fadeInWithDelayQuick(float delay)
    74.         {
    75.             return this._fadeInWithDelay(delay, _TIME_BETWEEN_FADE_COLORS_QUICK);
    76.         }
    77.  
    78.  
    79.         public IEnumerator fadeToColor(Color NewColor, float delay)
    80.         {
    81.             yield return new WaitForSeconds(delay);
    82.  
    83.             TintColorRenderPass TheRenderPass = TintColorRenderPass.getInstance();
    84.             TheRenderPass.setColor(NewColor);
    85.  
    86.             yield return null;
    87.         }
    88.  
    89.  
    90.         public void enableTintColorRenderPass()
    91.         {
    92.             TintColorRenderPass TheRenderPass = TintColorRenderPass.getInstance();
    93.             TheRenderPass.enable();
    94.         }
    95.  
    96.  
    97.         public void disableTintColorRenderPass()
    98.         {
    99.             TintColorRenderPass TheRenderPass = TintColorRenderPass.getInstance();
    100.             TheRenderPass.disable();
    101.         }
    102.  
    103.  
    104.         public void doResetLogic()
    105.         {
    106.             TintColorRenderPass TheRenderPass = TintColorRenderPass.getInstance();
    107.             TheRenderPass.setColor(Color.white);
    108.         }
    109.  
    110.  
    111.         private float _fadeOutWithDelay(float delay, float timeBeweenFadeColors)
    112.         {
    113.             this.enableTintColorRenderPass();
    114.  
    115.             // Color Magenta = new Color(1.0f, 0.0f, 1.0f, 1.0f);
    116.             float totalDelay = delay;
    117.             totalDelay += timeBeweenFadeColors;
    118.             totalDelay += timeBeweenFadeColors * 2;
    119.             totalDelay += timeBeweenFadeColors * 3;
    120.  
    121.             StartCoroutine(fadeToColor(this._FadeOutStep1Color, (delay * Time.timeScale)));
    122.             StartCoroutine(fadeToColor(this._FadeOutStep2Color, (delay + timeBeweenFadeColors) * Time.timeScale));
    123.             StartCoroutine(fadeToColor(this._FadeOutStep3Color, (delay + timeBeweenFadeColors * 2) * Time.timeScale));
    124.             StartCoroutine(fadeToColor(this._FadeOutColor, (delay + timeBeweenFadeColors * 3) * Time.timeScale));
    125.          
    126.             // if (disableColorTintAfterFade) {
    127.             //     Invoke("disableTintColorRenderPass", (totalDelay + 0.5f) * Time.timeScale);
    128.             // }
    129.  
    130.             // Invoker.InvokeDelayed(disableTintColorRenderPass, totalDelay + 0.5f);
    131.  
    132.             // must include the extra time delay from disableTintColorRenderPass
    133.             return (totalDelay + 0.15f) * Time.timeScale;
    134.         }
    135.  
    136.  
    137.         private float _fadeInWithDelay(float delay, float timeBeweenFadeColors)
    138.         {
    139.             this.enableTintColorRenderPass();
    140.  
    141.             // Color Magenta = new Color(1.0f, 0.0f, 1.0f, 1.0f);
    142.             float totalDelay = delay;
    143.             totalDelay += timeBeweenFadeColors;
    144.             totalDelay += timeBeweenFadeColors * 2;
    145.             totalDelay += timeBeweenFadeColors * 3;
    146.  
    147.             StartCoroutine(fadeToColor(this._FadeOutStep3Color, (delay * Time.timeScale)));
    148.             StartCoroutine(fadeToColor(this._FadeOutStep2Color, (delay + timeBeweenFadeColors) * Time.timeScale));
    149.             StartCoroutine(fadeToColor(this._FadeOutStep1Color, (delay + timeBeweenFadeColors * 2) * Time.timeScale));
    150.             StartCoroutine(fadeToColor(this._FadeInColor, (delay + timeBeweenFadeColors * 3) * Time.timeScale));
    151.  
    152.             Invoke("disableTintColorRenderPass", (totalDelay + 0.15f) * Time.timeScale);
    153.          
    154.             // Invoker.InvokeDelayed(disableTintColorRenderPass, totalDelay + 0.5f);
    155.  
    156.             // must include the extra time delay from disableTintColorRenderPass
    157.             return (totalDelay + 0.15f) * Time.timeScale;
    158.         }
    159.     }
    160.  
    161.  
    162. }
    So as I said, the result is that it works perfectly in the editor, as well as on builds for my Windows desktop and my older Macbook from 2017. However, it's not working on another Macbook I got in 2021. On this one, whenever I blit, it's just causing the screen to go completely black, instead of tinting the color it's supposed to be. When I take out the blit lines, it obviously doesn't work, but the screen doesn't go black, so I've narrowed it down to those two lines (but obviously the solution could lie anywhere).

    Here's a couple of recordings of how it should look... apologies for the video downloads, but I couldn't think of an easier way to display it for those who might be interested.

    https://cdn.discordapp.com/attachme...045a533df50845a2424ef5dab9e0abe31cb595a6c6ea&

    vs. how it looks on the problem Macbook - note the screen just goes black when it's supposed to be fading:

    https://cdn.discordapp.com/attachme...4b32d735b9b1cfc6825d16fa000c241479419e7850ad&

    If it helps, my project is 2D, and I'm using the Universal Render Pipeline.

    Any ideas for things I can try? It's frustrating that it is working in both editor and Mac and Windows builds, just not this one particular Macbook. I haven't seen any errors on the build logs. Having said that, I'm a total noob with all this render pass stuff, as well as shaders, so any help would be appreciated.

    I am aware that some of the way the render pass class is doing stuff is deprecated, and maybe I should be using the RTHandle class, but I tried that and couldn't get the fade working even in the Editor. I wrote this a while ago and I'm not even sure where I got the original code from, but only noticed the problem recently as I started testing more on actual builds.
     
  2. thawhtaik

    thawhtaik

    Joined:
    Jan 26, 2013
    Posts:
    2
    Well, it took a few days but I found a solution! This thread here https://forum.unity.com/threads/urp-2d-renderer-custom-feature.778946/ led me to this github repo for a blit render pass that gave me what I needed: https://github.com/Cyanilux/URP_BlitRenderFeature.

    Specifically it seems like the code in this blit render pass takes into account whatever version of Unity you're using and makes the blit accordingly. In all my searches over the past few days, I had a lot of trouble figuring out what the right way was to do this fade-blit for my project which is:

    1. 2D
    2. Using the URP
    3. Using a simple material if possible (and just setting the color on said material).

    Going by the posts on that thread, and my searches in general, it seems like I'm not the only one having trouble figuring the proper way to do a blit for 2D. Would be nice to have some straightforward documentation as all the official Unity examples for this kind of thing seem to be in 3D.

    Anyway, I'm very thankful to have found this as my blit-fade now works on my Mac builds. Hopefully it's helpful to more people in the future.