GLSL 卡通shader
来源:互联网 发布:mac怎么看央视直播 编辑:程序博客网 时间:2024/04/25 04:13
浏览Geeks3D,发现一个好玩儿的shader,改了个gles版本,用cocos2dx在iphone上跑了一下,效果如图:
原文shader是gl版,在手机上跑的很慢,iphone5全屏处理情况下帧数仅为10帧左右,我做了一些优化,使得全屏情况下帧数提升到30左右。
shader我加了些注释,如下:
#ifdef GL_ES
precision mediump float;
#endif
varying vec2 v_texCoord;
varying vec4 v_fragmentColor;
uniform float u_texWidth;
uniform float u_texHeight;
uniform float touch_x_offset;
const float edge_thres = 0.2;
const float edge_thres2 = 5.0;
#define HueLevCount 6
#define SatLevCount 7
#define ValLevCount 4
//HSV区间
float HueLevels[HueLevCount];
float SatLevels[SatLevCount];
float ValLevels[ValLevCount];
//H(色调)范围(0-360)分为6份
void buildHueLevel()
{
HueLevels[0]= 0.0;
HueLevels[1]= 140.0;
HueLevels[2]= 160.0;
HueLevels[3]= 240.0;
HueLevels[4]= 240.0;
HueLevels[5]= 360.0;
}
//S(饱和度)范围(0-1.0)分为7份
void buildSatLevel()
{
SatLevels[0]= 0.0;
SatLevels[1]= 0.15;
SatLevels[2]= 0.3;
SatLevels[3]= 0.45;
SatLevels[4]= 0.6;
SatLevels[5]= 0.8;
SatLevels[6]= 1.0;
}
//V(亮度)范围(0-1.0)分为4份
void buildValLevel()
{
ValLevels[0]= 0.0;
ValLevels[1]= 0.3;
ValLevels[2]= 0.6;
ValLevels[3]= 1.0;
}
//将RGB转为HSV
vec3 RGBtoHSV(float r, float g, float b)
{
float minv,maxv, delta;
vec3res;
minv =min(min(r, g), b);
maxv =max(max(r, g), b);
res.z =maxv; // v
delta = maxv- minv;
if( maxv !=0.0 )
res.y = delta /maxv; // s
else {
// r = g = b =0 // s = 0, v is undefined
res.y = 0.0;
res.x = -1.0;
return res;
}
if( r ==maxv )
res.x = ( g - b ) /delta; // between yellow & magenta
else if( g== maxv )
res.x = 2.0 + ( b - r ) /delta; // between cyan &yellow
else
res.x = 4.0 + ( r - g ) /delta; // between magenta &cyan
res.x =res.x *60.0; // degrees
if( res.x< 0.0 )
res.x = res.x + 360.0;
returnres;
}
//HSV转RGB
vec3 HSVtoRGB(float h, float s, float v)
{
int i;
float f, p,q, t;
vec3res;
if( s == 0.0)
{
// achromatic (grey)
res.x = v;
原文shader是gl版,在手机上跑的很慢,iphone5全屏处理情况下帧数仅为10帧左右,我做了一些优化,使得全屏情况下帧数提升到30左右。
shader我加了些注释,如下:
#ifdef GL_ES
precision mediump float;
#endif
varying vec2 v_texCoord;
varying vec4 v_fragmentColor;
uniform float u_texWidth;
uniform float u_texHeight;
uniform float touch_x_offset;
const float edge_thres = 0.2;
const float edge_thres2 = 5.0;
#define HueLevCount 6
#define SatLevCount 7
#define ValLevCount 4
//HSV区间
float HueLevels[HueLevCount];
float SatLevels[SatLevCount];
float ValLevels[ValLevCount];
//H(色调)范围(0-360)分为6份
void buildHueLevel()
{
}
//S(饱和度)范围(0-1.0)分为7份
void buildSatLevel()
{
}
//V(亮度)范围(0-1.0)分为4份
void buildValLevel()
{
}
//将RGB转为HSV
vec3 RGBtoHSV(float r, float g, float b)
{
}
//HSV转RGB
vec3 HSVtoRGB(float h, float s, float v)
{