Ejemplo n.º 1
0
    public void Generate(NodeMeshData data)
    {
        datas = data;
        trans = transform;

        var Filter = gameObject.AddComponent <MeshFilter>();

        Filter.mesh = new Mesh();
        rend        = gameObject.AddComponent <MeshRenderer>();

        var   vertices  = new Vector3[((int)datas.MeshSize.x + 1) * ((int)datas.MeshSize.y + 1)];
        var   triangles = new int[(int)datas.MeshSize.x * (int)datas.MeshSize.y * 6];
        var   uv        = new Vector2[vertices.Length];
        var   normal    = new Vector3[vertices.Length];
        float longitude;
        float latitude;

        for (int i = 0, y = 0; y <= (int)datas.MeshSize.y; y++)
        {
            for (int x = 0; x <= (int)datas.MeshSize.x; x++, i++)
            {
                if (datas.level == 0)
                {
                    longitude = CoordinatesProjector.GetLongitude(x, (int)data.MeshSize.x) * (Mathf.PI / 180.0f);
                    latitude  = CoordinatesProjector.GetLatitude(y, (int)data.MeshSize.y) * (Mathf.PI / 180.0f);
                }
                longitude = CoordinatesProjector.GetLongitudeFromPositions(x, (int)datas.MeshSize.x, datas.BeginCoordinates.x, datas.LengthCoordinates.x) * (Mathf.PI / 180.0f);
                latitude  = CoordinatesProjector.GetLatitudeFromPositions(y, (int)datas.MeshSize.y, datas.BeginCoordinates.y, datas.LengthCoordinates.y) * (Mathf.PI / 180.0f);

                vertices[i] = CoordinatesProjector.InverseMercatorProjector(longitude, latitude, 1f);//new Vector3(x,0, y);

                uv[i]     = new Vector2((float)x / (int)datas.MeshSize.x, (float)y / (int)datas.MeshSize.y);
                normal[i] = vertices[i] - transform.position;
            }
        }

        Filter.mesh.vertices = vertices;
        Filter.mesh.uv       = uv;
        Filter.mesh.normals  = normal;

        for (int ti = 0, vi = 0, y = 0; y < (int)datas.MeshSize.y; y++, vi++)
        {
            for (int x = 0; x < (int)datas.MeshSize.x; x++, ti += 6, vi++)
            {
                triangles[ti]     = vi;
                triangles[ti + 3] = triangles[ti + 2] = vi + 1;
                triangles[ti + 4] = triangles[ti + 1] = vi + (int)datas.MeshSize.x + 1;
                triangles[ti + 5] = vi + (int)datas.MeshSize.x + 2;
            }
        }
        Filter.mesh.triangles = triangles;
        MeshCenter            = vertices[vertices.Length / 2];
    }
Ejemplo n.º 2
0
            public Dictionary <long, NodeMeshData> Compile()
            {
                Dictionary <long, NodeMeshData> mesh = new Dictionary <long, NodeMeshData>();

                //calculate offset and rotation matrix
                var matrix = Mat4.Identity;

                if (parent != null || children.Count > 0)
                {
                    Mat4.Translate(matrix, matrix, offset);
                }

                Mat4.Multiply(matrix, matrix, Mat4.FromMat3(mat3, null));

                //generate new vertices
                for (int i = 0; i < vertices.Count; i++)
                {
                    float x = vertices[i].x;
                    float y = vertices[i].y;
                    float z = vertices[i].z;

                    //(vec3)vert = (mat4)modelViewMat * (vec3)vertices[i];
                    Vector3 transformed = new Vector3
                    {
                        x = matrix[0] * x + matrix[4] * y + matrix[8] * z + matrix[12],
                        y = matrix[1] * x + matrix[5] * y + matrix[9] * z + matrix[13],
                        z = matrix[2] * x + matrix[6] * y + matrix[10] * z + matrix[14]
                    };

                    vertices[i] = transformed;
                }

                //initialize buffer
                for (int i = 0; i < textures.Length; i++)
                {
                    //initialize mesh for texture i
                    mesh[textures[i]] = new NodeMeshData
                    {
                        node = this
                    };
                }

                GenerateMesh(vertices, mesh);

                return(mesh);
            }