Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
2
Question by Grimmy · Oct 27, 2010 at 07:50 PM · shadergreyscale

How do I make a texture turn greyscael?

So I've tried :

myImageObject.renderer.material.color=Color.black;

but that just makes my texture black. I imagine I want to change the materials shader but is there a shader that just renders in greyscale? If so what's it called and where can I get it?

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

7 Replies

· Add your reply
  • Sort: 
avatar image
6

Answer by VS48 · Oct 27, 2010 at 08:18 PM

I don't know if a grayscale shader is included w/ Unity, but this should be pretty easy with a custom shader. Create a new shader, then double-click it to open it in the editor. It might look like this:

Shader "GrayscaleLol" { Properties { _MainTex ("Base (RGB)", 2D) = "white" {} } SubShader { Tags { "RenderType"="Opaque" } LOD 200

     CGPROGRAM
     #pragma surface surf Lambert

     sampler2D _MainTex;

     struct Input {
         float2 uv_MainTex;
     };

     void surf (Input IN, inout SurfaceOutput o) {
         half4 c = tex2D (_MainTex, IN.uv_MainTex);
         o.Albedo = (c.r + c.g + c.b)/3;
         o.Alpha = c.a;
     }
     ENDCG
 } 
 FallBack "Diffuse"

}

The only change from the default shader is

o.Albedo = c.rgb; 

becomes

o.Albedo = (c.r + c.g + c.b)/3;

EDIT: In response to comment, transparent grayscale

Shader "GrayscaleLolTransparent" { Properties { _MainTex ("Base (RGB) Trans (A)", 2D) = "white" {} }

 SubShader {
     Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
     LOD 200

     CGPROGRAM
     #pragma surface surf Lambert alpha

         sampler2D _MainTex;

         struct Input {
             float2 uv_MainTex;
         };

         void surf (Input IN, inout SurfaceOutput o) {
             half4 c = tex2D(_MainTex, IN.uv_MainTex);
             o.Albedo = dot(c.rgb, float3(0.3, 0.59, 0.11));
             o.Alpha = c.a;
         }

     ENDCG
 }

 Fallback "Transparent/VertexLit"

}

Comment
Add comment · Show 3 · 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 VS48 · Oct 27, 2010 at 08:22 PM 0
Share

Another method would be to do: o.Albedo = dot(c.rgb, float3(0.3, 0.59, 0.11)); That makes grayscaling look better if you have realistic textures, since it takes into account the eye's sensitivity to different colors. So it just gives certain colors more weight than others in your grayscale output.

avatar image Grimmy · Oct 27, 2010 at 08:33 PM 0
Share

Thats cool. As I know nothing about shaders how about with transparency too? I've tried a few things (copying from the vertex lit/transparent shader) but they dont seem to work.

avatar image VS48 · Oct 27, 2010 at 09:29 PM 0
Share

Ok, edited my post, see if it works for you. That's a modified default transparent diffuse shader which comes with unity.

avatar image
5

Answer by P1LL4G3 · Jan 20, 2012 at 11:07 PM

I'm going to add a little tip since this thread has helped me get by. I've extended this code to let you vary the greyscale amount.

Add:

     _EffectAmount ("Effect Amount", Range (0, 1)) = 1.0

to the Properties list.

Add:

     uniform float _EffectAmount;

right below "sampler2D _MainTex;"

Finally, change

     o.Albedo = dot(c.rgb, float3(0.3, 0.59, 0.11));

to

     o.Albedo = lerp(c.rgb, dot(c.rgb, float3(0.3, 0.59, 0.11)), _EffectAmount);

This will result in a little slider on the shader options which you can drag to modify it. However, the real prize from this is that it allows you to script the effect amount. To set it, you can simply call this on the game object you're drawing:

     GAMEOBJECT.renderer.material.SetFloat('_EffectAmount', AMOUNT_OF_GREYSCALE);

If you want it to animate this greyscale fade, you can use a coroutine. I'll leave that part as an exercise for the reader.

Comment
Add comment · 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 ActionScripter · Jul 10, 2012 at 04:07 PM 0
Share

Thank you for this addition. I just came across this thread and your answer really ties it together.

avatar image
4

Answer by Eric5h5 · Oct 27, 2010 at 08:39 PM

Depending on what you're doing, you might prefer to make the actual texture grayscale:

function Start () { var texClone = Instantiate (renderer.material.mainTexture); renderer.material.mainTexture = texClone; MakeGrayscale (texClone); }

function MakeGrayscale (tex : Texture2D) { var texColors = tex.GetPixels(); for (i = 0; i < texColors.Length; i++) { var grayValue = texColors[i].grayscale; texColors[i] = Color(grayValue, grayValue, grayValue, texColors[i].a); } tex.SetPixels(texColors); tex.Apply(); }

However that will probably use more memory, since the texture has to be marked readable, and be RGB24 or ARGB32. On the other hand it will work on anything and not need a pixel shader.

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
avatar image
4

Answer by Ruzihm · Apr 01, 2014 at 09:30 PM

This forum post has a great Sprite shader that has a greyscale parameter, in case anyone is looking.

 Shader "Sprites/GrayScale"
 {
     Properties
     {
         [PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
         _Color ("Tint", Color) = (1,1,1,1)
         [MaterialToggle] PixelSnap ("Pixel snap", Float) = 0
         _EffectAmount ("Effect Amount", Range (0, 1)) = 1.0
     }
  
     SubShader
     {
         Tags
         {
             "Queue"="Transparent"
             "IgnoreProjector"="True"
             "RenderType"="Transparent"
             "PreviewType"="Plane"
             "CanUseSpriteAtlas"="True"
         }
  
         Cull Off
         Lighting Off
         ZWrite Off
         Fog { Mode Off }
         Blend SrcAlpha OneMinusSrcAlpha
  
         Pass
         {
         CGPROGRAM
             #pragma vertex vert
             #pragma fragment frag
             #pragma multi_compile DUMMY PIXELSNAP_ON
             #include "UnityCG.cginc"
            
             struct appdata_t
             {
                 float4 vertex   : POSITION;
                 float4 color    : COLOR;
                 float2 texcoord : TEXCOORD0;
             };
  
             struct v2f
             {
                 float4 vertex   : SV_POSITION;
                 fixed4 color    : COLOR;
                 half2 texcoord  : TEXCOORD0;
             };
            
             fixed4 _Color;
  
             v2f vert(appdata_t IN)
             {
                 v2f OUT;
                 OUT.vertex = mul(UNITY_MATRIX_MVP, IN.vertex);
                 OUT.texcoord = IN.texcoord;
                 OUT.color = IN.color * _Color;
                 #ifdef PIXELSNAP_ON
                 OUT.vertex = UnityPixelSnap (OUT.vertex);
                 #endif
  
                 return OUT;
             }
  
             sampler2D _MainTex;
             uniform float _EffectAmount;
  
             fixed4 frag(v2f IN) : COLOR
             {
                 half4 texcol = tex2D (_MainTex, IN.texcoord);              
                 texcol.rgb = lerp(texcol.rgb, dot(texcol.rgb, float3(0.3, 0.59, 0.11)), _EffectAmount);
                 texcol = texcol * IN.color;
                 return texcol;
             }
         ENDCG
         }
     }
     Fallback "Sprites/Default"
 }


Comment
Add comment · Show 2 · 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 adrianadrian · Nov 24, 2015 at 01:04 AM 1
Share

this one worked wonders for me, thanks!

avatar image GuirieSanchez · Feb 10 at 03:08 PM 0
Share

Why do I get 122 compiled errors when copy&pasting this?

avatar image
1

Answer by RKSandswept · Dec 12, 2013 at 01:13 AM

My 2 cents...

  • You make the shader file, in my case called MyShader_GUI_Desaturated.shader

  • Make a material, in my case called gui_desaturated_material and select the shader for it.

  • Make the shader texture be a small white square. We have a 16x16 pixel white texture.

  • In your code for the GUI draw instead of GUI.DrawTexture, use Graphics.DrawTexture and use the material.

Here are the complete code pieces...

Shader "MyShaders/GUI_Desaturated" { Properties { _MainTex ("Base (RGB) Trans (A)", 2D) = "white" {} _EffectAmount ("Effect Amount", Range (0, 1)) = 1.0 }

     SubShader {
         Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
         LOD 200
  
         CGPROGRAM
         #pragma surface surf Lambert alpha
  
             sampler2D _MainTex;
             uniform float _EffectAmount;
  
             struct Input {
                 float2 uv_MainTex;
             };
  
             void surf (Input IN, inout SurfaceOutput o) {
                 half4 c = tex2D(_MainTex, IN.uv_MainTex);
                 o.Albedo = lerp(c.rgb, dot(c.rgb, float3(0.3, 0.59, 0.11)), _EffectAmount);
                 o.Alpha = c.a;
              }
  
         ENDCG
     }
  
     Fallback "Transparent/VertexLit"
 }

In the C# component...

    public Material craftingItemMaterial;

And in the OnGUI code. We only draw during the Repaint event. Note: Unity3D calls OnGUI for events that happen. It is typical that OnGUI gets called twice, first with a Layout event which is determining all the GUI... calls and their screen position layout, and then with a Repaint event where the GUI items actually get drawn. You also may get other events in OnGUI like mouse and keyboard events. (That is how GUI and GUILayout get the job done so transparently.)

// Only do the draw on the Repaint event. if(Event.current.type.Equals(EventType.Repaint)) { // The craftingItemMaterial is a MyShaders_GUI_Desaturated shader. Graphics.DrawTexture( itemRect, item_icon_texture, craftingItemMaterial ); }

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
  • 1
  • 2
  • ›

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.

Follow this Question

Answers Answers and Comments

9 People are following this question.

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

Related Questions

How do I access the 'Shader' property of the Greyscale Effect? 1 Answer

Modifying Greyscale Shader for selective Colouring. 2 Answers

Greyscale Shader Removes UI Masking 0 Answers

A Grayscale Shaders does not work on Android? 0 Answers

How can I combine a color texture with a b&w texture and get color? 2 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