goglinet.blogg.se

Circles in rectangle illusion
Circles in rectangle illusion










circles in rectangle illusion circles in rectangle illusion

If we attach the script to an empty GameObject, we should see an I-shaped polygon with lots of colors ( see above). Note that it is a good practice to call mesh.RecalculateBounds() and mesh.RecalculateNormals() to ensure the mesh behaves as it should. The last step is to create the mesh from the vertices, triangles, and color data and apply the mesh to a MeshFilter to draw it. Here they are initialized to random colors with Random.ColorHSV() using Linq syntax. The next step involves creating an array of colors, one for each vertex. There’s nothing too fancy here just give the object a list of points and it will give back the indices of each triangle. I copied the well-suited Triangulator.cs class specified in the Unit圓D wiki and dropped it in the project. To find the triangles in a 2D polygon’s list of points, we can use a triangulation algorithm. A small complication is that Unity wants an array of Vector3s, so we need to convert them. Specifying vertices is easy, as we can list them in an array of Vector2s. To display the mesh, we apply it to a GameObject’s MeshFilter component.īelow is a script that can be added to any empty object to draw the shape in the image above.

circles in rectangle illusion

Unity allows each vertex to be colored differently, so we will demonstrate this by feeding our mesh random colors. To draw a mesh programmatically, Unity requires an array of its vertices and triangles. This tutorial will implement drawing rectangles, circles, and triangles. So let’s do the same thing in Unity! Conveniently, Unity has excellent support for 2D physics, so implementing a basic prototype is fairly simple to do.












Circles in rectangle illusion