0
\$\begingroup\$

I am making a custom mesh with the help of Traingulator script. At runtime, user can click and and generate a point then mesh creation start. Here is the way i am getting user input.

void Update()
    {
        RaycastHit hit;
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        if (Physics.Raycast(ray, out hit))
        {
            if (Input.GetMouseButtonDown(0))
            {
                GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
                sphere.transform.position = hit.point;
                sphere.transform.localScale = new Vector3(0.2f, 0.2f, 0.2f);
                positions.Add(sphere.transform.position);
                positionsVector2.Add(new Vector2(sphere.transform.position.x, sphere.transform.position.z));

                traingulatorTester.MeshMaker(positionsVector2.ToArray());
            }
        }

    }

And here is the mesh maker function that calling tringulator

 public void MeshMaker(Vector2[] vertices2D)
    {
        // Use the triangulator to get indices for creating triangles
        Triangulator tr = new Triangulator(vertices2D);
        int[] indices = tr.Triangulate();

        // Create the Vector3 vertices
        Vector3[] vertices = new Vector3[vertices2D.Length];
        for (int i = 0; i < vertices.Length; i++)
        {
            vertices[i] = new Vector3(vertices2D[i].x, vertices2D[i].y, 0);
        }

        // Create the mesh
        Mesh msh = new Mesh();
        msh.vertices = vertices;
        msh.triangles = indices;
        msh.RecalculateNormals();
        msh.RecalculateBounds();
        if (gameObject.GetComponent<MeshRenderer>() == null)
        {
            // Set up game object with mesh;
            gameObject.AddComponent(typeof(MeshRenderer));
            filter = gameObject.AddComponent(typeof(MeshFilter)) as MeshFilter;
        }
        filter.mesh = msh;

    }

Now the problem is the rotation of the custom mesh object is not correct look at this. enter image description here

Now I have manually set the rotation. enter image description here

\$\endgroup\$
2
  • \$\begingroup\$ Try vertices[i] = new Vector3(vertices2D[i].y, vertices2D[i].x, 0); or vertices[i] = new Vector3(vertices2D[i].x, 0, vertices2D[i].y); \$\endgroup\$ Jun 12, 2019 at 11:01
  • \$\begingroup\$ It slightly improved the orientation but still in the air and not well placed. \$\endgroup\$ Jun 12, 2019 at 11:05

1 Answer 1

1
\$\begingroup\$

Triangles, normals and other things seem fine in the images, so it must be a problem with vertices. you placed Z axis of spheres in Y axis of positionsVector2 so you have to put Y axis of positionsVector2 into Z axis of new vectors.

vertices[i] = new Vector3(vertices2D[i].x, 0, vertices2D[i].y);
\$\endgroup\$
10
  • \$\begingroup\$ Thanks it is very helpful but after adding multiple points the whole mesh become invisible as appear showing form bottom. \$\endgroup\$ Jun 12, 2019 at 11:11
  • \$\begingroup\$ @MuhammadFaizanKhan maybe not enough vertices for triangle generation? \$\endgroup\$ Jun 12, 2019 at 11:31
  • \$\begingroup\$ Vertex count should be Multiples of 3. You can add more vertices if you don't have enough vertices in MeshMaker method. \$\endgroup\$ Jun 12, 2019 at 11:33
  • \$\begingroup\$ but the mesh normal are rotated to 180. \$\endgroup\$ Jun 12, 2019 at 12:14
  • \$\begingroup\$ when i add more points the normal get inverted. \$\endgroup\$ Jun 12, 2019 at 12:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .