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 雖然他是並行計算的 你可以按照串行理解


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM