控制3D对象的变换属性,使用一组点。

huangapple go评论73阅读模式
英文:

Controlling the Transfromation Properties of a 3D Object With a Set Of Points

问题

允许我详细解释一下,我在Python中对帧进行预处理,并提取一个大致对应于吉他品板的矩形区域。控制3D对象的变换属性,使用一组点。

然后,我将这个位置信息发送到Unity中进行可视化。
控制3D对象的变换属性,使用一组点。

这个过程非常顺利,不过我还想让我的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.控制3D对象的变换属性,使用一组点。

I then send this position information to Unity to visualize it there.
控制3D对象的变换属性,使用一组点。

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.
控制3D对象的变换属性,使用一组点。

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

控制3D对象的变换属性,使用一组点。

这是您想要的理想设置。您有两个点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;

最后,您需要将吉他移动到所需的位置。我们希望AX重合,BY重合。让我们为A解决。

Vector3 movementVector = posX - posA;
transform.Translate(movementVector, Space.World);

现在,吉他应该与矩形对齐。


需要注意的几个事项:

  • 变量 posXposYposAposB 必须处于世界坐标系中。
  • 我们假设矩形与吉他前部位于同一平面上。

这就是我要说的全部。祝您的项目顺利。希望这对您有所帮助!

英文:

控制3D对象的变换属性,使用一组点。

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, and posB 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!

huangapple
  • 本文由 发表于 2023年7月27日 23:16:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76781201.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定