Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Join us on March 30, 2023, between 5 am & 1 pm EST, in the Performance Profiling Dev Blitz Day 2023 - Q&A forum and Discord where you can connect with our teams behind the Memory and CPU Profilers.
    Dismiss Notice

Question 2D sprites with black backfaces.

Discussion in 'Shaders' started by KPSZH, Dec 28, 2022.

  1. KPSZH

    KPSZH

    Joined:
    Dec 17, 2022
    Posts:
    1
    I'm almost comepletely newbie to Unity. So I wanted to make a new shader that would draw pure black backface of 2D sprites. This is what I have now:
    Code (CSharp):
    1. Shader "Custom/NewSpriteShader"
    2. {
    3.     Properties
    4.     {
    5.         [PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
    6.         _Color ("Tint", Color) = (1,1,1,1)
    7.         [MaterialToggle] PixelSnap ("Pixel snap", Float) = 0
    8.         [HideInInspector] _RendererColor ("RendererColor", Color) = (1,1,1,1)
    9.         [HideInInspector] _Flip ("Flip", Vector) = (1,1,1,1)
    10.         [PerRendererData] _AlphaTex ("External Alpha", 2D) = "white" {}
    11.         [PerRendererData] _EnableExternalAlpha ("Enable External Alpha", Float) = 0
    12.     }
    13.  
    14.     SubShader
    15.     {
    16.         Tags
    17.         {
    18.             "Queue"="Transparent"
    19.             "IgnoreProjector"="True"
    20.             "RenderType"="Opaque"
    21.             "PerformanceChecks"="False"
    22.             "PreviewType"="Plane"
    23.             "CanUseSpriteAtlas"="True"
    24.         }
    25.  
    26.         Lighting Off
    27.         ZWrite Off
    28.         Blend SrcAlpha OneMinusSrcAlpha
    29.         Pass
    30.         {
    31.             Cull Front
    32.             Color (0,0,0,1)
    33.         }
    34.         Pass
    35.         {
    36.         CGPROGRAM
    37.             #pragma vertex SpriteVert
    38.             #pragma fragment SpriteFrag
    39.             #pragma target 2.0
    40.             #pragma multi_compile_instancing
    41.             #pragma multi_compile_local _ PIXELSNAP_ON
    42.             #pragma multi_compile _ ETC1_EXTERNAL_ALPHA
    43.             #include "UnitySprites.cginc"
    44.         ENDCG
    45.         }
    46.     }
    47. }
    Basically, it's a default sprite shader with
    Cull Front
    feature. However, it doesn't work brilliant and it draws backfaces very roughly, like a chunk of polygons.
    How can I make backface use alpha correctly?
     
    Last edited: Dec 28, 2022