Author Topic: UITexture custom shader not working at all.  (Read 6341 times)

castor76

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 18
    • View Profile
UITexture custom shader not working at all.
« on: February 17, 2016, 10:04:02 AM »
I want to use a custom shader for UITexture so I have done some experiments but I can't never get it to work properly.

The test I have done is using shader from the NGUI itself, just copy pasted into different file and then changing its shader name. ( I know how shader works and so the shader itself is working)

I used the transparent 2 for clipping, but this is what happens.

If I assign test shader to the shader option, and just link its maintexture by API script, it does show the texture, but with no clipping, and it just seems to be using the default one that NGUI is using, because no matter what I do to the shader code, it doesn't change at all. This is also true when I restart the game completely.

So for example if I let shader to out put 0 , for extreme testing, it doesn't... it still shows my texture itself. This is why I believe that NGUI is not using the shader I have told it to.

I have also tried to use material instead, but the same thing happens.

I have gone through forum and found something about dynamicMaterial.. is this something I have to tap into? I don't need to modify the material on the fly.. I just want it to use my custom shader (for example invert)

I don't mind draw calls and all that because there is only one of this widget in the UI.

Any ideas? I think I am using NGUI version 3.7 something.. last year Aug something like that... Not sure.. how do I check for the version number? I would also like to not update NGUI this time, because the project is closed and I don't want to introduce any possible changes other than the one that I am trying to do.

Thanks!

castor76

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 18
    • View Profile
Re: UITexture custom shader not working at all.
« Reply #1 on: February 17, 2016, 10:18:44 AM »
Further experiment tells me that no matter what I do, UITexture always just use Unlit - Transparent Colored only..

There is no use of putting in my own or any other shader to the UITexture inspector options. Or material. It just uses Unlit - Transparent Colored shader no matter what I do...

Am I missing something here?

dogfacedesign

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 3
  • Posts: 33
    • View Profile
Re: UITexture custom shader not working at all.
« Reply #2 on: February 17, 2016, 10:20:42 AM »
Do you have a code snippet or two we could look at?

Cheers.

castor76

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 18
    • View Profile
Re: UITexture custom shader not working at all.
« Reply #3 on: February 17, 2016, 10:27:10 AM »
For the testing purpose, I didn't even use script API to assign shader , only texture.

public UITexture mapTexture;

mapTexture.mainTexture = Get_CurrentMap();

And this works.. it gets the texture and assigns to UITexture. Problem is shader.

I have tried to remove shader from the option and then run ... it automatically creates Unlit - Transparent Colored, which is correct.

When I assign any other shader, it does stay like that in the UITexture options. But! it still uses Unlit - Transparent Colored shader. I can confirm this because when I edit Unlit - Transparent Colored shader, it reflects the changes.

castor76

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 18
    • View Profile
Re: UITexture custom shader not working at all.
« Reply #4 on: February 17, 2016, 10:31:40 AM »
This leads to me thinking that maybe the UITexure is batched in NGUI for some reason, and not a dynamic as I want it to be... I don't know how to set it to... there isn't any options and NGUI code is deep...

But then again, strange thing is that when I assign any other shader, it does uses Unlit - Transparent Colored shader at the end, but it fails to clip from my scrollView parent.

When I select scrollView and then disable clipping, and then enable it again, it then... all of sudden clips! But obviously, this isn't my custom shader..

dogfacedesign

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 3
  • Posts: 33
    • View Profile
Re: UITexture custom shader not working at all.
« Reply #5 on: February 17, 2016, 10:33:38 AM »
Try assigning the shader via code after you set your texture, if you haven't tried that already.  Untested, and off the top of my head:

  1.  
  2. Renderer r = go.GetComponent<Renderer>();
  3. r.sharedMaterial.shader = Resources.Load<Shader>("YourShaderDirUnderResources/ShaderNameWithNoExtension");
  4.  
  5.  

Cheers.

castor76

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 18
    • View Profile
Re: UITexture custom shader not working at all.
« Reply #6 on: February 17, 2016, 10:44:19 AM »
It is not going to work.. I don't think there is even Renderer attached to UITexture widget. I think NGUI handles it somewhere else.. (not sure)

But even when I try it return null.

Renderer ren = mapTexture.GetComponent<Renderer>();

Is null and trying to Debug.Log(ren.sharedMaterial) freezes Unity. (not the usual error pause stuff.. total Unity unresponsive freeze.)

castor76

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 18
    • View Profile
Re: UITexture custom shader not working at all.
« Reply #7 on: February 17, 2016, 10:51:34 AM »
I did more experiments ..

I have tried to assign shader by code after assigning texture.

The UITexture inspector indicates the correctly assigned shader on its shader option, but it still uses Unlit - Transparent Colored.

And I think this has to be batching issue. Somehow, I need to make this UITexture not batched by using the same Unlit - Transparent Colored shader..

dogfacedesign

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 3
  • Posts: 33
    • View Profile
Re: UITexture custom shader not working at all.
« Reply #8 on: February 17, 2016, 10:53:42 AM »
Are you doing anything else funky involving NGUI after you add the shader?  Maybe something under the hood is switching it back?

Still learning NGUI myself, but happy to be a separate set of eyes for you, as it were.

Cheers.

castor76

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 18
    • View Profile
Re: UITexture custom shader not working at all.
« Reply #9 on: February 17, 2016, 11:05:31 AM »
No.. I am not doing anything funky with NGUI after I assigned it.. I don't really touch NGUI code, and the experiment is simple one really..

dogfacedesign

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 3
  • Posts: 33
    • View Profile
Re: UITexture custom shader not working at all.
« Reply #10 on: February 17, 2016, 11:14:31 AM »
If your code/experiment isn't anything proprietary, feel free to bundle it up and post a link to it.  Happy to take a closer look and see what I can find, if anything.

Cheers.

castor76

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 18
    • View Profile
Re: UITexture custom shader not working at all.
« Reply #11 on: February 17, 2016, 11:15:26 AM »
Tried assigning dynamicMaterial.. but it says it is null :(

mapTexture.drawCall.dynamicMaterial.shader = testShader;

Garh....

I even tried :

mapTexture.mainTexture = Get_CurrentMap();
mapTexture.drawCall.dynamicMaterial.shader = testShader;

And this is with the UITexture inspector saying that I have my custom material selected. ( I put one in to test..)

I thnk UITexture when used with its own texture (not shared from atlas) or when supplied my own custom material (and shader) , it should render separately and not use the default shader. but for some reason, it always keep using the default shader... very sad.. Perhaps because the UITexture itself is child of ScrollView?
« Last Edit: February 17, 2016, 11:30:40 AM by castor76 »

castor76

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 18
    • View Profile
Re: UITexture custom shader not working at all.
« Reply #12 on: February 17, 2016, 11:44:13 AM »
Yeap... I think that's it...

If I unparent it off the ScrollView, it seems to work!

So ScrollView is forcing the issue here... humm...... I think NGUI is trying to figure out which type of shader it needs to use to properly apply its clipping to its child widgets and changing it....

Now I got to figure out so that ScrollView doesn't force it upon my UITexture...

I will try applying the texture and shader first under different parent widget and then move it back?

castor76

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 18
    • View Profile
Re: UITexture custom shader not working at all.
« Reply #13 on: February 17, 2016, 12:19:42 PM »
OHHHH

I got it!

Wow... ok so I had to create some shaders with specific naming convention.. original , original 1 2 3 etc... I see!

I think this info should really be available upfront somewhere in the doc under UITexture.. or at ScrollView.. or at Shader section if there is one.

It took me really long time to find this info. Just didn't realize that NGUI was expecting such convention.