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!
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.
As above, you can map E1's [Min Y > Max Y] to 0-1, so it fits a color-ramp.
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:
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.