RobynsVeil opened this issue on Jan 24, 2009 · 490 posts
bagginsbill posted Wed, 28 January 2009 at 11:19 PM
Well there are two node things that are like an IF.
One is a specific test to see if one number is less than or equal to another. It is the math function Step.
Step(a, b) is 1 if a <= b. It is 0 otherwise. If used in Color_Math, as usual each channel is processed separately. So if the red in a is <= the red in b, then the result will have red = 1, else red = 0. Same thing for green and blue.
There are certain situations where the Step function is very handy. There are others where it would not be wise to use it, because it is too abrupt. But for purposes of the test for a hyper color, it would be very good.
Step(x, 1) would be checking where x is less than or equal to 1. In such cases, the value would result in a 1. So a non-hyper color (such as .8) would yield a 1 here. But a hyper color, such as 1.1, would not meet this test, and so it would return a 0. So another way to test for hyper colors is just that: Step(x, 1).
This test would not tell you anything about how hyper is it. It would simply tell you it is or isn't hyper. So where you have a hyper, you'd get 0, and were you have a normal, you'd get 1.
Now if you wanted your result ot actually be the reverse, i.e. hyper is 1 and normal is 0, you just do the "complement" function on that. The complement function is one I use a lot. It is not a node, per se - there is no complement node. You build it from a subtract node.
complement(x) = 1 - x
So the combined tester would be 1 - Step(x, 1)
A gradual version of step can be built from Clamp. Consider that x - 1 is 0 for any value where x is <= 1. It is greater than 0 for any value where x is greater than 1. With me?
Now consider if x is only slightly more than 1, say perhaps 1.1. But we want to make that produce a value of 1. Well then 10 * (x - 1) will do it, right? But what does 1.05 get us?
10 * (1.05 - 1) = .5
Interesting. What about 1.02?
10 * (1.02 - 1) = .2
Hmmm! So as x goes above 1 on its way to 1.1 this function will gradually increase towards 1. In fact the rate at which it increases is 10 in this case, but we could make the rate faster or slower by using 20 or 3, see?
So suppose we want the function f(1) = 0, and f(1+delta) = 1? Simple:
f(x) = (x - 1) / delta
See that? Imagine I want a function that gradually goes from 0 to 1 as x goes from 1 to 1.01. The delta is .01. So:
f(x) = (x - 1) / .01
which is the same as
f(x) = (x - 1) * 100
Now what happens to that when x is 1.02?
f(1.02) = (1.02 - 1) * 100 = 2
Hmmm - maybe I don't want this function producing hyper colors. I just want that gradual rise from 0 to 1, and then after that don't change. Clamp time!
f(x) = Clamp(100 * (x - 1))
I use this all the time. Really I'm not making this up. Any place I'm doing color work and I have some pattern driving the color and I want a shift in color to occur as the pattern value crosses a threshold, this is what I use. The width of the threshold can be easily adjusted with that multiplier, and the threshold itself is whatever I subtract from x.
But we still haven't come to the big winner for IF. So far, we've been doing variations on testing whether something is above or below a particular value. But how do you actually implement the "then" and "else"? Meaning, big whoop I found what I'm looking for and I have a function that gives me 0 for "no" and 1 for "yes" (and perhaps numbers in-between for "maybe" or "sort of").
How do we connect that to something like IF foo then Clouds(...) else Image_Map(...)?
The answer is Blender. If you were to count up all the nodes I've ever used and see which one was used the most, I'd bet it is Blender.
The math behind Blender is actually assembled from more primitive ones. We could build Blender functionality into shaders even if it didn't exist, but we'd have to assemble 5 nodes every time to get the same thing done. Here is the equivalence formula for Blender
Blender(a, b, f) = (1 - Clamp(f)) * a + Clamp(f) * b
Or another way to look at it is:
Blender(a, b, f) = a - Clamp(f) * a + Clamp(f) * b
Or another way to look at it is
Blender(a, b, f) = a + Clamp(f) * (b - a)
Basically it works like this. If f is 0 or less, you get a. If f is 1 or more, you get b. If f is between 0 and 1, you get a mixture of a and b, with the proportions following the transition of f between 0 and 1. When f is .5, you get exactly half of a and half of b added together, which is to say, you get the average of a and b.
Wow. That's a lot of things you get with it.
I use Blender to:
1) Choose among two values or colors based on the "truth" of some other value.
2) Softly mix color in exact proportions
Renderosity forum reply notifications are wonky. If I read a follow-up in a thread, but I don't myself reply, then notifications no longer happen AT ALL on that thread. So if I seem to be ignoring a question, that's why. (Updated September 23, 2019)