1

I have a Quad whose vertices I'm printing like this:

public MeshFilter quadMeshFilter;
for(var vertex in quadMeshFilter.mesh.vertices)
{
  print(vertex);
}

And, the localScale like this:

public GameObject quad;
print(quad.transform.localScale);

Vertices are like this:

(-0.5, -0.5), (0.5, 0.5), (0.5, -0.5), (-0.5, 0.5)

while the localScale is:

(6.4, 4.8, 0)

How is this possible - because the vertices make a square but localScale does not.

How do I use vertices and draw another square in front of the quad?

Faizuddin Mohammed
  • 4,118
  • 5
  • 27
  • 51

2 Answers2

2

I am not well versed in the matters of meshes, but I believe I know the answer to this question.

Answer

How is this possible

Scale is a value which your mesh is multiplied in size by in given directions (x, y, z). A scale of 1 is default size. A scale of 2 is double size and so on. Your localSpace coordinates will then be multiplied by this scale.

Say a localSpace coordinate is (1, 0, 2), the scale however, is (3, 1, 3). Meaning that the result is (1*3, 0*1, 2*3).

How do I use vertices and draw another square in front of the quad?

I'd personally just create the object and then move it via Unity's Transform system. Since it allows you to change the worldSpace coordinates using transform.position = new Vector3(1f, 5.4f, 3f);

You might be able to move each individual vertex in WorldSpace too, but I haven't tried that before.

I imagine it is related to this bit of code though: vertices[i] = transform.TransformPoint(vertices[i]); since TransformPoint converts from localSpace to worldSpace based on the Transform using it.


Elaboration

Why do I get lots of 0's and 5's in my space coordinates despite them having other positions in the world?

If I print the vertices of a quad using the script below. I get these results, which have 3 coordinates and can be multiplied as such by localScale.

Print result:

Quad vertices

Script:

Mesh mesh = GetComponent<MeshFilter>().mesh;
var vertices = mesh.vertices;
Debug.Log("Local Space.");
foreach (var v in vertices)
{
   Debug.Log(v);
}

This first result is what we call local space.

There also exists something called WorldSpace. You can convert between local- and worldSpace.

localSpace is the objects mesh vertices in relation to the object itself while worldSpace is the objects location in the Unity scene.

Then you get the results as seen below, first the localSpace coordinates as in the first image, then the WorldSpace coordinates converted from these local coordinates.

world- and localSpace vertices of quad

Here is the script I used to print the above result.

Mesh mesh = GetComponent<MeshFilter>().mesh;
var vertices = mesh.vertices;
Debug.Log("Local Space.");
foreach (var v in vertices)
{
    Debug.Log(v);
}
Debug.Log("World Space");
for (int i = 0; i < vertices.Length; ++i)
{
    vertices[i] = transform.TransformPoint(vertices[i]);
    Debug.Log(vertices[i]);
}

Good luck with your future learning process.

Doh09
  • 2,324
  • 1
  • 16
  • 31
  • I'm actually trying to use the vertices from OpenCV `Homography` and `PerspectiveTransform` to find an object in an image and put a Unity's plane on that object. Do you have any idea how to do this? – Faizuddin Mohammed Mar 17 '18 at 10:23
  • 1
    I have a new question here: https://stackoverflow.com/questions/49334958/how-to-use-opencvs-homography-for-a-target-and-position-camera-in-unity – Faizuddin Mohammed Mar 17 '18 at 10:33
  • I'll take a look and answer if I feel I can contribute. – Doh09 Mar 17 '18 at 10:34
2

This becomes clear once you understand how Transform hierarchies work. Its a tree, in which parent transform [3x3] matrix (position, rotation, scale (rotation is actually a quaternion but lets assume its euler for simplicity so that math works). by extension of this philosophy, the mesh itself can be seen as child to the gameoobject that holds it.

If you imagine a 1x1 quad (which is what is described by your vertexes), parented to a gameobject, and that gameobject's Transform has a non-one localScale, all the vertexes in the mesh get multiplied by that value, and all the positions are added.

now if you parent that object to another gameObject, and give it another localScale, this will again multiply all the vertex positions by that scale, translate by its position etc.

to answer your question - global positions of your vertexes are different than contained in the source mesh, because they are feed through a chain of Transforms all the way up to the scene root.

This is both the reason that we only have localScale and not scale, and this is also the reason why non-uniform scaling of objects which contain rotated children can sometimes give very strange results. Transforms stack.

zambari
  • 4,797
  • 1
  • 12
  • 22