Search Unity

Sprite Shader from Shader Graph, doesn't contain _MainTex?

Discussion in 'Shaders' started by imaginaryhuman, Apr 4, 2019.

  1. imaginaryhuman

    imaginaryhuman

    Joined:
    Mar 21, 2010
    Posts:
    5,834
    I've built a shader in Shader Graph (lightweight pipeline) which I want to use on sprites, but on the Sprite Renderer it gives the message:

    "Material does not have a _MainTex texture property. It is required for SpriteRenderer"

    In the shader, the property for the main sprite texture is currently named "MainSprite" and I did not touch the 'reference' which seems to be autogenerated.

    I tried changing the reference to _MainTex or MainTex. This doesn't work, because it turns the shader pink (breaks it), due to a "redefinition of MainTex". Renaming the parameter also has no effect. I have no other use of the term 'maintex' anywhere.

    It seems the sprite renderer wants a parameter in the compiled material to be _MainTex but the only parameters are internally generated reference names, and it's not possible to rename a reference to _MainTex in the shader graph without it totally breaking the shader at the code level.

    Provided I don't try to mess with the reference names the shader does work perfectly fine. I however have no idea if internally this would break batching or sprite sheets or something relating to _MainTex not being able to be accessed?

    How to fix?
    I filed a bug report just in case its a bug, but am i missing something?
     
    Last edited: Apr 4, 2019
    unity_7EOgRZp31vlLtA likes this.
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,339
    Yes, not actually using _MainTex for your texture property's reference name will break sprite sheets, and thus break sprite batching.

    Until they fix this, there's nothing you can do apart from not use Shader Graph.
     
  3. imaginaryhuman

    imaginaryhuman

    Joined:
    Mar 21, 2010
    Posts:
    5,834
    Curious why if I set the texture reference to _MainTex or MainTex, it says there it's a RE-definition, ie it's already defined somewhere in the shader code. I have no nodes using that reference at all, so it doesn't make any sense why unity is putting a maintex in the code when it's not even being used.
     
  4. imaginaryhuman

    imaginaryhuman

    Joined:
    Mar 21, 2010
    Posts:
    5,834
    When I rename any other texture reference to a custom name, there is no problem. Renaming any texture to _MainTex immediately breaks the shader with the pink error and 'redefinition' message in the console.

    Seems unity is defining _MainTex somewhere, but there's no real way to use it?
     
  5. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,339
    Yep.

    It should be noted, this is actually fixed in LWRP 5.10 for 2019.1+
     
  6. imaginaryhuman

    imaginaryhuman

    Joined:
    Mar 21, 2010
    Posts:
    5,834
    Upgraded to the 2019.2 beta, no change.
     
  7. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,339
    Did you update the Lightweight Render Pipeline? It's a separate package, just updating the editor doesn't update that.
     
    MadeFromPolygons likes this.
  8. imaginaryhuman

    imaginaryhuman

    Joined:
    Mar 21, 2010
    Posts:
    5,834
    LWP was updated automatically to 5.2.3, still has the issue.
     
  9. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,339
    5.2.3 is 4 months old, which is why I said 5.10, which is from two weeks ago.
     
  10. imaginaryhuman

    imaginaryhuman

    Joined:
    Mar 21, 2010
    Posts:
    5,834
    oh i though it read 5 point 1.

    there is no option to upgrade to 5.10 . 5.2.3 is the most recent in the 2019.2 beta.
     
  11. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,339
    Click on "See all versions" under 5.2.3
    upload_2019-4-5_14-3-38.png

    You'll see a lot more versions suddenly.
    upload_2019-4-5_14-4-13.png
     
  12. imaginaryhuman

    imaginaryhuman

    Joined:
    Mar 21, 2010
    Posts:
    5,834
    Holy mother of God, that fixed it! Thank you so much :)
     
  13. imaginaryhuman

    imaginaryhuman

    Joined:
    Mar 21, 2010
    Posts:
    5,834
    Hmm. Only strange thing is, as soon as I upgraded the render pipeline, suddenly there is absolutely nothing showing in the game view window. Nothing from any camera at all. Then when I hit play either. Totally blank.
     
  14. imaginaryhuman

    imaginaryhuman

    Joined:
    Mar 21, 2010
    Posts:
    5,834
    i bet this alpha version is just buggy, shouldn't have tried it. going back to an older one.
    ... yep that fixed it.
     
    Last edited: Apr 6, 2019
  15. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,339
    Actually, I think they changed all the shaders from using a ref name of _BaseMap to _MainTex
     
  16. imaginaryhuman

    imaginaryhuman

    Joined:
    Mar 21, 2010
    Posts:
    5,834
    It works now. And in the 2019.2 beta there is support for unlit and lit 2d shaders now.