Alpha Erosion Function

After fixing my normal issues, I moved onto the main purpose of my shader – alpha erosion! I wanted to replicate a sort of burn erosion effect using a mask input by the user.

7

I didn’t want to clutter up my surface function by doing it all in there, so split it out into a new one. The syntax was a little odd for me – being used to Python I keep forgetting to give everything a type – but I got there in the end!

I’m using a user input to subtract from the erosion texture, in order to move it from the original texture to fully transparent.

However, I didn’t want to start with the alpha being the texture, I wanted to start at fully opaque and then start eroding. To do this, the value I subtracted was a lerp between -1 (essentially adding 1 to the texture to make everything white) and the erosion value. To determine the interpolator I used a smoothstep. This takes a value and a max and min input and outputs a value between 0 and 1 based on a smooth curve between the min and max values. The reason I used this was because I could set the maximum input to 0.1 – meaning any value equal or greater than this would evaluate as 1.

After this I did a ciel on the subtraction, giving me a hard edge as every pixel comes out as 0 or 1. Using a ciel rather than a round keeps the values within the correct range. I then saturated it to prevent any values lower than 0 or higher than 1.

float alpha_erosion (fixed4 erosion_texture, float erosion_value) { 
//If user erosion input is 1, make alpha fully opaque by setting 
//subtracted value to -1. 
float fully_opaque = smoothstep(0, 0.01, erosion_value); 
float opaque_erosion_value = lerp(-1.0, erosion_value, fully_opaque); 
//Subtract alpha 
return saturate(ceil(erosion_texture.r - opaque_erosion_value)); 
}

 

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s