• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by deathripper · Oct 01, 2020 at 08:08 AM · shader programming

2D sprite shader

Hey guys, I found the following shader online in a tutorial course, its supposed to turn any 3D or 2D object into a checkerboard looking object. Applying it to a material and a sprite works nicely however it cover the entire sprite (square). I was wondering what would I need to add to the shader (if possible at all) to make it only affect the non-transparent pixels of a sprite.

 Shader "Custom/Checkerboard"
 {
     //show values to edit in inspector
     Properties{
         _Scale("Pattern Size", Range(0,10)) = 1
         _EvenColor("Color 1", Color) = (0,0,0,1)
         _OddColor("Color 2", Color) = (1,1,1,1)
     }
 
         SubShader{
         //the material is completely non-transparent and is rendered at the same time as the other opaque geometry
         Tags{ "RenderType" = "Opaque" "Queue" = "Geometry"}
 
 
         Pass{
             CGPROGRAM
             #include "UnityCG.cginc"
 
             #pragma vertex vert
             #pragma fragment frag
 
             float _Scale;
 
             float4 _EvenColor;
             float4 _OddColor;
 
             struct appdata {
                 float4 vertex : POSITION;
             };
 
             struct v2f {
                 float4 position : SV_POSITION;
                 float3 worldPos : TEXCOORD0;
             };
 
             v2f vert(appdata v) {
                 v2f o;
                 //calculate the position in clip space to render the object
                 o.position = UnityObjectToClipPos(v.vertex);
                 //calculate the position of the vertex in the world
                 o.worldPos = mul(unity_ObjectToWorld, v.vertex);
 
                 return o;
             }
 
             fixed4 frag(v2f i) : SV_TARGET{
                 //scale the position to adjust for shader input and floor the values so we have whole numbers
                 float3 adjustedWorldPos = floor(i.worldPos / _Scale);
                 //add different dimensions 
                 float chessboard = adjustedWorldPos.x + adjustedWorldPos.y + adjustedWorldPos.z;
                 //divide it by 2 and get the fractional part, resulting in a value of 0 for even and 0.5 for off numbers.
                 chessboard = frac(chessboard * 0.5);
                 //multiply it by 2 to make odd values white instead of grey
                 chessboard *= 2;
 
                 //interpolate between color for even fields (0) and color for odd fields (1)
                 float4 color = lerp(_EvenColor, _OddColor, chessboard);
                 return color;
             }
 
             ENDCG
         }
     }
         FallBack "Standard" //fallback adds a shadow pass so we get shadows on other objects
 }

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Klarzahs · Oct 01, 2020 at 09:54 AM

Transparency is stored in the color values as alpha (so color.a). You could set a step value, to divide between "visible" and "non visible" pixels, or do it with alpha directly, which would blend it.

 color.a = step(0.1, color.a);


If the alpha value is above 0.1, it will use the checkerboard, otherwise alpha will be set to 0 and it will be invisible

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

141 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

UNITY_ASSUME_UNIFORM_SCALING 1 Answer

Vertex Color Shader 0 Answers

PBR Shader Graph Textures Stretched on Android 0 Answers

,Unable to change property in shader graph 1 Answer

Reading from Depth Texture in Unity 2021.1.7f1 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges