マイペースなプログラミング日記

DTMやプログラミングにお熱なd-kamiがマイペースに書くブログ

シェーダーでモザイクをかけてみた

画素周辺の平均値を使ってモザイクをかけてみた

頂点シェーダー

varying vec2 pos;

void main(void){
    pos = gl_Vertex.xy;
    gl_Position = ftransform();
}

フラグメントシェーダー

uniform sampler2D image; //画像
uniform int imgWidth, imgHeight; //画像の幅、高さ
uniform int nPixel; //周囲のピクセル数(行、または列)
varying vec2 pos;

void main(void){
    int x = int(floor(pos.x / float(nPixel))) * nPixel;
    int y = int(floor(pos.y / float(nPixel))) * nPixel;
    vec2 texCoord;

    vec4 sum = vec4(0.0);
    int cnt = 0;
        
    for(int j = y; j < y + nPixel ; j++){
        for(int i = x; i < x + nPixel; i++){
            texCoord = vec2(float(i) / float(imgWidth), float(j) / float(imgHeight));
            sum += texture2D(image, texCoord);
            cnt++;
        }
    }

    gl_FragColor.rgb = sum.rgb / (float(cnt));
}