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;

Unity shader 调节材质的色调 饱和度明度
调节RGB 就可以了

上一篇:错误的时间跨度转换LINQ查询?到列表<>?


下一篇:HSV到RGB(和后面)没有Python中的浮点数学