Пример #1
0
        public static void HandleBuilding(XmlReader reader, CityGml2GO cityGml2Go)
        {
            var buildingName = "";

            while (reader.MoveToNextAttribute())
            {
                if (reader.LocalName == "id")
                {
                    buildingName = reader.Value;
                }
            }

            var buildingGo         = new GameObject(string.IsNullOrEmpty(buildingName) ? "Building" : buildingName);
            var buildingProperties = buildingGo.AddComponent <Scripts.BuildingProperties>();
            var semanticType       = buildingGo.AddComponent <SemanticType>();

            buildingGo.transform.SetParent(cityGml2Go.Parent.transform);

            while (reader.Read())
            {
                if (reader.NodeType == XmlNodeType.Element && reader.LocalName == "Polygon")
                {
                    var polyGo = PolygonHandler.PolyToMesh(reader, buildingName, cityGml2Go, semanticType);
                    if (polyGo != null)
                    {
                        polyGo.transform.SetParent(buildingGo.transform);
                    }
                }

                if (cityGml2Go.ShowCurves)
                {
                    if (reader.NodeType == XmlNodeType.Element && reader.LocalName == "MultiCurve")
                    {
                        MultiCurveHandler.HandleMultiCurve(reader, buildingGo, semanticType, cityGml2Go);
                    }
                }

                if (cityGml2Go.Semantics)
                {
                    if (reader.NodeType == XmlNodeType.Element && cityGml2Go.SemanticSurfaces.Any(x => x == reader.LocalName))
                    {
                        semanticType.Name = reader.LocalName;
                        reader.MoveToFirstAttribute();
                        if (reader.LocalName == "id")
                        {
                            semanticType.Id = reader.Value;
                        }
                        else
                        {
                            while (reader.MoveToNextAttribute())
                            {
                                semanticType.Id = reader.Value;
                            }
                        }
                    }
                }

                BuildingPropertiesHandler.HandleBuildingProperties(reader, buildingProperties);

                if (reader.NodeType == XmlNodeType.EndElement && reader.LocalName == "Building")
                {
                    break;
                }
            }

            Object.Destroy(semanticType);
        }
Пример #2
0
        public static GameObject PolyToMesh(XmlReader reader, string id, CityGml2GO cityGml2Go, SemanticType semanticType = null)
        {
            var extPositions = new List <Vector3>();
            var intPositions = new List <List <Vector3> >();
            var polyName     = "";

            while (reader.MoveToNextAttribute())
            {
                if (reader.LocalName == "id")
                {
                    polyName = reader.Value;
                }
            }

            while (reader.Read())
            {
                if (((IXmlLineInfo)reader).LineNumber >= 14459)
                {
                    int idfg = 0;
                }
                if (string.IsNullOrEmpty(polyName))
                {
                    while (reader.MoveToNextAttribute())
                    {
                        if (reader.LocalName == "id")
                        {
                            polyName = reader.Value;
                        }
                    }
                }

                if (reader.NodeType == XmlNodeType.Element && reader.LocalName == "exterior")
                {
                    while (reader.Read())
                    {
                        if (reader.NodeType == XmlNodeType.Element && reader.LocalName == "posList")
                        {
                            var range = PositionHandler.GetPosList(reader, cityGml2Go.ActualTranslate);
                            if (range != null)
                            {
                                extPositions.AddRange(range);
                            }
                        }

                        if (reader.NodeType == XmlNodeType.Element && reader.LocalName == "pos")
                        {
                            extPositions.Add(PositionHandler.GetPos(reader, cityGml2Go.ActualTranslate));
                        }

                        if (reader.NodeType == XmlNodeType.EndElement && reader.LocalName == "exterior")
                        {
                            break;
                        }
                    }
                }

                if (reader.NodeType == XmlNodeType.Element && reader.LocalName == "interior")
                {
                    var curInt = new List <Vector3>();
                    while (reader.Read())
                    {
                        if (reader.NodeType == XmlNodeType.Element && reader.LocalName == "posList")
                        {
                            var range = PositionHandler.GetPosList(reader, cityGml2Go.ActualTranslate);
                            if (range != null)
                            {
                                curInt.AddRange(range);
                            }
                        }

                        if (reader.NodeType == XmlNodeType.Element && reader.LocalName == "pos")
                        {
                            curInt.Add(PositionHandler.GetPos(reader, cityGml2Go.ActualTranslate));
                        }
                        if (reader.NodeType == XmlNodeType.EndElement && reader.LocalName == "interior")
                        {
                            break;
                        }
                    }
                    intPositions.Add(curInt);
                }
                if (reader.NodeType == XmlNodeType.EndElement && reader.LocalName == "Polygon")
                {
                    break;
                }
            }

            var lastPoint = extPositions.Last();

            //extPositions = extPositions.Distinct().ToList();
            extPositions.Add(lastPoint);

            for (var index = 0; index < intPositions.Count; index++)
            {
                lastPoint = intPositions[index].Last();
                //intPositions[index] = intPositions[index].Distinct().ToList();
                intPositions[index].Add(lastPoint);
            }


            if (!PolygonHandler.IsPolyValid(extPositions))
            {
                IXmlLineInfo xmlInfo    = (IXmlLineInfo)reader;
                int          lineNumber = xmlInfo.LineNumber;
                Debug.Log(lineNumber);
                return(null);
            }

            foreach (var intRing in intPositions)
            {
                if (!IsPolyValid(intRing))
                {
                    IXmlLineInfo xmlInfo    = (IXmlLineInfo)reader;
                    int          lineNumber = xmlInfo.LineNumber;
                    Debug.Log(lineNumber);
                    return(null);
                }
            }

            var polygon = new Poly2Mesh.Polygon();

            extPositions.Reverse();
            intPositions.Reverse();

            polygon.outside = extPositions;
            polygon.holes   = intPositions;

            GameObject go = null;

            try
            {
                go = Poly2Mesh.CreateGameObject(polygon, string.IsNullOrEmpty(polyName) ? id : polyName);
            }
            catch (Exception ex)
            {
                Debug.Log(ex);
            }

            if (cityGml2Go.GenerateColliders)
            {
                //go.AddComponent<MeshCollider>();
            }

            if (go != null)
            {
                if (cityGml2Go.Semantics && semanticType != null)
                {
                    SemanticsHandler.HandleSemantics(go, semanticType, cityGml2Go);
                }
                else if (cityGml2Go.DefaultMaterial != null)
                {
                    var mr = go.GetComponent <MeshRenderer>();
                    if (mr != null)
                    {
                        mr.sharedMaterial = cityGml2Go.DefaultMaterial;
                    }
                }
            }

            cityGml2Go.Polygons.Add(go);

            return(go);
        }