Color Correction

introduce

In Unity, Color Correction is a technique used to adjust the color of a scene or game screen. Among them, Curves (curve) and Saturation (saturation) are commonly used Color Correction tools. Through Curves, non-linear adjustments can be made to the RGB channels, thereby affecting the tone and contrast of the picture. And Saturation is used to adjust the saturation of the picture to make the color more vivid or lighten.

method

Curves

In Unity, Curves is used to adjust the curve of the RGB channel, and its parameters are as follows:

  • Input Range: Specifies the pixel range of the input. Usually 0 to 1, but other ranges are possible.
  • Output Range: Specify the pixel range for the output. Usually 0 to 1, but other ranges are possible.
  • Curve: Changes the color value of a pixel by adjusting the points on the curve. Points can be added or subtracted from the curve to create different colormaps.

Saturation

In Unity, Saturation is used to adjust the saturation of the picture, and its parameters are as follows:

  • Saturation: Set the saturation value. 0 means full grayscale (black and white), 1 means original saturation, greater than 1 increases saturation, and less than 1 decreases saturation.

for example

Curves example

1. Increase contrast

using UnityEngine;

public class ColorCorrectionExample : MonoBehaviour
{
    
    
    public AnimationCurve redCurve;
    public AnimationCurve greenCurve;
    public AnimationCurve blueCurve;

    private Material material;

    private void Awake()
    {
    
    
        material = new Material(Shader.Find("Custom/ColorCorrection"));
    }

    private void OnRenderImage(RenderTexture source, RenderTexture destination)
    {
    
    
        material.SetTexture("_MainTex", source);
        material.SetFloatArray("_RedCurve", redCurve.keys.Select(key => key.value).ToArray());
        material.SetFloatArray("_GreenCurve", greenCurve.keys.Select(key => key.value).ToArray());
        material.SetFloatArray("_BlueCurve", blueCurve.keys.Select(key => key.value).ToArray());
        Graphics.Blit(source, destination, material);
    }
}

2. Create the Toning Effect

using UnityEngine;

public class ColorCorrectionExample : MonoBehaviour
{
    
    
    public AnimationCurve redCurve;
    public AnimationCurve greenCurve;
    public AnimationCurve blueCurve;

    private Material material;

    private void Awake()
    {
    
    
        material = new Material(Shader.Find("Custom/ColorCorrection"));
    }

    private void OnRenderImage(RenderTexture source, RenderTexture destination)
    {
    
    
        material.SetTexture("_MainTex", source);
        material.SetFloatArray("_RedCurve", redCurve.keys.Select(key => Mathf.Sin(key.value)).ToArray());
        material.SetFloatArray("_GreenCurve", greenCurve.keys.Select(key => Mathf.Cos(key.value)).ToArray());
        material.SetFloatArray("_BlueCurve", blueCurve.keys.Select(key => Mathf.Tan(key.value)).ToArray());
        Graphics.Blit(source, destination, material);
    }
}

Saturation example

1. Increase Saturation

using UnityEngine;

public class SaturationExample : MonoBehaviour
{
    
    
    [Range(0f, 2f)]
    public float saturationValue = 1f;

    private Material material;

    private void Awake()
    {
    
    
        material = new Material(Shader.Find("Custom/Saturation"));
    }

    private void OnRenderImage(RenderTexture source, RenderTexture destination)
    {
    
    
        material.SetTexture("_MainTex", source);
        material.SetFloat("_Saturation", saturationValue);
        Graphics.Blit(source, destination, material);
    }
}

2. Reduce Saturation

using UnityEngine;

public class SaturationExample : MonoBehaviour
{
    
    
    [Range(0f, 2f)]
    public float saturationValue = 1f;

    private Material material;

    private void Awake()
    {
    
    
        material = new Material(Shader.Find("Custom/Saturation"));
    }

    private void OnRenderImage(RenderTexture source, RenderTexture destination)
    {
    
    
        material.SetTexture("_MainTex", source);
        material.SetFloat("_Saturation", 1f / saturationValue);
        Graphics.Blit(source, destination, material);
    }
}

The above code uses a custom Shader (Custom/ColorCorrection and Custom/Saturation) for color correction operations. Here we just provide a simple example to demonstrate how to use Curves and Saturation to adjust colors. In fact, more complex color effects can be achieved by adjusting curves and parameters.

Guess you like

Origin blog.csdn.net/qq_20179331/article/details/132036743