• 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
Question by CarterG81 · Aug 11, 2014 at 04:35 PM · 2dshadershader writinggreyscale

Why does this shader make my 2D sprites disappear if they are rotated 180 degrees? Also, pixel snap?

 Shader "RGB -> Grey Texture/Alpha Blended" {
  
 Properties{
     _MainTex ("Sprite Texture", 2D) = ""
      [MaterialToggle] PixelSnap ("Pixel snap", Float) = 1
 }
  
 Subshader {
     Tags {"Queue"="Transparent"}
     ZWrite Off
     Blend SrcAlpha OneMinusSrcAlpha
     Pass {
         CGPROGRAM
         #pragma vertex vert
         #pragma fragment frag
         struct v2f {
             float4 position : SV_POSITION;
             float2 uv_mainTex : TEXCOORD;
               
 
         };
        
         uniform float4 _MainTex_ST;
         v2f vert(float4 position : POSITION, float2 uv : TEXCOORD0) {
             v2f o;
             o.position = mul(UNITY_MATRIX_MVP, position);
             o.uv_mainTex = uv * _MainTex_ST.xy + _MainTex_ST.zw;
             return o;
         }
        
         uniform sampler2D _MainTex;
         fixed4 frag(float2 uv_mainTex : TEXCOORD) : COLOR {
             fixed4 mainTex = tex2D(_MainTex, uv_mainTex);
             fixed4 fragColor;
             fragColor.rgb = dot(mainTex.rgb, fixed3(.222, .707, .071));
             fragColor.a = mainTex.a;
             return fragColor;
         }
         ENDCG
     }
 }
  
 }

I am not much into shaders, and my game has very limited use for them. One of the only uses I have, is to use shaders to "greyscale" my scenes, almost like the fullscreen effect in the pro license.

I have two questions about this shader, with one of them being a problem.

1) Any time I rotate my sprite gameobjects with this shader, to 180 degrees (horizontal flip), and the sprite vanishes. This is a problem because I flip some sprites horizontally using a 180 degree rotation. All my other shaders work fine, it is this exclusively.

2) How do I add PixelSnap to this shader? I have browsed a bit to try to do it myself, but I failed. Without digging in deeper and learning some shader code (which is honestly unnecessary as this shader is just a placeholder until I get a pro license) I'd like to enable Pixel Snap, if it is as simple as I think it will be.

Thank you :)

Comment
Bunny83

People who like this

1 Show 0
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

3 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by frogsbo · Aug 11, 2014 at 06:36 PM

you have to add Cull Off

to avoid backface culling of mesh, put it after zwrite off

Comment
CarterG81
Bunny83
jilleJr
correia55
FoxyShadow
aouniaouni

People who like this

6 Show 1 · 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
avatar image CarterG81 · Aug 12, 2014 at 03:14 AM 0
Share

Thanks. That was so simple, and looking over the shader compared to another with Pixel Snap, it just clicked and was easy to add pixel snap too.

Sometimes it's best to just step away and not program for a bit, come back, and bam 3 seconds later problem solved.

avatar image

Answer by CarterG81 · Aug 12, 2014 at 03:13 AM

Greyscale effect shader for 2D Sprites + Pixel Snap + rotation enabled

 Shader "RGB -> Grey Texture/Alpha Blended" {
  
 Properties{
     _MainTex ("Sprite Texture", 2D) = ""
      [MaterialToggle] PixelSnap ("Pixel snap", Float) = 1
 }
  
 Subshader {
     Tags {"Queue"="Transparent"}
     ZWrite Off
     Blend SrcAlpha OneMinusSrcAlpha
     Cull Off
     Lighting Off
     Fog { Mode Off }
     
     Pass {
         CGPROGRAM
         #pragma vertex vert
         #pragma fragment frag
             #pragma multi_compile DUMMY PIXELSNAP_ON
             #pragma target 3.0
             #include "UnityCG.cginc"
             
         struct v2f {
             float4 position : SV_POSITION;
             float2 uv_mainTex : TEXCOORD;                
         };
         
 
        
         uniform float4 _MainTex_ST;
         
         v2f vert(float4 position : POSITION, float2 uv : TEXCOORD0) {
             v2f o;
             o.position = mul(UNITY_MATRIX_MVP, position);
             o.uv_mainTex = uv * _MainTex_ST.xy + _MainTex_ST.zw;
             
             #ifdef PIXELSNAP_ON
             o.position = UnityPixelSnap (o.position);
             #endif
             
             return o;
         }
        
         uniform sampler2D _MainTex;
         fixed4 frag(float2 uv_mainTex : TEXCOORD) : COLOR {
             fixed4 mainTex = tex2D(_MainTex, uv_mainTex);
             fixed4 fragColor;
             fragColor.rgb = dot(mainTex.rgb, fixed3(.222, .707, .071));
             fragColor.a = mainTex.a;
             return fragColor;
         }
         ENDCG
     }
 }
  
 }





To greyscale the entire scene, it's simple:

 public Material greyscaleMAT;
 List<Material> normalMATs = new List<Material>();

 void GreyScaleSceneOn ()
     {
         object[] allObjects = FindObjectsOfTypeAll(typeof(GameObject)) ;
         int index = 0;
         
         foreach(object thisObject in allObjects)
             if (((GameObject) thisObject).activeInHierarchy)
         {
             GameObject thisGO =    (GameObject)thisObject;
             
             if(thisGO.renderer && thisGO.tag != "NoGrey")
             {
                 normalMATs.Add(thisGO.renderer.material);
                 thisGO.renderer.material = greyscaleMAT;
                 index++;
             }
         }
     }
     
     void GreyScaleSceneOff ()
     {
         object[] allObjects = FindObjectsOfTypeAll(typeof(GameObject)) ;
         int index = 0;
         
         foreach(object thisObject in allObjects)
             if (((GameObject) thisObject).activeInHierarchy)
         {
             GameObject thisGO =    (GameObject)thisObject;
             if(thisGO.renderer && thisGO.tag != "NoGrey")
             {
                 thisGO.renderer.material = normalMATs[index];
                 index++;
             }
         }
         
         normalMATs.Clear ();
         
     }

Comment
Bunny83

People who like this

1 Show 0 · 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
avatar image

Answer by unity_oVsJgtACmNlH5w · May 01, 2022 at 09:08 AM

Anyone struggling with this today. I found that for me worked a simple solution seen below alt text


simplesol.jpg (27.4 kB)
Comment

People who like this

0 Show 0 · 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.

Update about the future of Unity Answers

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta later in June. Please note, we are aiming to set Unity Answers to read-only mode on the 31st of May in order to prepare for the final data migration.

For more information, please read our full announcement.

Follow this Question

Answers Answers and Comments

24 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

Related Questions

How to create 2D faded into itself shader for custom meshes ? 0 Answers

Overlay that creates only black and white underneath. 3 Answers

Cut out the black parts of a sprite in a shader 1 Answer

2D Greyscale shader with lighting and color. 1 Answer

point light illuminates makes transparent object visible 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