您的位置:首页 > 其它

RGB转HSV的shader

2011-10-22 15:08 225 查看
转帖以作备份

原帖:http://www.virtools.com.cn/bbs/viewthread.php?tid=7189&page=1

//Write By Jare 2011-01-01

//s_fantasy@163.com QQ:56788395

//#define HALF_PI 1.570795

bool UseRotate;

float2 rotation_Center <float2 Default = {0.5,0.5};>;

float rotation_Angle <float UIMin = 0.0; float UIMax = 6.2831853;> = 0.0;

//bool UseColor;

float color_Brightness <float UIMin = 0.0; float UIMax = 5.0;> = 1.0;

//float color_Gamma <float UIMin = 0.0; float UIMax = 5.0;> = 1.0;

float4 color_AddColor <float4 Default = {1,1,1,0};>;

bool UseHue;

float hue_Hue <float UIMin = 0.0; float UIMax = 1.0;> = 0.0;

float hue_Saturation <float UIMin = -1.0; float UIMax = 1.0;> = 0.0;

float hue_Brightness <float UIMin = -1.0; float UIMax = 1.0;> = 0.0;

float4 diff : Diffuse;

texture RenderMap : TEXTURE;

float4x4 wvp : WORLDVIEWPROJECTION;

sampler RenderMapSampler3D = sampler_state

{

Texture = <RenderMap>;

MagFilter = LINEAR;

MinFilter = LINEAR;

// MipFilter = POINT;

};

sampler RenderMapSampler2D = sampler_state

{

Texture = <RenderMap>;

MagFilter = LINEAR;

MinFilter = LINEAR;

// MipFilter = POINT;

AddressU = BORDER;

AddressV = BORDER;

};

sampler RenderMapSampler2DPoint = sampler_state

{

Texture = <RenderMap>;

MagFilter = POINT;

MinFilter = POINT;

// MipFilter = POINT;

AddressU = BORDER;

AddressV = BORDER;

};

static float3 hue_All = float3(hue_Hue, hue_Saturation, hue_Brightness);

static float tcosA = cos(rotation_Angle);

static float tsinA = sin(rotation_Angle);

static float cosR = abs(tcosA) < 0.005 ? 0.0 : tcosA;

static float sinR = abs(tsinA) < 0.005 ? 0.0 : tsinA;

static float2x2 rotateMatrix = float2x2(cosR, sinR, -sinR ,cosR);

//static float tDeltaAngle = abs(rotation_Angle % HALF_PI);

static bool UseNearestColor = (cosR == 0.0 || sinR == 0.0);//(tDeltaAngle <= 0.005 || tDeltaAngle >= HALF_PI - 0.005);

//static sampler RenderMapSampler = Is3D ? RenderMapSampler3D : (UseNearestColor ? RenderMapSampler2DPoint : RenderMapSampler2D);

struct oVertex

{

float4 Pos : POSITION;

float2 UV : TEXCOORD0;

};

float2 Rotate(float2 UV, float2 vCenter)

{

return mul(UV-vCenter, rotateMatrix) + vCenter;

}

oVertex VS(

float4 Pos : POSITION,

float2 UV : TEXCOORD0,

uniform bool Is3D

)

{

oVertex Out = (oVertex)0;

Out.Pos = Is3D ? mul(Pos,wvp) : Pos;

Out.UV = UV;

return Out;

}

oVertex VS_Rotate(

float4 Pos : POSITION,

float2 UV : TEXCOORD0,

uniform bool Is3D

)

{

oVertex Out = VS(Pos,UV, Is3D);

if(UseRotate){

Out.UV = Rotate(Out.UV, rotation_Center);

}

return Out;

}

float4 PS(oVertex In) : COLOR0

{

float4 col;

if(UseNearestColor){

col = tex2D(RenderMapSampler2DPoint, In.UV);

}else{

col = tex2D(RenderMapSampler2D, In.UV);

}

return col * diff;

}

float4 PS_3D(oVertex In) : COLOR0

{

float4 col = tex2D(RenderMapSampler3D, In.UV);

return col * diff;

}

float __min_channel(float3 v)

{

float t = (v.x<v.y) ? v.x : v.y;

t = (t<v.z) ? t : v.z;

return t;

}

float __max_channel(float3 v)

{

float t = (v.x>v.y) ? v.x : v.y;

t = (t>v.z) ? t : v.z;

return t;

}

float3 rgb_to_hsv(float3 RGB)

{

float minVal = __min_channel(RGB);//min(min(RGB.x, RGB.y), RGB.z);

float maxVal = __max_channel(RGB);//max(max(RGB.x, RGB.y), RGB.z);

float delta = maxVal - minVal; //Delta RGB value

float3 HSV;

HSV.y = delta / maxVal;

HSV.z = maxVal;

if (delta != 0.0) { // If gray, leave H & S at zero

float3 delRGB = (maxVal.xxx - RGB) / 6.0 / delta + 0.5;

if ( RGB.x == maxVal ) HSV.x = delRGB.z - delRGB.y;

else if ( RGB.y == maxVal ) HSV.x = delRGB.x - delRGB.z + 0.3333333333333;

else/* RGB.z == maxVal*/ HSV.x = delRGB.y - delRGB.x + 0.6666666666667;

HSV.x += 6.0;//don't know why

}

return (HSV);

}

float3 hsv_to_rgb(float3 HSV)

{

HSV.x = frac(HSV.x);

HSV.y = saturate(HSV.y);

HSV.z = saturate(HSV.z);

float3 RGB = HSV.z;

if ( HSV.y != 0 ) {

float var_h = HSV.x * 6.0;

int var_i = floor(var_h);

float t1, t2;

t1 = HSV.z * HSV.y;

t2 = t1 * (var_h - var_i);

float var_1 = HSV.z - t1;

float var_2 = HSV.z - t2;

float var_3 = var_1 + t2;

if (var_i == 1) RGB = float3(var_2, HSV.z, var_1);

else if (var_i == 2) RGB = float3(var_1, HSV.z, var_3);

else if (var_i == 3) RGB = float3(var_1, var_2, HSV.z);

else if (var_i == 4) RGB = float3(var_3, var_1, HSV.z);

else if (var_i == 5) RGB = float3(HSV.z, var_1, var_2);

else RGB = float3(HSV.z, var_3, var_1);

}

return (RGB);

}

float4 changeColor(float4 col)

{

//if(UseColor){

float colA = col.w;

col.xyz *= color_Brightness;

col = lerp(col, color_AddColor, color_AddColor.w);

col.w = colA;

//}

if(UseHue){

col.rgb = rgb_to_hsv(col.rgb);

col.rgb += hue_All;

col.rgb = hsv_to_rgb(col.rgb);

}

return col;

}

float4 PS_Color(oVertex In) : COLOR0

{

float4 col = PS(In);

return changeColor(col);

}

float4 PS_Color_3D(oVertex In) : COLOR0

{

float4 col = PS_3D(In);

return changeColor(col);

}

technique Base

{

pass p

{

AlphaBlendEnable = true;

DestBlend = INVSRCALPHA;

SrcBlend = SRCALPHA;

VertexShader = compile vs_1_1 VS_Rotate(false);

PixelShader = compile ps_2_0 PS();

}

}

technique Base_3D

{

pass p

{

AlphaBlendEnable = true;

DestBlend = INVSRCALPHA;

SrcBlend = SRCALPHA;

VertexShader = compile vs_1_1 VS_Rotate(true);

PixelShader = compile ps_2_0 PS_3D();

}

}

technique Base_ColorAndHue

{

pass p

{

AlphaBlendEnable = true;

DestBlend = INVSRCALPHA;

SrcBlend = SRCALPHA;

VertexShader = compile vs_1_1 VS_Rotate(false);

PixelShader = compile ps_2_0 PS_Color();

}

}

technique Base_ColorAndHue_3D

{

pass p

{

AlphaBlendEnable = true;

DestBlend = INVSRCALPHA;

SrcBlend = SRCALPHA;

VertexShader = compile vs_1_1 VS_Rotate(true);

PixelShader = compile ps_2_0 PS_Color_3D();

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: