Created
November 22, 2019 15:10
-
-
Save robertleeplummerjr/3d7d969aed28db39c714d6fa343363cc to your computer and use it in GitHub Desktop.
Playing around with better random values in GPU.js
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
// language=GLSL | |
const source = `// https://www.shadertoy.com/view/4t2SDh | |
//note: uniformly distributed, normalized rand, [0,1] | |
highp float randomSeedShift = 0.0; | |
highp float previousRandomSeedShift = 0.0; | |
bool flip = false; | |
const highp float factor = 9999.0; | |
uniform highp float randomSeed1; | |
uniform highp float randomSeed2; | |
highp float nrand(highp vec2 n) { | |
previousRandomSeedShift = randomSeedShift; | |
if (previousRandomSeedShift > 0.0) { | |
if (flip) { | |
flip = false; | |
return randomSeedShift = fract(sin(dot(n.xy * vec2(randomSeedShift * factor, previousRandomSeedShift * factor), vec2(12.9898, 78.233))) * 43758.5453); | |
} | |
flip = true; | |
return randomSeedShift = fract(sin(dot(n.xy * vec2(previousRandomSeedShift * factor, randomSeedShift * factor), vec2(12.9898, 78.233))) * 43758.5453); | |
} | |
if (randomSeedShift > 0.0) { | |
return randomSeedShift = fract(sin(dot(n.xy * vec2(randomSeed1 * factor, randomSeedShift * factor), vec2(12.9898, 78.233))) * 43758.5453); | |
} | |
return randomSeedShift = fract(sin(dot(n.xy * vec2(randomSeed1 * factor, randomSeed2 * factor), vec2(12.9898, 78.233))) * 43758.5453); | |
}`; | |
const name = 'math-random-uniformly-distributed'; | |
// language=JavaScript | |
const functionMatch = `Math.random()`; | |
const functionReplace = `nrand(vTexCoord)`; | |
const functionReturnType = 'Number'; | |
/** | |
* | |
* @param {Kernel} kernel | |
*/ | |
const onBeforeRun = (kernel) => { | |
kernel.setUniform1f('randomSeed1', Math.random()); | |
kernel.setUniform1f('randomSeed2', Math.random()); | |
}; | |
/** | |
* | |
* @type IPlugin | |
*/ | |
module.exports = { | |
name, | |
onBeforeRun, | |
functionMatch, | |
functionReplace, | |
functionReturnType, | |
source | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment