前言
本文把cocos2d-x 2.x版本中预定义的所有shader源码贴出来,便于集中参考。作者的引擎版本是2.2.3.
下面以ccShaders.h中定义的片段着色器变量名作为标题,逐条列出。(为什么不用顶点着色器的变量名,因为有几个shader只有片段着色器变量名)
ccPosition_uColor_frag
ccShader_Position_uColor_vert.h
attribute vec4 a_position;
uniform vec4 u_color;
uniform float u_pointSize;
#ifdef GL_ES
varying lowp vec4 v_fragmentColor;
#else
varying vec4 v_fragmentColor;
#endif
void main()
{
gl_Position = CC_MVPMatrix * a_position;
gl_PointSize = u_pointSize;
v_fragmentColor = u_color;
}
ccShader_Position_uColor_frag.h
#ifdef GL_ES
precision lowp float;
#endif
varying vec4 v_fragmentColor;
void main()
{
gl_FragColor = v_fragmentColor;
}
ccPositionColor_frag
ccShader_PositionColor_vert.h
attribute vec4 a_position;
attribute vec4 a_color;
#ifdef GL_ES
varying lowp vec4 v_fragmentColor;
#else
varying vec4 v_fragmentColor;
#endif
void main()
{
gl_Position = CC_MVPMatrix * a_position;
v_fragmentColor = a_color;
}
ccShader_PositionColor_frag.h
#ifdef GL_ES
precision lowp float;
#endif
varying vec4 v_fragmentColor;
void main()
{
gl_FragColor = v_fragmentColor;
}
ccPositionTexture_frag
ccShader_PositionTexture_vert.h
attribute vec4 a_position;
attribute vec2 a_texCoord;
#ifdef GL_ES
varying mediump vec2 v_texCoord;
#else
varying vec2 v_texCoord;
#endif
void main()
{
gl_Position = CC_MVPMatrix * a_position;
v_texCoord = a_texCoord;
}
ccShader_PositionTexture_frag.h
#ifdef GL_ES
precision lowp float;
#endif
varying vec2 v_texCoord;
uniform sampler2D CC_Texture0;
void main()
{
gl_FragColor = texture2D(CC_Texture0,v_texCoord);
}
ccPositionTextureA8Color_frag
ccShader_PositionTextureA8Color_vert.h
attribute vec4 a_position;
attribute vec2 a_texCoord;
attribute vec4 a_color;
#ifdef GL_ES
varying lowp vec4 v_fragmentColor;
varying mediump vec2 v_texCoord;
#else
varying vec4 v_fragmentColor;
varying vec2 v_texCoord;
#endif
void main()
{
gl_Position = CC_MVPMatrix * a_position;
v_fragmentColor = a_color;
v_texCoord = a_texCoord;
}
ccShader_PositionTextureA8Color_frag.h
#ifdef GL_ES
precision lowp float;
#endif
varying vec4 v_fragmentColor;
varying vec2 v_texCoord;
uniform sampler2D CC_Texture0;
void main()
{
gl_FragColor = vec4( v_fragmentColor.rgb,// RGB from uniform
v_fragmentColor.a * texture2D(CC_Texture0,v_texCoord).a // A from texture & uniform
);
}
ccPositionTextureColor_frag
ccShader_PositionTextureColor_vert.h
attribute vec4 a_position;
attribute vec2 a_texCoord;
attribute vec4 a_color;
#ifdef GL_ES
varying lowp vec4 v_fragmentColor;
varying mediump vec2 v_texCoord;
#else
varying vec4 v_fragmentColor;
varying vec2 v_texCoord;
#endif
void main()
{
gl_Position = CC_MVPMatrix * a_position;
v_fragmentColor = a_color;
v_texCoord = a_texCoord;
}
ccShader_PositionTextureColor_frag.h
#ifdef GL_ES
precision lowp float;
#endif
varying vec4 v_fragmentColor;
varying vec2 v_texCoord;
uniform sampler2D CC_Texture0;
void main()
{
gl_FragColor = v_fragmentColor * texture2D(CC_Texture0,v_texCoord);
}
ccPositionTextureColorAlphaTest_frag
ccShader_PositionTextureColorAlphaTest_frag.h
#ifdef GL_ES
precision lowp float;
#endif
varying vec4 v_fragmentColor;
varying vec2 v_texCoord;
uniform sampler2D CC_Texture0;
uniform float CC_alpha_value;
void main()
{
vec4 texColor = texture2D(CC_Texture0,v_texCoord);
// mimic: glAlphaFunc(GL_GREATER)
// pass if ( incoming_pixel >= CC_alpha_value ) => fail if incoming_pixel < CC_alpha_value
if ( texColor.a <= CC_alpha_value )
discard;
gl_FragColor = texColor * v_fragmentColor;
}
ccPositionTexture_uColor_frag
ccShader_PositionTexture_uColor_vert.h
attribute vec4 a_position;
attribute vec2 a_texCoord;
#ifdef GL_ES
varying mediump vec2 v_texCoord;
#else
varying vec2 v_texCoord;
#endif
void main()
{
gl_Position = CC_MVPMatrix * a_position;
v_texCoord = a_texCoord;
}
ccShader_PositionTexture_uColor_frag.h
#ifdef GL_ES
precision lowp float;
#endif
uniform vec4 u_color;
varying vec2 v_texCoord;
uniform sampler2D CC_Texture0;
void main()
{
gl_FragColor = texture2D(CC_Texture0,v_texCoord) * u_color;
}
ccExSwitchMask_frag
ccShaderEx_SwitchMask_frag.h
#ifdef GL_ES
precision lowp float;
#endif
varying vec4 v_fragmentColor;
varying vec2 v_texCoord;
uniform sampler2D u_texture;
uniform sampler2D u_mask;
void main()
{
vec4 texColor = texture2D(u_texture,v_texCoord);
vec4 maskColor = texture2D(u_mask,v_texCoord);
vec4 finalColor = vec4(texColor.r,texColor.g,texColor.b,maskColor.a * texColor.a);
gl_FragColor = v_fragmentColor * finalColor;
}
ccPositionColorLengthTexture_frag
ccShader_PositionColorLengthTexture_vert.h
#ifdef GL_ES
attribute mediump vec4 a_position;
attribute mediump vec2 a_texcoord;
attribute mediump vec4 a_color;
varying mediump vec4 v_color;
varying mediump vec2 v_texcoord;
#else
attribute vec4 a_position;
attribute vec2 a_texcoord;
attribute vec4 a_color;
varying vec4 v_color;
varying vec2 v_texcoord;
#endif
void main()
{
v_color = vec4(a_color.rgb * a_color.a,a_color.a);
v_texcoord = a_texcoord;
gl_Position = CC_MVPMatrix * a_position;
}
ccShader_PositionColorLengthTexture_frag.h
#ifdef GL_ES
// #extension GL_OES_standard_derivatives : enable
varying mediump vec4 v_color;
varying mediump vec2 v_texcoord;
#else
varying vec4 v_color;
varying vec2 v_texcoord;
#endif
void main()
{
// #if defined GL_OES_standard_derivatives
// gl_FragColor = v_color*smoothstep(0.0,length(fwidth(v_texcoord)),1.0 - length(v_texcoord));
// #else
gl_FragColor = v_color*step(0.0,1.0 - length(v_texcoord));
// #endif
}
作者水平有限,对相关知识的理解和总结难免有错误,还望给予指正,非常感谢!