# Grain texture in Processing

Hello! Does anyone know how to make such textures in Processing as in the photo. Maybe it’s some kind of GLSL shader?

Hi

I am not sure that I understand your photos here is sketch close for second photo
and try to run it with this line
// pixels[j] = color( R,G,B); once again

``````void setup() {
size(500, 400);
}
void draw() {

for (int i = 0; i < pixels.length; i = i+25) {
int j = int (random (pixels.length ));

float R = random(255);
float G = random (255);
float B = random (255);
pixels[j] = color( random(255));

// pixels[j] = color( R,G,B);
}

updatePixels();
}

``````
1 Like
1 Like

You could do that by picking up a random x value following a linear distribution and a random y y value following a uniform distribution.

For each (x,y) pair, you can draw a point (or small circle) the color you want.

Here’s the code:

Summary
``````void setup() {
size(1000, 500);
background(255);

noStroke();
fill(206, 164, 245);

for (int i = 0; i < (width * height) / 2; i++) {
float x = pickRandom();
float y = random(500);
ellipse(x, y, 1, 1);
}

saveFrame("Reslut.png");
}

float pickRandom() {
while(true) {
float qualifier = random(1001);
float rnd = random(1001);
if (rnd < qualifier) {
return qualifier;
}
}
}
``````

And here’s an example result:

EDIT: The cool thing with this “technique” is that you can choose any random distribution that you want. So by tweaking the `pickRandom()` function, you can easily get something like this:

6 Likes

Thanks jafal ! It’s good version.

Thanks jb4x! This is what i need.

1 Like