#So the first point in my Vertices array

1 messages · Page 1 of 1 (latest)

honest wyvern
#

indeed. If you will be reusing the center point, the code for adding the triangles is in fact correct

graceful quest
#

So if I change it to

for (int i = 0; i < contourPoints.Count; i++)
        {
            //The points are 2D so we are using the Z position of the provided center point for our vertices
            if (i == 0)
                vertices[i] = centerPosition;
            else
                vertices[i] = new Vector3(contourPoints[i].x, contourPoints[i].y, 0f);
        }      
...

int[] triangles = new int[(contourPoints.Count - 2) * 3];
        for (int i = 0; i < contourPoints.Count - 2; i++)
        {
            triangles[i * 3] = 0;
            triangles[i * 3 + 1] = i + 1;
            triangles[i * 3 + 2] = i + 2;
        }

should be fine ?

#

sorry for the silly questions but it is the first time creating a mesh

#

@honest wyvern the result looks like this, only one of them has depth

honest wyvern
#

It's hard to tell what's wrong without seeing the raw data

#

What often helps is to draw out the shape you want on paper (may be simplified) and think about how it should be stored

#

Are they supposed to be cones?

graceful quest
#

I use the data of some Json files. It is for a project. But they haven't defined what the shapes should look like. In my understanding they should be circular. This is an example file

#

The XYZ is the center point, and for some reason they say that
x corresponds to Unity’s X axis
y corresponds to Unity’s Z axis
z corresponds to Unity’s Y axis

honest wyvern
graceful quest
#

Comparing the 3 files, the one that has different depth than the others, also has negtive Z so this should be normal for the created meshes

honest wyvern
#

Since none of the contour points have a z defined, and you're setting it to center position, i would not expect any depth within the object to be honest (they should be flat discs)

#

But when you're creting the vertices (vertices[i] = new Vector3(..)) you're not actually converting the axes, even though you are doing that for the centerPosition

graceful quest
#

I am converting them here

Vector3 centerPosition = new Vector3(data.x, data.z, data.y);
        objectBehaviour.GenerateMesh(data.object_id,data.contour_points,centerPosition);
honest wyvern
#

so what happens if you change that line to new Vector3(contourPoints[i].x, data.z, contourPoints[i].y)

#

(i'm using data.z, if you want to use centerPosition it should be centerPosition.y

honest wyvern
#

not all the contour points as well

#

their y is unity's z

graceful quest
#

So, when I am calling the function to generate the meshes it is like this (inverting the axis):

Vector3 centerPosition = new Vector3(data.x, data.z, data.y);
        objectBehaviour.GenerateMesh(data.object_id,data.contour_points,centerPosition);

and then when I am creating the vertices the code is

 Vector3[] vertices = new Vector3[contourPoints.Count];
        for (int i = 0; i < contourPoints.Count; i++)
        {
            //The points are 2D so we are using the Z position of the provided center point for our vertices
            if (i == 0)
                vertices[i] = centerPosition;
            else
                vertices[i] = new Vector3(contourPoints[i].x, contourPoints[i].y, centerPosition.z);
        }      

They appear 2D now, but he Z position of the GameObect appears wrong

#

The second script is part of the GenerateMesh function

honest wyvern
#

Okay this looks like progress, nice. There still one issue: When you're adding the vertices, it should contain all the contour points plus the center point. You shouldn't be adding the center point in the loop, that will make you lose the first contour point:

Vector3[] vertices = new Vector3[contourPoints.Count + 1]; // Add 1 because we also want the center point
vertices[0] = centerPoint;
for (int i = 0; i < contourPoints.Count; i++) {
  // Here we convert to Unity's coordinate space
  Vector3 convertedPoint = new Vector3(contourPoints[i].x, centerPoint.z, contourPoints[i].y);

  // use i + 1 because we have that center point sitting at index 0; se we need to offset everything by 1
  vertices[i + 1] = convertedPoint;
}
#

could you also post the entire GenerateMesh function as you have it now? It seems you've changed a few things since your original post

graceful quest
honest wyvern
#

To understand that the mesh is offset from the origin of the gameobject, you need to understand how the positions of the vertices of the mesh are relative to the position of the gameobject. I hope I can explain it clearly:

  • If your gameobject is at position (0, 0, 0), then if you copy all the data in your vertices to your mesh, all your vertices will appear exactly in the place that was in the original file
  • Your file provides a (x, y, z) coordinate for the object (that's the data.x, data.y, data.z you are using). We can interpret this as the position of the entire object, in other words, as your transform.position.
  • Sometimes, especially in mesh data, some points are implied. I think your file doesn't necessarily provide a center point, as much as an object position. All your contour points are relative to this position. That means that the origin for the object is implied. This is essentially a vertex at position (0,0,0).
    What I'm getting at is that I suspect your data is like this. Instead of using the center point as a separate vertex with position (data.x, data.z, data.y), you actually need a center point with position (0,0,0) and then you place all your vertices around that.
graceful quest
#
public void GenerateMesh(int objectID, List<Point> contourPoints, Vector3 centerPosition)
    { 
       
        Debug.Log("Generating Mesh...");

        Mesh mesh = new Mesh()
        {
            name = "Mesh"+ objectID.ToString()
        };

        GetComponent<MeshFilter>().mesh = mesh;

        Vector3[] vertices = new Vector3[contourPoints.Count+1];
        vertices[0] = centerPosition;
        for (int i = 0; i < contourPoints.Count; i++)
        {
            vertices[i+1] = new Vector3(contourPoints[i].x, centerPosition.z , contourPoints[i].y);
        }      

        //Creating triangles from the provided vertices, assuming the contour points form a closed loop
        int[] triangles = new int[(contourPoints.Count - 2) * 3];
        for (int i = 0; i < contourPoints.Count - 2; i++)
        {
            triangles[i * 3] = 0;
            triangles[i * 3 + 1] = i + 1;
            triangles[i * 3 + 2] = i + 2;
        }

        Vector2[] uvs = new Vector2[contourPoints.Count+1];
        for (int i = 0; i < uvs.Length; i++)
        {
            uvs[i] = new Vector2(vertices[i].x, vertices[i].y);
        }

        mesh.vertices = vertices;
        mesh.triangles = triangles;
        mesh.uv = uvs;
        mesh.bounds = new Bounds(centerPosition, mesh.bounds.size);
       

        mesh.RecalculateNormals();
        mesh.RecalculateBounds();
#

This is the result from the code

honest wyvern
#

Yes this looks as I expect it to!

#

There's a triangle missing, i can tell you why but ill challenge you to figure it out yourself first

graceful quest
#

The strange this is that only for this object the Center is very off

honest wyvern
graceful quest
# honest wyvern There's a triangle missing, i can tell you why but ill challenge you to figure i...

Yeah I noticed that. I ve just change the triangles function to this and I am not getting a missing triangle anymore

//Creating triangles from the provided vertices, assuming the contour points form a closed loop
        int[] triangles = new int[(contourPoints.Count - 1) * 3];
        for (int i = 0; i < contourPoints.Count - 1; i++)
        {
            triangles[i * 3] = 0;
            triangles[i * 3 + 1] = i + 1;
            triangles[i * 3 + 2] = i + 2;
        }
#

certainly a progress 😄

honest wyvern
graceful quest
#

since we get one more point

#

in the vertices

honest wyvern
#

that's only sort of right, because you are not sampling the vertices array, but rather the contourpoints (this one doesn't have that extra vertex)

#

actually I'm not sure why this works...

graceful quest
# honest wyvern To understand that the mesh is offset from the origin of the gameobject, you nee...

Ok so regarding the offset, I changed the center to be 0,0,0 and the objects are flat again. I will share with you the description. I think that the center of the object, should be the position that I should instantiate the object, rather than the center of the mesh from which I draw the triangles


JSON info Details:
Three JSON files are included so we have 3 different kinds of objects.
object_id: The generated ID of the object.
(x,y,z): The calculated position of the spawned object’s center. 
contour_points: List of points that describe the perimeter of the object.  
velocity: The object’s movement velocity m/s 
Warning:  
x corresponds to Unity’s X axis
y corresponds to Unity’s Z axis
z corresponds to Unity’s Y axis
honest wyvern
#

the center of the mesh should just be a zero vertex (Vector3.zero)

#

but you still need it to draw the triangles

graceful quest
honest wyvern
#

Exactly, nice

#

I'm breaking my head over the triangles to the point where I performed the loop on paper, but I think i'm making wrong assumptions about the data you actually use (after interpreting the json) because the loop like that shouldn't be creating enough triangles

graceful quest
honest wyvern
#

consider it like this: each contour point forms a triangle with itself, the center of the object, and the next contour point. That means if you have say 5 points, there should be 5 triangles. in other words, if contourPoints.Count = 5, then triangles.Count = 5*3 = 15, and vertices.Count = 6 because of the extra center point

#

so your loop should run to contourPoints.Count (5 triangles) and not to contourPoints.Count - 1 (4 triangles)

graceful quest
#

If i do that I get

#

Failed setting triangles. Some indices are referencing out of bounds vertices. IndexCount: 1014, VertexCount: 339

honest wyvern
#

With the caveat being that running the loop itself should add 4 triangles and the last should be added manually because you need a different algo for the last one as it loops back around to the first vertex

#

for 5 tris you should get the following indices:

#
[
  tri | indices
  --- | -------
  0   | 0 1 2
  1   | 0 2 3
  2   | 0 3 4
  3   | 0 4 5
  4   | 0 5 1
]```
as you can see the first 4 follow the pattern of 0, i + 1, i + 2, but the last is different so it can't be done by the same loop
graceful quest
#

I ll try and see the result

honest wyvern
#

( the error you get is the mesh system trying to get vertex 6 from the list, because you added a triangle with 0, 5, 6 to the tri list, but 6 is out of bounds for the vertex array of length 6 (indices 0-5)

graceful quest
#

I think i found why the script was working before

graceful quest
#

@honest wyvern Anyway, I think I m going to move on with the other tasks of the exercice. Thank you very much for your valuable help ! You cant imagine how much I appreciate it! DO you have a patreon or something to get you a beer or some coffee ?

honest wyvern
#

Best of luck with the rest of the implementation!

graceful quest