Objective:
Drawing a circle, it is expected to draw the geometry associated with it, in order to more flexibly grasp the mechanism and method of mesh dynamic creation model.
First, Analysis:
First, draw the ring in conjunction with the process of drawing the circle:
The circle is composed of segments a isosceles triangle (in the previous article, the circle is divided into segments parts, that is, segments isosceles triangle), the ring is a segments isosceles trapezoidal composition.
So what is the isosceles trapezoid made of? Two triangles (many are also possible).
Therefore: Add a variable, inner circle radius. that is : to this moment, we need to base on: the outer radius, the inner radius, the number of splits ( of course, if it is a fan or sector, we also need an angle angle) to determine the vertex vertices Vector3 array, and a triangles triangle index array.
Second, draw the ring
The code is as follows:
usingUnityengine; [Requirecomponent (typeof(Meshrenderer),typeof(Meshfilter))] Public classyuan:monobehaviour{ Public floatRadius =6;//Outer radius Public floatInnerradius =3;//Inner radius Public floatAngledegree = the;//the angle of a fan or sector Public intsegments = -;//Split number PrivateMeshfilter Meshfilter; voidStart () {Meshfilter= getcomponent<meshfilter>(); Meshfilter.mesh=Createmesh (Radius, Innerradius, Angledegree, segments); } Mesh Createmesh (floatRadiusfloatInnerradius,floatAngledegree,intsegments) { //vertices (vertex): intVertices_count = segments*2+2;//because the number of vertices (vertices) must match the triangles (number of index triangle vertices)vector3[] vertices =NewVector3[vertices_count]; floatAnglerad = Mathf.deg2rad *Angledegree; floatAnglecur =Anglerad; floatAngledelta = Anglerad/segments; for(intI=0;i< Vertices_count; i+=2) { floatCosA =Mathf.cos (anglecur); floatSinA =Mathf.sin (anglecur); Vertices[i]=NewVector3 (RADIUS * CosA,0, RADIUS *SinA); Vertices[i+1] =NewVector3 (Innerradius * CosA,0, Innerradius *SinA); Anglecur-=Angledelta; } //Triangles: intTriangle_count = segments *6; int[] Triangles =New int[Triangle_count]; for(intI=0, vi=0; i<triangle_count;i+=6, vi+=2) {Triangles[i]=VI; Triangles[i+1] = vi+3; Triangles[i+2] = VI +1; Triangles[i+3] =vi+2; Triangles[i+4] =vi+3; Triangles[i+5] =VI; } //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; }}
:
Third, draw sector
To actually draw a fan face: Just change the parameters in the script, the angle of the sector coverage, namely:
Angledegree
The effect is as follows:
Four, draw sector
Similarly, you only need to change the inner radius to 0, changing the angle of the sector cover
innerradius=0;
Angledegree
Can! As follows:
Summarize:
Quiet heart, slowly analysis, very simple, but junior high school geometry.
"Welcome to Reprint"
Reprint please indicate source: le study
Unity3d Mesh (vi) Draw sector, fan, ring