Unity Implements 3D Pie chart

Source: Internet
Author: User

For some time did not play unity, or take a break to pick up and toss a bit. Recently upgraded to 4.6, the new version has been added to Unity's own unity system and looks much fresher. In the absence of Ugui days, it is good to have ngui with nature, but good and bad are relative, believe that unity official to come to the new UI system can be more excellent, easy to use.

For the new UI system, many of the online Daniel has been out of a series of tutorials, I do not have too much energy to ponder. Hopefully, you'll be able to delve deeper into something, rather than floating on the surface. In a word, I have several bodies from Daniel, the revolution has not been successful, comrades need to work hard.

In the past, the idea of unity developing a variety of quirky gadgets, such as making 3D charts, looks cool. Although the self-feeling executive power is not bad, but sometimes still a little lazy, helpless graduate student life ~ ~ Yesterday saw a buddy made a 3D chart blog, using Linerenderer to draw a line chart, feel good. So I was determined to write the power of this blog.

/************************************* Split Line *************************************/

OK, here's the start.

First


First of all, a pie chart is actually dividing a cylinder into several parts in proportion. Each part is a sector-shaped area.

And then, I'm going to divide the whole column roughly into 100 triangular areas as shown (actually three-dimensional, where the triangular area is called a primitive).

Why is it roughly 100 parts, because we may encounter a portion of a fan page is a fraction of the case, then in this case, I will have a fractional region divided into two, the respective calculation of the ratio, and then the proportion of the scale.

After that, we calculated the sum of the data, the ratio of each fan page, the number of primitives needed, the rotation angle of each primitive, and the same color for the primitives within the same fan page, based on the statistical data.

Finally, the ratio is displayed above each fan page.

Primitive


Several sectors:




In front of it, said the approximate process of implementation. The overall implementation is relatively simple, but also encountered some small problems.

The 1.unity comes with a model of a sandwich shape that is not similar to a primitive in 3D gameobject. But I do not want to go from the outside into a model, so simply use the code to generate a good. Use a clumsy method to set the vertices and triangles of the mesh yourself. The key code is as follows.

    <summary>///Initialize grid///</summary> void Initmesh () {Float y = Mathf.pi * CONFIG.R        /Config.num;        float x = mathf.sqrt (CONFIG.R * config.r-y * y);        Vector3 origin1 = new Vector3 (0, 0, 0);        Vector3 origin2 = new Vector3 (x, 0, y);        Vector3 origin3 = new Vector3 (x, 0,-y);        Vector3 offset = new Vector3 (0, config.height, 0);        mesh = new mesh ();                                         Mesh.vertices = new Vector3[6] {origin1 + offset, origin2 + offset, origin3 + offset,        Origin1-offset, Origin2-offset, origin3-offset}; Mesh.triangles = new Int[24] {0,1,2,3,5,4, 0,3,2,2,3,5, 0,1        , 3,4,3,1, 2,1,4,2,4,5};        Meshfilter = gameobject.addcomponent<meshfilter> ();        Meshfilter.mesh = mesh;        Meshrenderer = gameobject.addcomponent<meshrenderer> (); MeshRenderer.material.shader = ShAder.    Find ("diffuse"); }

2. Because each of our primitives here is 1%, there may be 20.5% of such data in the proportion of a fan page, which requires special handling.

    <summary>    ///Create a set of fanbase    ///Start and end parts Special handling///    because it may not be an integer, there will be scaling/    //</summary>    private void Initfanbaselist ()    {        float Beginceil = Mathf.ceil (begin);        float Endfloor = Mathf.floor (end);        Createfanbase (Beginceil + begin)/2-0.5f, beginceil-begin);        for (int i = (int) mathf.ceil (begin), I < (int) Mathf.floor (end), i++)        {            createfanbase (I, 1f);        }        Createfanbase (end + Endfloor)/2-0.5f, End-endfloor);    }

3. The last small problem is that the stereoscopic text shows that in the new version of Unity, I can't find the 3D text.

So, like the following, you create a separate gameobject, plus the Textmesh and Meshrenderer components can display three-dimensional text.



In short, the implementation is still relatively simple. The source code is saved in the cloud disk, need friends can see. (may have compatibility issues with older versions)

Http://pan.baidu.com/s/16g4nc


Unity Implements 3D Pie chart

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.