# How to multiply vector by scalar in material nodes?

I'd like to do arbitrary vector math with the camera position, shading point, and possibly other points. As a test case (blend file), suppose I want to make a shader that will draw a simple gradient based on distance from the shading point to the camera in one of two ways.

The two methods of getting the desired distance are:

1. "Camera Data.View Distance" as the control.

2. Computing the length of a vector equal to the unit vector given by "Camera Data.View Vector" multiplied by the scalar "Camera Data.View Distance". This is the issue.

The node setup looks like this: What can I put in the "Vec * Scalar?" frame so that the material will look the same no matter how the "Selector" Mix Shader Factor is set?

You should think more about the data types. If you want vector*scalar, use MixRGB set to Multiply (an RGB color has three components, like a vector), and not a simple scalar math node. See my answer to this question for an example of vector*scalar: Scale Texture with a Value-/Math-Node

EDIT Just for completeness: There are also other ways of doing vector*scalar, like using a vector Mapping node and setting the same X, Y, Z Scale, or separating the vector components with a Separate XYZ and multiplying the three scalar coordinates individually, and merging them again with Combine XYZ.

• MixRGB only does vectorscalar in the 0-1 range, otherwise you have to use vectorvector. It also doesn't accept negative constants. Mapping is only vector*vector constant. Separate XYZ seems to be the best solution; just stick it in a group and copy it around. – Fax Jul 1 '18 at 20:42

If you don't mind using Open Shading Language, it's pretty easy to do make a script node that does vector component multiplication:

shader osl_vector_multiply(
vector InVector1 = 1,
vector InVector2 = 1,
float InScalar = 1,
output vector OutVector = 1)
{
OutVector = InVector1 * InVector2 * InScalar;
}