Code Snippet: HSV to RGB and Vice versa in Shader


This Code snipped will show two custom operation that we normally need to use in our post effect shaders. They are the conversion between the RGB space to HSV and vice versa.

The shader is pretty simple and is shown below (this code is used inside a post effect, you can grab the HSVtoRBG and the RGBtoHSV functions and use at your own):

float2 halfPixel;
float4 toAdd;
float4 toMultiply;

texture cena;
sampler cenaSampler = sampler_state
{
   Texture = ;
   MinFilter = LINEAR;
   MagFilter = LINEAR;
   MipFilter = LINEAR;
   AddressU  = Clamp;
   AddressV  = Clamp;
};

struct VertexShaderOutput
{
    float4 Position : POSITION0;
    float2 TexCoord : TEXCOORD0;
    float2 Pos : TEXCOORD1;
};
VertexShaderOutput VShader( float4 Pos: POSITION, float2 Tex : TEXCOORD)
{
	VertexShaderOutput output;
    output.Position = float4(Pos);
    output.Pos = float4(Pos);
    output.TexCoord = Tex - halfPixel;
    return output;
}

float3 Hue(float H)
{
    float R = abs(H * 6 - 3) - 1;
    float G = 2 - abs(H * 6 - 2);
    float B = 2 - abs(H * 6 - 4);
    return saturate(float3(R,G,B));
}

float4 HSVtoRGB(in float3 HSV)
{
    return float4(((Hue(HSV.x) - 1) * HSV.y + 1) * HSV.z,1);
}

float4 RGBtoHSV(in float3 RGB)
{
    float3 HSV = 0;
    HSV.z = max(RGB.r, max(RGB.g, RGB.b));
    float M = min(RGB.r, min(RGB.g, RGB.b));
    float C = HSV.z - M;
    if (C != 0)
    {
        HSV.y = C / HSV.z;
        float3 Delta = (HSV.z - RGB) / C;
        Delta.rgb -= Delta.brg;
        Delta.rg += float2(2,4);
        if (RGB.r >= HSV.z)
            HSV.x = Delta.b;
        else if (RGB.g >= HSV.z)
            HSV.x = Delta.r;
        else
            HSV.x = Delta.g;
        HSV.x = frac(HSV.x / 6);
    }
    return float4(HSV,1);
}

float4 PShader(VertexShaderOutput input) : COLOR
{
    float4 cen = tex2D(cenaSampler ,input.TexCoord );
    return HSVtoRGB(toMultiply * RGBtoHSV(cen) + toAdd);

}

technique Normal
{
	pass P0
	{
		VertexShader = compile vs_3_0 VShader();
		PixelShader = compile ps_3_0 PShader();
	}
}

, ,

  1. #1 by utah law on car insurance on 28 de maio de 2017 - 7:46 am

    I searched a bunch of sites and this was the best.

  2. #2 by Travel Package on 28 de maio de 2017 - 7:47 am

    Excellent post. I was checking constantly this blog and I’m impressed! Very useful information specially the last part :) I care for such info much. I was looking for this certain info for a long time. Thank you and best of luck.

  3. #3 by Summer School on 28 de maio de 2017 - 8:12 am

    Whats Taking place i am new to this, I stumbled upon this I’ve discovered It absolutely helpful and it has aided me out loads. I’m hoping to give a contribution & assist other users like its helped me. Good job.

  4. #4 by sterling casualty car insurance on 28 de maio de 2017 - 8:14 am

    Well done to think of something like that

  5. #5 by http://www.chalet-la-giettaz.com/louboutinshoes.aspx on 28 de maio de 2017 - 8:28 am

    I have this particular present of my mother for the Christmas simply because she was the best precious jewelry freak. Your one thing she cannot use a lot to is, bracelets. I bought this girl our charm bracelet then once she exposed things yesterday she absolutley adored information technology! Right now the issue is actually, the lady buying that regarding and/or down with by herself. Haha… overall great goods, things transported furthermore appeared severely very early as well as the mother was experiencing things. Thank people.

  6. #6 by Cartier Love Bracelet Replica on 28 de maio de 2017 - 8:29 am

    it comes in a perfect container appears ideal and yet their slightly limited still of our mothers wrist but it styles great only want it was further

  7. #7 by wie sie helfen koennen on 28 de maio de 2017 - 8:57 am

    Es dauerte nicht lange bis ich fühlte, wie sein Schwanz mir zu pochen bi und schwule maenner begann und seine Sahne mehreren Schüben mich hinein spritzte.Trotzdem schäme ich mich, murmelte er.

  8. #8 by schauen sie sich hier on 28 de maio de 2017 - 10:21 am

    Ein Glück, dass das Seminar hier stattfindet.

  9. #9 by californiaautoinsurancequotes on 28 de maio de 2017 - 10:31 am

    Please keep throwing these posts up they help tons.

1 499 500 501
(não será publicado)