cocos2dx 修改亮度、对比度、色调、饱和度

废话少说,直接修改CCSprite使用的片面着色器ccShader_PositionTextureColor_noMVP.frag:


/*
* cocos2d for iPhone: http://www.cocos2d-iphone.org
*
* Copyright (c) 2011 Ricardo Quesada
* Copyright (c) 2012 Zynga Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/


const char* ccPositionTextureColor_noMVP_frag = R"(
#ifdef GL_ES
precision lowp float;
#endif


varying vec4 v_fragmentColor;
varying vec2 v_texCoord;


uniform float u_hue;
uniform float u_saturation;
uniform float u_brightness;
uniform float u_contrast;


vec3 rgbtohsv(vec3 rgb)
{
float R = rgb.x;
float G = rgb.y;
float B = rgb.z;
vec3 hsv;
float max1 = max(R, max(G, B));
float min1 = min(R, min(G, B));


if (R == max1)
{
if(max1 == min1)
{
hsv.x = 0.0;
}
else
{
hsv.x = (G - B) / (max1 - min1);
}

}
if (G == max1)
{
if(max1 == min1)
{
hsv.x = 2.0;
}
else
{
hsv.x = 2.0 + (B - R) / (max1 - min1);
}
}
if (B == max1)
{
if(max1 == min1)
{
hsv.x = 4.0;
}
else
{
hsv.x = 4.0 + (R - G) / (max1 - min1);
}
}
hsv.x = hsv.x * 60.0;
if (hsv.x < 0.0)
{
hsv.x = hsv.x + 360.0;
}
hsv.z = max1;
if(max1 == 0.0)
{
hsv.y = 0.0;
}
else
{
hsv.y = (max1 - min1) / max1;
}

return hsv;
}
vec3 hsvtorgb(vec3 hsv)
{
float R;
float G;
float B;
if (hsv.y == 0.0)
{
R = G = B = hsv.z;
}
else
{
hsv.x = hsv.x / 60.0;
int i = int(hsv.x);
float f = hsv.x - float(i);
float a = hsv.z * (1.0 - hsv.y);
float b = hsv.z * (1.0 - hsv.y * f);
float c = hsv.z * (1.0 - hsv.y * (1.0 - f));
if (i == 0)
{
R = hsv.z;
G = c;
B = a;
}
else if (i == 1)
{
R = b;
G = hsv.z;
B = a;
}
else if (i == 2)
{
R = a;
G = hsv.z;
B = c;
}
else if (i == 3)
{
R = a;
G = b;
B = hsv.z;
}
else if (i == 4)
{
R = c;
G = a;
B = hsv.z;
}
else
{
R = hsv.z;
G = a;
B = b;
}
}
return vec3(R, G, B);
}


void main()
{
vec4 pixColor = texture2D(CC_Texture0, v_texCoord);
vec3 hsv;
hsv.xyz = rgbtohsv(pixColor.xyz);
hsv.x += u_hue;
hsv.x = mod(hsv.x, 360.0);
hsv.y *= u_saturation;
hsv.z *= u_brightness;
vec3 f_color = hsvtorgb(hsv);
f_color = ((f_color - 0.5) * max(u_contrast + 1.0, 0.0)) + 0.5;
gl_FragColor = v_fragmentColor * vec4(f_color, pixColor.a);
}
)";


 

 

 

u_hue : 色调H, 范围[0 ~ 1], 默认0
u_saturation : 饱和度 [0 ~ 2], 默认1
u_brightness : 亮度 [0 ~ 2], 默认1
u_contrast : 对比度 [0 ~ 2], 默认0


初始化该着色器需要设置饱和度和亮度:
GLProgramState::getOrCreateWithGLProgramName(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR_NO_MVP)->setUniformFloat("u_saturation", 1);
GLProgramState::getOrCreateWithGLProgramName(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR_NO_MVP)->setUniformFloat("u_brightness", 1);

转载请注明出处,from 博客园 HemJohn

 

posted on 2019-04-11 11:26  HemJohn  阅读(1115)  评论(0编辑  收藏  举报

导航