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

Resolved Can't use _MainTex with Unlit Graph

Discussion in 'Shader Graph' started by PiezPiedPy, Apr 8, 2019.

  1. PiezPiedPy

    PiezPiedPy

    Joined:
    Mar 22, 2019
    Posts:
    14
    Hi, I'm procedurally generating a texture and applying it to a material on a plane with `sharedMaterial.mainTexture` this main texture is usually accessed in a shader by using _MainTex

    Using a PBR shader graph works fine but using an Unlit shader graph gives errors, even though the graphs are identical with the only change being the Master node.

    The error is: Compilation error in graph at line 66 (on d3d11): redefinition of '_MainTex'

    btw using shader graph has saved me so much time so far, I owe you lot a full tanker truck of beer :)
     

    Attached Files:

  2. Kink3d

    Kink3d

    Joined:
    Nov 16, 2015
    Posts:
    45
    Hi,

    I believe the issue is that the Unlit LWRP subshader defines _MainTex for use in its meta pass. Changing this requires changing all of LWRP...

    May I ask if there is a specific reason you are using _MainTex?

    We dont recommend you use those shortcuts anymore and instead use `Material.SetTexture("_MyTexture", texture)` instead. This way you can be sure to avoid naming conflicts with properties required by the render pipeline.
     
  3. PiezPiedPy

    PiezPiedPy

    Joined:
    Mar 22, 2019
    Posts:
    14
    Thx for such a fast reply ;)

    No specific reason for using _MainTex other than that is what I was using when writing shaders with the old shader system.

    I changed my script to use `Material.SetTexture` and a different name to _MainTex and all is fine :D
     
  4. jayatubi

    jayatubi

    Joined:
    Dec 9, 2013
    Posts:
    143
    The '_MainTex' is required by the SpriteRenderer if we are going to create an unlit shader graph for sprites.
     
  5. KAJed

    KAJed

    Joined:
    Mar 15, 2013
    Posts:
    122
    Second this. We cannot use SpriteRenderer due to this limitation.

    While we are on the topic, it would be nice if once SpriteRenderer is supported if Unity would provide the Pixel Snapping option that default Sprite shader has as a node.
     
  6. alexandral_unity

    alexandral_unity

    Unity Technologies

    Joined:
    Jun 18, 2018
    Posts:
    163
    There are going to be new specific master nodes to write Shader Graph shaders for 2D sprite rendering, which won't be using the meta pass that defines _MainTex. So once you're able to use the new master nodes, you should also be able to use _MainTex for your intended use case.
     
    KAJed likes this.