10

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

Peter Mortensen
  • 681
  • 4
  • 11
Václav Musil
  • 350
  • 1
  • 13

2 Answers2

17

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.

Markus von Broady
  • 36,563
  • 3
  • 30
  • 99
13

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

Gorgious
  • 30,723
  • 2
  • 44
  • 101
  • 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... – jachym michal Jul 11 '21 at 13:05
  • 1
    @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 :) – Gorgious Jul 11 '21 at 13:21