This article has participated in the Denver Nuggets Creators Camp 3 “More Productive writing” track, see details: Digg project | creators Camp 3 ongoing, “write” personal impact.

Chestnut 8 — Landscape of urban architecture

In this example, we will write a script to draw the outline of a city building. This is very useful for creating backgrounds or generating design ideas.

Since most buildings are rectangular, we want to use a function similar to the square wave we used earlier. But this function is too repetitive to use. Fortunately, there is another predefined function that is perfect for this: cellNoise. Here’s a preview of the chart.

It behaves like the noise function you saw in the previous example, but is discontinuous. Its output produces lines that jump randomly from one level to the next. This is perfect for many applications, including building profiles! Try this script:

p1 = period; 
p2 = period/3; 
p3 = period/7; 
p4 = period/11; 
offsetDist = d + seed; 
w1 = w1Amp * cellNoise(offsetDist/p1); 
w2 = w2Amp * cellNoise(offsetDist/p2); 
w3 = w3Amp * cellNoise(offsetDist/p3); 
w4 = w4Amp * cellNoise(offsetDist/p4); 
offset = d < 20 ? 0 : (w1+w2+w3+w4); 
ox = x + nx * offset; 
oy = y + ny * offset;
Copy the code

Set the following parameters: Period between [100..300], seed between [0.. 50000], w1Amp through w4Amp between [0..100].

A common technique for generating random patterns is to combine several noise waves with decreasing periods. Waves with the highest period set the rough outline of the curve, then added details by adding high-frequency waves.

That’s what we’ve done here: divide the main period argument by 3, 7, and 11 to generate three smaller periods, and then use them in successive calls to the cellNoise function. A global seed is shared by all waveforms. Each wave is scaled according to its own amplitude parameters, and the final offset is generated by stacking the four waves together. You’ll also notice that if the current distance is less than 20, we set offset to 0. So when we start drawing, the line won’t go straight up or straight down.

Now for the fun part! Open the script graphics window, play with amplitude parameters, and find the seed you like!

Very cool! But now, if we want to map the urban architectural landscape at different depths, we have to adjust the period and amplitude each time. To simplify things, let’s add a global scale parameter and set its scope to [0..10].

p1 = period*scale; 
p2 = p1/3; 
p3 = p1/7; 
p4 = p1/11; 
offsetDist = d + seed; 
w1 = w1Amp * cellNoise(offsetDist/p1); 
w2 = w2Amp * cellNoise(offsetDist/p2); w3 = w3Amp * cellNoise(offsetDist/p3); 
w4 = w4Amp * cellNoise(offsetDist/p4); 
offset = d < 20 ? 0 : scale*(w1+w2+w3+w4); 
ox = x + nx * offset; 
oy = y + ny * offset;
Copy the code

We can now quickly create deep cityscape scenes by reducing the scaling parameters for each line. Here is an example created using a parallel ruler that provides a perfectly straightforward guide to the script:

Chestnut 9 — Lightning

Next time

Chestnut 10 – Signal misidentification

Next time

Chestnut 11 — Debugging

Next time