2
$\begingroup$

I'd like a mesh representing a muscle change color (say, from white to red) when the muscle is contracted. I hooked the mesh to the bone it controls (an element of exoskeleton actually, since I'm modeling an insect). The effected bone can move in all directions, but the muscle contracts only along the Y axis. I'd like to be able to feed the Y position of either the empty I used as the hook, or a nearby vertex group of the muscle mesh itself, to, say, a mix shader fac input. Any ideas how to accomplish that? Thanks!

$\endgroup$

2 Answers 2

6
$\begingroup$

This will depend on your rig, I guess, but you could use a driver.

To keep it simple, you could:

  • Drive a 'Value' Input Node
  • Parent a relatively-moving Empty (E1) at one end of the muscle to a root Empty (E0) at the other end. You may already have components that are effectively parented this way in your rig.
  • In this case, E1 is constrained to move only in its parent's local Y
  • It has a (Local Space) Limit Location constraint along that axis to stop it going out of range.. your rig may be doing this another way.

The driver can then be created by (right-click) 'Copy as New Driver' in E1's Y location field in the 'N' panel, and 'Paste Driver' in the Value node. The Y location will be in E0's space.

enter image description here

As above, you can map E1's [Min Y > Max Y] to 0-1, so it fits a color-ramp.

enter image description here

$\endgroup$
3
$\begingroup$

One way you to do this is with a Texture Coordinate Node where you have set the Object to your empty, similar to this trivial example:

Using a texture coordinate node for empty coordinates

I've hooked the output of the texture's Object coordinates to a Separate XYZ node. For my example I used the X output, but the Y output will work for you.

The tricky bit is that the object coordinates are no longer clamped to -1 to 1 on each axis, so you have to do a bit of math to use them effectively, since you have to map those coordinates to the 0 to 1 range expected by most nodes. Rather, the Object coordinates are the distances to the origin of the empty, measured along its own local coordinate system

To make up for my earlier bad explanation, here's a shader that controls the colors of a cube by using all three axis of an empty. Note that the cube's color varies across the cube because it's the distance from the given point on the cube to the origin of the empty that matters.

The mapping nodes mean that the empty can move +/- 5 units along any axis to achieve the full effect, with negative distances being darker and positive lighter.

3d control of cube color by empty

$\endgroup$
10
  • 3
    $\begingroup$ I think I see what you mean, but the Object texture coordinates are measured in the Object Space of the referenced object, or the shaded object ('self') if there's no reference. A subtraction of the spaces will be constant throughout the shaded object if they are aligned in Y and the objects are at the same scale. .. ? $\endgroup$
    – Robin Betts
    Oct 8, 2021 at 17:44
  • $\begingroup$ Thank you, it's working with 2 texture coordinate nodes and a "less than" math node pretty well, although the change is a bit abrupt, I might try smoothing it out with more math. Or a color ramp node somewhere down the line :) $\endgroup$ Oct 8, 2021 at 18:02
  • $\begingroup$ You guys don't disappoint, thank you so much! $\endgroup$ Oct 8, 2021 at 18:02
  • 2
    $\begingroup$ @Marty YDNRC... imgur.com/a/wxKHgY0 :) $\endgroup$
    – Robin Betts
    Oct 8, 2021 at 19:22
  • 1
    $\begingroup$ @RobinBetts It was mostly me being sloppy and wrong. Your last comment is spot on. The sneaky bit about using an object in the Object input of a Texture Coordinate node is not that it is in World Space, but that it's not clamped to -1,1 on any axis. So my first method works because it is the delta between the origins of the empty and the other object, but as you point out in your answer, has issues with scaling. I think your way works welll. $\endgroup$ Oct 8, 2021 at 20:40

You must log in to answer this question.

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