Jump to content
AmiPolizeiFunk

javascript to make pavement tiles in Photoshop

2 posts in this topic Last Reply

Highlighted Posts

Posted:
Last Online:  
 

pavement1024.png.04a320cece8897622930d77dd5dc404f.png

I hacked the photoshop pattern "Brick Fill" script from photoshop CS6 to tile the pavement with color, brightness, and rotation variations. I'm a noob w javascript so any help or suggestions would be appreciated. I wanted to randomize the flip (mirror) sometimes too, but couldn't figure out how to do it. I was looking for a function like "RenderAPI.flip" or "RenderAPI.mirror" but those don't exist. I don't even know where to look for a list of these functions... maybe this whole method is totally out-of-date. I know you can do similar stuff in blender by routing texture variations into nodes, but I still haven't taken the time to learn the blender node system.

Anyway, Mr.Maison asked me on twitter to share the script, so here it is:

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Here are a few parameters that you can change to modify the behavior of the patterm
// Feel free to modify the values, don't change the variable names

// Variation of color of the pattern. 
// For example, value of 0.2 means that each of the red, green, and blue color components
// will be multiplied by a DIFFERENT random value from interval 0.8 and 1. 
// Set to 0 if you do not want to modify the pattern color.
var colorRandomness = 0.01    // use a value between 0 and 1. The default is 0.2.

// Variation of pattern brightness. 
// For example, value of 0.6 means that each of the red, green, and blue color components
// will be multiplied by THE SAME random value from interval 0.4 and 1. 
// Set to 0 if you do not want to modify the pattern brightness.
var brightnessRandomness = 0.1   // use a value between 0 and 1. The default is 0.4.

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// You can modify the code below but keep in mind that as with any scripting
// you can break things. Keep a backup copy.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

// We could use Math.random but  then we do not have control over the seed

// Object rand
rand.seed = 1 

function rand()
{
    rand.seed = (rand.seed*9301+49297) % 233280;
	return rand.seed/(233280.0);
}

function getRandomInt(max) {
  return Math.floor(Math.random() * Math.floor(max));
}

///////////////////////////////////////////////////////////////////////////

// get the size of the output area  
var outputSize = RenderAPI.getParameter(kpsSize)
// get the location of the top left corner of the bounding rectangle around the selected area
var outputOrigin = RenderAPI.getParameter(kpsOrigin)


// Get pattern and its size
var pattern = RenderAPI.getParameter(kpsPattern)

var patternSize = pattern.getParameter(kpsSize)

RenderAPI.translateRel (patternSize.x/2, patternSize.y/2)

RenderAPI.translateRel ( -(outputOrigin.x % patternSize.x), -(outputOrigin.y % patternSize.y))

var row = Math.floor( outputOrigin.y / patternSize.y )
var column = Math.floor( outputOrigin.x / patternSize.x )

pattern.setParameter (kpsColorBlendMode, kpsBlendMultiply)
//pattern.setParameter (kpsColorBlendMode, kpsBlendLinearLight)

for (var y = 0; y < outputSize.y + patternSize.y;  y+= patternSize.y, row++)
{
    RenderAPI.pushMatrix()
    
    var x = 0
   
    for (var c = column; x < outputSize.x + patternSize.x;  x+= patternSize.x, c ++)
    {
        if (getRandomInt(4)==1){
                RenderAPI.pushMatrix()
            
            // Set the seed based on the current row and column - this assures that the color will be modified
            // in the same way for the pattern in the neighboring selected area
            rand.seed =  row * 1234567 + c * 7654321
             // setting seed for each pattern is not that good, but calling an extra rand helps
            rand()
            var rc = colorRandomness  // color randomness
            var br = 1 - brightnessRandomness + rand()*brightnessRandomness  // brightness
            RenderAPI.Color (kFillColor, br *(1 - rc + rand()*rc), br*(1 - rc + rand()*rc), br*(1 - rc + rand()*rc))
            
            //RenderAPI.scale(rand()*0.1 + 1)
            RenderAPI.rotate(getRandomInt(4)*90)
            // my own little mod to rotate the pattern either 0, 90, 180, or 270 deg
            pattern.render(RenderAPI)

            RenderAPI.popMatrix()
        }
        RenderAPI.translate(patternSize.x, 0)
    }

    RenderAPI.popMatrix()
    RenderAPI.translate(0, patternSize.y)
}



To use this script in Photoshop, save it as "Pavement fill.jsx" and put it in your ...Photoshop->presets->Deco directory. Restart Photoshop. Do a Pattern fill an tick the "Scripted Patterns" box at the bottom. Select "Pavement fill" from the dropdown and use a square pattern.

This particular version of the script scatters the selected pattern across the whole image, with a 1 in 4 chance to place a tile. The idea is that after you do a background fill of one tile, you use this script to make multiple passes, one time for each of 4 different tiles. It's probably a terrible way to do an already solved task, but in the end I was happy to get the result that I wanted. Have you guys solved this problem in other ways?

 

 

  • Like 3

Share this post


Link to post
Share on other sites

Sign In or register to comment...

To comment in reply, you must be a community member

Sign In  

Already have an account? Sign in here.

Sign In Now

Create an Account  

Sign up to join our friendly community. It's easy!  

Register a New Account


×

Thank You for the Continued Support!

Simtropolis depends on donations to fund site maintenance costs.
Without your support, we just would not be in our 24th year online!  You really help make this a great community. *:thumb:

But we still need your support to stay online. If you're able to, please consider a donation to help us stay up and running. This helps sustain a platform where we can share our community creations for years to come.

Make a Donation, Get a Gift!

Expand your city with the best from the Simtropolis Exchange.
Make a Donation and get one or all three discs today!

STEX Collections

By way of a "Thank You" gift, we'd like to send you our STEX Collector's DVD. It's some of the best buildings, lots, maps and mods collected for you over the years. Check out the STEX Collections for more info.

Each donation helps keep Simtropolis online, open and free!

Thank you for reading and enjoy the site!

More About STEX Collections