与网格平行定位 objects
Positioning objects parallel with a mesh
我正在尝试根据三角形在网格中的方向沿着人体 body 圆周对齐多条线 objects。我想把线平行于网格。我正确地分配了沿圆周的线的位置,但我还需要添加线的旋转,以便与 body 平行。
body是由多个三角形组成的网格,每条线都与一个三角形“相连”。
我只有:
每条线距离网格最近的三角形有 3 个点
三角形的法线
实例化线的位置(2 点,起点和终点)
我需要计算线的每个 X、Y、Z 轴的角度,以便三角形的法线与线网格垂直。我不知道如何获得所需的角度。如果有人愿意帮助我,我将不胜感激。
输入:
FVector 三角点[3];
F向量Triangle_Normal; //计算为(B-A)^(C-A),其中A,B,C为三角形的点
F向量线位置; //如果有帮助,我还有起始线和结束线位置
输出:
//FRotator旋转(x,y,z),使三角形法线与直线object垂直
圆周线建设概览。现在使用每条线的开始位置和结束位置计算旋转。当我们穿过网格的一些不规则部分时,我们希望正确地旋转线条。现在旋转是固定的,仅取决于行的开始和结束位置。
如果我没有正确理解你的目标,这里有一些相关的向量几何:
A,B,C are the vertices of the triangle:
A = [xA, yA, zA],
B = [xB, yB, zB]
C = [xC, yC, zC]
K,L are the endpoints of the line-segment:
K = [xK, yK, zK]
L = [xL, yL, zL]
vectors are interpreted as row-vectors
by . I denote matrix multiplication
by x I denote cross product of 3D vectors
by t() I denote the transpose of a matrix
by | | I denote the norm (magnitude) of a vector
Goal: find the rotation matrix and rotation transformation of segment KL
around its midpoint, so that after rotation KL is parallel to the plane ABC
also, the rotation is the "minimal" angle rotation by witch we need to
rotate KL in order to make it parallel to ABC
AB = B - A
AC = C - A
KL = L - K
n = AB x AC
n = n / |n|
u = KL x n
u = u / |u|
v = n x u
cos = ( KL . t(v) ) / |KL|
sin = ( KL . t(n) ) / |KL|
U = [[ u[0], u[1], u[2] ],
[ v[0], v[1], v[2] ],
[ n[0], n[1], n[2] ],
R = [[1, 0, 0],
[0, cos, sin],
[0, -sin, cos]]
ROT = t(U).R.U
then, one can rotate the segment KL around its midpoint
M = (K + L)/2
Y = M + ROT (X - M)
这里是python脚本版本
A = np.array([0,0,0])
B = np.array([3,0,0])
C = np.array([2,3,0])
K = np.array([ -1,0,1])
L = np.array([ 2,2,2])
KL = L-K
U = np.empty((3,3), dtype=float)
U[2,:] = np.cross(B-A, C-A)
U[2,:] = U[2,:] / np.linalg.norm(U[2,:])
U[0,:] = np.cross(KL, U[2,:])
U[0,:] = U[0,:] / np.linalg.norm(U[0,:])
U[1,:] = np.cross(U[2,:], U[0,:])
norm_KL = np.linalg.norm(KL)
cos_ = KL.dot(U[1,:]) / norm_KL
sin_ = KL.dot(U[2,:]) / norm_KL
R = np.array([[1, 0, 0],
[0, cos_, sin_],
[0,-sin_, cos_]])
ROT = (U.T).dot(R.dot(U))
M = (K+L) / 2
K_rot = M + ROT.dot( K - M )
L_rot = M + ROT.dot( L - M )
print(L_rot)
print(K_rot)
print(L_rot-K_rot)
print((L_rot-K_rot).dot(U[2,:]))
一个更有灵感的解决方案是使用在运行时生成的程序网格,它具有我需要的所有要求:
- 连续沿多个顶点
- 轻松应用 UV 贴图进行纹理平铺
- 可以在运行时更新
- 不难compute/work用它
我正在尝试根据三角形在网格中的方向沿着人体 body 圆周对齐多条线 objects。我想把线平行于网格。我正确地分配了沿圆周的线的位置,但我还需要添加线的旋转,以便与 body 平行。 body是由多个三角形组成的网格,每条线都与一个三角形“相连”。
我只有:
每条线距离网格最近的三角形有 3 个点
三角形的法线
实例化线的位置(2 点,起点和终点)
我需要计算线的每个 X、Y、Z 轴的角度,以便三角形的法线与线网格垂直。我不知道如何获得所需的角度。如果有人愿意帮助我,我将不胜感激。
输入:
FVector 三角点[3];
F向量Triangle_Normal; //计算为(B-A)^(C-A),其中A,B,C为三角形的点
F向量线位置; //如果有帮助,我还有起始线和结束线位置
输出:
//FRotator旋转(x,y,z),使三角形法线与直线object垂直
圆周线建设概览。现在使用每条线的开始位置和结束位置计算旋转。当我们穿过网格的一些不规则部分时,我们希望正确地旋转线条。现在旋转是固定的,仅取决于行的开始和结束位置。
如果我没有正确理解你的目标,这里有一些相关的向量几何:
A,B,C are the vertices of the triangle:
A = [xA, yA, zA],
B = [xB, yB, zB]
C = [xC, yC, zC]
K,L are the endpoints of the line-segment:
K = [xK, yK, zK]
L = [xL, yL, zL]
vectors are interpreted as row-vectors
by . I denote matrix multiplication
by x I denote cross product of 3D vectors
by t() I denote the transpose of a matrix
by | | I denote the norm (magnitude) of a vector
Goal: find the rotation matrix and rotation transformation of segment KL
around its midpoint, so that after rotation KL is parallel to the plane ABC
also, the rotation is the "minimal" angle rotation by witch we need to
rotate KL in order to make it parallel to ABC
AB = B - A
AC = C - A
KL = L - K
n = AB x AC
n = n / |n|
u = KL x n
u = u / |u|
v = n x u
cos = ( KL . t(v) ) / |KL|
sin = ( KL . t(n) ) / |KL|
U = [[ u[0], u[1], u[2] ],
[ v[0], v[1], v[2] ],
[ n[0], n[1], n[2] ],
R = [[1, 0, 0],
[0, cos, sin],
[0, -sin, cos]]
ROT = t(U).R.U
then, one can rotate the segment KL around its midpoint
M = (K + L)/2
Y = M + ROT (X - M)
这里是python脚本版本
A = np.array([0,0,0])
B = np.array([3,0,0])
C = np.array([2,3,0])
K = np.array([ -1,0,1])
L = np.array([ 2,2,2])
KL = L-K
U = np.empty((3,3), dtype=float)
U[2,:] = np.cross(B-A, C-A)
U[2,:] = U[2,:] / np.linalg.norm(U[2,:])
U[0,:] = np.cross(KL, U[2,:])
U[0,:] = U[0,:] / np.linalg.norm(U[0,:])
U[1,:] = np.cross(U[2,:], U[0,:])
norm_KL = np.linalg.norm(KL)
cos_ = KL.dot(U[1,:]) / norm_KL
sin_ = KL.dot(U[2,:]) / norm_KL
R = np.array([[1, 0, 0],
[0, cos_, sin_],
[0,-sin_, cos_]])
ROT = (U.T).dot(R.dot(U))
M = (K+L) / 2
K_rot = M + ROT.dot( K - M )
L_rot = M + ROT.dot( L - M )
print(L_rot)
print(K_rot)
print(L_rot-K_rot)
print((L_rot-K_rot).dot(U[2,:]))
一个更有灵感的解决方案是使用在运行时生成的程序网格,它具有我需要的所有要求:
- 连续沿多个顶点
- 轻松应用 UV 贴图进行纹理平铺
- 可以在运行时更新
- 不难compute/work用它