Search Unity

Sprite Shader w/ Tinting

Discussion in 'Shaders' started by MikaMobile, Dec 18, 2014.

  1. MikaMobile

    MikaMobile

    Joined:
    Jan 29, 2009
    Posts:
    845
    I've been making 2D games in Unity for a long time, well before the sprite tools Unity added in 4.something, and for years I've been using this shader to handle 2D characters, but I'm finding it doesn't work properly with Unity's built-in sprites.

    Code (csharp):
    1.  
    2. Shader "My Shaders/AlphaSelfIllum" {
    3.     Properties {
    4.         _Color ("Tint", Color) = (0.25,0.25,0.25,1)
    5.         _MainTex ("Color (RGB) Alpha (A)", 2D) = "white" {}
    6.     }
    7.     Category {
    8.         Tags {"Queue"="Transparent"}
    9.         ZWrite Off
    10.         Blend SrcAlpha OneMinusSrcAlpha
    11.         SubShader {
    12.             Pass {
    13.                 GLSLPROGRAM
    14.                 varying mediump vec2 uv;
    15.                 varying mediump vec4 color;
    16.                
    17.                 #ifdef VERTEX
    18.                 uniform mediump vec4 _Color;
    19.                 void main() {
    20.                     gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
    21.                     uv = gl_MultiTexCoord0.xy;
    22.                     color = vec4(_Color.rgb * 4., _Color.a);
    23.                 }
    24.                 #endif
    25.                
    26.                 #ifdef FRAGMENT
    27.                 uniform lowp sampler2D _MainTex;
    28.                 void main() {
    29.                     gl_FragColor = color * texture2D(_MainTex, uv);
    30.                 }
    31.                 #endif      
    32.                 ENDGLSL
    33.             }
    34.         }
    35.        SubShader {
    36.            Pass {
    37.                SetTexture [_MainTex] {
    38.                    constantColor [_Color]
    39.                    combine texture * constant quad, texture * constant
    40.                }
    41.            }
    42.        }
    43.     }
    44. }
    45.  
    Basically it's a self-illuminated alpha shader, with a key twist - the tint color is "quadded" for lack of a better term so that the texture looks normal when the tint is set to (0.25,0.25,0.25) instead of 1.0. Why? So I can brighten the texture above normal, which is super useful for flashing players/enemies sprites to indicate something (taking damage, being selected, etc).

    So on to my question - anyone have a guess as to why this shader doesn't work with sprite renderers on an iOS device? I'm evaluating whether to try using Unity's built-in sprite features for a future 2D game, and discovered while testing that my trusty old shader looks/functions as expected in the editor when applied to a sprite, but when built to an iOS device it comes out solid white instead.
     
  2. Flailer

    Flailer

    Joined:
    Apr 1, 2014
    Posts:
    66
    Where in this shader is the sprite assigned from the atlas? I can't see how it would work without it.
     
  3. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,408
  4. MikaMobile

    MikaMobile

    Joined:
    Jan 29, 2009
    Posts:
    845
    Ah, didn't know where to find the source for the built in shaders, thanks for that. Regarding not assigning from the atlas - that doesn't seem to be the issue, as my old shader doesn't behave correctly regardless of whether a unity-made atlas is used or not. Still a bit of a mystery why it works in the editor but not on the device. At any rate, simply modifying the default sprite shader worked just fine.
     
    Flailer likes this.