Unity3d Mesh (v) Draw the Circle

Source: Internet
Author: User

Objective:

The basic unit of mesh in Unity3d is a triangle, and the circle is made up of many triangles. Then we'll know. It takes two variables to draw a circular mesh: the radius of the circle and the number of splits;

First, the realization process

The basic process is similar to the previous one, the most basic is still the vertex and triangle index array, namely: we need to according to the radius of the circle and the number of pre-divided , through the algorithm to obtain: Vertex Vector3 array and corresponding triangle index array;

1, the Basic Program implementation Framework is as follows:

usingUnityengine; [Requirecomponent (typeof(Meshrenderer),typeof(Meshfilter))] Public classyuan:monobehaviour{ Public floatRadius =6;//radius     Public intsegments = -;//Split number    PrivateMeshfilter Meshfilter; voidStart () {Meshfilter= getcomponent<meshfilter>(); Meshfilter.mesh=Createmesh (Radius, segments); } Mesh Createmesh (floatRadiusintsegments) {Mesh Mesh=NewMesh (); returnmesh; }}

2, next need to implement the basic functions of the Createmesh () function (the following program contains the function of the implementation of the functions); The code for the entire program is as follows:

usingUnityengine; [Requirecomponent (typeof(Meshrenderer),typeof(Meshfilter))] Public classyuan:monobehaviour{ Public floatRadius =6;//radius     Public intsegments = -;//Split number    PrivateMeshfilter Meshfilter; voidStart () {Meshfilter= getcomponent<meshfilter>(); Meshfilter.mesh=Createmesh (Radius, segments); } Mesh Createmesh (floatRadiusintsegments) {        //vertices:        intVertices_count = segments +1; Vector3[] vertices=NewVector3[vertices_count]; vertices[0] =Vector3.zero; floatAngledegree =360.0f; floatAnglerad = Mathf.deg2rad *Angledegree; floatAnglecur =Anglerad; floatAngledelta = Anglerad/segments;  for(intI=1;i< Vertices_count; i++)        {            floatCosA =Mathf.cos (anglecur); floatSinA =Mathf.sin (anglecur); Vertices[i]=NewVector3 (Radius * CosA,0, Radius *SinA); Anglecur-=Angledelta; }        //Triangles        intTriangle_count = segments *3; int[] Triangles =New int[Triangle_count];  for(intI=0, vi=1; i<= triangle_count-1; i+=3, vi++)//because the case splits 60 triangles, the last index order should be: 0 60 1; So it needs to be handled separately.{Triangles[i]=0; Triangles[i+1] =VI; Triangles[i+2] = VI +1; } Triangles[triangle_count-3] =0; Triangles[triangle_count-2] = Vertices_count-1; Triangles[triangle_count-1] =1;//to complete the closed loop, lift the last triangle alone.//UV:vector2[] Uvs =NewVector2[vertices_count];  for(inti =0; i < Vertices_count; i++) {Uvs[i]=NewVector2 (Vertices[i].x/radius/2+0.5f, Vertices[i].z/radius/2+0.5f); }        //load properties and meshMesh mesh =NewMesh (); Mesh.vertices=vertices; Mesh.triangles=triangles; Mesh.uv=Uvs; returnmesh; }}

3.:

Little knowledge of the encounter:

"Welcome to Reprint"

Reprint please indicate source: le study

Unity3d Mesh (v) Draw the Circle

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.