Author Topic: Custom shaders and NGUI atlas  (Read 13620 times)

rav3n256

  • Guest
Custom shaders and NGUI atlas
« on: August 04, 2012, 03:05:45 AM »
If I have an atlas using a custom shader, and this shader has an extra property (such as another color called _ColorNew), how do you pass a color to this property in code when working with a UISprite object?  In tutorials for Unity, all you needed do was get access to the renderer.material and call the appropriate setter function (ie. SetColor()).  From my research on this forum and on the web, the conclusion points me to the UISprite.atlas.spritematerial as the point of interest.  Unfortunately, from my tests, setting the color here does work, but it affects the ENTIRE atlas and not the sprite in question.  This brings me to my next similar question, how do you pass a unique color to each sprite in an atlas?

Cheers,
rav3n256

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Custom shaders and NGUI atlas
« Reply #1 on: August 04, 2012, 03:10:14 AM »
You can't. What you're trying to do is set a color for one sprite... but all sprites get bundled into one draw call, so when you change it for one, it changes it for all of them. You actually have to create a new material for whatever widget you're trying to change this way in order for it to work... however when you do that, you will create an extra draw call, and you will have to adjust transform's Z to tell Unity what's in front of what. Your life will become much more complicated.

rav3n256

  • Guest
Re: Custom shaders and NGUI atlas
« Reply #2 on: August 04, 2012, 03:19:08 AM »
Thanks for the speedy reply.  A part of me was expecting this type of answer after spending a few hours on it.  If that's true, how exactly is the color tinting able to work with your shaders exactly?  I can't recall the name, but there is shader in the NGUI library that has a tint property that must be set.  Isn't that similar to my situation?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Custom shaders and NGUI atlas
« Reply #3 on: August 04, 2012, 03:23:44 AM »
Color tinting is done via vertex colors. When I create a mesh for the widgets geometry, the color buffer gets filled in with that of each widget.

zedia.net

  • Guest
Re: Custom shaders and NGUI atlas
« Reply #4 on: October 03, 2013, 09:36:20 AM »
Let's say I really wanted to do this and I didn't care about the extra draw call or managing the z position, how would I do this? I tried this way to no success:

Shader shader = Shader.Find("Unlit/Transparent Colored with a twist");
Material newMaterial = new Material(selectedState.material);
newMaterial.hideFlags = HideFlags.DontSave;
newMaterial.CopyPropertiesFromMaterial(selectedState.material);
newMaterial.shader = shader;
myUISprite.material = newMaterial;

Doesn't really work, when I log the name of the shader right after, it is the changed one, but when I log it some time after, it reverted back

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Custom shaders and NGUI atlas
« Reply #5 on: October 04, 2013, 02:39:02 AM »
You can't do that. Sprite's material comes from the atlas. The only way you can alter it like you're trying to, is to have a different atlas using a different material.

Willem Kokke

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 4
    • View Profile
Re: Custom shaders and NGUI atlas
« Reply #6 on: October 04, 2013, 03:36:54 AM »
I draw some sprites with a material that simulates the photoshop screen layer blend effect.

  • duplicate the original atlas prefab and material
  • change the shader used by the duplicated material
  • make sure the duplicated atlas uses the duplicated material
  • make you sprite use the duplicated atlas

It's not ideal, You will have to redo those steps every time you update the original atlas, but it does work.
It will create an extra draw call, but that's acceptable in my case.
I use a reference atlas as well to handle multiple resolutions, so I have two reference atlasses now, one for each material I use to draw it.

If you require your material/shader to be used on clipped panels, you will have to make sure there (AlphaClip) and (SoftClip) version of your shaders as well, but that was not required for me.