英文:
Controlling the Transfromation Properties of a 3D Object With a Set Of Points
问题
允许我详细解释一下,我在Python中对帧进行预处理,并提取一个大致对应于吉他品板的矩形区域。
这个过程非常顺利,不过我还想让我的3D吉他模型与通过Python获得的标记点相匹配,这意味着它应该根据这些点的位置、缩放和旋转来自动调整自己的位置。
我应该如何实现这一点呢?
我对Unity相对陌生,坦白说,我不知道如何实现这样的功能。有没有人以前做过类似的事情?
英文:
Allow me to elaborate, so I preprocess frames in python and extract a rectangle which roughly corresponds to the fretboard of the guitar.
I then send this position information to Unity to visualize it there.
Which works charmingly, though I also want to make my 3D guitar model fit the marked dots to the dots obtained through python, meaning that it should position, scale and rotate itself according to these points.
How can I achieve this?
I am fairly new to Unity and frankly have no idea how I can achieve such a feat. Has anyone done something similar before?
答案1
得分: 2
这是您想要的理想设置。您有两个点X和Y作为目标,还有吉他上的参考点A和B。O是质心的位置。
首先,设置您的比例。
// dist(X,Y) = scale * dist(A,B)
float scale = Vector3.Distance(posX,posY) / Vector3.Distance(posA, posB);
transform.localScale = Vector3.Scale(baseScale, scale);
您必须知道吉他的基础(初始)比例。通常情况下是 1,1,1
,但在某些情况下(例如从Blender导入时),它可能是类似于 100,100,100
的东西,所以请确保您有正确的值。
接下来,您需要旋转吉他。我们可以简单地修改吉他的 transform.up
。
Vector3 delXY = posY - posX;
transform.up = delXY.normalized;
最后,您需要将吉他移动到所需的位置。我们希望A
与X
重合,B
与Y
重合。让我们为A
解决。
Vector3 movementVector = posX - posA;
transform.Translate(movementVector, Space.World);
现在,吉他应该与矩形对齐。
需要注意的几个事项:
- 变量
posX
、posY
、posA
和posB
必须处于世界坐标系中。 - 我们假设矩形与吉他前部位于同一平面上。
这就是我要说的全部。祝您的项目顺利。希望这对您有所帮助!
英文:
This is the ideal setup you want. You have two points X and Y as your targets, and you have reference points A and B on the guitar. O is the location of the centre of mass
First, set your scale.
// dist(X,Y) = scale * dist(A,B)
float scale = Vector3.Distance(posX,posY) / Vector3.Distance(posA, posB);
transform.localScale = Vector3.Scale(baseScale, scale);
You must know the base (initial) scale of the guitar. This is usually 1,1,1
but in some cases (like when importing from Blender) it tends to be something like 100,100,100
so just make sure you have the right values.
Next, you need to rotate the guitar. We can simply modify the guitar's transform.up
.
Vector3 delXY = posY - posX;
transform.up = delXY.normalized;
Finally, you need to move your guitar to the desired position. We want A
to coincide with X
and B
with Y
. Let us just solve for A
.
Vector3 movementVector = posX - posA;
transform.Translate(movementVector, Space.World);
The guitar should now be aligned with the rectangle.
A few things to note:
- The variables
posX
,posY
,posA
, andposB
must be in world space - We are assuming that the rectangle is on the same plane as the front of the guitar
That's all from me. All the best for your project. I hope this helped!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论