Author Topic: Sprite UV's at runtime and custom shader not working  (Read 3474 times)

sirival

  • Guest
Sprite UV's at runtime and custom shader not working
« on: May 31, 2012, 12:20:29 PM »
Hello,

I want to create a sky in the background that will have a radial gradient color. I did that by making a custom shader which makes the color change depending on the distance of the scaled uv's from (0.5, 0.5).

Since I want this shader to be applied on a sprite that will cover the whole screen I figured I could use NGUI to do that since I can attach a script that makes the sprite have the same scale as the screen resolution and everything should work. However since I need a different shader than what my other GUI uses, I created a new atlas that uses my shader, and I added a sprite in my GUI that scales with the screen resolution.

This works fine in the editor. However when I build this for the web player I just see a flat color. I tested various changes in the shader code just in case there is an incompatible command somewhere however what I realized is that the reason why this doesn't work is because the UV's of the sprite are not what I expect.

Do you change the uv's of a sprite at runtime? Here is the code for my shader:

  1. Shader "Unlit/RadialGradient" {
  2.         Properties {
  3.                 _Color ("Center Color", Color) = (1,1,1,1)
  4.                 _OuterColor ("OuterColor", Color) = (1,1,1,1)
  5.                 _Radius( "Radius", Float ) = 1.0
  6.                 _Falloff( "Falloff", Float ) = 1.0
  7.         }
  8.         SubShader {
  9.                 Tags { "RenderType"="Opaque" }
  10.                 LOD 200
  11.                
  12.                 Pass
  13.                 {
  14.                         Cull Off
  15.                         Lighting Off
  16.                         ZWrite Off                     
  17.                         Fog { Mode Off }
  18.                         ColorMask RGB                  
  19.                
  20.                         CGPROGRAM
  21.                         #pragma vertex vert
  22.                         #pragma fragment frag                  
  23.  
  24.                         #include "UnityCG.cginc"
  25.                        
  26.                         fixed4 _Color;                                         
  27.                         fixed4 _OuterColor;
  28.                         fixed _Radius;
  29.                         fixed _Falloff;
  30.  
  31.                         struct appdata_t
  32.                         {
  33.                                 float4 vertex : POSITION;                              
  34.                                 float2 texcoord : TEXCOORD0;
  35.                         };
  36.  
  37.                         struct v2f
  38.                         {
  39.                                 float4 vertex : POSITION;                              
  40.                                 float2 texcoord : TEXCOORD0;                           
  41.                         };
  42.  
  43.                         v2f vert (appdata_t v)
  44.                         {
  45.                                 v2f o;
  46.                                 o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);                            
  47.                                 o.texcoord = v.texcoord;                               
  48.                                 return o;
  49.                         }
  50.  
  51.                         fixed4 frag (v2f IN) : COLOR
  52.                         {                              
  53.                                 float2 d = IN.texcoord.xy - float2( 0.5, 0.5 );
  54.                                 float l = length( d );
  55.                                 fixed4 col = lerp( _Color, _OuterColor, l );                           
  56.                                 return pow( col,_Falloff )* _Radius ;
  57.                         }
  58.                         ENDCG
  59.                 }
  60.         }
  61.         FallBack Off
  62. }
  63.  
  64.  

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Sprite UV's at runtime and custom shader not working
« Reply #1 on: May 31, 2012, 02:20:00 PM »
You should use a UITexture instead. No need to create a new atlas just for that. UVs will also be exactly what you expect there.

sirival

  • Guest
Re: Sprite UV's at runtime and custom shader not working
« Reply #2 on: June 01, 2012, 02:12:56 AM »
Awesome! Thanks man ( and for continuously supporting your product... )