Author Topic: writing vertex/fragment shader for NGUI?  (Read 2367 times)

atcampbell

  • Guest
writing vertex/fragment shader for NGUI?
« on: April 22, 2013, 03:51:31 PM »
I'm trying to write a custom shader for NGUI. It's a variation on the Unlit/Transparent Color shader, but it combines the texture and tint colors in a different way.The functionality I want is more complicated than can be done with a fixed function shader, so I'm writing a shader with vertex and fragment functions. The main problem I'm having with the shader is getting shader access to the color tint that can be specified in the UISprite's inspector. Below is a simple vertex/fragment shader I wrote that attempts to duplicate the functionality of Unlit/Transparent Color. The texture comes through fine (_MainTex), but the color (_Color) always seems to be white. Any suggestions on how I can access the tint color? Do I simply need to rename _Color, and if so what is the appropriate name?

Shader "Custom/NGUIVertFragTranspCol" {
 Properties {
     _MainTex ("Base (RGB)", 2D) = "white" {}
     _Color ("Main Color", Color) = (1,1,1,1)
 }
    SubShader
    {
        Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
        LOD 100

        AlphaTest Greater .01
        Blend SrcAlpha OneMinusSrcAlpha

        Pass
        {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"

            struct v2f
            {
                float4 pos : POSITION;
                half2 uv : TEXCOORD0;
            };
            sampler2D _MainTex;
            fixed4 _Color;

            v2f vert(appdata_img v)
            {
                v2f o;
                o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
                o.uv = v.texcoord;
                return o;
            }


            half4 frag(v2f i) : COLOR
            {
                return _Color * tex2D(_MainTex, i.uv);
            }
ENDCG
        }
    }
    Fallback "VertexLit"
}

Thanks!

A. T.

atcampbell

  • Guest
Re: writing vertex/fragment shader for NGUI?
« Reply #1 on: April 23, 2013, 10:27:27 AM »
BTW I figured this out. The tint color was being passed in as vertex colors. So I had to change the type of the vertex shader input argument to appdata_full, which allowed access to the vertex color, and then use the color in the shader. Below is the working shader, which duplicates the functionality of Unlit/Transparent Colored:

Shader "Custom/NGUIVertFragTranspCol" {
 Properties {
     _MainTex ("Base (RGB)", 2D) = "white" {}
 }
    SubShader
    {
        Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
        LOD 100

        //AlphaTest Greater .01
        Blend SrcAlpha OneMinusSrcAlpha

        Pass
        {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"

            struct v2f
            {
                float4 pos : POSITION;
                half2 uv : TEXCOORD0;
                fixed4 color;
            };
            sampler2D _MainTex;

            v2f vert(appdata_full v)
            {
                v2f o;
                o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
                o.uv = v.texcoord;
                o.color = v.color;
                return o;
            }


            half4 frag(v2f i) : COLOR
            {
                return i.color * tex2D(_MainTex, i.uv);
            }
ENDCG
        }
    }
    Fallback "VertexLit"
}