Hello Guest

Author Topic: Discoloring of sprites  (Read 3993 times)

kremedved

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 14
    • View Profile
Discoloring of sprites
« on: September 04, 2013, 03:28:45 pm »
Could you help me with making a shader of discoloring of sprites? There are some sprites in my game that need to be discolored, so that I wouldn't have to add graphics in color and with no color. I'm trying to modify the shader, but all I get is that the alpha-channel is displayed as black color.

Shader code:

Code: [Select]
// unlit, vertex colour, alpha blended
// cull off

Shader "tk2d/BlendVertexColor"
{
  Properties
  {
    _MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
  }
 
  SubShader
  {
    Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
    ZWrite Off Lighting Off Cull Off Fog { Mode Off } Blend SrcAlpha OneMinusSrcAlpha
    LOD 110
   
    Pass
    {
      CGPROGRAM
      #pragma vertex vert_vct
      #pragma fragment frag_mult
      #pragma fragmentoption ARB_precision_hint_fastest
      #include "UnityCG.cginc"

      sampler2D _MainTex;
      float4 _MainTex_ST;

      struct vin_vct
      {
        float4 vertex : POSITION;
        float4 color : COLOR;
        float2 texcoord : TEXCOORD0;
      };

      struct v2f_vct
      {
        float4 vertex : POSITION;
        fixed4 color : COLOR;
        float2 texcoord : TEXCOORD0;
      };

      v2f_vct vert_vct(vin_vct v)
      {
        v2f_vct o;
        o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
        o.color = v.color;
        o.texcoord = v.texcoord;
        return o;
      }

      fixed4 frag_mult(v2f_vct i) : COLOR
      {
        fixed4 col = tex2D(_MainTex, i.texcoord) * i.color;
        float coef = 0.3333f;
       
        fixed4 outCol = dot(fixed4(col.x * coef, col.y * coef, col.z * coef, 0), 1f);
        fixed4 ooo = fixed4(outCol.x, outCol.y, outCol.z, 1);
        return ooo;
      }
     
      ENDCG
    }
  }
 
  SubShader
  {
    Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
    ZWrite Off Blend SrcAlpha OneMinusSrcAlpha Cull Off Fog { Mode Off }
    LOD 100

    BindChannels
    {
      Bind "Vertex", vertex
      Bind "TexCoord", texcoord
      Bind "Color", color
    }

    Pass
    {
      Lighting Off
      SetTexture [_MainTex] { combine texture * primary }
    }
  }
}

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Discoloring of sprites
« Reply #1 on: September 04, 2013, 10:02:23 pm »
You're writing out 1 into the alpha channel - you need to leave that exactly as it is in the tk2d shader you modified this from.

kremedved

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: Discoloring of sprites
« Reply #2 on: September 06, 2013, 10:17:16 am »
Like this?

Quote
         fixed4 frag_mult(v2f_vct i) : COLOR
         {
            fixed4 col = tex2D(_MainTex, i.texcoord) * i.color;
            float coef = 0.3333f;
       
            fixed4 outCol = dot(fixed4(col.x * coef, col.y * coef, col.z * coef, 0), 1f);
            fixed4 ooo = fixed4(outCol.x, outCol.y, outCol.z, outCol.w / coef);
            return ooo;
         }
In this case, I really do not like the sprites are drawn on the edges / borders (which begins alpha channel)

Think, i find a solution of my problem ^^

Quote
         fixed4 frag_mult(v2f_vct i) : COLOR
         {
                fixed4 col = tex2D(_MainTex, i.texcoord);
                float3 w = float3(0.2125, 0.7154, 0.0721);
                float outCol = dot(float3(col.x, col.y, col.z), w);
                return fixed4(outCol,outCol,outCol,col.w);
         }