Search Unity

Help with custom shader (2D)

Discussion in 'Shaders' started by Garrom, May 12, 2017.

  1. Garrom

    Garrom

    Joined:
    Oct 26, 2016
    Posts:
    45
    As start i want to say i have no experience with writing shaders.
    Hi all. I preparing for making 2d game and i need to help with shader. From http://www.6502b.com/Unity_3D_Normal_Map_Sprites.html i copy-pasted sprite shader what alows me using normal maps (code below). But i need to make it capable of using emission maps too. Simple emission maps what are used in standard shader( I guess i can't simply copy-paste emission code from 3d shader to 2d shader ) is enough. Can someone help me ?
    Code (CSharp):
    1. Shader "Sprites/Normal Map Sprite"
    2. {
    3.     Properties
    4.     {
    5.         [PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
    6.         _Color ("Tint", Color) = (1,1,1,1)
    7.         _BumpMap ("Normalmap", 2D) = "bump" {}
    8.         _BumpIntensity ("NormalMap Intensity", Range (-1, 2)) = 1
    9.         _BumpIntensity ("NormalMap Intensity", Float) = 1
    10.         [MaterialToggle] PixelSnap ("Pixel snap", Float) = 0
    11.         _Cutoff ("Alpha Cutoff", Range (0,1)) = 0.5
    12.     }
    13.     SubShader
    14.     {
    15.         Tags
    16.         {
    17.             "Queue"="AlphaTest"
    18.             "IgnoreProjector"="True"
    19.             "RenderType"="TransparentCutOut"
    20.             "PreviewType"="Plane"
    21.             "CanUseSpriteAtlas"="True"
    22.         }
    23.         LOD 300
    24.         Cull Off
    25.         Lighting On
    26.         ZWrite On
    27.         Fog { Mode Off }
    28.         CGPROGRAM
    29.         #pragma target 3.0
    30.         #pragma surface surf Lambert alpha vertex:vert  alphatest:_Cutoff fullforwardshadows
    31.         #pragma multi_compile DUMMY PIXELSNAP_ON
    32.         #pragma exclude_renderers flash
    33.         sampler2D _MainTex;
    34.         sampler2D _BumpMap;
    35.         fixed _BumpIntensity;
    36.        
    37.         fixed4 _Color;
    38.         struct Input
    39.         {
    40.             float2 uv_MainTex;
    41.             float2 uv_BumpMap;
    42.             fixed4 color;
    43.         };
    44.        
    45.         void vert (inout appdata_full v, out Input o)
    46.         {
    47.             #if defined(PIXELSNAP_ON) && !defined(SHADER_API_FLASH)
    48.             v.vertex = UnityPixelSnap (v.vertex);
    49.             #endif
    50.             v.normal = float3(0,0,-1);
    51.             v.tangent =  float4(1, 0, 0, 1);
    52.             UNITY_INITIALIZE_OUTPUT(Input, o);
    53.             o.color = _Color;
    54.         }
    55.         void surf (Input IN, inout SurfaceOutput o)
    56.         {
    57.             fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * IN.color;
    58.             o.Albedo = c.rgb;
    59.             o.Alpha = c.a;
    60.             o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
    61.             _BumpIntensity = 1 / _BumpIntensity;
    62.             o.Normal.z = o.Normal.z * _BumpIntensity;
    63.             o.Normal = normalize((half3)o.Normal);
    64.         }
    65.         ENDCG
    66.     }
    67. Fallback "Transparent/Cutout/Diffuse"
    68. }
     
  2. jvo3dc

    jvo3dc

    Joined:
    Oct 11, 2013
    Posts:
    1,520
    You should be able to add that part quite easily. Just sample an additional texture and store it in o.Emission.
     
  3. Garrom

    Garrom

    Joined:
    Oct 26, 2016
    Posts:
    45
    Thanks for reply. As i said i have no experience with writing shaders. Do you have idea where i can lern basics ?
     
  4. Garrom

    Garrom

    Joined:
    Oct 26, 2016
    Posts:
    45
    Wait a sec. can i use standart shader for sprites ?
     
  5. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,609