Search Unity

Interpolate to black & white shader

Discussion in 'Shaders' started by Hoshiqua, Oct 1, 2018.

  1. Hoshiqua

    Hoshiqua

    Joined:
    Jul 22, 2016
    Posts:
    77
    Greetings !

    I am totally new to Shaders in Unity, and seeing as I want to try and make something that looks good for once, I came up with a handful of ideas to improve my current project's graphics.

    In that project, the player is able to slow / stop time. When that happens, I'd like for the whole world (UI included, maybe, but not necessarily) to go to shades of grey instead of colors (for example by taking the highest color value and applying it to the other two channels, and keeping the current alpha).

    I don't think writing / "graphing" such a shader would be all that difficult. However, I need some pointers on the transition itself. See, I'd like for the effect to settle in smoothly, altough even that is optionnal really. As such, I came up with two potential solutions, and I'd really appreciate if someone could tell me if at least one of these is the right one given my objective, and if I've missed any alternatives.

    Solution 1 : use whatever shader I currently use for when the world is in colored, as normal. When I want to switch to black & white, I somehow manually change the shader of all of the relevant objects to a shader that makes them render in black & white, keeping their former shader and properties in memory for when I want to transition back. This solution sounds pretty simple but crude, and I don't see how it could allow for a smooth transition.

    Solution 2 : write a single shader for all relevant objects that basically works the same as the standard shader, except that it gets an additional property called "greyness" or something which triggers a "greying" effect to a certain extent. This solution sounds better, allows for a smooth transition, but the second I want to have multiple shaders (which is going to happen eventually) for different objects that have to go black & white too, I will have to rewrite my own version of that shader aswell.

    What do you think ? Am I missing something obvious ?
    Thanks in advance !
     
  2. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    Have you tried using Post Processing for this one?
    I'm pretty sure v2 Post Processing Stack got something similar.
     
  3. Hoshiqua

    Hoshiqua

    Joined:
    Jul 22, 2016
    Posts:
    77
    Post proce-what ?

    Just kidding, I'll look into that. Thank you !
     
    karen_ann likes this.
  4. Przemyslaw_Zaworski

    Przemyslaw_Zaworski

    Joined:
    Jun 9, 2017
    Posts:
    328
    You can use Post Processing Stack and following code to dynamically change saturation in real-time:

    Code (CSharp):
    1. // https://github.com/przemyslawzaworski/Unity3D-C-programming/blob/master/post_processing_stack_setup.cs
    2.  
    3. using UnityEngine;
    4. using UnityEngine.PostProcessing;
    5.  
    6. public class post_processing_stack_setup : MonoBehaviour
    7. {
    8.     public PostProcessingProfile PPP;
    9.    
    10.     void Start ()
    11.     {
    12.         PPP.colorGrading.enabled = true;      
    13.     }
    14.    
    15.     void Update ()
    16.     {
    17.         ColorGradingModel.Settings cgms = PPP.colorGrading.settings;
    18.         cgms.basic.saturation = Mathf.Lerp(0.0f,1.0f,Mathf.Sin(Time.time)*0.5f+0.5f);
    19.         PPP.colorGrading.settings = cgms;      
    20.     }
    21. }
    or write shader manually with example function:

    Code (CSharp):
    1.     float3 full = tex2D(_MainTex,uv).xyz;
    2.     float d = dot(full, float3(0.2126, 0.7152, 0.0722));
    3.     float3  grayscale = float3 (d,d,d);
    4.     float3 c = lerp(full,grayscale,cos(_Time.g)*0.5+0.5);
    5.     float4 result  = float4(c,1.0);
     
  5. Hoshiqua

    Hoshiqua

    Joined:
    Jul 22, 2016
    Posts:
    77
    Is that a pro version only feature ? Unity doesn't recognize "UnityEngine.PostProcessing", even after I tried installing the PostProcessing package.
     
  6. Przemyslaw_Zaworski

    Przemyslaw_Zaworski

    Joined:
    Jun 9, 2017
    Posts:
    328
    Hmmmm.....
    If you use PPS version 1.0:
    First, check Player Settings, should has Scripting Define Symbol: UNITY_POST_PROCESSING_STACK_V1

    Or check PPS version 2:
    https://github.com/Unity-Technologies/PostProcessing (button Clone or Download, unpack and move to project directory)
     
  7. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    It's not a pro version. Just download the package from the Asset Store.
     
  8. Hoshiqua

    Hoshiqua

    Joined:
    Jul 22, 2016
    Posts:
    77
    Couldn't get it to work yet, strictly speaking, but I did find the UnityEngine.Rendering.PostProcessing package with similar content. Is that something different ?
     
  9. Hoshiqua

    Hoshiqua

    Joined:
    Jul 22, 2016
    Posts:
    77