28
$\begingroup$

Sometimes I create Material Groups to be used as appendable reusable assets. Unfortunately sometimes parameters I want to control using a Group Node's slider are not available for routing to a Group Node's input socket. Some examples of these are the Color Ramp and the Mapping Node.

So here's what I want to do:

I want to control this driver using the group's input

Can this be done? If so, how?

Thanks!

$\endgroup$
8
  • $\begingroup$ All the functionality of the mapping node can be reconstructed fairly easily (you definition of easy math may be different from mine) via some math nodes, color ramp is a bit trickier but I believe it is still possible. Would you be open to such solutions? $\endgroup$
    – PGmath
    Commented Oct 29, 2015 at 14:34
  • 1
    $\begingroup$ Could you please add a small example file with the node setup? $\endgroup$
    – batFINGER
    Commented Oct 29, 2015 at 15:12
  • $\begingroup$ @batFINGER You got it! A .blend is now included. $\endgroup$
    – Mentalist
    Commented Oct 29, 2015 at 16:29
  • $\begingroup$ @PGmath I'm certainly curious about how you could do that, and it would be useful. A solution that doesn't vary by node type is ideal (drivers?) - but if that can't be worked out, then at least being able to make something like a color ramp is better than nothing. $\endgroup$
    – Mentalist
    Commented Oct 29, 2015 at 16:40
  • $\begingroup$ @Mentalist The disadvantage to making a homemade color ramp is that it would have a set number of color swatches, and you obviously can't make the nice graphical display etc. But if you are just controlling it via a group socket neither of those should matter. The homemade mapping nodes, on the other hand, is extremely useful, even for normal use. I'll work on an answer showing those two, if you can think of any other similar nodes I can try to make them from scratch too. $\endgroup$
    – PGmath
    Commented Oct 29, 2015 at 17:44

1 Answer 1

33
$\begingroup$

As far as I know, I don't think it is possible to drive a Color Ramp or Mapping nodes from another socket (but I am not super experienced in drivers). However I have managed to re-create the color ramp and mapping node with math nodes, which you can plug inputs into directly.


Color Ramp

Unfortunately there is no way to create a group node exactly like the Color Ramp, with addable and removable color swatches. To get around this I have created a node with two movable swatches, you can then combine multiple of these nodes together to have the functionality of multiple swatches.

The theory:

The two input colors are plugged directly into a Mix RGB node. The two Pos inputs need to be sent through a function and plugged into the mix factor. The position of the first swatch needs to be mapped to $0$ to get just the first color out of the mix node. The position of the second swatch needs to be mapped to $1$ to get just the second color out of the mix node.

The math:

Here's a graph to visualize what we are trying to do, on the x-axis is the input factor of the color ramp, on the y-axis is the desired output, $a$ and $b$ are the positions of the two swatches.

enter image description here

With some simple algebra we can find the equation of the line to be:

$$ y = \frac{1}{b-a}x + \frac{a}{a-b} $$

The math nodes below are simply replicating this equation. The final Add node also has Clamp checked to clamp the output to the interval $[0,1]$, which is what the mix node accepts.

enter image description here


Mapping node

The mapping node has three functions it can Translate (move), Rotate, and Scale the texture. Since often not all of these are needed I will create separate group nodes for each of these functions.

If you don't have a decent understanding about how texture coordinates work you may want to read my answer here. The basic theory of manipulation texture coordinates is to separate the components of the vector with a Separate XYZ node, manipulate the components individually, then combine them back into a vector with a Combine XYZ node.


Mapping Node - Translation

Translation is easy, to translate $V$ to $V^\prime$ just add the desired amounts to each of the components of $V$.

enter image description here

In the above node setup I actually used Subtract nodes instead of Add so positive values will move the texture to the right instead of left.


Mapping Node - Scaling

Scaling is the same as translating, just multiply the individual coordinates by the scale factor instead of adding.

enter image description here


Mapping Node - Rotation

Rotation is a little more complicated and I won't take the time to derive the equations here, but they are pretty standard pre-calc formulas. Here are the formulas for rotation.

Around the x-axis

$$ \begin{aligned} x^\prime &= x\\ y^\prime &= y\cos{\theta} - z\sin{\theta}\\ z^\prime &= y\sin{\theta} + z\cos{\theta} \end{aligned} $$

enter image description here

Around the y-axis

$$ \begin{aligned} x^\prime &= x\cos{\theta} - z\sin{\theta}\\ y^\prime &= y\\ z^\prime &= x\sin{\theta} + z\cos{\theta} \end{aligned} $$

enter image description here

Around the z-axis $$ \begin{aligned} x^\prime &= x\cos{\theta} - y\sin{\theta}\\ y^\prime &= x\sin{\theta} + y\cos{\theta}\\ z^\prime &= z \end{aligned} $$

enter image description here

Note: the first two nodes in each of the rotation setups convert the degree input to radians, which is what the trig math nodes want.


And finally, here's a .blend file with all the above node groups.

$\endgroup$
10
  • 2
    $\begingroup$ Wow! This is amazing... useful and really fun to play around with too. It's like Xmas on Halloween, haha! You're a genius. Thank you for dedicating your time to make something so cool! I'm going to be twisting vectors around like a madman now. $\endgroup$
    – Mentalist
    Commented Oct 31, 2015 at 8:23
  • 1
    $\begingroup$ @Mentalist Glad you like it! But I'm no genius, I just love math! $\endgroup$
    – PGmath
    Commented Oct 31, 2015 at 12:36
  • 1
    $\begingroup$ Well, humble sir, I could never have put together something like this. So how about I phrase it as: Thanks for using your talent of math comprehension to share new possibilities with the rest of us! I'm really terrible with math unless I can see it in geometric relationships. I can't take an equation and translate it into something meaningful. So your node setups are like a wheelchair to someone who is mathematically unable to stand on two legs. Ironically though, seeing it as nodes makes more sense than as equations, so I might actually learn something about math from your constructions. $\endgroup$
    – Mentalist
    Commented Oct 31, 2015 at 15:24
  • 1
    $\begingroup$ I was trying to consolidate your Rotation X, Rotation Y, and Rotation Z nodes into a single Rotation node with three sliders for X,Y,Z values. However somewhere in the process I seem to have screwed something up because when set at the default 0,0,0 values it is still causing a change in rotation. Would you mind taking a look at my .blend to see what I did wrong, and perhaps show me how these X,Y,Z values could be input through a single Group Node to match the format of the Translate and Scale ones? Thank you! $\endgroup$
    – Mentalist
    Commented Nov 3, 2015 at 17:22
  • 1
    $\begingroup$ Never mind my last comment - I realized it's easier to just chain them one after another: X, Y, Z and then group those three groups into a single "Rotate" node. $\endgroup$
    – Mentalist
    Commented Nov 4, 2015 at 20:44

You must log in to answer this question.

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