Author Topic: Using a Custom Shader  (Read 6507 times)

Yuemari

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 12
    • View Profile
Using a Custom Shader
« on: December 07, 2012, 07:45:05 PM »
Hi there people, want to know if somebody had try to use a custom shader with NGUI elements in iPod, in my experience when I change the shader to a custom made the elements of NGUI that use it just disappear, but this doesn't happen in editor, or other objects in the world that use this shader, is there a way to change unlit transparent colored shader and set it to use another? If so, which ones are the requirements to do it? I mean things like variable names for textures, can they use multiple textures?, etc...

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Using a Custom Shader
« Reply #1 on: December 07, 2012, 09:45:04 PM »
Yes it's possible to use a custom shader. You can use a unity's Particle shader for example, or the Transparent series of shaders (if you have a light source). In fact to get clipping to work, NGUI changes the Unlit Transparent shader to another one.

Unlit shaders generally work best.

Yuemari

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 12
    • View Profile
Re: Using a Custom Shader
« Reply #2 on: December 19, 2012, 01:16:44 PM »
I have been trying to use a shader I use in other world effects, this use some variable names that differ from the standard in other Unity built - in shaders, is this a problem for NGUI? When I play the game in the editor I can see everything, but as soon as I compile the game for the IOs device nothing of NGUI  using that shader can be seen, but all the other things in the world using it can, do you have any hint of what is happening?

Yuemari

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 12
    • View Profile
Re: Using a Custom Shader
« Reply #3 on: December 19, 2012, 01:38:39 PM »
If it is useful, this is a sample code of one of the shaders that have this effect:
   
  1. Properties {
  2.  
  3.          
  4.         _MainTexture ("Base layer (RGB)", 2D) = "white" {}
  5.         _DetailTex ("2nd layer (RGB)", 2D) = "white" {}
  6.         _ScrollX ("Base layer Scroll speed X", Float) = 1.0
  7.         _ScrollY ("Base layer Scroll speed Y", Float) = 0.0
  8.         _Scroll2X ("2nd layer Scroll speed X", Float) = 1.0
  9.         _Scroll2Y ("2nd layer Scroll speed Y", Float) = 0.0
  10.         _SineFreqX2 ("2nd layer sine freq X",Float) = 10
  11.         _SineFreqY2 ("2nd layer sine freq Y",Float) = 10
  12.         _MMultiplier ("Layer Multiplier", Float) = 2.0
  13.         _QOffset ("Offset", Vector) = (0,0,0,0)
  14.        
  15. }
  16.  
  17.        
  18. SubShader {
  19.         Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" }
  20.  
  21.         Blend One One
  22.         Cull Off Lighting Off ZWrite Off Fog { Color (0,0,0,0) }
  23.        
  24.         CGINCLUDE
  25.         #pragma multi_compile LIGHTMAP_OFF LIGHTMAP_ON
  26.         #include "UnityCG.cginc"
  27.         sampler2D _MainTexture;
  28.         sampler2D _DetailTex;
  29.  
  30.         half4 _MainTexture_ST;
  31.         half4 _DetailTex_ST;
  32.                
  33.         half _ScrollX;
  34.         half _ScrollY;
  35.         half _Scroll2X;
  36.         half _Scroll2Y;
  37.         half _MMultiplier;
  38.  
  39.         half _SineFreqX2;
  40.         half _SineFreqY2;
  41.         half4 _QOffset;
  42.        
  43.         struct appdata
  44.         {
  45.             half4 vertex : POSITION;
  46.             half4 texcoord : TEXCOORD0;
  47.         };
  48.         struct v2f {
  49.                 half4 pos : SV_POSITION;
  50.                 half4 uv : TEXCOORD0;
  51.                 half4 color : TEXCOORD1;
  52.         };
  53.  
  54.        
  55.         v2f vert (appdata v)
  56.         {
  57.                 v2f o;
  58.                 half4 vPos = mul (UNITY_MATRIX_MV, v.vertex);
  59.                 half zOff = vPos.z*0.05;///_Dist;
  60.                 vPos += _QOffset*zOff*zOff;
  61.                 half4 time = _Time;
  62.                 o.pos = mul (UNITY_MATRIX_P, vPos);
  63.                 o.uv.xy = TRANSFORM_TEX(v.texcoord.xy,_MainTexture) + frac(half2(_ScrollX, _ScrollY) * time);
  64.                 o.uv.zw = TRANSFORM_TEX(v.texcoord.xy,_DetailTex) + frac(half2(_Scroll2X, _Scroll2Y) * time);
  65.                 o.uv.z += sin(time * _SineFreqX2);
  66.                 o.uv.w += sin(time * _SineFreqY2);
  67.                
  68.                 o.color = _MMultiplier.xxxx;
  69.                 return o;
  70.         }
  71.         ENDCG
  72.  
  73.  
  74.         Pass {
  75.                 CGPROGRAM
  76.                 #pragma vertex vert
  77.                 #pragma fragment frag
  78.                 #pragma fragmentoption ARB_precision_hint_fastest              
  79.                 half4 frag (v2f i) : COLOR
  80.                 {
  81.                         half4 o;
  82.                         half4 tex = tex2D (_MainTexture, i.uv.xy);
  83.                         half4 tex2 = tex2D (_DetailTex, i.uv.zw);
  84.                        
  85.                         o = tex * tex2 * i.color;
  86.                        
  87.                        
  88.                         return o;
  89.                 }
  90.                 ENDCG
  91.         }      
  92. }
  93.  

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Using a Custom Shader
« Reply #4 on: December 20, 2012, 01:23:37 AM »
That I don't know. I suggest you experiment and try out different things. Also if your shader needs normals, make sure to enable that option on your panels.

Yuemari

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 12
    • View Profile
Re: Using a Custom Shader
« Reply #5 on: December 21, 2012, 10:45:39 AM »
Ok, I will double check everything and post results. =D