UE4 Material HLSL RGB-HSV互转
参考公式:
1.RGBtoHSV
float3 rgbw = RGB;
float maxt = rgbw.y;
float mint = rgbw.x;
float h = 0;
float s = 0;
float v = 0;
if(max(rgbw.x, rgbw.y) > rgbw.z)
{
maxt = max(rgbw.x, rgbw.y);
}
else
{
maxt = rgbw.z;
}
if(min(rgbw.x, rgbw.y) < rgbw.z)
{
mint = min(rgbw.x, rgbw.y);
}
else
{
mint = rgbw.z;
}
if(maxt == mint)
{
h = 0;
}
if(maxt == rgbw.x && rgbw.y >= rgbw.z)
{
h = (rgbw.y - rgbw.z)/(maxt - mint) * 60;
}
if(maxt == rgbw.x && rgbw.y < rgbw.z)
{
h = (rgbw.y - rgbw.z)/(maxt - mint) * 60 + 360;
}
if(maxt == rgbw.y)
{
h = (rgbw.z - rgbw.x)/(maxt - mint) * 60 + 120;
}
if(maxt == rgbw.z)
{
h = (rgbw.x - rgbw.y)/(maxt - mint) * 60 + 240;
}
if(maxt == 0)
{
s = 0;
}
else
{
s = (maxt - mint)/maxt;
}
v = maxt;
return float3(h, s, v);
2.HSVtoRGB
float3 color = hsv;
float h = color.x;
float s = color.y;
float v = color.z;
float hi = floor(h/60);
float f = h/60 - hi;
float p = v * (1 - s);
float q = v * (1- f*s);
float t = v* (1-(1-f)*s);
if(hi == 0 )
{
return float3(v, t, p);
}
if(hi == 1 )
{
return float3(q, v, p);
}
if(hi == 2 )
{
return float3(p, v, t);
}
if(hi == 3 )
{
return float3(p, q, v);
}
if(hi == 4 )
{
return float3(t, p, v);
}
if(hi == 5 )
{
return float3(v, p, q);
}
return color;
最后在虚幻材质里面表现:
完全按照公式实现,仅供参考。
hlsl书写规范了解一下
怎么个规范法?