precision mediump float;/* 變量申明*/varying vec2 uv; // 從頂點着色器傳入的UV座標uniform sampler2D u_texture0; // 全局變量,紋理1(就是空洞騎士的原圖)uniform sampler2D u_texture1; // 全局變量,紋理2(後續咱們要使用的ColorRamp)uniform float u_r; // 全局變量,ColorRamp中紅通道權重uniform float u_g; // 全局變量,ColorRamp中綠通道權重uniform float u_b; // 全局變量,ColorRamp中藍通道權重uniform float u_blurIntensity; // 全局變量,模糊強度uniform float u_negativeAmount; // 全局變量,負片程度uniform float u_colorRampIntensity; // 全局變量,ColorRamp光度強度uniform float u_glowAmount; // 全局變量,發光強度uniform vec3 u_glowColor; // 全局變量,發光顏色void main () { vec4 color = texture2D (u_texture0, uv); gl_FragColor =color;}
發光效果(Glow)
vec4 color = vec4(0,0,1.0,0.5,1.0);
void main () { vec4 color = texture2D (u_texture0, uv); color *= 2.0; gl_FragColor =color;}
....vec4 color = texture2D (u_texture0, uv); color.rgb *= vec3(1.0,0.0,0.5); //這是洋紅色....
color.rgb += vec3(1.0,0.0,0.5)*color.rgb;
vec4 color = texture2D (u_texture0, uv); vec4 emission = color; *= u_glowAmount * u_glowColor; += emission.rgb; *= color.a; gl_FragColor =color;....
模糊(Blur)
.....void main () { vec4 color = texture2D (u_texture0, uv); float step = 0.005; // 申明一個變量,控制像素偏移的步幅 color += texture2D (u_texture0, uv+vec2(step,0.0)); // 正上方點 color += texture2D (u_texture0, uv+vec2(-step,0.0)); // 正下方點 color += texture2D (u_texture0, uv+vec2(0.0,step)); // 左側點 color += texture2D (u_texture0, uv+vec2(0.0,-step)); // 右側點 color /= 5.0; // 取平均.....
vec4 color = 4.0*texture2D (u_texture0, uv); float step = 0.005; // 申明一個變量,控制像素偏移的步幅 color += 2.0*texture2D (u_texture0, uv+vec2(step,0.0)); // 正上方點 color += 2.0*texture2D (u_texture0, uv+vec2(-step,0.0)); // 正下方點 color += 2.0*texture2D (u_texture0, uv+vec2(0.0,step)); // 左側點 color += 2.0*texture2D (u_texture0, uv+vec2(0.0,-step)); // 右側點 color += 1.0*texture2D (u_texture0, uv+vec2(-step,-step)); // 第4象限點 color += 1.0*texture2D (u_texture0, uv+vec2(step,-step)); // 第3象限點 color += 1.0*texture2D (u_texture0, uv+vec2(-step,step)); // 第2象限點 color += 1.0*texture2D (u_texture0, uv+vec2(step,step)); // 第1象限點 color /= (4.0+4.0*2.0+4.0*1.0); // 取平均
float Blur_Gauss (float bhqp, float x) { return exp (-(x * x) / (2.0 * bhqp * bhqp));}vec4 Blur (vec2 uv, sampler2D source, float Intensity) { const int iterations = 16; // 常量才能夠進行for循環 int halfIterations = iterations / 2; float sigmaX = 0.1 + Intensity * 0.5; float sigmaY = sigmaX; float total = 0.0; vec4 ret = vec4 (0., 0., 0., 0.); float step = 0.00390625; // 增多到8*8個點 for (int iy = 0; iy < iterations; ++iy) { float fy = Blur_Gauss (sigmaY, float (iy - halfIterations)); float offsety = float (iy - halfIterations) * step; for (int ix = 0; ix < iterations; ++ix) { float fx = Blur_Gauss (sigmaX, float (ix - halfIterations)); float offsetx = float (ix - halfIterations) * step; total += fx * fy; vec4 a = texture2D (source, uv + vec2 (offsetx, offsety)); a.rgb *= a.a; ret += a * fx * fy; } } return ret / total;}.....color = Blur (uv, u_texture0, u_blurIntensity);.....
負片(negative)
color.rgb = abs(u_negativeAmount - color.rgb);
colorRamp
colorRamp 簡單理解就是色卡,就是下面這種:
......vec4 color = texture2D (u_texture0, uv); float p = color.r+color.g+color.b;vec3(p/3.0); = ......
vec4 color = texture2D (u_texture0, uv); float p = color.r+color.g+color.b; texture2D (u_texture1, vec2 (p/3.0, .0)).rgb; = ......
...... vec4 color = texture2D (u_texture0, uv); float p = dot(color.rgb,vec3(u_r,u_g,u_b)); // 等同於color.r*u_r+color.g*u_g+color.b*u_b p += u_colorRampIntensity; // u_colorRampIntensity控制顏色統一貫colorRamp的左/右側移動 color.rgb = texture2D (u_texture1, vec2 (p, .0)).rgb;......
固然這個 shader 也可用在真人身上,好比封面圖。其中 blurIntensity 越大風格越卡通,你們能夠本身實踐一下。
最近很經常使用的一張表情包