uniform sampler2D texture;uniform vec2 resolution;uniform float cutoff;uniform float order;void main(){ vec2 texCoord = gl_FragCoord.xy / resolution.xy; vec4 color = texture2D(texture, texCoord); // 计算巴特沃斯滤波器的系数 float pi = 3.14159265359; float wc = 2.0 * pi * cutoff; float wc2 = wc * wc; float sqrt2 = sqrt(2.0); float sqrt2wc = sqrt2 * wc; float sqrt2wc2 = sqrt2 * wc2; float a = 1.0 / tan(sqrt2wc * (1.0 / order)); float b = 2.0 * cos(sqrt2wc * (1.0 / order)); float c = 1.0 / (1.0 + a + sqrt2wc2); // 应用滤波器 vec4 sum = vec4(0.0);sum += color * c; sum += texture2D(texture, vec2(texCoord.x, texCoord.y + 1.0 / resolution.y)) * a * c; sum += texture2D(texture, vec2(texCoord.x, texCoord.y - 1.0 / resolution.y)) * a * c; sum += texture2D(texture, vec2(texCoord.x + 1.0 / resolution.x, texCoord.y)) * b * c; sum += texture2D(texture, vec2(texCoord.x - 1.0 / resolution.x, texCoord.y)) * b * c; gl_FragColor = sum;}
------
#version 330 coreconst float PI = 3.14159265359;void fft(inout vec2[1024] x){ int N = x.length(); if (N <= 1) { return;} vec2[1024] even = vec2[1024](0); vec2[1024] odd = vec2[1024](0); for (int i = 0; i < N/2; i++) { even[i] = x[2*i]; odd[i] = x[2*i+1];} fft(even); fft(odd); for (int i = 0; i < N/2; i++) { float t = i * 2.0 * PI / float(N); vec2 w = vec2(cos(t), -sin(t)) * odd[i];x[i] = even[i] + w; x[i + N/2] = even[i] - w;}}void main(){ vec2[1024] x = vec2[1024](0); // 从纹理采样器中读取输入数据 for (int i = 0; i < 1024; i++) { vec4 texel = texture(input_texture, vec2(float(i) / 1024.0, 0.5)); x[i] = vec2(texel.r, 0.0);} // 计算傅里叶变换fft(x); // 将结果写回到纹理中 for (int i = 0; i < 1024; i++) { vec2 value = x[i]; vec4 texel = vec4(value.r, value.i, 0.0, 0.0); gl_FragColor = texel;}}
----------
#version 300 es
precision mediump float;
in vec2 v_texCoord;
out vec4 outColor;
uniform sampler2D s_texture;
uniform sampler2D textureLUT;
uniform lowp mat4 colorMatrix;
uniform int colorFlag;//滤镜类型
uniform float colorYZ;//阈值类型
const float offset = 1.0f / 300.0f;
uniform float iTime;
uniform vec3 iResolution;
uniform vec3 iResolution1;
float hash(vec2 p)// replace this by something better
{
p = 50.0*fract(p*0.3183099 + vec2(0.71, 0.113));
return -1.0+2.0*fract(p.x*p.y*(p.x+p.y));
}
float noise(in vec2 p)
{
vec2 i = floor(p);
vec2 f = fract(p);
vec2 u = f*f*(3.0-2.0*f);
return mix(mix(hash(i + vec2(0.0, 0.0)),
hash(i + vec2(1.0, 0.0)), u.x),
mix(hash(i + vec2(0.0, 1.0)),
hash(i + vec2(1.0, 1.0)), u.x), u.y);
}
//模糊
void mohu(inout vec4 color1){
vec2 tex_offset =vec2(1.0/300.0, 1.0/300.0);
vec4 orColor=texture(s_texture, v_texCoord);
float orAlpha=orColor.a;
float weight[5] = float[] (0.227027, 0.1945946, 0.1216216, 0.054054, 0.016216);
vec3 color=orColor.rgb*weight[0];
for (int i=1;i<5;i++)
{
color+=texture(s_texture, v_texCoord+vec2(tex_offset.x * float(i), 0.0)).rgb*weight[i];
color+=texture(s_texture, v_texCoord-vec2(tex_offset.x * float(i), 0.0)).rgb*weight[i];
// color+=texture(s_texture,v_texCoord+vec2(0.0,tex_offset.y * float(i))).rgb*weight[i];
// color+=texture(s_texture,v_texCoord-vec2(0.0,tex_offset.y * float(i))).rgb*weight[i];
}
color1=vec4(color, orAlpha);
}
//grouped
void grouped(inout vec4 color){
// float threshold = colorYZ;
// float mean = (color.r + color.g + color.b) / 3.0;
// color.r = color.g = color.b = mean >= threshold ? 1.0 : 0.0;
vec4 textureColor = texture(s_texture, v_texCoord);
float contrast = 1.0;
color = vec4(((textureColor.rgb - vec3(0.5)) * contrast + vec3(0.5)), textureColor.w);
}
void grouped1(inout vec4 color){
// 3840,1080 1080,3768
// float dx = 1.0 / 1920.0;
// float dy = 1.0 / 1080.0;
float texelWidth = 1.0 / 1920.0;
float texelHeight = 1.0 / 1080.0;
vec2 widthStep = vec2(texelWidth, 0.0);
vec2 heightStep = vec2(0.0, texelHeight);
vec2 widthHeightStep = vec2(texelWidth, texelHeight);
vec2 widthNegativeHeightStep = vec2(texelWidth, -texelHeight);
vec2 textureCoordinate = v_texCoord.xy;
vec2 leftTextureCoordinate = v_texCoord.xy - widthStep;
vec2 rightTextureCoordinate = v_texCoord.xy + widthStep;
vec2 topTextureCoordinate = v_texCoord.xy - heightStep;
vec2 topLeftTextureCoordinate = v_texCoord.xy - widthHeightStep;
vec2 topRightTextureCoordinate = v_texCoord.xy + widthNegativeHeightStep;
vec2 bottomTextureCoordinate = v_texCoord.xy + heightStep;
vec2 bottomLeftTextureCoordinate = v_texCoord.xy - widthNegativeHeightStep;
vec2 bottomRightTextureCoordinate = v_texCoord.xy + widthHeightStep;
float bottomLeftIntensity = texture(s_texture, bottomLeftTextureCoordinate).r;
float topRightIntensity = texture(s_texture, topRightTextureCoordinate).r;
float topLeftIntensity = texture(s_texture, topLeftTextureCoordinate).r;
float bottomRightIntensity = texture(s_texture, bottomRightTextureCoordinate).r;
float leftIntensity = texture(s_texture, leftTextureCoordinate).r;
float rightIntensity = texture(s_texture, rightTextureCoordinate).r;
float bottomIntensity = texture(s_texture, bottomTextureCoordinate).r;
float topIntensity = texture(s_texture, topTextureCoordinate).r;
vec2 gradientDirection;
gradientDirection.x = -bottomLeftIntensity - 2.0 * leftIntensity - topLeftIntensity + bottomRightIntensity + 2.0 * rightIntensity + topRightIntensity;
gradientDirection.y = -topLeftIntensity - 2.0 * topIntensity - topRightIntensity + bottomLeftIntensity + 2.0 * bottomIntensity + bottomRightIntensity;
float gradientMagnitude = length(gradientDirection);
vec2 normalizedDirection = normalize(gradientDirection);
normalizedDirection = sign(normalizedDirection) * floor(abs(normalizedDirection) + 0.617316);// Offset by 1-sin(pi/8) to set to 0 if near axis, 1 if away
normalizedDirection = (normalizedDirection + 1.0) * 0.5;// Place -1.0 - 1.0 within 0 - 1.0
color = vec4(gradientMagnitude, normalizedDirection.x, normalizedDirection.y, 1.0);
}
void grouped2(inout vec4 color){
const highp vec3 W = vec3(0.2125, 0.7154, 0.0721);
lowp vec4 textureColor = texture(s_texture, v_texCoord);
float luminance = dot(textureColor.rgb, W);
color = vec4(vec3(luminance), textureColor.a);
}
void sobel11(inout vec4 color){
// 3840,1080 1080,3768
//分屏
float x = v_texCoord.x;
if (x<0.5){
x+=0.25;
} else {
x-=0.25;
}
float texelWidth = 1.0/3768.0;
float texelHeight = 1.0/1080.0;
vec2 widthStep = vec2(texelWidth, 0.0);
vec2 heightStep = vec2(0.0, texelHeight);
vec2 widthHeightStep = vec2(texelWidth, texelHeight);
vec2 widthNegativeHeightStep = vec2(texelWidth, -texelHeight);
vec2 textureCoordinate = v_texCoord.xy;
vec2 leftTextureCoordinate = v_texCoord.xy - widthStep;
vec2 rightTextureCoordinate = v_texCoord.xy + widthStep;
vec2 topTextureCoordinate = v_texCoord.xy - heightStep;
vec2 topLeftTextureCoordinate = v_texCoord.xy - widthHeightStep;
vec2 topRightTextureCoordinate = v_texCoord.xy + widthNegativeHeightStep;
vec2 bottomTextureCoordinate = v_texCoord.xy + heightStep;
vec2 bottomLeftTextureCoordinate = v_texCoord.xy - widthNegativeHeightStep;
vec2 bottomRightTextureCoordinate = v_texCoord.xy + widthHeightStep;
float bottomLeftIntensity = texture(s_texture, bottomLeftTextureCoordinate).r;
float topRightIntensity = texture(s_texture, topRightTextureCoordinate).r;
float topLeftIntensity = texture(s_texture, topLeftTextureCoordinate).r;
float bottomRightIntensity = texture(s_texture, bottomRightTextureCoordinate).r;
float leftIntensity = texture(s_texture, leftTextureCoordinate).r;
float rightIntensity = texture(s_texture, rightTextureCoordinate).r;
float bottomIntensity = texture(s_texture, bottomTextureCoordinate).r;
float topIntensity = texture(s_texture, topTextureCoordinate).r;
float h = -topLeftIntensity - 2.0 * topIntensity - topRightIntensity + bottomLeftIntensity + 2.0 * bottomIntensity + bottomRightIntensity;
float v = -bottomLeftIntensity - 2.0 * leftIntensity - topLeftIntensity + bottomRightIntensity + 2.0 * rightIntensity + topRightIntensity;
float mag = length(vec2(h, v));
color = vec4(vec3(mag), 1.0);
}
//rgb转hsl
vec3 rgb2hsl(vec3 color){
vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);
vec4 p = mix(vec4(color.bg, K.wz), vec4(color.gb, K.xy), step(color.b, color.g));
vec4 q = mix(vec4(p.xyw, color.r), vec4(color.r, p.yzx), step(p.x, color.r));
float d = q.x - min(q.w, q.y);
float e = 1.0e-10;
return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x);
}
//hsl转rgb
vec3 hsl2rgb(vec3 color){
vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
vec3 p = abs(fract(color.xxx + K.xyz) * 6.0 - K.www);
return color.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), color.y);
}
//灰度
void grey(inout vec4 color){
float weightMean = color.r * 0.3 + color.g * 0.59 + color.b * 0.11;
color.r = color.g = color.b = weightMean;
}
//黑白
void blackAndWhite(inout vec4 color){
float threshold = colorYZ;
float mean = (color.r + color.g + color.b) / 3.0;
color.r = color.g = color.b = mean >= threshold ? 1.0 : 0.0;
}
//白黑
void WhiteAndblack(inout vec4 color){
float threshold = colorYZ;
float mean = (color.r + color.g + color.b) / 3.0;
color.r = color.g = color.b = mean >= threshold ? 0.0 : 1.0;
}
//黑黄
void BlackAndYellow(inout vec4 color){
float threshold = colorYZ;
float mean = (color.r + color.g + color.b) / 3.0;
if (mean >= threshold){
color.r =0.0;
color.g =0.0;
color.b =0.0;
} else {
color.r =1.0;
color.g =1.0;
color.b =0.0;
}
}
//黄黑
void YellowAndBlack(inout vec4 color){
float threshold = colorYZ;
float mean = (color.r + color.g + color.b) / 3.0;
if (mean >= threshold){
color.r =1.0;
color.g =1.0;
color.b =0.0;
} else {
color.r =0.0;
color.g =0.0;
color.b =0.0;
}
}
//负片
void fupian(inout vec4 color){
//" gl_FragColor = vec4((1.0 - textureColor.rgb), textureColor.w);
color = vec4((1.0 - color.rgb), color.w);
}
//三色模式
void sanse(inout vec4 color){
//gl_FragColor = floor((textureColor * colorLevels) + vec4(0.5)) / colorLevels;
color = floor((color * 1.0) + vec4(0.5)) / 1.0;
}
//动态轮廓
void dtlk(inout vec4 color){
float intensity;
float quantizationLevels;
float threshold = 0.2;
float texelWidth = 1.0 / 1920.0;
float texelHeight = 1.0 / 1080.0;
vec2 widthStep = vec2(texelWidth, 0.0);
vec2 heightStep = vec2(0.0, texelHeight);
vec2 widthHeightStep = vec2(texelWidth, texelHeight);
vec2 widthNegativeHeightStep = vec2(texelWidth, -texelHeight);
vec2 textureCoordinate = v_texCoord.xy;
vec2 leftTextureCoordinate = v_texCoord.xy - widthStep;
vec2 rightTextureCoordinate = v_texCoord.xy + widthStep;
vec2 topTextureCoordinate = v_texCoord.xy - heightStep;
vec2 topLeftTextureCoordinate = v_texCoord.xy - widthHeightStep;
vec2 topRightTextureCoordinate = v_texCoord.xy + widthNegativeHeightStep;
vec2 bottomTextureCoordinate = v_texCoord.xy + heightStep;
vec2 bottomLeftTextureCoordinate = v_texCoord.xy - widthNegativeHeightStep;
vec2 bottomRightTextureCoordinate = v_texCoord.xy + widthHeightStep;
float bottomLeftIntensity = texture(s_texture, bottomLeftTextureCoordinate).r;
float topRightIntensity = texture(s_texture, topRightTextureCoordinate).r;
float topLeftIntensity = texture(s_texture, topLeftTextureCoordinate).r;
float bottomRightIntensity = texture(s_texture, bottomRightTextureCoordinate).r;
float leftIntensity = texture(s_texture, leftTextureCoordinate).r;
float rightIntensity = texture(s_texture, rightTextureCoordinate).r;
float bottomIntensity = texture(s_texture, bottomTextureCoordinate).r;
float topIntensity = texture(s_texture, topTextureCoordinate).r;
float h = -topLeftIntensity - 2.0 * topIntensity - topRightIntensity + bottomLeftIntensity + 2.0 * bottomIntensity + bottomRightIntensity;
float v = -bottomLeftIntensity - 2.0 * leftIntensity - topLeftIntensity + bottomRightIntensity + 2.0 * rightIntensity + topRightIntensity;
float mag = length(vec2(h, v));
vec3 posterizedImageColor = floor((color.rgb * 10.0) + 0.5) / 10.0;
float thresholdTest = 1.0 - step(threshold, mag);
color = vec4(posterizedImageColor * thresholdTest, color.a);
}
//反负片
void ffupian(inout vec4 color){
//" gl_FragColor = vec4((1.0 - textureColor.rgb), textureColor.w);
color = vec4((1.0 - color.rgb), color.w);
color.r = 1.0 - color.r;
color.g = 1.0 - color.g;
color.b = 1.0 - color.b;
}
//反三色模式
void fsanse(inout vec4 color){
//gl_FragColor = floor((textureColor * colorLevels) + vec4(0.5)) / colorLevels;
color = floor((color * 1.0) + vec4(0.5)) / 1.0;
color.r = 1.0 - color.r;
color.g = 1.0 - color.g;
color.b = 1.0 - color.b;
}
//反动态轮廓
void fdtlk(inout vec4 color){
float intensity;
float quantizationLevels;
float threshold = 0.2;
float texelWidth = 1.0 / 1920.0;
float texelHeight = 1.0 / 1080.0;
vec2 widthStep = vec2(texelWidth, 0.0);
vec2 heightStep = vec2(0.0, texelHeight);
vec2 widthHeightStep = vec2(texelWidth, texelHeight);
vec2 widthNegativeHeightStep = vec2(texelWidth, -texelHeight);
vec2 textureCoordinate = v_texCoord.xy;
vec2 leftTextureCoordinate = v_texCoord.xy - widthStep;
vec2 rightTextureCoordinate = v_texCoord.xy + widthStep;
vec2 topTextureCoordinate = v_texCoord.xy - heightStep;
vec2 topLeftTextureCoordinate = v_texCoord.xy - widthHeightStep;
vec2 topRightTextureCoordinate = v_texCoord.xy + widthNegativeHeightStep;
vec2 bottomTextureCoordinate = v_texCoord.xy + heightStep;
vec2 bottomLeftTextureCoordinate = v_texCoord.xy - widthNegativeHeightStep;
vec2 bottomRightTextureCoordinate = v_texCoord.xy + widthHeightStep;
float bottomLeftIntensity = texture(s_texture, bottomLeftTextureCoordinate).r;
float topRightIntensity = texture(s_texture, topRightTextureCoordinate).r;
float topLeftIntensity = texture(s_texture, topLeftTextureCoordinate).r;
float bottomRightIntensity = texture(s_texture, bottomRightTextureCoordinate).r;
float leftIntensity = texture(s_texture, leftTextureCoordinate).r;
float rightIntensity = texture(s_texture, rightTextureCoordinate).r;
float bottomIntensity = texture(s_texture, bottomTextureCoordinate).r;
float topIntensity = texture(s_texture, topTextureCoordinate).r;
float h = -topLeftIntensity - 2.0 * topIntensity - topRightIntensity + bottomLeftIntensity + 2.0 * bottomIntensity + bottomRightIntensity;
float v = -bottomLeftIntensity - 2.0 * leftIntensity - topLeftIntensity + bottomRightIntensity + 2.0 * rightIntensity + topRightIntensity;
float mag = length(vec2(h, v));
vec3 posterizedImageColor = floor((color.rgb * 10.0) + 0.5) / 10.0;
float thresholdTest = 1.0 - step(threshold, mag);
color = vec4(posterizedImageColor * thresholdTest, color.a);
color.r = 1.0 - color.r;
color.g = 1.0 - color.g;
color.b = 1.0 - color.b;
}
void redColor(inout vec4 color){
// mat4 colorMatrix = {
// 1.0f, 0.0f, 0.0f, 0.0f,
// 0.0f, 1.0f, 0.0f, 0.0f,
// 0.0f, 0.0f, 1.0f, 0.0f,
// 0.0f, 0.0f, 0.0f, 1.0f
// };
// vec4 outputColor = color * colorMatrix;
// color = outputColor;
color.b = color.b*0.0233;
}
void yellowColor(inout vec4 color){
//黄绿色盲
color.r = color.r*0.0685;
}
//蓝白
void BlueAndWhite(inout vec4 color){
float threshold = colorYZ;
float mean = (color.r + color.g + color.b) / 3.0;
if (mean >= threshold){
color.r =0.0;
color.g =0.0;
color.b =1.0;
} else {
color.r =1.0;
color.g =1.0;
color.b =1.0;
}
}
//白蓝
void WhiteAndBlue(inout vec4 color){
float threshold = colorYZ;
float mean = (color.r + color.g + color.b) / 3.0;
if (mean >= threshold){
color.r =1.0;
color.g =1.0;
color.b =1.0;
} else {
color.r =0.0;
color.g =0.0;
color.b =1.0;
}
}
//蓝黄
void BlueAndYellow(inout vec4 color){
float threshold = colorYZ;
float mean = (color.r + color.g + color.b) / 3.0;
if (mean >= threshold){
color.r =0.0;
color.g =0.0;
color.b =1.0;
} else {
color.r =1.0;
color.g =1.0;
color.b =0.0;
}
}
//黄蓝
void YellowAndBlue(inout vec4 color){
float threshold = colorYZ;
float mean = (color.r + color.g + color.b) / 3.0;
if (mean >= threshold){
color.r =1.0;
color.g =1.0;
color.b =0.0;
} else {
color.r =0.0;
color.g =0.0;
color.b =1.0;
}
}
//反向
void reverse(inout vec4 color){
color.r = 1.0 - color.r;
color.g = 1.0 - color.g;
color.b = 1.0 - color.b;
}
//亮度
void light(inout vec4 color){
vec3 hslColor = vec3(rgb2hsl(color.rgb));
hslColor.z += 0.15;
color = vec4(hsl2rgb(hslColor), color.a);
}
void light2(inout vec4 color){
color.r += 0.15;
color.g += 0.15;
color.b += 0.15;
}
//查找表滤镜
vec4 lookupTable(vec4 color){
float blueColor = color.b * 63.0;
vec2 quad1;
quad1.y = floor(floor(blueColor) / 8.0);
quad1.x = floor(blueColor) - (quad1.y * 8.0);
vec2 quad2;
quad2.y = floor(ceil(blueColor) / 8.0);
quad2.x = ceil(blueColor) - (quad2.y * 8.0);
vec2 texPos1;
texPos1.x = (quad1.x * 0.125) + 0.5/512.0 + ((0.125 - 1.0/512.0) * color.r);
texPos1.y = (quad1.y * 0.125) + 0.5/512.0 + ((0.125 - 1.0/512.0) * color.g);
vec2 texPos2;
texPos2.x = (quad2.x * 0.125) + 0.5/512.0 + ((0.125 - 1.0/512.0) * color.r);
texPos2.y = (quad2.y * 0.125) + 0.5/512.0 + ((0.125 - 1.0/512.0) * color.g);
vec4 newColor1 = texture(textureLUT, texPos1);
vec4 newColor2 = texture(textureLUT, texPos2);
vec4 newColor = mix(newColor1, newColor2, fract(blueColor));
return vec4(newColor.rgb, color.w);
}
//色调分离
void posterization(inout vec4 color){
//计算灰度值
float grayValue = color.r * 0.3 + color.g * 0.59 + color.b * 0.11;
//转换到hsl颜色空间
vec3 hslColor = vec3(rgb2hsl(color.rgb));
//根据灰度值区分阴影和高光,分别处理
if (grayValue < 0.3){
//添加蓝色
if (hslColor.x < 0.68 || hslColor.x > 0.66){
hslColor.x = 0.67;
}
//增加饱和度
hslColor.y += 0.3;
} else if (grayValue > 0.7){
//添加黄色
if (hslColor.x < 0.18 || hslColor.x > 0.16){
hslColor.x = 0.17;
}
//降低饱和度
hslColor.y -= 0.3;
}
color = vec4(hsl2rgb(hslColor), color.a);
}
void sobel(inout vec4 color) {
//给出卷积内核中各个元素对应像素相对于待处理像素的纹理坐标偏移量
vec2 offset0=vec2(-1.0, -1.0); vec2 offset1=vec2(0.0, -1.0); vec2 offset2=vec2(1.0, -1.0);
vec2 offset3=vec2(-1.0, 0.0); vec2 offset4=vec2(0.0, 0.0); vec2 offset5=vec2(1.0, 0.0);
vec2 offset6=vec2(-1.0, 1.0); vec2 offset7=vec2(0.0, 1.0); vec2 offset8=vec2(1.0, 1.0);
const float scaleFactor = 0.99;//给出最终求和时的加权因子(为调整亮度)
//卷积内核中各个位置的值
float kernelValue0 = 0.0; float kernelValue1 = 1.0; float kernelValue2 = 0.0;
float kernelValue3 = 1.0; float kernelValue4 = -4.0; float kernelValue5 = 1.0;
float kernelValue6 = 0.0; float kernelValue7 = 1.0; float kernelValue8 = 0.0;
vec4 sum;//最终的颜色和
//获取卷积内核中各个元素对应像素的颜色值
vec4 cTemp0, cTemp1, cTemp2, cTemp3, cTemp4, cTemp5, cTemp6, cTemp7, cTemp8;
cTemp0=texture(s_texture, v_texCoord.st + offset0.xy/1920.0);
cTemp1=texture(s_texture, v_texCoord.st + offset1.xy/1920.0);
cTemp2=texture(s_texture, v_texCoord.st + offset2.xy/1920.0);
cTemp3=texture(s_texture, v_texCoord.st + offset3.xy/1920.0);
cTemp4=texture(s_texture, v_texCoord.st + offset4.xy/1920.0);
cTemp5=texture(s_texture, v_texCoord.st + offset5.xy/1920.0);
cTemp6=texture(s_texture, v_texCoord.st + offset6.xy/1920.0);
cTemp7=texture(s_texture, v_texCoord.st + offset7.xy/1920.0);
cTemp8=texture(s_texture, v_texCoord.st + offset8.xy/1920.0);
//颜色求和
sum =kernelValue0*cTemp0+kernelValue1*cTemp1+kernelValue2*cTemp2+
kernelValue3*cTemp3+kernelValue4*cTemp4+kernelValue5*cTemp5+
kernelValue6*cTemp6+kernelValue7*cTemp7+kernelValue8*cTemp8;
color = sum * scaleFactor;//进行亮度加权后将最终颜色传递给管线
}
void sobel2(inout vec4 color) {
//给出卷积内核中各个元素对应像素相对于待处理像素的纹理坐标偏移量
vec2 offset0=vec2(-1.0, -1.0); vec2 offset1=vec2(0.0, -1.0); vec2 offset2=vec2(1.0, -1.0);
vec2 offset3=vec2(-1.0, 0.0); vec2 offset4=vec2(0.0, 0.0); vec2 offset5=vec2(1.0, 0.0);
vec2 offset6=vec2(-1.0, 1.0); vec2 offset7=vec2(0.0, 1.0); vec2 offset8=vec2(1.0, 1.0);
const float scaleFactor = 0.99;//给出最终求和时的加权因子(为调整亮度)
//卷积内核中各个位置的值
float kernelValue0 = 0.0; float kernelValue1 = 1.0; float kernelValue2 = 0.0;
float kernelValue3 = 1.0; float kernelValue4 = -4.0; float kernelValue5 = 1.0;
float kernelValue6 = 0.0; float kernelValue7 = 1.0; float kernelValue8 = 0.0;
vec4 sum;//最终的颜色和
//获取卷积内核中各个元素对应像素的颜色值
vec4 cTemp0, cTemp1, cTemp2, cTemp3, cTemp4, cTemp5, cTemp6, cTemp7, cTemp8;
cTemp0=texture(s_texture, v_texCoord.st + offset0.xy/512.0);
cTemp1=texture(s_texture, v_texCoord.st + offset1.xy/512.0);
cTemp2=texture(s_texture, v_texCoord.st + offset2.xy/512.0);
cTemp3=texture(s_texture, v_texCoord.st + offset3.xy/512.0);
cTemp4=texture(s_texture, v_texCoord.st + offset4.xy/512.0);
cTemp5=texture(s_texture, v_texCoord.st + offset5.xy/512.0);
cTemp6=texture(s_texture, v_texCoord.st + offset6.xy/512.0);
cTemp7=texture(s_texture, v_texCoord.st + offset7.xy/512.0);
cTemp8=texture(s_texture, v_texCoord.st + offset8.xy/512.0);
//颜色求和
sum =kernelValue0*cTemp0+kernelValue1*cTemp1+kernelValue2*cTemp2+
kernelValue3*cTemp3+kernelValue4*cTemp4+kernelValue5*cTemp5+
kernelValue6*cTemp6+kernelValue7*cTemp7+kernelValue8*cTemp8;
color = sum * scaleFactor;//进行亮度加权后将最终颜色传递给管线
}
float luma(vec3 color) {
return 0.2126 * color.r + 0.7152 * color.g +0.0722 * color.b;
}
void pass2(inout vec4 color){
float dx = 1.0 / 1920.0;
float dy = 1.0 / 1080.0;
float s00 = luma(texture(s_texture, v_texCoord + vec2(-dx, dy)).rgb);
float s10 = luma(texture(s_texture, v_texCoord + vec2(-dx, 0.0)).rgb);
float s20 = luma(texture(s_texture, v_texCoord + vec2(-dx, -dy)).rgb);
float s01 = luma(texture(s_texture, v_texCoord + vec2(0.0, dy)).rgb);
float s21 = luma(texture(s_texture, v_texCoord + vec2(0.0, -dy)).rgb);
float s02 = luma(texture(s_texture, v_texCoord + vec2(dx, dy)).rgb);
float s12 = luma(texture(s_texture, v_texCoord + vec2(dx, 0.0)).rgb);
float s22 = luma(texture(s_texture, v_texCoord + vec2(dx, -dy)).rgb);
float sx = s00 + 2.0 * s10 + s20 - (s02 + 2.0 * s12 + s22);
float sy = s00 + 2.0 * s01 + s02 - (s20 + 2.0 * s21 + s22);
float dist = sx * sx + sy * sy;
float threshold = colorYZ;
if (dist>threshold)
color = vec4(1.0);
else
color = vec4(0.0, 0.0, 0.0, 1.0);
}
void pass3(inout vec4 color){
float dx = 1.0 / 1920.0;
float dy = 1.0 / 1080.0;
float s00 = luma(texture(s_texture, v_texCoord + vec2(-dx, dy)).rgb);
float s10 = luma(texture(s_texture, v_texCoord + vec2(-dx, 0.0)).rgb);
float s20 = luma(texture(s_texture, v_texCoord + vec2(-dx, -dy)).rgb);
float s01 = luma(texture(s_texture, v_texCoord + vec2(0.0, dy)).rgb);
float s21 = luma(texture(s_texture, v_texCoord + vec2(0.0, -dy)).rgb);
float s02 = luma(texture(s_texture, v_texCoord + vec2(dx, dy)).rgb);
float s12 = luma(texture(s_texture, v_texCoord + vec2(dx, 0.0)).rgb);
float s22 = luma(texture(s_texture, v_texCoord + vec2(dx, -dy)).rgb);
float sx = s00 + 2.0 * s10 + s20 - (s02 + 2.0 * s12 + s22);
float sy = s00 + 2.0 * s01 + s02 - (s20 + 2.0 * s21 + s22);
float dist = sx * sx + sy * sy;
float threshold = colorYZ;
if (dist>threshold)
color = vec4(0.0, 0.0, 0.0, 1.0);
else
color = vec4(1.0);
}
void pass4(inout vec4 color){
float dx = 1.0 / 1920.0;
float dy = 1.0 / 1080.0;
float s00 = luma(texture(s_texture, v_texCoord + vec2(-dx, dy)).rgb);
float s10 = luma(texture(s_texture, v_texCoord + vec2(-dx, 0.0)).rgb);
float s20 = luma(texture(s_texture, v_texCoord + vec2(-dx, -dy)).rgb);
float s01 = luma(texture(s_texture, v_texCoord + vec2(0.0, dy)).rgb);
float s21 = luma(texture(s_texture, v_texCoord + vec2(0.0, -dy)).rgb);
float s02 = luma(texture(s_texture, v_texCoord + vec2(dx, dy)).rgb);
float s12 = luma(texture(s_texture, v_texCoord + vec2(dx, 0.0)).rgb);
float s22 = luma(texture(s_texture, v_texCoord + vec2(dx, -dy)).rgb);
float sx = s00 + 2.0 * s10 + s20 - (s02 + 2.0 * s12 + s22);
float sy = s00 + 2.0 * s01 + s02 - (s20 + 2.0 * s21 + s22);
float dist = sx * sx + sy * sy;
float threshold = colorYZ;
if (dist>0.5)
//color = vec4(0.627,0.125,0.941,1.0);
color = vec4(threshold, threshold/2.0, 1.0-threshold, 1.0);
else
color = vec4(0.0, 0.0, 0.0, 1.0);
}
float step1 = 1.0;
float intensity(in vec4 color){
return sqrt((color.x*color.x)+(color.y*color.y)+(color.z*color.z));
}
vec3 sobelb(float stepx, float stepy, vec2 center){
// get samples around pixel
float tleft = intensity(texture(s_texture,center + vec2(-stepx,stepy)));
float left = intensity(texture(s_texture,center + vec2(-stepx,0)));
float bleft = intensity(texture(s_texture,center + vec2(-stepx,-stepy)));
float top = intensity(texture(s_texture,center + vec2(0,stepy)));
float bottom = intensity(texture(s_texture,center + vec2(0,-stepy)));
float tright = intensity(texture(s_texture,center + vec2(stepx,stepy)));
float right = intensity(texture(s_texture,center + vec2(stepx,0)));
float bright = intensity(texture(s_texture,center + vec2(stepx,-stepy)));
// Sobel masks (see http://en.wikipedia.org/wiki/Sobel_operator)
// 1 0 -1 -1 -2 -1
// X = 2 0 -2 Y = 0 0 0
// 1 0 -1 1 2 1
// You could also use Scharr operator:
// 3 0 -3 3 10 3
// X = 10 0 -10 Y = 0 0 0
// 3 0 -3 -3 -10 -3
float x = tleft + 2.0*left + bleft - tright - 2.0*right - bright;
float y = -tleft - 2.0*top - tright + bleft + 2.0 * bottom + bright;
float color = sqrt((x*x) + (y*y));
return vec3(color,color,color);
}
void mainImage1( out vec4 fragColor, in vec2 fragCoord ){
vec2 uv = fragCoord.xy / iResolution.xy;
vec4 color = texture(s_texture, uv.xy);
fragColor.xyz = sobelb(step1/iResolution[0], step1/iResolution[1], uv);
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec2 uv = fragCoord.xy / iResolution.xy;
vec4 col;
vec4 edge_x = vec4(0,0,0,0);
vec4 edge_y = vec4(0,0,0,0);
for (int dx = -1; dx <= 1; dx++)
{
for (int dy = -1; dy <= 1; dy++)
{
vec2 uv = (fragCoord.xy + vec2(dx,dy)) / iResolution.xy;
vec4 pixel = texture(s_texture, uv);
edge_x += float(dx + dx * abs(dy)) * pixel;
edge_y += float(dy + dy * abs(dx)) * pixel;
}
}
float edge_x_max = max(edge_x.r, max(edge_x.g, edge_x.b));
float edge_y_max = max(edge_y.r, max(edge_y.g, edge_y.b));
float edge = edge_x_max * edge_x_max + edge_y_max * edge_y_max;
if(edge > 0.1)
col = texture(s_texture, uv);
else
col = vec4(0., 0., 0., 0.);
fragColor = col;
}
void main(){
//画中画 画中放大放
//分屏
// float x = v_texCoord.x;
// if (x<0.5){
// x+=0.25;
// } else {
// x-=0.25;
// }
// float dis=distance(vec2(gPosition.x,gPosition.y/uXY),vec2(vChangeColor.r,vChangeColor.g));
// if(dis
// nColor=texture(vTexture,vec2(aCoordinate.x/2.0+0.25,aCoordinate.y/2.0+0.25));
// }
// gl_FragColor=nColor;
// texture(s_texture,v_texCoord);
vec4 tmpColor = texture(s_texture, vec2(v_texCoord.x, v_texCoord.y));
if (colorFlag == 1){ //灰度
blackAndWhite(tmpColor);
} else if (colorFlag == 2){ //黑白
sobel11(tmpColor);
// WhiteAndblack(tmpColor);
} else if (colorFlag == 3){ //反向
BlackAndYellow(tmpColor);
} else if (colorFlag == 4){ //亮度
YellowAndBlack(tmpColor);
} else if (colorFlag == 5){ //亮度2
BlueAndWhite(tmpColor);
} else if (colorFlag == 6){ //lut
WhiteAndBlue(tmpColor);
} else if (colorFlag == 7){ //色调分离
BlueAndYellow(tmpColor);
} else if (colorFlag == 8){
YellowAndBlue(tmpColor);
} else if (colorFlag == 9){
pass2(tmpColor);
//sobel(tmpColor);
} else if (colorFlag == 10){
pass3(tmpColor);
//sobel2(tmpColor);
//grey(tmpColor);
} else if (colorFlag == 11){
pass4(tmpColor);
//reverse(tmpColor);
}
//负片
else if (colorFlag == 13){
fupian(tmpColor);
}
//反负片
else if (colorFlag == 14){
ffupian(tmpColor);
}
//三色
else if (colorFlag == 15){
sanse(tmpColor);
}//反三色
else if (colorFlag == 16){
fsanse(tmpColor);
}
//动态轮廓
else if (colorFlag == 17){
dtlk(tmpColor);
}//反动态轮廓
else if (colorFlag == 18){
fdtlk(tmpColor);
} else if (colorFlag == 19){
//红绿盲
redColor(tmpColor);
} else if (colorFlag == 20){
//黄绿盲
yellowColor(tmpColor);
} else if (colorFlag == 21){
//灰度
grey(tmpColor);
}
//mohu(tmpColor);
// vec2 p = v_texCoord.xy / iResolution.xy;
//
// vec2 uv = p*vec2(iResolution.x/iResolution.y, 1.0) + iTime*0.25;
// // vec2 uv = p*vec2(iResolution.x/iResolution.y, 1.0) + 0.25;
// float f = 0.0;
//
// uv *= 8.0;
// mat2 m = mat2(1.6, 1.2, -1.2, 1.6);
// f = 0.5000*noise(uv); uv = m*uv;
// f += 0.2500*noise(uv); uv = m*uv;
// f += 0.1250*noise(uv); uv = m*uv;
// f += 0.0625*noise(uv); uv = m*uv;
//
// f = 0.5 + 0.5*f;
//
// //f *= smoothstep( 0.0, 0.005, abs(p.x-0.6) );
// outColor = vec4(f,f,f, 1.0);
vec2 offsets[9];
offsets[0] = vec2(-offset, offset); //左上
offsets[1] = vec2( 0.0f, offset); //正上
offsets[2] = vec2( offset, offset); //右上
offsets[3] = vec2(-offset, 0.0f); //左
offsets[4] = vec2( 0.0f, 0.0f); //中
offsets[5] = vec2( offset, 0.0f); //右
offsets[6] = vec2(-offset, -offset); //左下
offsets[7] = vec2( 0.0f, -offset); //正下
offsets[8] = vec2( offset, -offset); //右下
//核函数
float kernel[9];
kernel[0] = 1.0f;
kernel[1] = 1.0f;
kernel[2] = 1.0f;
kernel[3] = 1.0f;
kernel[4] = -8.0f;
kernel[5] = 1.0f;
kernel[6] = 1.0f;
kernel[7] = 1.0f;
kernel[8] = 1.0f;
//计算采样值
vec3 sampleTex[9];
for(int i = 0; i < 9; i++)
{
sampleTex[i] = vec3(texture(s_texture, v_texCoord.xy + offsets[i]));
}
vec3 col = vec3(0.0);
for(int i = 0; i < 9; i++)
col += sampleTex[i] * kernel[i] ;
vec4 blackColor = vec4(col , 1.0);
// outColor = blackColor;
// vec4 ggColor=texture(textureLUT, v_texCoord);
// outColor = ggColor;
// outColor = blackColor;
vec4 temp =blackColor;
// mainImage1(temp, v_texCoord * iResolution.xy);
blackAndWhite(temp);
#define threshold 0.55
#define padding 0.05
vec4 greenScreen = vec4(0.,0.,0.,1.);
vec2 uv = v_texCoord.xy / iResolution1.xy;
vec3 diff = temp.xyz - greenScreen.xyz;
float fac = smoothstep(threshold-padding,threshold+padding, dot(diff,diff));
// vec4 color1 = mix(temp, texture(textureLUT, uv), 1.-fac);
vec4 color1 = mix(texture(textureLUT, uv), temp, .2);
outColor = color1;
}
//将颜色值约束在[0.0-1.0]之间
void checkColor(vec4 color){
color.r=max(min(color.r, 1.0), 0.0);
color.g=max(min(color.g, 1.0), 0.0);
color.b=max(min(color.b, 1.0), 0.0);
color.a=max(min(color.a, 1.0), 0.0);
}
在 OpenGL 3.3 版本中,可以通过使用 GLSL(OpenGL Shading Language)编写片段着色器来实现快速傅里叶变换(FFT)。下面是一个简单的实现 FFT 的片段着色器示例:
glsl
Copy
#version 330 coreconst float PI = 3.14159265359;uniform sampler2D input_texture;uniform int width;uniform int height;vec2 twiddle(int i, int n){ float phase = 2.0 * PI * float(i) / float(n); return vec2(cos(phase), sin(phase));}void main(){ vec2 uv = gl_FragCoord.xy / vec2(width, height); vec4 texel = texture(input_texture, uv); // Bit-reversal permutation int n = width * height; int j = 0; for (int i = 0; i < n; i++) { if (j > i) { vec4 temp = texel; texel = texture(input_texture, vec2(float(j % width) / float(width), float(j / width) / float(height))); texture(input_texture, vec2(float(i % width) / float(width), float(i / width) / float(height))) = temp;} int m = n / 2; while (m >= 1 && j >= m) {j -= m; m /= 2;} j += m; } // Cooley-Tukey FFT for (int i = 2; i <= n; i *= 2) { for (int j = 0; j < n; j += i) { for (int k = 0; k < i/2; k++) { int index1 = j + k; int index2 = j + k + i/2; vec2 t = twiddle(k * n / i, n) * vec2(texel[index2].r, texel[index2].g); vec4 u = vec4(texel[index1].r + t.x, texel[index1].g + t.y, 0.0, 0.0); vec4 v = vec4(texel[index1].r - t.x, texel[index1].g - t.y, 0.0, 0.0);texel[index1] = u; texel[index2] = v; } } } // Output the result gl_FragColor = texel;}
在该片段着色器中,我们首先对输入纹理进行了一次位反转置换(bit-reversal permutation),以满足 Cooley-Tukey FFT 算法的要求。然后,我们使用 Cooley-Tukey FFT 算法对输入纹理进行了快速傅里叶变换。最后,我们将变换后的结果输出到输出纹理中。
需要注意的是,该示例中的 FFT 实现仅适用于二维信号,并且使用了简化的 Cooley-Tukey FFT 算法。在实际应用中,需要根据具体的情况选择合适的 FFT 算法和参数,并考虑性能和精度等因素。
-------
#version 330 core
const float PI = 3.14159265359;
void fft(inout vec2[1024] x)
{
int N = x.length();
if (N <= 1) {
return;
}
vec2[1024] even = vec2[1024](0);
vec2[1024] odd = vec2[1024](0);
for (int i = 0; i < N/2; i++) {
even[i] = x[2*i];
odd[i] = x[2*i+1];
}
fft(even);
fft(odd);
for (int i = 0; i < N/2; i++) {
float t = i * 2.0 * PI / float(N);
vec2 w = vec2(cos(t), -sin(t)) * odd[i];
x[i] = even[i] + w;
x[i + N/2] = even[i] - w;
}
}
void main()
{
vec2[1024] x = vec2[1024](0);
// 从纹理采样器中读取输入数据
for (int i = 0; i < 1024; i++) {
vec4 texel = texture(input_texture, vec2(float(i) / 1024.0, 0.5));
x[i] = vec2(texel.r, 0.0);
}
// 计算傅里叶变换
fft(x);
// 将结果写回到纹理中
for (int i = 0; i < 1024; i++) {
vec2 value = x[i];
vec4 texel = vec4(value.r, value.i, 0.0, 0.0);
gl_FragColor = texel;
}
}