《Unity着色器圣经》5.0.3 | 点乘

目录索引

译文

点乘是我们在计算光照和反射时经常会用到的一种运算,因为它可以确定两个向量之间的角度,并返回一个标量(即一个一维变量)。一般来说点乘得到的结果会进行归一化(normalized)处理,以确保返回值的范围在 [1, -1]。

a・b = || a || || b || Cos θ

在前面的函数中:当向量 a 与向量 b 之间的夹角等于 0 度时,a 点乘 b 将返回结果“1”。反过来,当向量 ab 之间的夹角等于 90 度时,a 点乘 b 将返回结果“0”。最后,当夹角等于 180 度时,a 点乘 b 将返回结果“-1”。

点乘运算是如何进行的呢?要理解它,我们必须先计算两个向量之间夹角的余弦值:

Cos θ = (a・b) / (|| a || || b ||)

为了理解上述操作,我们假设现在有以下两个二维向量:

  • 向量 a (1, 1)
  • 向量 b (1, 1)
图片[1]-《Unity着色器圣经》5.0.3 | 点乘-软件开发学习笔记

这两个向量的方向和大小都相等。要计算 a 和 b 之间的点积,我们必须先用 ax 乘以 bx,再用 ay 乘以 by,最后将两个值相加:

ax * bx = 1
ay * by = 1
1 + 1 = 2

所以,a 点乘 b 的结果等于 2。为了说明这一点,让我们在上述函数中替换这些值:

Cos θ = 2 / (|| a || || b ||)

为此,我们必须执行以下操作:

|| v || = √(Vx² + Vy²)

所以,对于向量 a:

|| a || = √(1x² + 1y²)
|| a || = √2

由于向量 b 与向量 a 是完全相等的,所以我们可以推断出向量 b 的大小也是:

|| b || = √(1x² + 1y²)
|| b || = √2

如果我们用 a 的大小乘以 b 的大小,就会得到下面的值:

|| a || * || b || = √2 * √2 = √(2 * 2) = √(2²) = 2
|| a || * || b || = 2

我们可以看到,a 的大小乘以 b 的大小等于 2。为了更好地理解,我们将再次替换前面函数中的这个值:

Cos θ = 2 / 2
Cos θ = 1
θ = Cos⁻¹(1) = 0°
Cos (0°) = 1

0 度的余弦值等于 1,所以现在如果我们回顾最开始运算,结果如下:

// a・b = || a || || b || Cos θ
a・b = 2 * 1 = 2

a 和 b 的点积结果等于“2”。现在,如果我们对这个值进行归一化处理,就会得到“1”:

normalize(a・b) = 1

解释点乘的概念有什么用呢?当我们在着色器中实现光照效果时,必须使用到两个向量:一个用于光照计算,另一个用于法线计算。那么在我们刚才的例子中,向量 a 就可以代表全局光照,向量 b 代表模型的法线。

在具体的实现过程中,光照计算是针对模型中的每一个顶点进行的。对于与光源方向相同的法线,点积将返回 1,而对于方向相反的法线,点积将返回 -1。


原文对照

The dot product is an operation that we will use frequently in the calculation of illumination and reflection since it allows us to determine the angle between two vectors and returns a scalar output value, that is, a one-dimensional variable. Generally, the resulting value will be normalized to ensure that the return range is between one and minus one [1, -1].

a・b = || a || || b || Cos θ

In the previous function: when the angle between vectors a and b equals 0°, the dot product will return “one”. In turn, when the angle equals 90°, the dot product will return “zero”. Finally, when the angle equals 180°, the dot product will return “minus one”.

How does this operation work? To understand it, we must calculate the cosine of the angle between these two vectors.

Cos θ = (a・b) / (|| a || || b ||)

To understand the above function, let’s assume that we have two two-dimensional vectors with the following values.

  • vector a (1, 1)
  • vector b (1, 1)
图片[1]-《Unity着色器圣经》5.0.3 | 点乘-软件开发学习笔记

Both vectors are equal in both direction and magnitude. To calculate the dot product between a and b we have to multiply ax times bx, then ay times by and finally add the values.

ax * bx = 1
ay * by = 1
1 + 1 = 2

Consequently, the product point between a and b equals 2. To show this, let’s replace these values in the function mentioned above.

Cos θ = 2 / (|| a || || b ||)

Then we must calculate the magnitude of both a and b. To do this, we must perform the following operation.

|| v || = √(Vx² + Vy²)

So, for vector “a.”

|| a || = √(1x² + 1y²)
|| a || = √2

Since vector “b” is exactly equal to vector “a” we can deduce that its value is the same.

|| b || = √(1x² + 1y²)
|| b || = √2

If we multiply the magnitude of a by the magnitude of b, we get the following value.

|| a || * || b || = √2 * √2 = √(2 * 2) = √(2²) = 2
|| a || * || b || = 2

As we can see, the factor between the magnitude of a and b equals two. Again, we are going to replace this value in the previous function to understand it better.

Cos θ = 2 / 2
Cos θ = 1
θ = Cos⁻¹(1) = 0°
Cos (0°) = 1

So, the cosine of zero degrees equals one, therefore, if we carry out the initial operation it would be as follows.

// a・b = || a || || b || Cos θ
a・b = 2 * 1 = 2

The result of the product point between a and b equals “two”. Now, if we normalize this value, we will get a magnitude of one.

normalize(a・b) = 1

What is the use of all this explanation? When we implement lighting in our shader, we have to use two vectors: one for the light calculation and the other for the calculation of the normals. Then vector a could represent the global illumination and vector b represent the object normals.

In its implementation, the lighting calculation is performed for each vertex in the object, so for those normals that are in the same direction as the illumination, the dot product will return one, and for those that are on the opposite side will be minus one.

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容