3
$\begingroup$

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:

enter image description here

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?


$\endgroup$
4
$\begingroup$

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

Also your selector seems to be a Mix Shader (I didn't download your file, but it has shader inputs and outputs), and you should not put scalars into a node that expects shaders.

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.

$\endgroup$
  • 1
    $\begingroup$ 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. $\endgroup$ – Fax Jul 1 '18 at 20:42
0
$\begingroup$

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;
}
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.