Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save robertleeplummerjr/3d7d969aed28db39c714d6fa343363cc to your computer and use it in GitHub Desktop.

Select an option

Save robertleeplummerjr/3d7d969aed28db39c714d6fa343363cc to your computer and use it in GitHub Desktop.

Revisions

  1. robertleeplummerjr created this gist Nov 22, 2019.
    54 changes: 54 additions & 0 deletions math-random-uniformly-distributed.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,54 @@
    // 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
    };