Skip to content

Instantly share code, notes, and snippets.

@hmans
Last active March 20, 2022 19:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hmans/550390e62cd1a9678bfd2cda7b612985 to your computer and use it in GitHub Desktop.
Save hmans/550390e62cd1a9678bfd2cda7b612985 to your computer and use it in GitHub Desktop.
Lens Dirt Shader for Three.js
/* A simple lens dirt texture shader for Three.js. Grab a lens dirt texture,
plug it into the tDirt uniform, and watch things get diirrtttttaayyyyeee.
Like, really dirty. It can get very confusing if your monitor also has
fingerprints, believe me.
- hendrik@mans.de 🚀 */
export const LensDirtShader = {
uniforms: {
tDiffuse: { value: null },
tDirt: { value: null },
strength: { value: 1.0 },
minLuminance: { value: 0.1 },
maxLuminance: { value: 0.5 }
},
vertexShader: `
varying vec2 vUv;
void main() {
vUv = uv;
gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
}`,
fragmentShader: `
#include <common>
uniform float strength;
uniform float minLuminance;
uniform float maxLuminance;
uniform sampler2D tDiffuse;
uniform sampler2D tDirt;
varying vec2 vUv;
void main() {
vec4 colorDirt = texture2D(tDirt, vUv);
vec4 colorGame = texture2D(tDiffuse, vUv);
float luminance = linearToRelativeLuminance(colorGame.rgb);
float amount = smoothstep(minLuminance, maxLuminance, luminance) * strength;
gl_FragColor = colorGame + colorDirt * amount;
}`
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment