//例如如下一个简单的变色程序,蓝色变红色 ,js 代码:
-
var url = "res/sb.jpg";
-
var sp = new cc.Sprite(url);
-
sp.setAnchorPoint(0,0);
-
this.addChild(sp);
-
this.shader = new cc.GLProgram("res/Shaders/example_Flower.vsh", "res/Shaders/example_Flower.fsh");
-
this.shader.retain();
-
this.shader.link();
-
this.shader.updateUniforms();
-
var program = this.shader.getProgram();
-
sp.shaderProgram = this.shader;
VertexShader
-
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_PMatrix * a_position;
-
v_fragmentColor = a_color;
-
v_texCoord = a_texCoord;
-
}
FragmentShader
-
#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);
-
float r = gl_FragColor[0];
-
float g = gl_FragColor[1];
-
float b = gl_FragColor[2];
-
float a = gl_FragColor[3];
-
if(b > r && b > g && b > (r*0.5 + g*0.5)*1.5) {
-
gl_FragColor[0] = b;
-
gl_FragColor[2] = r;
-
}
-
}
原文地址:https://blog.csdn.net/jiexiaopei_2004/article/details/50542860
原文地址:https://www.cnblogs.com/wodehao0808/p/9455697.html