问题描述
我有一个归一化的方向矢量(从3d位置到亮位置),我希望将此矢量旋转一定角度,以便创建一个圆锥体".
I have a normalized direction vector (from a 3d position to a light position) and I would like this vector to be rotated by some angle so I can create a "cone".
Id喜欢通过使用方向矢量作为圆锥体的中心来模拟圆锥体跟踪,并创建X个样本,以创建更多的光线来作为样本.
Id like to simulate cone tracing by using the direction vector as the center of the cone and create an X number of samples to create more rays to sample from.
我想知道的基本上是背后的数学:
What I would like to know is basically the math behind:
https://docs.unrealengine.com/Latest/INT/BlueprintAPI/Math/Random/RandomUnitVectorinCone/index.html
似乎完全符合我的要求.
Which seems to do exactly what Im looking for.
推荐答案
1)使任意矢量P垂直于方向矢量D.您可以选择幅度最大的分量,将其与中等幅度的分量交换,取反,并将幅度最小的分量设为零.
1) Make arbitrary vector P, perpendicular to your direction vector D.You can choose component with max magnitude, exchange it with middle-magnitude component, negate it, and make min magnitude component zero.
例如,如果z分量最大而y分量最小,则可以将其设置为P:
For example, if z- component is maximal and y-component is minimal, you may make such P:
D = (dx, dy, dz)
p = (-dz, 0, dx)
P = Normalize(p) //unit vector
2)通过向量积使向量Q垂直于D和P:
2) Make vector Q perpendicular both D and P through vector product:
Q = D x P //unit vector
3)生成 PQ平面磁盘中的随机点
RMax = Tan(Phi) //where Phi is cone angle
Theta = Random(0..2*Pi)
r = RMax * Sqrt(Random(0..1))
V = r * (P * Cos(Theta) + Q * Sin(Theta))
4)归一化向量V
请注意,向量分布在球体段上稍微不均匀(在平面圆盘上均匀).有一些方法可以生成球体上的均匀分布,但是需要一些工作来将它们应用于细分(我编辑之前的第一次尝试是错误的.)
Note that distribution of vectors is slightly non-uniform on the sphere segment.(it is uniform on the plane disk). There are methods to generate uniform distribution on the sphere but some work needed to apply them to segment (my first attempt before edit was wrong).
编辑:进行修改以使球面分布均匀(未彻底检查)
Modification to make sphere-uniform distribution (not checked thoroughly)
RMax = Tan(Phi) //where Phi is cone angle
Theta = Random(0..2*Pi)
u = Random(Cos(Phi)..1)
r = RMax * Sqrt(1 - u^2)
V = r * (P * Cos(Theta) + Q * Sin(Theta))
这篇关于方向矢量锥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!