其他分享
首页 > 其他分享> > Unity shader 调节材质的色调 饱和度明度

Unity shader 调节材质的色调 饱和度明度

作者:互联网

参考https://blog.csdn.net/cjb_king/article/details/79275461

新建一个
HSV.cginc

#ifndef HSV_INCLUDED
#define HSV_INCLUDED


// RGB to HSV
float3 RGBConvertToHSV(float3 rgb)
{
    float R = rgb.x,G = rgb.y,B = rgb.z;
    float3 hsv;
    float max1=max(R,max(G,B));
    float min1=min(R,min(G,B));
    if (R == max1) 
    {
        hsv.x = (G-B)/(max1-min1);
    }
    if (G == max1) 
    {
        hsv.x = 2 + (B-R)/(max1-min1);
    }
    if (B == max1) 
    {
        hsv.x = 4 + (R-G)/(max1-min1);
    }
    hsv.x = hsv.x * 60.0;   
    if (hsv.x < 0) 
        hsv.x = hsv.x + 360;
    hsv.z=max1;
    hsv.y=(max1-min1)/max1;
    return hsv;
}

// HSV to RGB
float3 HSVConvertToRGB(float3 hsv)
{
    float R,G,B;
    //float3 rgb;
    if( hsv.y == 0 )
    {
        R=G=B=hsv.z;
    }
    else
    {
        hsv.x = hsv.x/60.0; 
        int i = (int)hsv.x;
        float f = hsv.x - (float)i;
        float a = hsv.z * ( 1 - hsv.y );
        float b = hsv.z * ( 1 - hsv.y * f );
        float c = hsv.z * ( 1 - hsv.y * (1 - f ) );
        switch(i)
        {
            case 0: R = hsv.z; G = c; B = a;
                break;
            case 1: R = b; G = hsv.z; B = a; 
                break;
            case 2: R = a; G = hsv.z; B = c; 
                break;
            case 3: R = a; G = b; B = hsv.z; 
                break;
            case 4: R = c; G = a; B = hsv.z; 
                break;
            default: R = hsv.z; G = a; B = b; 
                break;
        }
    }
    return float3(R,G,B);
}

float4 HSVProcess(float4 col, float3 hsvColor)
{
    float3 colorHSV;
    colorHSV.xyz = RGBConvertToHSV(col.xyz);
    colorHSV.x += lerp(0, 359, hsvColor.r);
    colorHSV.x = colorHSV.x % 360;

    colorHSV.y *= lerp(0, 3.0, hsvColor.g);
    colorHSV.z *= lerp(0, 3.0, hsvColor.b);

    float4 r =  float4(HSVConvertToRGB(colorHSV.xyz), col.a);
    return r;
}
#endif

用法:

// 声明
_HSVColor ("_HSVColor", color) = (0,0.333,0.333)
// .............
float3 _HSVColor;
// ...................
#include "../Include/HSV.cginc"
// .............
fixed4 hsvProcessCol = HSVProcess(mainColor, _HSVColor);
col = hsvProcessCol;

在这里插入图片描述
调节RGB 就可以了

标签:float,colorHSV,max1,shader,明度,Unity,hsv,HSV,float3
来源: https://blog.csdn.net/A13155283231/article/details/100538824