Masking and clipping textures using shaders in NGUI

A question that showed up on Tasharen’s forum, where I happen to be a moderator, was this one:

(..) I have a panel that’s draggable and clipped, which displays a list of GameObjects, each one composed of usernames, avatars, and some other text.

The avatars are not square, they have an hexagonal mask. To achieve this, they are Texture widgets with a special shader I found here. (..) when they get clipped by the panel, they keep showing even outside the boundaries. Only when completely out of sight, they get hidden(..).

Source: http://www.tasharen.com/forum/index.php?topic=5621

First: the reason it gets disabled when its entirely outside the clip area is the way NGUI culls widgets by simply not drawing them. It’s not that the gameObject is disabled, but the UIPanel does not feed this widget into the UIDrawCall’s geometry which is the thing that gets drawn on screen.

The shader he used for the masking was obtained at http://www.zedia.net/2013/masking-textures-using-shaders-ngui/ who based the shader on the old non-clipping version of NGUIs builtin Transparent Alpha shader (it has since changed to not use the combine keyword).

This doesn’t work with the different clipping options that NGUI uses (Alpha Clipping, Soft Clipping) since the UIPanels change the shader used and also feed in the coordinates where it needs to be clipped. It’s very worth noting that the way the UIPanel chooses shaders is entirely based on the name of the shader. This means that when you make a new shader, you should make a similar named shader but with ” (AlphaClip)” added to it, so it’s used when alpha cliping is enabled.

Source: UIDrawCall.cs in NGUI.

Note also that the former HardClipping is entirely deprecated and just changes to be AlphaClip under the surface.

Here’s a shader that works with Alpha Clipping based on the Transparent Colored (AlphaClip) builtin shader in NGUI:

You will have to use the non clipping shader that was provided on the other blog or make one based on the new version of the Transparent Colored shader in NGUI. This shader will get all sorts of weird if you use it in a non-clipping panel.

Clipping and masking using a modified example 7 in NGUI

Clipping and masking using a modified example 7 in NGUI

 

Custom material with a alpha-8 mask texture

I use a custom Material which I put onto a UITexture. If you instantiate the material, you can feed it different mainTextures such as facebook images or something similar.

 

I hope this will be helpful to someone.

Nicki.

 

New website

Hello folks.

I’ve decided to change my website to use wordpress as a base, which will make it easier to make blogposts about stuff I think are interesting. I looked into the solutions for blogs out there, tumblr, blogger but none of them seemed really to offer the freedom I wanted. I first implemented wordpress as a separate blog section on the site, but since the styles clashed and wordpress is so easy to work with, I figured I might as well move the whole thing, and that’s where we are now.

I hope to do some blogging about tips and tricks with Unity3D, NGUI and the other goodies I work with for a living.

Nicki.