0
\$\begingroup\$

I'm a beginner with Unity, but I know how to code. I'm open to tutorials, of course.

Ideally, I would like to code spacial vertices and connect them into a simple polygon. The polygon would then be animated by changing the vertices over time.

\$\endgroup\$
1

1 Answer 1

0
\$\begingroup\$

As a Unity programmer, you usually do not work with polygons directly.

The usual way to create and animate 3d objects in Unity is not to do that with code. You usually do that by creating them in a 3d modeling program like Maya or Blender. These programs also allow you to define animations for the model. When you import the model into Unity, you can also import its animations. You can then trigger these animations through the Unity Animation System. This approach is both more design-friendly (3d modeling programs offer you WYSIWYG editing instead of just typing numbers and hoping they look good) and more performance-friendly (the deformation logic can be processed on the GPU).

But if you really want to create and manipulate 3d geometry in code (there are some edge-cases where this is indeed useful, like procedural generation for example), check out the class Mesh. It represents the geometry used by a MeshFilter component.

You can either create an entirely new mesh:

    Mesh mesh = new Mesh();
    GetComponent<MeshFilter>().mesh = mesh;

Or you can obtain the reference to an existing mesh

    Mesh mesh =  GetComponent<MeshFilter>().mesh;

You can then manipulate that mesh:

    mesh.vertices = /* Vector3[] of polygon corners */;
    mesh.uv = /* Vector2[] of the UV texture coordinates for the polygon corners */;
    mesh.triangles = /* int[] of indexes in the vertices array. Every 3 entries define one polygon */

When you are finished manipulating the mesh, you might need to update a couple precomputed values. Depending on what you changed and what kind of material you use, not all three of these might be necessary:

    mesh.RecalculateBounds(); /* important for culling calculation */
    mesh.RecalculateNormals(); /* required by some shaders */
    mesh.RecalculateTangents(); /* required by some shaders */
\$\endgroup\$
1
  • \$\begingroup\$ @TomTitaniumLento Is that a question? \$\endgroup\$
    – Philipp
    Commented Apr 17, 2020 at 17:42

You must log in to answer this question.

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