Author Topic: Slider with Custom Shader  (Read 4504 times)

Sarper

  • Newbie
  • *
  • Thank You
  • -Given: 5
  • -Receive: 0
  • Posts: 21
    • View Profile
Slider with Custom Shader
« on: August 21, 2015, 01:33:49 PM »
Attached is a screenshot from a shader I've made for a healthbar. It is a very simple shader/mesh effect, using distorted UV coords and a panner.

Is it at all possible to use such a shader and a custom mesh as a slider with the default NGUI classes? If not, what would be the best way to implement this visual effect so that it works as an NGUI widget/slider?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Slider with Custom Shader
« Reply #1 on: August 22, 2015, 02:34:54 AM »
That's a cool effect. If you can do it without custom meshes, then you can do it with NGUI. Use UITexture to draw any custom material. The slider can reference any widget, UITexture included.

Sarper

  • Newbie
  • *
  • Thank You
  • -Given: 5
  • -Receive: 0
  • Posts: 21
    • View Profile
Re: Slider with Custom Shader
« Reply #2 on: August 22, 2015, 05:50:00 AM »
That's a cool effect. If you can do it without custom meshes, then you can do it with NGUI. Use UITexture to draw any custom material. The slider can reference any widget, UITexture included.

Managed to achieve the exact same effect by offsetting the texture coordinates with another texture's r and g channels instead of having a custom mesh with distorted uv's. Which made the shader more versatile too. Works perfectly, thanks a lot!

Sarper

  • Newbie
  • *
  • Thank You
  • -Given: 5
  • -Receive: 0
  • Posts: 21
    • View Profile
Re: Slider with Custom Shader
« Reply #3 on: August 29, 2015, 07:43:04 PM »
I have encountered a strange problem. If I leave the "texture" field of the UITexture null, the widget appears completely transparent in the build only. Looks fine in the Editor. Is this intentional? I'm already assigning a material which contains all the textures and other properties I need and want to leave the texture field null.

Sarper

  • Newbie
  • *
  • Thank You
  • -Given: 5
  • -Receive: 0
  • Posts: 21
    • View Profile
Re: Slider with Custom Shader
« Reply #4 on: August 29, 2015, 07:48:37 PM »
Nevermind, I see that NGUI assumes that I have a "_MainTex" property for the primary texture in my shader if I leave the texture field null. Question is, why is it a requirement, what does NGUI do with the mainTexture property in UITexture.cs?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Slider with Custom Shader
« Reply #5 on: August 30, 2015, 03:33:09 PM »
_MainTex is the standard Unity binding for the main texture. UITexture lets you specify a texture + shader, meaning NGUI will create a material here and set the _MainTex for you.

Sarper

  • Newbie
  • *
  • Thank You
  • -Given: 5
  • -Receive: 0
  • Posts: 21
    • View Profile
Re: Slider with Custom Shader
« Reply #6 on: August 30, 2015, 03:37:20 PM »
_MainTex is the standard Unity binding for the main texture. UITexture lets you specify a texture + shader, meaning NGUI will create a material here and set the _MainTex for you.

When I assign a material, the shader field is removed from the custom UITexture inspector, why not texture field also? My material has all the textures assigned and if I don't use _MainTex in my shader for any textures and don't assign anything to the mainTexture member, UITexture is automatically disabled on the build only. If it's not a bug, then it's a very frustrating way of doing it.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Slider with Custom Shader
« Reply #7 on: August 30, 2015, 03:46:41 PM »
It's just how NGUI works in general. The material you specify isn't used as-is either. A copy of it is created instead. When clipping is used, the shader gets replaced with a clipped version, if one is present. Otherwise clipping won't work at all. I advise you to have a main texture field on your shader just so that it behaves properly. One of the textures you specified can just be renamed to it and NGUI will be happy.