10
$\begingroup$

I want to drive a shader with a value node. If the value would be from 0.1 to 0.3, I want the output to be 1 and otherwise I want it to be 0.

I was thinking about using Greater Than and Less Than, but I can’t quite figure it out (I attach an idea I thought would work, but it doesn’t).

Idea I though would work, but it doesn’t

$\endgroup$

2 Answers 2

13
$\begingroup$

You can do it with a Color Ramp node. Set the interpolation to Constant, bring the black stop to the position 0.3, the white stop to 0.1 and add a black stop at 0.0.

enter image description here

enter image description here

$\endgroup$
2
  • $\begingroup$ Very nice :). I noticed that Fac=0,1 still outputs 0. But when I set the white stop to 0,97 it works perfectly. Probably a rounding error in the ColorRamp... $\endgroup$ Jul 11, 2021 at 13:05
  • 1
    $\begingroup$ @JachymMichal Hmm yes the Color ramp node is notorious for its rounding errors (and inefficiency) since it converts values to colors and then back to values afterwards... But that's a very high margin of error even for floating point precision errors... It's definitely possible to mimic the same setup with a combination of math nodes but it would take more space in the editor :) $\endgroup$
    – Gorgious
    Jul 11, 2021 at 13:21
17
$\begingroup$

In your example, it works for the given input, that is a value 0.2 produces 1 output: value is both greater than 0.1 and lesser than 0.3, both condition nodes outputs sum up to 2 which is clamped into the 0...1 range, so becomes 1.

However let's see what happens if your value is below the 0.1...0.3 range: it passes only one condition, but since summing 0 and 1 results in 1, which is the same result as in previous case due to clamping in that previous case.

So Math > Add is an equivalent to Logical OR: if either of operands is True, the result is True. You need something different, Logical AND: if both of operands are True (both less than .3 and more than .1) the result is True, otherwise it's not True (False). In order to do that, replace your Math > Add with Math > Multiply - now if either of the comparison nodes will result with 0, the other result will be multiplied by 0, resulting, of course, with 0. So the only way to get 1 will be to have a value both lesser than .3 and greater than .1!

There's a simpler way, however, Math > Compare will result in 1 if you're not more than Epsilon away from Value (otherwise 0)

In general to output 1 if input is in range a...b, set the Math > Compare this way:

  • Epsilon = (b-a)/2
  • Value = a + Epsilon

so for range 0.1 ... 0.3:

  • Epsilon = (0.3-0.1) / 2 = 0.2 / 2 = 0.1
  • Value = 0.1 + 0.1 = 0.2

In simpler terms set the the Value to the middle of the range, and Epsilon to half of the range.

$\endgroup$
6
  • $\begingroup$ in my opinion this should be the right answer...because it's easier to setup and hopefully more precise. $\endgroup$
    – Chris
    Jul 11, 2021 at 13:50
  • 1
    $\begingroup$ @Chris this is a tricky thing on SE, in the end the userbase decides what it likes more - with votes. :) $\endgroup$ Jul 11, 2021 at 14:17
  • $\begingroup$ i know. I just wanted to express my thinking ;) $\endgroup$
    – Chris
    Jul 11, 2021 at 14:19
  • $\begingroup$ This is exactly the way I would do it. $\endgroup$ Jul 12, 2021 at 6:14
  • $\begingroup$ Oh, right, completely forgot about that :) $\endgroup$
    – Gorgious
    Jul 12, 2021 at 6:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .