Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

I need a shader that transforms parts not lit grayscale.

Discussion in 'Shaders' started by heyider, Mar 25, 2016.

  1. heyider

    heyider

    Joined:
    Jan 20, 2013
    Posts:
    16
    I need a shader(or other solution) that transforms parts not lit grayscale. In my game,
    The player has a flashlight, the flashlight will light a scene in grayscale, it can only be colored the areas illuminated by the flashlight and other types of lights. All areas without light should be in grayscale.

    I know very little shader.

    If anyone has a solution I thank you.
     
    Last edited: Mar 25, 2016
  2. MSplitz-PsychoK

    MSplitz-PsychoK

    Joined:
    May 16, 2015
    Posts:
    1,278
    well... I'm not going to write a whole shader for you, but I can tell you what you'd need to do differently in a shader.

    Usually with lights, you have a value representing the clamped Vector3 dot product of the normal and reverse light direction (often called NdotL, between 0 and 1). You normally multiply NdotL with the pixel color to get the lit pixel color.
    Code (CSharp):
    1. finalColor = color * NdotL;
    What you'd want to do differently is also add the grayscale color times one minus NdotL.
    Code (CSharp):
    1. finalColor = (color * NdotL) + (grayscaleColor * (1 - NdotL));
    If you have the normal color and want the grayscale, you can set the rgb values each to the average of the 3.
    Code (CSharp):
    1. float average = (color.r + color.g + color.b) / 3;
    2. grayscaleColor.rgb = float3(average, average, average);
     
  3. heyider

    heyider

    Joined:
    Jan 20, 2013
    Posts:
    16
    Thank you very much, is not a solution, but it's a starting point.
    I'll poke around here.
     
  4. mholub

    mholub

    Joined:
    Oct 3, 2012
    Posts:
    123
    All unlit areas in real world are black. So what do you mean when you say they should be grayscale?

    Do you mean all areas which are illuminated only by ambient lighting are grayscale or do you mean when object is unlit, then show its albedo as grayscale (basically any light will not make object brighter, it will only make it more satured)

    Can you make example picture in photoshop and show us?
     
  5. heyider

    heyider

    Joined:
    Jan 20, 2013
    Posts:
    16
    Anyway this good, provided it is equal to the result of the image.
    The game is nightly.
     

    Attached Files:

  6. heyider

    heyider

    Joined:
    Jan 20, 2013
    Posts:
    16
    Someone?
     
  7. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,248
    A way to do this would be to have very custom setup shaders that handle different lights differently. By your earlier comments I suspect this is somewhat outside your skill.

    Another option is with color grading where any area too dark is made greyscale and only bright areas are in color. Might be the easiest method, assuming you can work out how to make the LUT for that.
    http://docs.unity3d.com/Manual/script-ColorCorrectionLookup.html
     
  8. heyider

    heyider

    Joined:
    Jan 20, 2013
    Posts:
    16
    I did using mascaras in cameras using 3 cameras:
    1 for grayscale
    1 for masks
    1 for colored areas

    But doubled the number of polygons, and was not very dynamic. I believe that a shader would be more appropriate.