Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

random number #164

Closed
radio412 opened this issue Aug 15, 2017 · 2 comments
Closed

random number #164

radio412 opened this issue Aug 15, 2017 · 2 comments

Comments

@radio412
Copy link

I am new to gpu.js.
In order to learn how to effectively use gpu.js I selected this function to port to a gpu.js kernel as it seemed appropriate. It simply creates a noise field.

     var
     x, y,
     number,
     opacity = opacity || .2;
     ctx = canvas.getContext('2d');
     for ( x = 0; x < canvas.width; x++ ) {
        for ( y = 0; y < canvas.height; y++ ) {
           n = Math.floor( Math.random() * 60 );
           ctx.fillStyle = "rgba(" + n + "," + n + "," + n+ "," + opacity + ")";
           ctx.fillRect(x, y, 1, 1);
        }
     }

I do not understand how to implement a random value for n. I do understand I can color any pixel of the canvas using this.color() with setGraphical(true).

Do I need to populate an array with random values and send it to the kernel as a parameter?

@robertleeplummerjr
Copy link
Member

For the time being, use Math.random() in js, and not inside the gpu.js kernel. I'll resolve this soon.

@robertleeplummerjr
Copy link
Member

For the time being, if it was absolutley needed, you could do something like:

var gpu = new GPU();
var myFunction = gpu.createKernel(function() {
  return Math.random();
});
myFunction.addGLSLFunction('random', `highp float random(vec2 co)
{
    highp float a = 12.9898;
    highp float b = 78.233;
    highp float c = 43758.5453;
    highp float dt= dot(co.xy ,vec2(a,b));
    highp float sn= mod(dt,3.14);
    return fract(sin(sn) * c);
}`);

I exposed a means of defining a glsl function directly, but as for the random function itself, there are many examples far and wide, but none that I feel are particularly noteworthy. The example I got from: http://byteblacksmith.com/improvements-to-the-canonical-one-liner-glsl-rand-for-opengl-es-2-0/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants