Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

URP Toon Shader Ambient color turns black on mobile build

Discussion in 'Editor & General Support' started by Reid_Taylor, Feb 20, 2021.

  1. Reid_Taylor

    Reid_Taylor

    Joined:
    Oct 9, 2019
    Posts:
    57
    Hello, I created a urp toon shader with shader graph... this: Screen Shot 2021-02-20 at 1.42.22 PM.png
    And it worked! This was the result: Screen Shot 2021-02-18 at 5.05.44 PM.png
    Pretty cool! But when I built it with Xcode and put it on my phone the grey ambient light turns black...?:
    Display.png
    Anyone understand why this would happen? I cannot figure out...
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,522
    Check if it is really black for those colors or if perhaps the gamma on mobile is just different enough to hide it. You can do that by replacing that solid gray area with a sequence of grays between the current gray and white.
     
  3. Reid_Taylor

    Reid_Taylor

    Joined:
    Oct 9, 2019
    Posts:
    57
    Thanks for your advice! It's my birthday today so I'll try it tomorrow and let you know if it works! Thanks
     
    Kurt-Dekker likes this.
  4. Reid_Taylor

    Reid_Taylor

    Joined:
    Oct 9, 2019
    Posts:
    57
    So I went ahead and tried that and some other things but it seams it isn't that problem no matter how I change it it still turns black... any other ideas?
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,522
    How about wiggle the banding or the colors chosen at the cutoffs for each stage of lightness, see if dramatically moving it from one end to the other brings ANYTHING out of that black area.

    Otherwise I'm not really sure how to inspect what all might not be working. I'm guessing it is some step or stage in your graph that is just failing, but the fact that it's failing only in the low end is weird. Hm. Can you refactor how the banding is created?
     
  6. Reid_Taylor

    Reid_Taylor

    Joined:
    Oct 9, 2019
    Posts:
    57
    Yah I can try reworking it a bit... : ) One thing to note is that it turns black on build but when I use the unity remote to test the shader it looks right. Although that might be because the unity remote is just kind of like screen mirroring and the computer is still the device running the application.
     
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,522
    I'm guessing that's how it works; I haven't used it in years though.

    Before I forget, wanted to add that I thought the appearance of your characters is awesome, I mean when the toon shader is working and all. I want to hide from them and chase them around a map trying to finish them off!

    Oh man, that reminds me of one more thing: if you're on certain versions of Unity you can force Metal or force GL for the iOS build... that might illustrate a difference!
     
  8. Reid_Taylor

    Reid_Taylor

    Joined:
    Oct 9, 2019
    Posts:
    57
    Yah these characters are for our upcoming multiplayer game! Thanks for the complement! And I'll look into forcing Metal or GL!
     
  9. cyanryanlion

    cyanryanlion

    Joined:
    Feb 17, 2020
    Posts:
    25
    I had a very similar issue, toon shader rendered fine in the editor, windows builds and webgl. But on one of my iOS devices it rendered black in the darker side of the shader (was fine on iPhone 8 but issue occurred on iPhone 6S).

    I had warnings come up in the console and inspector for the shader, such as:

    Shader warning in 'Shader Graphs/Toon Color': floating point division by zero at line 1628 (on metal)

    Shader warning in 'Shader Graphs/Toon Color': pow(f, e) will not work for negative f, use abs(f) or conditionally handle negative values if you expect them at line 1648 (on metal)

    Shader warning in 'Shader Graphs/Toon Color': 'Unity_Add_half3': implicit truncation of vector type at line 1760 (on metal)

    I had things like smoothstep node with both edges set to 0.01 which meant there was a division by zero (part of smoothstep function is something divided by "edge1 - edge0" so if they're both the same value it'll be a divide by zero in there), causing undefined behaviour. Also had a power node that had the potential to receive a negative number for the first parameter.

    After fixing these things in the shadergraph it rendered fine on iOS for me. It's difficult to say for sure if this is the same issue as the one you're experiencing but I thought it was worth mentioning just in case. I hope this helps you or anyone else coming across this.
     
  10. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,522
    One possibility is somewhere in your hierarchy you set a zero scale, often on the Z vector.

    If you scale something with

    Code (csharp):
    1. somethingTransform.localScale = new Vector2( width, height);   /// NEVER NEVER NEVER DO THIS!
    Then you just zeroed out the Z scale, and often all child renderers will fail to light properly, usually resulting in black.

    So NEVER use Vector2 for scale. Instead, write it this way:

    Code (csharp):
    1. somethingTransform.localScale = new Vector3( width, height, 1);   // always include the Z = 1
     
  11. Reid_Taylor

    Reid_Taylor

    Joined:
    Oct 9, 2019
    Posts:
    57
    Yup! That (pow(f, e) => -f) was the problem. Thanks!