/// <inheritdoc />
        public void BuildMesh(Tile tile, Mesh mesh)
        {
            var gameObject = new GameObject(mesh.Name);

            var uMesh = new UnityEngine.Mesh();
            uMesh.vertices = mesh.Vertices;
            uMesh.triangles = mesh.Triangles;
            uMesh.colors = mesh.Colors;
            uMesh.uv = new Vector2[mesh.Vertices.Length];

            uMesh.RecalculateNormals();

            gameObject.isStatic = true;
            gameObject.AddComponent<MeshFilter>().mesh = uMesh;
            gameObject.AddComponent<MeshRenderer>().sharedMaterial =
                _customizationService.GetSharedMaterial(@"Materials/Default");
            gameObject.AddComponent<MeshCollider>();
            gameObject.transform.parent = tile.GameObject.transform;
        }
        /// <inheritdoc />
        public void BuildMesh(Tile tile, Mesh mesh)
        {
            EnsureTile(tile);

            var gameObject = new GameObject(mesh.Name);

            var uMesh = new UnityEngine.Mesh();

            uMesh.vertices  = mesh.Vertices;
            uMesh.triangles = mesh.Triangles;
            uMesh.colors    = mesh.Colors;
            uMesh.uv        = mesh.Textures;

            uMesh.RecalculateNormals();

            gameObject.isStatic = true;
            gameObject.AddComponent <MeshFilter>().mesh             = uMesh;
            gameObject.AddComponent <MeshRenderer>().sharedMaterial =
                _customizationService.GetSharedMaterial(@"Materials/Default");
            gameObject.AddComponent <MeshCollider>();
            gameObject.transform.parent = tile.GameObject.transform;
        }
Exemple #3
0
        /// <summary> Adapts mesh data received from utymap. </summary>
        public void AdaptMesh(string name, double[] vertices, int vertexCount,
                              int[] triangles, int triangleCount, int[] colors, int colorCount,
                              double[] uvs, int uvCount, int[] uvMap, int uvMapCount)
        {
            Vector3[] worldPoints;
            Color[]   unityColors;

            Vector2[] unityUvs;
            Vector2[] unityUvs2;
            Vector2[] unityUvs3;

            // NOTE process terrain differently to emulate flat shading effect by avoiding
            // triangles to share the same vertex. Remove "if" branch if you don't need it
            if (name.Contains("terrain"))
            {
                worldPoints = new Vector3[triangleCount];
                unityColors = new Color[triangleCount];

                unityUvs  = new Vector2[triangleCount];
                unityUvs2 = new Vector2[triangleCount];
                unityUvs3 = new Vector2[triangleCount];

                var textureMapper = CreateTextureAtlasMapper(unityUvs, unityUvs2, unityUvs3, uvs, uvMap);

                for (int i = 0; i < triangles.Length; ++i)
                {
                    int vertIndex = triangles[i] * 3;
                    worldPoints[i] = _tile.Projection
                                     .Project(new GeoCoordinate(vertices[vertIndex + 1], vertices[vertIndex]), vertices[vertIndex + 2]);

                    unityColors[i] = ColorUtils.FromInt(colors[triangles[i]]);
                    textureMapper.SetUvs(i, triangles[i] * 2);
                    triangles[i] = i;
                }
            }
            else
            {
                long id;
                if (!ShouldLoad(name, out id))
                {
                    return;
                }

                worldPoints = new Vector3[vertexCount / 3];
                for (int i = 0; i < vertices.Length; i += 3)
                {
                    worldPoints[i / 3] = _tile.Projection
                                         .Project(new GeoCoordinate(vertices[i + 1], vertices[i]), vertices[i + 2]);
                }

                unityColors = new Color[colorCount];
                for (int i = 0; i < colorCount; ++i)
                {
                    unityColors[i] = ColorUtils.FromInt(colors[i]);
                }

                if (uvCount > 0)
                {
                    unityUvs  = new Vector2[uvCount / 2];
                    unityUvs2 = new Vector2[uvCount / 2];
                    unityUvs3 = new Vector2[uvCount / 2];

                    var textureMapper = CreateTextureAtlasMapper(unityUvs, unityUvs2, unityUvs3, uvs, uvMap);
                    for (int i = 0; i < uvCount; i += 2)
                    {
                        unityUvs[i / 2] = new Vector2((float)uvs[i], (float)uvs[i + 1]);
                        textureMapper.SetUvs(i / 2, i);
                    }
                }
                else
                {
                    unityUvs  = new Vector2[worldPoints.Length];
                    unityUvs2 = new Vector2[worldPoints.Length];
                    unityUvs3 = new Vector2[worldPoints.Length];
                }

                /// ?
                // TODO this is not scalable: think about better solution for elements clipped by tile rect.
                //if (!name.StartsWith("barrier"))
                //    _tile.Register(id);
            }

            if (worldPoints.Length >= 65000)
            {
                _trace.Warn(TraceCategory, "mesh '{0}' has more vertices than allowed: {1}. " +
                            "It should be split but this is missing functionality in UtyMap.Unity.",
                            name, worldPoints.Length.ToString());
            }
            Mesh mesh = new Mesh(name, 0, worldPoints, triangles, unityColors, unityUvs, unityUvs2, unityUvs3);

            _observer.OnNext(new Union <Element, Mesh>(mesh));
        }
        /// <summary> Adapts mesh data received from utymap. </summary>
        public void AdaptMesh(string name, double[] vertices, int vertexCount, int[] triangles, 
            int triangleCount, int[] colors, int colorCount)
        {
            Vector3[] worldPoints;
            Color[] unityColors;

            // NOTE process terrain differently to emulate flat shading effect by avoiding
            // triangles to share the same vertex. Remove "if" branch if you don't need it
            if (name.Contains("terrain"))
            {
                worldPoints = new Vector3[triangleCount];
                unityColors = new Color[triangleCount];
                for (int i = 0; i < triangles.Length; ++i)
                {
                    int vertIndex = triangles[i] * 3;
                    worldPoints[i] = _tile.Projection
                        .Project(new GeoCoordinate(vertices[vertIndex + 1], vertices[vertIndex]), vertices[vertIndex + 2]);
                    unityColors[i] = ColorUtils.FromInt(colors[triangles[i]]);
                    triangles[i] = i;
                }
            }
            else
            {
                long id;
                if (!ShouldLoad(name, out id))
                    return;

                worldPoints = new Vector3[vertexCount / 3];
                for (int i = 0; i < vertices.Length; i += 3)
                    worldPoints[i / 3] = _tile.Projection
                        .Project(new GeoCoordinate(vertices[i + 1], vertices[i]), vertices[i + 2]);

                unityColors = new Color[colorCount];
                for (int i = 0; i < colorCount; ++i)
                    unityColors[i] = ColorUtils.FromInt(colors[i]);

                _tile.Register(id);
            }

            Mesh mesh = new Mesh(name, worldPoints, triangles, unityColors);
            _observer.OnNext(new Union<Element, Mesh>(mesh));
        }
Exemple #5
0
        /// <summary> Adapts mesh data received from utymap. </summary>
        public void AdaptMesh(string name, double[] vertices, int vertexCount,
                              int[] triangles, int triangleCount, int[] colors, int colorCount,
                              double[] uvs, int uvCount)
        {
            Vector3[] worldPoints;
            Color[]   unityColors;
            Vector2[] unityUvs;

            // NOTE process terrain differently to emulate flat shading effect by avoiding
            // triangles to share the same vertex. Remove "if" branch if you don't need it
            if (name.Contains("terrain"))
            {
                worldPoints = new Vector3[triangleCount];
                unityColors = new Color[triangleCount];
                unityUvs    = new Vector2[triangleCount];

                for (int i = 0; i < triangles.Length; ++i)
                {
                    int vertIndex = triangles[i] * 3;
                    worldPoints[i] = _tile.Projection
                                     .Project(new GeoCoordinate(vertices[vertIndex + 1], vertices[vertIndex]), vertices[vertIndex + 2]);

                    unityColors[i] = ColorUtils.FromInt(colors[triangles[i]]);

                    if (uvCount > 0)
                    {
                        int uvIndex = triangles[i] * 2;
                        unityUvs[i] = new Vector2((float)uvs[uvIndex], (float)uvs[uvIndex + 1]);
                    }
                    else
                    {
                        unityUvs[i] = new Vector2();
                    }

                    triangles[i] = i;
                }
            }
            else
            {
                long id;
                if (!ShouldLoad(name, out id))
                {
                    return;
                }

                worldPoints = new Vector3[vertexCount / 3];
                for (int i = 0; i < vertices.Length; i += 3)
                {
                    worldPoints[i / 3] = _tile.Projection
                                         .Project(new GeoCoordinate(vertices[i + 1], vertices[i]), vertices[i + 2]);
                }

                unityColors = new Color[colorCount];
                for (int i = 0; i < colorCount; ++i)
                {
                    unityColors[i] = ColorUtils.FromInt(colors[i]);
                }

                if (uvCount > 0)
                {
                    unityUvs = new Vector2[uvCount / 2];
                    for (int i = 0; i < uvCount; i += 2)
                    {
                        unityUvs[i / 2] = new Vector2((float)uvs[i], (float)uvs[i + 1]);
                    }
                }
                else
                {
                    unityUvs = new Vector2[worldPoints.Length];
                }

                _tile.Register(id);
            }

            if (worldPoints.Length >= 65000)
            {
                _trace.Warn(TraceCategory, "Mesh '{0}' has more vertices than allowed: {1}. " +
                            "It should be split but this is missing functionality in UtyMap.Unity.",
                            name, worldPoints.Length.ToString());
            }

            Mesh mesh = new Mesh(name, worldPoints, triangles, unityColors, unityUvs);

            _observer.OnNext(new Union <Element, Mesh>(mesh));
        }
 /// <inheritdoc />
 public void BuildMesh(Tile tile, Mesh mesh)
 {
 }