Search Unity

Two sprite textures shader

Discussion in 'Shaders' started by Andrei_S, Sep 8, 2015.

  1. Andrei_S

    Andrei_S

    Joined:
    Oct 31, 2012
    Posts:
    11
    Hello, everyone!

    I am really new to shaders, but I am trying to write a custom shader for a game I am working on. I am modifying Unity's Sprites/Default shader and what I want the shader to do is:
    • get two sprite textures, tint one of them with the Sprite Renderer tint color and then add both textures.

    Like this:

    I am tinting the bottom sprite (white shirt) with red and adding it to the upper sprite (character). Both textures are PerRendererData and I am setting them by code using MaterialPropertyBlock.

    This is all working right. The problem is that when I put these textures inside an Atlas, the shader don't work anymore.

    It looks like this:


    I believe that the problem is that the shader is usign the same texcoord to both sampler2D. This works on the first case because both textures have the same texcoord, but not on the second one, when they are different (because they are on different parts of the Atlas). But I don't know how to get the different texcoord in the shader.

    My current shader code:
    Code (CSharp):
    1. Shader "Custom/SpriteColor"
    2. {
    3.     Properties
    4.     {
    5.         [PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
    6.         [PerRendererData] _SecondTex ("Sprite Texture", 2D) = "white" {}
    7.         [MaterialToggle] PixelSnap ("Pixel snap", Float) = 0
    8.     }
    9.  
    10.     SubShader
    11.     {
    12.         Tags
    13.         {
    14.             "Queue"="Transparent"
    15.             "IgnoreProjector"="True"
    16.             "RenderType"="Transparent"
    17.             "PreviewType"="Plane"
    18.             "CanUseSpriteAtlas"="True"
    19.         }
    20.  
    21.         Cull Off
    22.         Lighting Off
    23.         ZWrite Off
    24.         Blend One OneMinusSrcAlpha
    25.  
    26.         Pass
    27.         {
    28.         CGPROGRAM
    29.             #pragma vertex vert
    30.             #pragma fragment frag
    31.             #pragma multi_compile _ PIXELSNAP_ON
    32.             #include "UnityCG.cginc"
    33.  
    34.             struct appdata_t
    35.             {
    36.                 float4 vertex   : POSITION;
    37.                 float4 color    : COLOR;
    38.                 float2 texcoord : TEXCOORD0;
    39.             };
    40.  
    41.             struct v2f
    42.             {
    43.                 float4 vertex   : SV_POSITION;
    44.                 fixed4 color    : COLOR;
    45.                 half2 texcoord : TEXCOORD0;
    46.             };
    47.  
    48.             v2f vert(appdata_t IN)
    49.             {
    50.                 v2f OUT;
    51.                 OUT.vertex = mul(UNITY_MATRIX_MVP, IN.vertex);
    52.                 OUT.texcoord = IN.texcoord;
    53.                 OUT.color = IN.color;
    54.                 #ifdef PIXELSNAP_ON
    55.                 OUT.vertex = UnityPixelSnap (OUT.vertex);
    56.                 #endif
    57.  
    58.                 return OUT;
    59.             }
    60.  
    61.             sampler2D _MainTex;
    62.             sampler2D _SecondTex;
    63.  
    64.             fixed4 frag(v2f IN) : SV_Target
    65.             {
    66.                 fixed4 c = tex2D(_MainTex, IN.texcoord);
    67.                 fixed4 s = tex2D(_SecondTex, IN.texcoord) * IN.color;
    68.  
    69.                 s.rgb *= s.a;
    70.                 c.rgb *= c.a;
    71.                 return c + s;
    72.             }
    73.         ENDCG
    74.         }
    75.     }
    76. }
    I need both textures to be on the same shader so that I have the resultant sprite on a single layer. I don't want to use two sprites, one above the other on different layers.

    Maybe I am doing it all wrong and someone may have a better idea. I will gladly accept it. :)

    Thanks!
     
  2. coshea

    coshea

    Joined:
    Dec 20, 2012
    Posts:
    319
    Sorry I know this is an old thread, but found it whilst searching for this issue. Did you solve it in the end? Or perhaps this bump someone else can make some recommendations?

    I'm trying to combine textures on the sprite renderer too.

    Thanks
     
  3. MadeFromPolygons

    MadeFromPolygons

    Joined:
    Oct 5, 2013
    Posts:
    3,980
    You could probably combine 2 normally (like splat maps) but render off screen and then render that to an on-screen quad so that it is effectively a sprite. This is how they do outlining in brawl stars and it could work here without extra magic