先看個簡單的代碼
%% 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 雖然他是並行計算的 你可以按照串行理解
