Пример #1
0
        void Start()
        {
            var sphe = new DtSphere {
                radius = sphereRadius
            };
            var cone = new DtCone {
                height = arrowHeight, topRadius = 0.001, bottomRadius = arrowConeBase, localPos = new Vector3(0, -arrowHeight, 0)
            };

            _aFrom = fun.meshes.CreateSphere(sphe).SetName("A_From").SetStandardShaderTransparentColor(0, 1, 0, 1).transform;
            _aTo   = fun.meshes.CreateCone(cone).SetName("A_To").SetStandardShaderTransparentColor(0, 1, 0, 1).transform;
            _bFrom = fun.meshes.CreateSphere(sphe).SetName("B_From").SetStandardShaderTransparentColor(0, 0, 1, 1).transform;
            _bTo   = fun.meshes.CreateCone(cone).SetName("B_To").SetStandardShaderTransparentColor(0, 0, 1, 1).transform;

            _cFrom = fun.meshes.CreateSphere(new DtSphere {
                radius = sphereRadius
            }).SetName("C_From").SetStandardShaderTransparentColor(1, 0, 1, 1).transform;
            _cTo = fun.meshes.CreateCone(new DtCone {
                height = arrowHeight / 3f, topRadius = 0.001, bottomRadius = arrowConeBase / 3f, localPos = new Vector3(0, -arrowHeight / 3f, 0)
            }).SetName("C_To").SetStandardShaderTransparentColor(1, 0, 1, 1).transform;

            _cFrom.position = V3(0, 0.5, 0);
            _aFrom.position = V3(0, 0, 0);
            _bFrom.position = V3(1, 0, 0);
            _aTo.position   = V3(0.4, 0, 0);
            _bTo.position   = V3(0.6, 0, 0);
        }
Пример #2
0
            public static GameObject CreateSphere(DtSphere dt)
            {
                Mesh m;
                var  gameObject = CreateGameObject("Sphere", dt, out m);

                var radius = (float)dt.radius;
                // Longitude |||
                int nbLong = dt.longitude;
                // Latitude ---
                int nbLat = dt.latitude;

                #region Vertices
                Vector3[] vertices = new Vector3[(nbLong + 1) * nbLat + 2];
                float     _pi      = Mathf.PI;
                float     _2pi     = _pi * 2f;

                vertices[0] = Vector3.up * radius;
                for (int lat = 0; lat < nbLat; lat++)
                {
                    float a1   = _pi * (float)(lat + 1) / (nbLat + 1);
                    float sin1 = Mathf.Sin(a1);
                    float cos1 = Mathf.Cos(a1);

                    for (int lon = 0; lon <= nbLong; lon++)
                    {
                        float a2   = _2pi * (float)(lon == nbLong ? 0 : lon) / nbLong;
                        float sin2 = Mathf.Sin(a2);
                        float cos2 = Mathf.Cos(a2);

                        vertices[lon + lat * (nbLong + 1) + 1] = new Vector3(sin1 * cos2, cos1, sin1 * sin2) * radius;
                    }
                }
                vertices[vertices.Length - 1] = Vector3.up * -radius;
                #endregion

                #region Normales
                Vector3[] normales = new Vector3[vertices.Length];
                for (int n = 0; n < vertices.Length; n++)
                {
                    normales[n] = vertices[n].normalized;
                }
                #endregion

                #region UVs
                Vector2[] uvs = new Vector2[vertices.Length];
                uvs[0] = Vector2.up;
                uvs[uvs.Length - 1] = Vector2.zero;
                for (int lat = 0; lat < nbLat; lat++)
                {
                    for (int lon = 0; lon <= nbLong; lon++)
                    {
                        uvs[lon + lat * (nbLong + 1) + 1] = new Vector2((float)lon / nbLong, 1f - (float)(lat + 1) / (nbLat + 1));
                    }
                }
                #endregion

                #region Triangles
                int   nbFaces     = vertices.Length;
                int   nbTriangles = nbFaces * 2;
                int   nbIndexes   = nbTriangles * 3;
                int[] triangles   = new int[nbIndexes];

                //Top Cap
                int i = 0;
                for (int lon = 0; lon < nbLong; lon++)
                {
                    triangles[i++] = lon + 2;
                    triangles[i++] = lon + 1;
                    triangles[i++] = 0;
                }

                //Middle
                for (int lat = 0; lat < nbLat - 1; lat++)
                {
                    for (int lon = 0; lon < nbLong; lon++)
                    {
                        int current = lon + lat * (nbLong + 1) + 1;
                        int next    = current + nbLong + 1;

                        triangles[i++] = current;
                        triangles[i++] = current + 1;
                        triangles[i++] = next + 1;

                        triangles[i++] = current;
                        triangles[i++] = next + 1;
                        triangles[i++] = next;
                    }
                }

                //Bottom Cap
                for (int lon = 0; lon < nbLong; lon++)
                {
                    triangles[i++] = vertices.Length - 1;
                    triangles[i++] = vertices.Length - (lon + 2) - 1;
                    triangles[i++] = vertices.Length - (lon + 1) - 1;
                }
                #endregion

                m.vertices  = vertices;
                m.normals   = normales;
                m.uv        = uvs;
                m.triangles = triangles;

                m.RecalculateBounds();
                ;

                return(gameObject);
            }