My Authors
Read all threads
SHADER BASICS PART 5

HELPERS
Before I get to the shaping functions, SDFs and other cool stuff, I thought it'd be useful to first mention some helpful general-purpose methods that can be proven useful later.



#glsl #shaderbasics #shaders
MIN/MAX

These are fairly intuitive and common; they take 2 parameters and return the minimum or the maximum of the two respectively. They're useful for masking things in/out and blending them.
In this example, the left half of the screen (which has a uv.y less than 0.5) is colored with the UV gradient, however the right half has a solid value of 0.5 (since it's smaller than uv.x).
FLOOR/CEIL

These methods are used for rounding values down and up respectively. They're useful for quantizing values and getting bands of your colors. Obviously you can find other use cases too.
Here I quantize the value of the UV gradient in increments of 0.1 by getting the integer values from 0.0 to 9.0 and then dividing by 10.
ABS

This is used to prevent your car wheels from locking up while braking. In the context of shader coding, though, it's a method used to get the absolute value of its parameter. Meaning, if the parameter value is negative, it returns the value as a positive number.
In this example, I remap the UV coordinates in the (-1, 1) range and I take the absolute value of the x coordinate, which goes from 1 to 0 in the middle and then back to 1.
CLAMP

This is used to restrict the range of a value. When used as clamp(t, a, ), the method will return

a if t <= a
b if t >= b
t otherwise

It's worth mentioning that CG (so, unity shaders too) also has the "saturate" method, which basically clamps a value from 0.0 to 1.0.
Here you can see that the shader outputs a solid value of 0.3 while the x coordinate is less or equal than 0.3, after it's bigger than 0.7 the output is also a solid value and from 0.3 to 0.7 it transitions normally.
SIGN

It's used to get the sign of its parameter by returning

-1.0 if the value is negative
0.0 if the value is zero
1.0 if the value is positive
The result here looks a lot like that of a step method but it's like that because, due to the remapping, the x coordinate on the left side of the screen is negative, while on the right side it's positive.
That's it! Combining these methods with the previous ones and with shaping functions, you can get do even more exciting math tricks! Keep in mind that all these helper methods also exist in the context of Unity (and other) shaders!
Missing some Tweet in this thread? You can try to force a refresh.

Enjoying this thread?

Keep Current with Harry 💬

Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

Twitter may remove this content at anytime, convert it as a PDF, save and print for later use!

Try unrolling a thread yourself!

how to unroll video

1) Follow Thread Reader App on Twitter so you can easily mention us!

2) Go to a Twitter thread (series of Tweets by the same owner) and mention us with a keyword "unroll" @threadreaderapp unroll

You can practice here first or read more on our help page!

Follow Us on Twitter!

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just three indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3.00/month or $30.00/year) and get exclusive features!

Become Premium

Too expensive? Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal Become our Patreon

Thank you for your support!