cocoscreator shader教程

先看个简单的代码

%% vs {

precision highp float;

uniform mat4 cc_matViewProj;
attribute vec3 a_position;
attribute vec2 a_uv0;
varying vec2 uv0;
void main () {
    vec4 pos = cc_matViewProj * vec4(a_position, 1);
    gl_Position = pos;
    uv0 = a_uv0;
}

}

%% fs {

precision highp float;

uniform sampler2D texture;
uniform vec4 color;
varying vec2 uv0;

void main () {
    vec4 c = color * texture2D(texture, uv0);
    float clrbright = (c.r + c.g + c.b) * (1. / 3.);
    float gray = (0.6) * clrbright;
    gl_FragColor = vec4(c);
}
}

  attribute 的变量名字不能该 都是绑定shader的 从底部传过来的

vec4 pos = cc_matViewProj * vec4(a_position, 1);
    gl_Position = pos;

  这句话的意思就是 设置顶点着色器

  vec4 c = color * texture2D(texture, uv0);

  这个c就是获取图片中每个像素的颜色

gl_FragColor = vec4(c);

  

最后赋值颜色 rbga 你懂的

最后讲个深入点的

 vec4 c = color * texture2D(texture, uv0);
    if(uv0.y>0.5){
        gl_FragColor = vec4(c);
    }else{
     gl_FragColor = vec4(0.3,0.3,0.3,1);
    }

可以发现上半是灰色的 因为uv控制了
总而言之 就是设置颜色。。gl_FragColor 虽然他是并行计算的 你可以按照串行理解

posted @ 2019-11-12 10:17  newmiracle宇宙  阅读(3511)  评论(0编辑  收藏  举报