Processing: How to Randomize Textures

TheHallCompressed

Scenes with custom textures always seem to turn out better for me, but they’re hellish to make. I’ve spent hours in GIMP hand-drawing patterns, carefully testing out layers to get  effects. It’s a hassle.

However, I’m learning to automate more of that sort of thing with Processing. What’s really nice about that is you can get a texture that changes subtly every time you run the script if you use the Random function to assign coordinates, weights, and transparency values.

Some important functions to note before you get started:

Conjure a Random Float
random(endNum, startNum);

Draw a Line
line(x1, y1, x2, y2);

Designate the Width of the Line
strokeWeight(#);

Designate the Color of a Shape
fill(redValue, greenValue, blueValue, alpha);
*RGB values are from 0 to 255
*Alpha is the transparency value

The meat of the script is a For Loop filled with variables that are set to random values. My image is 1000 pixels by 1000 pixels, so I’m randomizing the x and y coordinates to a maximum of 1000. If you keep the y value constant when making a line, you get a horizontal one. Keeping the x value constant makes one straight up and down.

for ( int i=0; i<150; i++ ) {  Setting this to 150 will create 149 sets of lines
float varx = random(1000);
float vary = random(1000);
float alpha = random(100);
float weight = random(5);  
strokeWeight(weight);  Make the line between 0 and 5 pixels wide
stroke(34, 139, 34, alpha);  Customize to any RGB color code
line(0, vary, 1000, vary);  Draw a horizontal line at the random y value
line(varx, 0, varx, 1000); Draw a vertical line at the random x value
}

That’ll get you somewhere near this:

Texture

If you don’t like a particular configuration, you can play with the variable parameters or just run the script again. I also threw in another For Loop to generate patchy rectangles. That can be saved to a png for easy importing into a Blender scene. The full script is below.

The Code

void setup(){
size(1000, 1000);
background(0);
for (int i=0; i&amp;lt;150;i++){
float varx = random(1000);
float vary = random(1000);
float alpha = random(100);
float weight = random(5);
strokeWeight(weight);
stroke(34, 139, 34, alpha);
line(0, vary, 1000, vary);
line(varx, 0, varx, 1000);
}

for (int x=0; x &amp;lt;30; x++){
strokeWeight(2);
float alpha = random(100);
fill(34, 139, 34, alpha);
float varx = random(1000);
float vary = random(1000);
float rwidth = random(300);
float rheight = random(300);
rect(varx, vary, rwidth, rheight);
}
save("C:\\Users\\Desktop\\Matrix.png");
}

 

 

Advertisements
Tagged , , , , , , , ,

5 thoughts on “Processing: How to Randomize Textures

  1. ARJ says:

    aaaah how nice !

    Liked by 1 person

  2. ARJ says:

    few are the blogs which valourise processing !

    Liked by 1 person

    • I have a theory that there aren’t many Processing blogs out there because The Coding Train already covers everything. And no one can teach like Daniel Shiffman. That guy’s got the energy of a hoard of meerkats.

      Like

      • ARJ says:

        for me it processing is unknown, and yes, after some years of coding in processing you too have ideas brimming in the head

        and yes tag your posts
        processing-language

        on wordpress only me do it as of now

        Liked by 1 person

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: