Author Topic: Custom unlit alpha mask texture not working with UIPanel soft clip  (Read 5410 times)

no1no

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 4
    • View Profile
I'm using a custom shader to show avatar inside a circle. It was all good until I put it inside a panel with soft clip option and the shader is not working anymore. I search around the forum and it seems the way to fix this is to modify the custom shader to work with NGUI. However, since I don't have knowledge in programming shader (the code looks like Assembly to me) I don't know where to fix at all. Could someone help me with this?

I'm using the shader code from here: http://www.bensilvis.com/unity3d-unlit-alpha-mask-shader/

  1. // Unlit alpha-blended shader.
  2. // - no lighting
  3. // - no lightmap support
  4. // - no per-material color
  5.  
  6. Shader "Unlit/AlphaMask" {
  7. Properties {
  8.     _MainTex ("Base (RGB)", 2D) = "white" {}
  9.     _AlphaTex ("Alpha mask (R)", 2D) = "white" {}
  10. }
  11.  
  12. SubShader {
  13.     Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
  14.     LOD 100
  15.    
  16.     ZWrite Off
  17.     Blend SrcAlpha OneMinusSrcAlpha
  18.    
  19.     Pass {  
  20.         CGPROGRAM
  21.             #pragma vertex vert
  22.             #pragma fragment frag
  23.            
  24.             #include "UnityCG.cginc"
  25.  
  26.             struct appdata_t {
  27.                 float4 vertex : POSITION;
  28.                 float2 texcoord : TEXCOORD0;
  29.             };
  30.  
  31.             struct v2f {
  32.                 float4 vertex : SV_POSITION;
  33.                 half2 texcoord : TEXCOORD0;
  34.             };
  35.  
  36.             sampler2D _MainTex;
  37.             sampler2D _AlphaTex;
  38.            
  39.             float4 _MainTex_ST;
  40.            
  41.             v2f vert (appdata_t v)
  42.             {
  43.                 v2f o;
  44.                 o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
  45.                 o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
  46.                 return o;
  47.             }
  48.            
  49.             fixed4 frag (v2f i) : SV_Target
  50.             {
  51.                 fixed4 col = tex2D(_MainTex, i.texcoord);
  52.                 fixed4 col2 = tex2D(_AlphaTex, i.texcoord);
  53.                
  54.                 return fixed4(col.r, col.g, col.b, col2.r);
  55.             }
  56.         ENDCG
  57.     }
  58. }
  59.  
  60. }

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Custom unlit alpha mask texture not working with UIPanel soft clip
« Reply #1 on: August 14, 2015, 08:46:35 AM »
I can only suggest comparing two shaders side by side:

Unlit - Transparent Colored
Unlit - Transparent Colored 1

The first one is used when there is no clipping, and the second one is used when there is one panel clipping it. The differences are fairly minor.

no1no

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 4
    • View Profile
Re: Custom unlit alpha mask texture not working with UIPanel soft clip
« Reply #2 on: August 15, 2015, 05:06:51 AM »
I compare the two shaders you suggest and I can see the different but when I tried to modify the AlphaMask shader base on that, nothing change. I guess you need to understand shader language to fix this.

I decided to use UIPanel Texture Mask instead and it looks ok but I have one problem: when I tried to scroll the list with the texture using Texture Mask past the clipping border of the scroll view, the texture will only disapear when all of it goes pass the border while other texture (which don't use texture mask) hid the part that goes past the border. Anyway to fix this?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Custom unlit alpha mask texture not working with UIPanel soft clip
« Reply #3 on: August 15, 2015, 11:12:45 PM »
The way clipping works in NGUI is you can't mix rect-based clipping with texture mask-based clipping. Your best bet is to get your original shader working by making adjustments like in Unlit - Transparent Colored 1.

Keep your original shader ("Unlit/AlphaMask") and make a copy of it -- "Unlit/AlphaMask 1". Make the same modifications as in the Unlit - Transparent Colored 1, and make sure that your shaders are in the Resources folder where they can be loaded. Note where NGUI's shaders are located -- just place them into the same place.

no1no

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 4
    • View Profile
Re: Custom unlit alpha mask texture not working with UIPanel soft clip
« Reply #4 on: August 15, 2015, 11:45:23 PM »
Quote
Keep your original shader ("Unlit/AlphaMask") and make a copy of it -- "Unlit/AlphaMask 1". Make the same modifications as in the Unlit - Transparent Colored 1, and make sure that your shaders are in the Resources folder where they can be loaded. Note where NGUI's shaders are located -- just place them into the same place.
Sorry I didn't say clearly in the original post. I don't set shader directly to UITexture but use it through a material. The shader require 2 components, the main texture and the alpha mask texture.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Custom unlit alpha mask texture not working with UIPanel soft clip
« Reply #5 on: August 18, 2015, 04:23:13 PM »
Be as it may, it doesn't change my suggestion. You need to make a version of your shader that supports clipping and place it in the same place as NGUI's shaders.

no1no

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 4
    • View Profile
Re: Custom unlit alpha mask texture not working with UIPanel soft clip
« Reply #6 on: August 19, 2015, 11:16:07 AM »
So I guess NGUI will try to change the shader to the version that support clipping regardless of whether I'm using material or not. I don't have enough time to spend on this issue so I have to use other solution and change the design a bit but I will try to fix it later. Thank you for your answer, ArenMook.