0
\$\begingroup\$

I was watching this tutorial and at timestamp 3:03 he did this:

enter image description here

This blows my mind because I didn't expect that to actually work, how the heck does that work ???? For example, if I have vector (2, 3, 4), that means:

$$ {(2, 3, 4)}^{(8, 8, 8)} $$

How does that work mathematically?

\$\endgroup\$
2
  • 1
    \$\begingroup\$ I suspect this is just done member-wise. so (2⁸, 3⁸,4⁸), since that is how shaders tend to handle scalar math on vectors. I can't really verify this, though. \$\endgroup\$
    – PepeOjeda
    Dec 14, 2023 at 8:14
  • \$\begingroup\$ @PepeOjeda if that is the case then in my opinion unity should throw error instead of doing something that is different from the mathematical syntax like that, and add another node which does that instead of doing it in power node, maybe name the node "vector power" \$\endgroup\$
    – aaa
    Dec 14, 2023 at 14:01

1 Answer 1

2
\$\begingroup\$

The Shader Graph documentation for Power Node says:

The following example code represents one possible outcome of this node.

void Unity_Power_float4(float4 A, float4 B, out float4 Out) 
{
    Out = pow(A, B); 
}

And if we then look up the documentation for the pow() function using float4 it says (emphasis mine):

pow(float4, float4)
Returns the componentwise result of raising x to the power y.

Thus the comment by @PepeOjeda is correct and with respect to Shader Graph, you have: $$(2,3,4)^{(8,8,8)} = (2^8,3^8,4^8)$$

From a float4 is a vector perspective this might feel like misleading overload. But from a SIMD (single instruction, multiple data) perspective, the operation makes sense. In that context, the float4 can be seen as parallel data packaged together for parallel processing, hence the componentwise math operation. As @DMGregory points out, this convention is common in in shader languages and many operators are treated this way. For example, if you search the Math class docs for "returns the componentwise", you'll see over a dozen functions operate as described.

\$\endgroup\$
2
  • 3
    \$\begingroup\$ It might be worth mentioning that this is a very common convention in shader languages and other SIMD implementations. Multi-component data types like float4 aren't always thought of as vectors in the mathematical sense, but as parallel streams of data that all need to go through the same series of operations, so we might as well do them four at a time in parallel. pow(a, b) then doesn't mean "raise vector a to the power of vector b" but "apply the power operation to these four separate pairs of bases and exponents, in parallel". The same happens even for basic multiplication. \$\endgroup\$
    – DMGregory
    Dec 14, 2023 at 15:22
  • \$\begingroup\$ It would also be good to state the effect on colors in terms of contrast. Values >1 increase contrast and values <1 decrease contrast. This can be used in creating masks from existing textures, without consuming additional texture samplers. \$\endgroup\$
    – agone
    Dec 17, 2023 at 2:33

You must log in to answer this question.

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