Search Unity

Bug A custom shader suddently stopped being updated after change were made in its code.

Discussion in 'General Graphics' started by Max_Bol, Jan 7, 2021.

  1. Max_Bol

    Max_Bol

    Joined:
    May 12, 2014
    Posts:
    168
    For the last 3 days, I have been working on a shader for a small project of mine.
    It runs through the Built-In rendering pipeline for those who wonders.

    To make things easier for me, I'm usually using Toon Color Pro 2 asset which really helps creating an optimized and simplified shader to start with. (It's especially useful as it allow me to implement the relevant codes for some third party plugins like Curved World 2020 and so on.)

    Long story short, my shader is custom made to allows me to generate "snow" and "dampness" in my scene by simply using 2 global float value. I'm using the simple trick of checking the angle of each faces (via the shader) to compare if the level is alright to display the snow or the texture colors. Dampness is a bit similar, but instead it make the color slightly darker. (The game visual has no specular... or at least for now, but the infrastructure for it is already in if I ever need it.)

    At one point, I decided to scrap the basic color selection for something a bit more controllable: The vertex color.
    This is so that I can change the color of the snow or regular colors by changing the vertex colors from things like explosions or blood. The game is a low polygon styled game. (It's rather simple to modify the color of a vertex from white to red, when a character is shot for example or to add some "dark" spot at the point of an explosion.)

    Still, when I tried to switch the color for the vertex color, I couldn't see any change in the scene and all my previous work on the weather effects stopped working. In case I broke something, I tried to undo my work and even as the code was exactly as before I tried to change the color source, the weather effect was still not visible. (To make it clear, the weather effects were working 100% prior to the small changes that I did undo after the bug started.)

    At one point, I was at a lost of words as to what I could do to fix it or where the problem was.
    I decided to do a funky thing and check if it had any effects: I delete the whole content of the Surface function (the line under the surf() function where you check up the colors from colors pickers and textures.) This means that the model would be, normally, pure black since no color data or alpha data or normal data are being calculated.

    But strangely, that had no effect at all. It was as if the scene's materials were stuck with an old version of the shader.

    Following this, I tried to copy my shader and give its copy a new name. The copy appears in the list of shader for my materials, but the result of selecting it was still the same as before: no change.

    I also tried to restart Unity, but the bug was still present.

    For some reason the Color selection (that I previously attempted to remove) was still working, but some of the other parameters in the material weren't working (or more like were updating anymore as they were also stuck with the old textures, colors and colors)

    As a last resort, I created a new shader from scratch.
    Initially, I attempted to do a small visible change to see if the bug was affecting the new shader.

    The first good news in a the bunch of hours I wasted on this bug: The small change worked perfectly!

    I opened the old shader and the new shader and copy/pasted the additional bits and changes I made in the old one (that doesn't work) into the new one (that works) and the changes and code now work properly in the new shader.

    The old shader is still bugged. I'll put it as a attached file for anyone who might want to check it out and try it out (you'll need at least Curved World 2020 + Toon Color Pro 2 from the asset store to make it work.)

    Have anyone faced this kind of issue before where materials wouldn't update anymore after modifying the shader? I'm posting about it because this was really wrecking my nerves as I was trying to find how to fix this. There might have been a simpler solution than remaking the shader from scratch?
     

    Attached Files:

  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,350
    Sometimes if Unity attempts to compile a shader and there’s a compilation error, it won’t show the error again when you change the shader file. This is especially common if the error was in an included file, but doesn’t have to be. If a shader isn’t updating with the changes you’re making, go to the shader in the project view and right click on it and select reimport. That should at least cause the error to show up again, either in the console or in the inspector. And sometimes it just fixes whatever weird problem was happening causing it to not update.

    If that doesn’t fix the issue, or doesn’t cause an error to show, click on “compile and show code”. That should pop up an incomplete compiled shader, and show errors in the inspector.

    If that doesn’t seem to do anything, check the system task manager for a UnityShaderCompiler.exe that’s frozen or taking a ton of CPU / memory resources, especially if it doesn’t go away when you close Unity. That’s a sign that something in your shader is causing the compiler itself to crash or get stuck in an infinite loop. Those are a huge pain to debug because you’ll get no error message, just a shader that doesn’t update.
     
  3. Max_Bol

    Max_Bol

    Joined:
    May 12, 2014
    Posts:
    168
    Thanks. I did try the "Compile and show code” and it didn't show any error in the inspector.
    As for reimport, I tried to do it manually by copying the file outside of the asset folder, deleting the file in the asset folder + its META file and importing it back in from outside of the asset folder. Didn't work.

    The only thing I haven't tried was if any UnityShaderCompiler.exe was taking more CPU/Memory or if any was remaining after closing Unity. I'll try that if it ever occurs again. My PC, at least, didn't show any form of struggle since I could still play, in the Editor, the scene at 2000+ calculated FPS like always.

    Just in case, since I haven't closed Unity since the restart attempt from the bug yet, I checked the system task manager and couldn't find any that was still taking a lot of resources (Unity is taking 0.05% of my CPU when not in play mode and around 20% while in play mode.)

    Closing Unity didn't leave any UnityShaderCompiler.exe hanging, but I did noticed that the number of UnityShaderCompiler.exe got reduced by half after restarting it so maybe while one wasn't exactly looping or hanging, the engine was stuck with one that wasn't working properly due to a bug that occured while I was modifying the old shader?
    Prior to the restart, 3 of the UnityShaderCompiler.exe (out of 12) were taking about 60MB of memory and the rest was between 20MB and 12MB approx. After restarting, all the 6 UnityShaderCompiler.exe takes 12MB each.

    Still thanks again for the heads up!
     
  4. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    3,023
    We spawn compiler processes on demand per thread that requires shader compilation. The compiler also caches some things, so some memory increase is expected.