Cycles Boolean Node (Not Shader)

Hello, sometimes you might feel that your materials should be more clever, to have them “make decisions” based on various circumstances and redirect flow in according to your needs.

You might have tried already that with some Math nodes, but the procedure is always “hackish” and complex. On the other hand using a simple Boolean Node is much easier and makes sense instantly. For months I was wondering why Blender is missing this feature, until today I got into it and to my amazement it was was much easier than expected.

I read about the OSL specification:
https://github.com/imageworks/OpenShadingLanguage/blob/master/src/doc/osl-languagespec.pdf

I visited the wiki:
http://wiki.blender.org/index.php/Doc:2.6/Manual/Render/Cycles/Nodes/OSL

Also I had a peek at some examples at the official repository:

For the time, I don’t have any other use cases or ideas about it except for what exactly it does. Hopefully you might find this thing interesting, to create your stuff and modify even further. Also I would be interested to see this script or something similar in the trunk, just as an extension to the Math node, if any developer plays with the source could act accordingly.

Thanks for the interest, have a good time.


shader boolean_node
    (
        float ValueA = 0,
        float ValueB = 0,
        string Operator = "",
        float TrueValue = 1,
        float FalseValue = 0,
        
        output float Result = 0
    )
{
    if (Operator == "!=") Result = (ValueA != ValueB) ? FalseValue : TrueValue;
    if (Operator == "||") Result = (ValueA != ValueB) ? TrueValue : FalseValue;
    if (Operator == "==") Result = (ValueA == ValueB) ? TrueValue : FalseValue;
    if (Operator == ">")  Result = (ValueA > ValueB) ? TrueValue : FalseValue;
    if (Operator == "<")  Result = (ValueA < ValueB) ? TrueValue : FalseValue;
}

Attachments

cycles boolean node.zip (920 KB)

If then else example


Or, Not example


Less than, Greater than example


That looks really good! Thats two great node developments in a really short space of time ( this one and the text one). This one opens up a range of possibilities I think.

Ok here’s one way of using it that has some possibilities -
Take the position value from a geometry node and feed it through to a Split XYZ node and feed that to a greater than ( or less than ) boolean node. You can then shade a mesh with two different colours split at a defined z position on the geometry. ( Sort of like a water level) I just hacked your greater/lesser noodle to do exactly that.
Now that might ( i think) have been done using the existing greater than or less than nodes. But now you can easily make something more sophisticated so that the shading changes in all sorts of ways depending upon an objects position in the world.

No doubt using tangents and normals as the tested values for these logic nodes then all sorts of other possibilities open up

Hi thanks for the feedback, yes indeed the shader is very simple so you might come up with something that’s exactly what you want.

Looks good! This is pretty much identical to Maya’s “condition” node, so anyone wants more ideas on uses, Google that for some more info. And const, sorry if I came across as too discouraging in the other thread. I was under the impression for some reason you didn’t want to do this with OSL and instead patch it into Blender directly (a bit more involved).

Hi Ninja, thanks for dropping by. In the beginning I thought that it could be done with OSL only, but now I realize that it could be done with Python as well. It seems that Python is more proper way to go because I can use all of the UI features. I already started looking at it so I will improve it gradually.

The deal with python is that it doesn’t actually implement it in Cycles, it just creates a UI. That was the point I was trying to make in the other thread. The nice part about OSL is that Blender/Cycles do most of the work for you. You script out what you want, and there’s hooks in place to compile both a node and an actual Cycles shader from that. The downside is OSL shaders can only be used when your shader compiler is set to OSL instead of SVM (that’s the Open Shading Language checkbox). OSL is slower than SVM, and only works in CPU mode.

I might be wrong on this, but I don’t think Cycles supports any kind of plug-in SVM shaders. I think OSL is the only way to add new functions, so you’d need to directly implement the shader into Cycles and rebuild the whole thing. At that point you may as well create the new node in C to go with the existing ones.

This is absolutely correct… SVM shaders are all hard coded and there is no SVM compile at render time.

I believe that Cycles works on a subset of C / C++… to make it compile compatible with OpenCL & CUDA.

Although Cycles’ math node doesn’t come with all these options, they are very easy to recreate using only nodes…

Here’s a nodegroup that I normally use with allmost all your functions:


Yes, I see that difference between SVM and OSL, it looks like SVM is the core of Cycles and OSL a superset.


@Secrop, it looks like your setup is very good, it might be also much preferred to avoid script dependencies. Thanks for posting.

Is the == operator will not have problem (because of using float) when trying to compare values that could look equals but have a slight difference (due to operation made before on the number) ?

if (Operator == “==”) Result = (ValueA == ValueB) ? TrueValue : FalseValue;

I mean, shouldn’t it be better to do abs(B - A) < 0.000001 (Epsilon) ?


Out of curiosity can you use <= and >=?

Yeah, it does.

However as of now I would try to think of better ways to create smart materials.

I have experimented with Drivers and they work great however very tedious to setup. Now the only thing I haven’t tried yet, is to look at Animation nodes. A more flexible and manageable way instead of simple Drivers.

I am not sure if this is what you are looking for , but I found it usefull, so I post the video link : https://www.youtube.com/watch?v=h5WwvCo1Fis&t=8s
Hope it helps.

It does the job. :slight_smile: