Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: khenkel on April 25, 2014, 09:32:51 AM

Title: UITexture - Changing shader in runtime doesn't work
Post by: khenkel on April 25, 2014, 09:32:51 AM
Hey guys,

so I was writing a simple grayscale shader, which can be applied to an UITexture in runtime. But when applying it to the texture it stays the old shader.

Here's the code: (it basically iterates through a bunch of effects when clicking on a button. The important part here is the one with "case GRAYSCALE_INDEX")
  1. public void OnClick()
  2.         {
  3.                 _currentIndex++;
  4.  
  5.                 if(_currentIndex >= Colors.Length)
  6.                         _currentIndex = DEFAULT_INDEX;
  7.  
  8.                 switch(_currentIndex)
  9.                 {
  10.                 case DEFAULT_INDEX:
  11.                         if(_coverTexture.enabled)
  12.                                 _coverTexture.enabled = false;
  13.                         if(DrawTexture.shader != _shaderDefault)
  14.                                 DrawTexture.shader = _shaderDefault;
  15.                         break;
  16.                 case GRAYSCALE_INDEX:
  17.                         if(_coverTexture.enabled)
  18.                                 _coverTexture.enabled = false;
  19.                         if(DrawTexture.shader != ShaderGrayscale)
  20.                                 DrawTexture.shader = ShaderGrayscale;
  21.                         break;
  22.                 default:
  23.                         if(DrawTexture.shader != _shaderDefault)
  24.                                 DrawTexture.shader = _shaderDefault;
  25.                         if(!_coverTexture.enabled)
  26.                                 _coverTexture.enabled = true;
  27.                         _coverTexture.color = Colors[_currentIndex];
  28.                         break;
  29.                 }
  30.  

As I said, I can't see the grayscale shader ingame, it just stays the old one.
But:

I also tried it with a material and modifying its shader, but this didn't work, too.

Can anybody help me here? I am using NGUI 3.5.5. Let me know if you need more information.
Thanks in advance!

Cheers
Kilian
Title: Re: UITexture - Changing shader in runtime doesn't work
Post by: ArenMook on April 25, 2014, 10:21:28 AM
Please update to the latest. If this is regarding what I think it is, it was resolved in one of the recent updates.
Title: Re: UITexture - Changing shader in runtime doesn't work
Post by: khenkel on April 26, 2014, 02:08:34 PM
Thanks for your answer!

Ok will do, I'll give you a shout in case this problem persists.
Title: Re: UITexture - Changing shader in runtime doesn't work
Post by: khenkel on April 28, 2014, 04:15:09 AM
Ok so I updated to 3.5.8 which (unfortunately) did not fix the problem.
The behaviour is the same as explained in the top post.

However I managed to fix the problem with a kind of workaround:

I randomly observed that the shader changed correctly when switching ON an additional overlay UITexture in front of the texture. (I could see the shader change because the overlay was transparent)

So what I did now was enabling and disabling this overlay texture within one frame (so that the player won't notice), and voilá - it works!

I changed
  1. case GRAYSCALE_INDEX:
  2.                 if(_coverTexture.enabled)
  3.                         _coverTexture.enabled = false;
  4.                 if(DrawTexture.shader != ShaderGrayscale)
  5.                         DrawTexture.shader = ShaderGrayscale;
  6.                 break;

to

  1. case GRAYSCALE_INDEX:
  2.                 _coverTexture.enabled = true;
  3.                 _coverTexture.enabled = false;
  4.                 if(DrawTexture.shader != ShaderGrayscale)
  5.                         DrawTexture.shader = ShaderGrayscale;
  6.                 break;

(Notice how the texture gets enabled and disabled in 2 lines ... but it works somehow)

I am not into graphical stuff, so maybe you guys have any idea why it behaves like this?
Title: Re: UITexture - Changing shader in runtime doesn't work
Post by: ArenMook on April 29, 2014, 02:25:27 PM
The code for setting the shader already removes the widget from the panel, which is what happens when you disable the widget, so I'm not sure why it is like that in your case.