0% found this document useful (0 votes)
90 views3 pages

Cartoon Shader

This document contains shader code for a cartoon shader effect. It defines functions to: 1) Convert between RGB and HSL color spaces, including calculating hue, saturation, and luminance values from RGB. 2) Convert between HSL and RGB by calculating individual RGB component values from hue, saturation, and luminance. 3) Sample colors from a texture at different offsets to determine edges, calculate an average color, and use this to produce a cartoonized output color by amplifying differences from the average.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
90 views3 pages

Cartoon Shader

This document contains shader code for a cartoon shader effect. It defines functions to: 1) Convert between RGB and HSL color spaces, including calculating hue, saturation, and luminance values from RGB. 2) Convert between HSL and RGB by calculating individual RGB component values from hue, saturation, and luminance. 3) Sample colors from a texture at different offsets to determine edges, calculate an average color, and use this to produce a cartoonized output color by amplifying differences from the average.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

// Cartoon Shader=ps_3_0

// Code by Andrew Alexander Price


// http://www.andrewalexanderprice.com/other/cartoonshader/
#define
#define
#define
#define

LineThickness 0.00025
SensitivityUpper 11
SensitivityLower 11
Saturation 2,8

sampler s0 : register(s0);
float4 p0 : register(c0);
float4 p1 : register(c1);
#define
#define
#define
#define
#define
#define

width (p0[0])
height (p0[1])
counter (p0[2])
clock (p0[3])
one_over_width (p1[0])
one_over_height (p1[1])

#define PI acos(-1)
float3 RGBToHSL(float3 color)
{
float3 hsl; // init to 0 to avoid warnings ? (and reverse if + remove fi
rst part)
float fmin = min(min(color.r, color.g), color.b);
//Min. value of RGB
float fmax = max(max(color.r, color.g), color.b);
//Max. value of RGB
float delta = fmax - fmin;
//Delta RGB value
hsl.z = (fmax + fmin) / 2.0; // Luminance
if (delta == 0.0)
//This is a gray, no chroma...
{
hsl.x = 0.0;
// Hue
hsl.y = 0.0;
// Saturation
}
else
//Chromatic data...
{
if (hsl.z < 0.5)
hsl.y = delta / (fmax + fmin); // Saturation
else
hsl.y = delta / (2.0 - fmax - fmin); // Saturation
float deltaR = (((fmax - color.r) / 6.0) + (delta / 2.0)) / delt
a;
float deltaG = (((fmax - color.g) / 6.0) + (delta / 2.0)) / delt
a;
float deltaB = (((fmax - color.b) / 6.0) + (delta / 2.0)) / delt
a;
if (color.r == fmax )
hsl.x = deltaB - deltaG; // Hue
else if (color.g == fmax)
hsl.x = (1.0 / 3.0) + deltaR - deltaB; // Hue
else if (color.b == fmax)
hsl.x = (2.0 / 3.0) + deltaG - deltaR; // Hue
if (hsl.x < 0.0)

hsl.x += 1.0; // Hue


else if (hsl.x > 1.0)
hsl.x -= 1.0; // Hue
}
return hsl;
}
float HueToRGB(float f1, float
{
if (hue < 0.0)
hue += 1.0;
else if (hue > 1.0)
hue -= 1.0;
float res;
if ((6.0 * hue) < 1.0)
res = f1 + (f2
else if ((2.0 * hue) <
res = f2;
else if ((3.0 * hue) <
res = f1 + (f2
else
res = f1;
return res;
}

f2, float hue)

- f1) * 6.0 * hue;


1.0)
2.0)
- f1) * ((2.0 / 3.0) - hue) * 6.0;

float3 HSLToRGB(float3 hsl)


{
float3 rgb;
if (hsl.y == 0.0)
rgb = float3(hsl.z, hsl.z, hsl.z); // Luminance
else
{
float f2;
if (hsl.z < 0.5)
f2 = hsl.z * (1.0 + hsl.y);
else
f2 = (hsl.z + hsl.y) - (hsl.y * hsl.z);
float f1 = 2.0 * hsl.z - f2;
rgb.r = HueToRGB(f1, f2, hsl.x + (1.0/3.0));
rgb.g = HueToRGB(f1, f2, hsl.x);
rgb.b= HueToRGB(f1, f2, hsl.x - (1.0/3.0));
}
return rgb;
}
float4 main(float2 tex : TEXCOORD0) : COLOR
{
float dx =width/height * LineThickness;
float dy =LineThickness;
float4
float4
float4
float4

c1
c2
c3
c4

=
=
=
=

tex2D(s0,
tex2D(s0,
tex2D(s0,
tex2D(s0,

tex
tex
tex
tex

+
+
+
+

float2(-dx,-dy));
float2(0,-dy));
float2(-dx,dy));
float2(-dx,0));

float4
float4
float4
float4
float4

c5
c6
c7
c8
c9

=
=
=
=
=

tex2D(s0,
tex2D(s0,
tex2D(s0,
tex2D(s0,
tex2D(s0,

tex
tex
tex
tex
tex

+
+
+
+
+

float2(0,0));
float2(dx,0));
float2(dx,-dy));
float2(0,dy));
float2(dx,dy));

float4 c0 = (-c1-c2-c3-c4+c6+c7+c8+c9);
float4 average = (c1 + c2 + c3 + c4 + c6 + c7 + c8 + c9) - (c5 * 6);
float av = (average .r + average .g + average .b) / 3;
c0 = 1-abs((c0.r+c0.g+c0.b)/av);
float val = pow(saturate((c0.r + c0.g + c0.b) / 3), SensitivityUpper);
val = 1 - pow(1 - val, SensitivityLower);
c0 = float4(val, val, val, val);
// colour
c1 = tex2D(s0,tex);
float3 hsl = RGBToHSL(c1.xyz);
hsl.g *= Saturation;
c1 = float4(HSLToRGB(hsl),1);
return c1 * c0;
}

You might also like