Search Unity

Anyway to change from greyscale to colour?

Discussion in 'Scripting' started by CoffeeConundrum, Jan 7, 2014.

  1. CoffeeConundrum

    CoffeeConundrum

    Joined:
    Dec 30, 2013
    Posts:
    46
    This probably seems a bit ridiculous but I'm wanting to create a game that features the use of going from greyscale to showing some colour in the scene after some interaction with an object, say like a push of a button. Is this possible to code or even do on Unity and if so, how or where can I find out more about it?

    Thank you in advance!
     
  2. softwizz

    softwizz

    Joined:
    Mar 12, 2011
    Posts:
    793
    You can change and objects texture using
    Code (csharp):
    1. renderer.material.mainTexture = Texture;
    http://docs.unity3d.com/Documentation/ScriptReference/Material-mainTexture.html

    I am not sure if there is a way to slowly change the texture over time so it looks like the colour is fading in, maybe by changing the opacity slowly over time would do this.

    In fact the second example on that link would work as well but would need a few textures to get a smooth fade in effect.
     
    Last edited: Jan 7, 2014
  3. CoffeeConundrum

    CoffeeConundrum

    Joined:
    Dec 30, 2013
    Posts:
    46
    Ah that's fantastic! I think to begin it would be a simple greyscale to colour transition but I may look into fading if I manage to get the time. Currently working on this as part of my society and we're given 8 weeks to complete a game so trying to get the major problems dealt with before I code it up. Thanks very much for the help.
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Probably the best way would be to make a grayscale shader where you control the amount of grayscaleness (new word!) for each texture. However, if you don't already know how to make shaders, then learning that on top of everything else in 8 weeks might be too much.

    --Eric
     
  5. softwizz

    softwizz

    Joined:
    Mar 12, 2011
    Posts:
    793
    You should have made pong
     
  6. JohnnyA

    JohnnyA

    Joined:
    Apr 9, 2010
    Posts:
    5,041
    Code (csharp):
    1.  
    2. Shader "Custom/DesaturationAndBrightnessWithColor" {
    3.  
    4.  
    5. Properties{
    6.     _Color ("Main Color", Color) = (1,1,1,1)
    7.     _MainTex ("Texture", 2D) = ""
    8. }
    9.  
    10.  
    11. Subshader {
    12.  
    13.     Tags
    14.         {
    15.             "Queue" = "Transparent"
    16.             "IgnoreProjector" = "True"
    17.             "RenderType" = "Transparent"
    18.         }
    19.  
    20.     ZWrite Off
    21.  
    22.     Blend SrcAlpha OneMinusSrcAlpha
    23.  
    24.     Pass {
    25.  
    26.         Cull Off
    27.         Lighting Off
    28.         ZWrite Off
    29.         Fog { Mode Off }
    30.         Offset -1, -1
    31.            
    32.         CGPROGRAM
    33.  
    34.         #pragma vertex vert
    35.  
    36.         #pragma fragment frag
    37.  
    38.         struct v2f {
    39.  
    40.             float4 position : SV_POSITION;
    41.  
    42.             float2 uv_mainTex : TEXCOORD;
    43.            
    44.             float4 color : COLOR ;
    45.  
    46.         };
    47.  
    48.  
    49.         uniform float4 _MainTex_ST;
    50.        
    51.         v2f vert(float4 position : POSITION, float2 uv : TEXCOORD0, float4 color: COLOR) {
    52.  
    53.             v2f o;
    54.  
    55.             o.position = mul(UNITY_MATRIX_MVP, position);
    56.  
    57.             o.uv_mainTex = uv * _MainTex_ST.xy + _MainTex_ST.zw;
    58.            
    59.             o.color = color;
    60.  
    61.             return o;
    62.  
    63.         }
    64.  
    65.        
    66.  
    67.         uniform sampler2D _MainTex;
    68.         uniform fixed4 _Color;
    69.  
    70.  
    71.         fixed4 frag(v2f input) : COLOR {
    72.             fixed4 mainTex = tex2D(_MainTex, input.uv_mainTex);
    73.             fixed4 mainColor = _Color;
    74.             fixed4 fragColor;
    75.             fixed4 tempColor;
    76.             tempColor.rgb = dot(mainTex.rgb, fixed3(.222, .707, .071));
    77.             fragColor = ((mainColor.r * mainTex) + ((1 - mainColor.r) * tempColor))/ ( 2 - mainColor.g);
    78.             fragColor.a = mainTex.a;
    79.             return fragColor;
    80.         }
    81.  
    82.         ENDCG
    83.  
    84.     }
    85.  
    86. }
    87. }
    88.  
    Use red channel of the colour to control desaturation and green to control brightness.

    Just a note that the shader is unlit nd transparent as I use it for sprites. Depending on what you want to do you could remove the Lighting Off, ZWrite Off, and change the queue to geometry.
     
    Last edited: Jan 7, 2014
  7. CoffeeConundrum

    CoffeeConundrum

    Joined:
    Dec 30, 2013
    Posts:
    46
    We've got a bit of incentive as we get to showcase our games off to the rest of the uni by the end of the semester.

    Also Eric this is why I'm trying to get a headstart now on learning how to do these tasks and we don't officially start for another few weeks on this project. I've got under two weeks of spare time so I'm trying to get some knowledge down on this by doing a prototype of the game before adding in assets to it. Thanks very much for your help though.
     
  8. Peacewise

    Peacewise

    Joined:
    Feb 27, 2014
    Posts:
    52
    Hi Johnny A,

    That's a great shader! I was looking for something just like it. I'm using Unity 4.3 with the built in sprite system. When I switch a sprite's material from "Sprites-Default" to a material that uses this shader, then those sprites no longer batch. Every sprite that uses this material requires its own draw call.

    Do you have any insight into this and how I might be able to apply this shader such that my sprites can still be batched normally?