Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: mdeletrain on June 11, 2012, 09:14:32 AM

Title: Clipped shaders compatibility
Post by: mdeletrain on June 11, 2012, 09:14:32 AM
Hi all,

Happy user of a Pro version of NGUI, I've encountered a porting issue on Android (on a Galaxy S II), using soft clipped panels : nothing was clipped. It happens the same goes for alpha and hard clipping too.
I finally managed to discover what was wrong : the "#pragma fragmentoption ARB_precision_hint_fastest" directive is not supported on those devices. I also discovered it is not supported on some MacMinis neither, so the problem probably spreads to a lot of other systems.

To fix that, I duplicated the subshader, just removing the aforementioned line of code, so that compatibility-based shader auto selection takes effect. It works, but I find it quite ugly to have the exact same shader duplicated just to remove a single line of code (in particular when its a pragma declaring a 'fragmentoption'  :P).

Isn't their a better way to achieve that using Unity's shader language ? (If not, I think its time for a feature request :) : optional options )
In all cases, could a fix be committed to NGUI's repository ?


EDIT: attached one of the modified shaders, with just a bit of code factorization using a Category. For the others, well, you've got the idea  ;D...

  1. Shader "Unlit/Transparent Colored (SoftClip)"
  2. {
  3.         Properties
  4.         {
  5.                 _MainTex ("Base (RGB), Alpha (A)", 2D) = "white" {}
  6.         }
  7.  
  8.         Category {
  9.  
  10.                 Tags
  11.                 {
  12.                         "Queue" = "Transparent"
  13.                         "IgnoreProjector" = "True"
  14.                         "RenderType" = "Transparent"
  15.                 }
  16.  
  17.                 Cull Off
  18.                 Lighting Off
  19.                 ZWrite Off
  20.                 Fog { Mode Off }
  21.                 ColorMask RGB
  22.                 Blend SrcAlpha OneMinusSrcAlpha
  23.  
  24.                 SubShader
  25.                 {
  26.                         LOD 200
  27.                        
  28.                         Pass
  29.                         {
  30.                                 Offset -1, -1
  31.        
  32.                                 CGPROGRAM
  33.                                 #pragma vertex vert
  34.                                 #pragma fragment frag
  35.                                 #pragma fragmentoption ARB_precision_hint_fastest
  36.        
  37.                                 #include "UnityCG.cginc"
  38.        
  39.                                 sampler2D _MainTex;
  40.                                 float4 _MainTex_ST;
  41.                                 float2 _ClipSharpness = float2(20.0, 20.0);
  42.        
  43.                                 struct appdata_t
  44.                                 {
  45.                                         float4 vertex : POSITION;
  46.                                         fixed4 color : COLOR;
  47.                                         float2 texcoord : TEXCOORD0;
  48.                                 };
  49.        
  50.                                 struct v2f
  51.                                 {
  52.                                         float4 vertex : POSITION;
  53.                                         fixed4 color : COLOR;
  54.                                         float2 texcoord : TEXCOORD0;
  55.                                         float2 worldPos : TEXCOORD1;
  56.                                 };
  57.        
  58.                                 v2f vert (appdata_t v)
  59.                                 {
  60.                                         v2f o;
  61.                                         o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
  62.                                         o.color = v.color;
  63.                                         o.texcoord = v.texcoord;
  64.                                         o.worldPos = TRANSFORM_TEX(v.vertex.xy, _MainTex);
  65.                                         return o;
  66.                                 }
  67.        
  68.                                 fixed4 frag (v2f IN) : COLOR
  69.                                 {
  70.                                         // Softness factor
  71.                                         float2 factor = (float2(1.0, 1.0) - abs(IN.worldPos)) * _ClipSharpness;
  72.                                
  73.                                         // Sample the texture
  74.                                         fixed4 col = tex2D(_MainTex, IN.texcoord) * IN.color;
  75.                                         col.a *= clamp( min(factor.x, factor.y), 0.0, 1.0);
  76.                                         return col;
  77.                                 }
  78.                                 ENDCG
  79.                         }
  80.                 }
  81.  
  82.                 SubShader
  83.                 {
  84.                         LOD 200
  85.                        
  86.                         Pass
  87.                         {
  88.                                 Offset -1, -1
  89.        
  90.                                 CGPROGRAM
  91.                                 #pragma vertex vert
  92.                                 #pragma fragment frag
  93.        
  94.                                 #include "UnityCG.cginc"
  95.        
  96.                                 sampler2D _MainTex;
  97.                                 float4 _MainTex_ST;
  98.                                 float2 _ClipSharpness = float2(20.0, 20.0);
  99.        
  100.                                 struct appdata_t
  101.                                 {
  102.                                         float4 vertex : POSITION;
  103.                                         fixed4 color : COLOR;
  104.                                         float2 texcoord : TEXCOORD0;
  105.                                 };
  106.        
  107.                                 struct v2f
  108.                                 {
  109.                                         float4 vertex : POSITION;
  110.                                         fixed4 color : COLOR;
  111.                                         float2 texcoord : TEXCOORD0;
  112.                                         float2 worldPos : TEXCOORD1;
  113.                                 };
  114.        
  115.                                 v2f vert (appdata_t v)
  116.                                 {
  117.                                         v2f o;
  118.                                         o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
  119.                                         o.color = v.color;
  120.                                         o.texcoord = v.texcoord;
  121.                                         o.worldPos = TRANSFORM_TEX(v.vertex.xy, _MainTex);
  122.                                         return o;
  123.                                 }
  124.        
  125.                                 fixed4 frag (v2f IN) : COLOR
  126.                                 {
  127.                                         // Softness factor
  128.                                         float2 factor = (float2(1.0, 1.0) - abs(IN.worldPos)) * _ClipSharpness;
  129.                                
  130.                                         // Sample the texture
  131.                                         fixed4 col = tex2D(_MainTex, IN.texcoord) * IN.color;
  132.                                         col.a *= clamp( min(factor.x, factor.y), 0.0, 1.0);
  133.                                         return col;
  134.                                 }
  135.                                 ENDCG
  136.                         }
  137.                 }
  138.                
  139.                 SubShader
  140.                 {
  141.                         LOD 100
  142.        
  143.                         AlphaTest Greater .01
  144.                        
  145.                         Pass
  146.                         {
  147.                                 ColorMaterial AmbientAndDiffuse
  148.                                
  149.                                 SetTexture [_MainTex]
  150.                                 {
  151.                                         Combine Texture * Primary
  152.                                 }
  153.                         }
  154.                 }
  155.         }
  156. }
  157.  
Title: Re: Clipped shaders compatibility
Post by: PhilipC on June 12, 2012, 09:50:44 AM
Once i get Mikes approval for this (should be later tonight) I will commit the fixed shaders not having the "#pragma fragmentoption ARB_precision_hint_fastest"

Hopefully tonight you should be able to use the default shader again :).
Title: Re: Clipped shaders compatibility
Post by: PhilipC on June 12, 2012, 01:55:13 PM
All done enjoy!
Title: Re: Clipped shaders compatibility
Post by: mdeletrain on June 14, 2012, 04:00:06 PM
Well, thanks !

Anyway, that's not exactly what I proposed  :P : in the example I gave, I did add a subshader that copies the original one but without the fragment option set, so that devices that do not support it still clip objects properly. But I did not remove the original one, so that devices that do support this option can take benefit of it...

Hence my first question : does Unity allow us to avoid this 99% code redundancy ?
Title: Re: Clipped shaders compatibility
Post by: pretender on December 19, 2012, 05:20:03 PM
how do use this? is this fixed?
Title: Re: Clipped shaders compatibility
Post by: ArenMook on December 20, 2012, 01:31:39 AM
This option was removed from the shaders a long time ago.