Пример #1
0
        public static void HandleMultiCurve(XmlReader reader, GameObject buildingGo, SemanticType semanticType, CityGml2GO cityGml2Go)
        {
            while (reader.Read())
            {
                if (reader.NodeType == XmlNodeType.Element && reader.LocalName == "curveMember")
                {
                    GameObject   lrGo;
                    LineRenderer lr;

                    if (cityGml2Go.LineRendererPrefab == null)
                    {
                        lrGo = new GameObject();
                        lr   = lrGo.AddComponent <LineRenderer>();
                    }
                    else
                    {
                        lrGo = GameObject.Instantiate(cityGml2Go.LineRendererPrefab);
                        lr   = lrGo.GetComponent <LineRenderer>();
                        if (lr == null)
                        {
                            lr = lrGo.AddComponent <LineRenderer>();
                        }
                    }
                    var points = new List <Vector3>();
                    while (reader.Read())
                    {
                        if (reader.NodeType == XmlNodeType.Element && reader.LocalName == "pos")
                        {
                            points.Add(PositionHandler.GetPos(reader, cityGml2Go.ActualTranslate));
                        }
                        if (reader.NodeType == XmlNodeType.Element && reader.LocalName == "posList")
                        {
                            var range = PositionHandler.GetPosList(reader, cityGml2Go.ActualTranslate);
                            if (range != null)
                            {
                                points.AddRange(range);
                            }
                        }

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

                    lr.positionCount = points.Count;
                    lr.SetPositions(points.ToArray());

                    lrGo.transform.SetParent(buildingGo.transform);
                    lrGo.transform.localPosition = Vector3.zero;
                    if (cityGml2Go.Semantics && semanticType != null)
                    {
                        SemanticsHandler.HandleSemantics(lrGo, semanticType, cityGml2Go);
                    }
                }

                if (reader.NodeType == XmlNodeType.EndElement && reader.LocalName == "MultiCurve")
                {
                    return;
                }
            }
        }
Пример #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);
        }