Created
November 3, 2018 12:42
-
-
Save mattdesl/854906361159c2694e5ad8eadde4eee7 to your computer and use it in GitHub Desktop.
A very nice looking (but terribly inefficient) random function in GLSL.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const canvasSketch = require('canvas-sketch'); | |
const createShader = require('canvas-sketch-util/shader'); | |
const random = require('canvas-sketch-util/random'); | |
// Optionally we can lock the randomness like so | |
// random.setSeed(10); | |
// Setup our sketch | |
const settings = { | |
context: 'webgl', | |
dimensions: 'A4', | |
pixelsPerInch: 300, | |
attributes: { | |
antialias: false, | |
alpha: false, | |
preserveDrawingBuffer: true | |
} | |
}; | |
// Your glsl code | |
const frag = /* glsl */` | |
precision highp float; | |
varying vec2 vUv; | |
uniform sampler2D randomness0; | |
uniform sampler2D randomness1; | |
// Use a new seed for each random invocation | |
float tick = 0.0 / 4096.0; | |
// A terribly inefficient and over-the-top | |
// random function (but it looks nice) | |
float random (in vec2 uv, in float seed) { | |
float hashA = texture2D(randomness0, vec2(uv.x, 0.5)).r; | |
float hashB = texture2D(randomness0, vec2(uv.y, 0.5)).r; | |
float seedMod = mod(seed, 1.0); | |
float hashC = texture2D(randomness0, vec2(seedMod, 0.5)).g; | |
float hashABC = mod(hashA + hashB + hashC, 1.0); | |
float index0 = texture2D(randomness0, vec2(hashABC, 0.5)).b; | |
float index1 = texture2D(randomness1, vec2(mod(index0 + seed, 1.0), 0.5)).r; | |
float index2 = texture2D(randomness1, vec2(index1, 0.5)).g; | |
return texture2D(randomness1, vec2(index2, 0.5)).b; | |
} | |
float random (in vec2 coordinate) { | |
float f = random(coordinate, tick); | |
tick += 1.0 / 4096.0; // increment by one random pixel | |
return f; | |
} | |
void main () { | |
float d = random(vUv); | |
vec3 color = vec3(d); | |
gl_FragColor = vec4(vec3(color), 1.0); | |
} | |
`; | |
// Your sketch, which simply returns the shader | |
const sketch = ({ gl }) => { | |
// Create a 4096x1 RGB random data texture | |
const createRandomTexture = () => { | |
const randoms = 4096; | |
const pixels = Array.from(new Array(randoms)).map(() => { | |
return [ random.value(), random.value(), random.value() ]; | |
}); | |
const randomness = { | |
shape: [ pixels.length, 1, 3 ], | |
type: 'float', | |
min: 'nearest', | |
mag: 'nearest', | |
data: pixels | |
}; | |
return randomness; | |
}; | |
// Create some REGL data textures | |
const randomness0 = createRandomTexture(); | |
const randomness1 = createRandomTexture(); | |
return createShader({ | |
// Enable float32 textures | |
extensions: [ | |
'OES_texture_float' | |
], | |
// Pass along WebGL context | |
gl, | |
// Specify fragment and/or vertex shader strings | |
frag, | |
// Specify additional uniforms to pass down to the shaders | |
uniforms: { | |
randomness0, | |
randomness1 | |
} | |
}); | |
}; | |
canvasSketch(sketch, settings); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment