cocos drawNode分析
2022-03-23 本文已影响0人
许彦峰
void GLProgramCache::loadDefaultGLPrograms()
{
GLProgram *p = new (std::nothrow) GLProgram();
// 加载对应的shader,kShaderType_PositionTextureColor来自#include "*.(frag,vert)"的字符串变量
// 参考 https://www.zhihu.com/question/431645051
loadDefaultGLProgram(p, kShaderType_PositionTextureColor);
// 更新目标字符串
_programs.emplace(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR, p);
}
drawNode使用的是GLProgram::SHADER_NAME_POSITION_LENGTH_TEXTURE_COLOR
const char* ccPositionColorLengthTexture_vert = R"(
#ifdef GL_ES
precision lowp float;
#endif
#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
uniform float u_alpha;
void main()
{
v_color = vec4(a_color.rgb * a_color.a * u_alpha, a_color.a * u_alpha);
v_texcoord = a_texcoord;
gl_Position = CC_MVPMatrix * a_position;
}
)";
const char* ccPositionColorLengthTexture_frag = R"(
#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
}
)";
MontionStreak使用的GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR
const char* ccPositionTextureColor_vert = R"(
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;
}
)";
const char* ccPositionTextureColor_frag = R"(
#ifdef GL_ES
precision lowp float;
#endif
varying vec4 v_fragmentColor;
varying vec2 v_texCoord;
void main()
{
gl_FragColor = v_fragmentColor * texture2D(CC_Texture0, v_texCoord);
}
)";