October 25, 2014

GLSL Palm Tree - Part 2 (Adding the Trunk)

Adding the trunk and final touches!

    
Instead of taking Euclidean distances from the center of the tree, we take only horizontal distances now.
We take the x component of the pixel and threshold it with the smoothstep. Now any point which is closer than 0.01 units from the center of the tree is black.

    
Making r bigger increases the trunk size. More pixels become black because they fall within the band of 0.05 pixels that are selected with the smoothstep function.
We are using abs because we want pixels to both right and left relative to the center of the tree.

    
Make the image sharper by decreasing the range.

But what about that annoying top part?

    
Just use the magic eraser! (He said the “over” operator in the video, which I think could mean the complement.)

    
It’s just doing a selection.
We can change the selection by changing the (2nd) smoothstep. Here we are selecting every pixel above the center of the canopy and 0.4 units above it. So everything above 0.4 is erased.

    
We have changed from 0.4 to 0.2, and observe the difference.

    
You may smooth the eraser if you so wish.

Move aside, tree.

    
Perform translation by modifying q.

They grow up so fast!

    
Translation by modifying q.

But, I’ve seen real palm trees and I don’t think the trunks grow quite that straight.

    
Well alright then.
Add a sin component to bend the trunk.

    
Tweak amplitude and frequency.

  
Decrease frequency to get a nice curve.

    
Subtract the sine wave to flip it.

The trunk still doesn’t look right.

    
We can add another cos component to r to make the trunk wavy.


    
We can also modify amplitude to change how much influence cos has.

But I thought palm trees liked living on beaches?

    
Ground the palm tree by using an exponential.

    
Tweak the value inside the exp.

But the sky’s boring.

    
Alright then let’s make a color gradient.
Use the mix function to supply two colors and a “weight” based on y value of pixel. (More yellow when y is closer to 1.0, more orange when closer to 0.0).

    
For a better sunset, push the orange down in the horizon using sqrt.
Coordinates go from 0 to 1. Taking the sqrt of a number from 0 to 1 makes the number bigger (like sqrt(0.5) ~ 0.77). Bigger numbers in this case means more yellow. It’s important that sqrt doesn’t change 0. That means the orange will always be there on the bottom, it won’t disappear completely.

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

And there you have it.  A tree for you and me.