Skip to content

Instantly share code, notes, and snippets.

@zouloux
Created March 18, 2016 15:01
Show Gist options
  • Save zouloux/dddd0c48f632077a20dc to your computer and use it in GitHub Desktop.
Save zouloux/dddd0c48f632077a20dc to your computer and use it in GitHub Desktop.
A Three.js shader showing digital static noise
(function (THREE)
{
/**
* @author Felix Turner / www.airtight.cc / @felixturner
*
* Static effect. Additively blended digital noise.
*
* amount - amount of noise to add (0 - 1)
* size - size of noise grains (pixels)
*
* The MIT License
*
* Copyright (c) 2014 Felix Turner
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
THREE.StaticNoiseShader = {
uniforms: {
"tDiffuse": { type: "t", value: null },
"time": { type: "f", value: 0.0 },
"amount": { type: "f", value: 0.5 },
"size": { type: "f", value: 4.0 }
},
vertexShader: [
"varying vec2 vUv;",
"void main() {",
"vUv = uv;",
"gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
"}"
].join("\n"),
fragmentShader: [
"uniform sampler2D tDiffuse;",
"uniform float time;",
"uniform float amount;",
"uniform float size;",
"varying vec2 vUv;",
"float rand(vec2 co) {",
"return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453);",
"}",
"void main() {",
"vec2 p = vUv;",
"vec4 color = texture2D(tDiffuse, p);",
"float xs = floor(gl_FragCoord.x / size);",
"float ys = floor(gl_FragCoord.y / size);",
"vec4 snow = vec4(rand(vec2(xs * time,ys * time))*amount);",
"gl_FragColor = color + snow;", //additive
"}"
].join("\n")
};
})(THREE);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment