• Sepia Tone: Apply a sepia filter to give the image a warm, brownish tone.
• Grayscale Conversion: Convert the image to grayscale by averaging or using luminance formulas.
• Posterization: Reduce the number of colors in the image to create a poster-like effect.
Complete Code
let div = document.createElement('div'); document.body.appendChild( div ); div.style['font-size'] = '20pt'; function log( s ) { console.log( s ); let args = [...arguments].join(' '); div.innerHTML += args + '<br><br>'; }
// Output texture - output from the compute shader written to this texture // Copy this texutre to the 'canvas' - needs to be the same size as the output // canvas size const texture1 = device.createTexture({ size: [imgWidth, imgHeight, 1], format: "rgba8unorm", usage: GPUTextureUsage.COPY_DST | GPUTextureUsage.COPY_SRC | GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.STORAGE_BINDING });
var coords = vec2<f32>(f32(globalId.x), f32(globalId.y)); var uv = coords / vec2<f32>(imgWidth, imgHeight); // normalize coordinates to 0.0 - 1.0 range
// -------------------------------------------
// Sepia Tone effect (top left corner) var sepiaColor = vec4<f32>(0.0); { var texCol0 = textureLoad(myTexture0, vec2<i32>(uv * vec2<f32>(imgWidth, imgHeight)), 0); var sepiaMatrix = mat3x3<f32>( vec3<f32>(0.393, 0.769, 0.189), vec3<f32>(0.349, 0.686, 0.168), vec3<f32>(0.272, 0.534, 0.131) ); sepiaColor = vec4<f32>(sepiaMatrix * texCol0.rgb, texCol0.a); }
// -------------------------------------------
// Grayscale Conversion (top right corner) var grayscaleColor = vec4<f32>(0.0); { var texCol1 = textureLoad(myTexture0, vec2<i32>(uv * vec2<f32>(imgWidth, imgHeight)), 0); var grayscaleValue = dot(texCol1.rgb, vec3<f32>(0.2126, 0.7152, 0.0722)); grayscaleColor = vec4<f32>(grayscaleValue, grayscaleValue, grayscaleValue, texCol1.a); }
// -------------------------------------------
// Posterization (bottom left corner) var posterizedColor = vec4<f32>(0.0); { var texCol2 = textureLoad(myTexture0, vec2<i32>(uv * vec2<f32>(imgWidth, imgHeight)), 0); var numColors = 4.0; // Change this value to adjust the number of colors posterizedColor = floor(texCol2 * numColors) / numColors; }
// -------------------------------------------
var finalColor = vec4<f32>(0);
// 4 corners of the output image - each with a different effect if ( uv.x < 0.5 && uv.y < 0.5 ) { finalColor = sepiaColor; } // top left else if ( uv.x > 0.5 && uv.y < 0.5 ) { finalColor = grayscaleColor; } // top right else if ( uv.x < 0.5 && uv.y > 0.5 ) { finalColor = posterizedColor; } // bottom left else { finalColor = textureLoad(myTexture0, vec2<i32>(uv * vec2<f32>(imgWidth, imgHeight)), 0); } // bottom right - original image
• Try converting and mapping to other colors (instead of gray-scale - try green-scale)
• Experiment with the coefficients
• Animate the color effect (disco)