示例#1
0
    public static void CreateBuilding(OSMWay way, OSMapInfo mapInfo, Material material)
    {
        // Create an instance of the object and place it in the centre of its points
        GameObject go          = new GameObject();
        Vector3    localOrigin = GetCenter(way, mapInfo);

        go.transform.position = localOrigin - mapInfo.Bounds.Center;

        // Add the mesh filter and renderer components to the object
        MeshFilter   mf = go.AddComponent <MeshFilter>();
        MeshRenderer mr = go.AddComponent <MeshRenderer>();

        // Apply the material
        mr.material = material;

        // Create the collections for the object's vertices, indices, UVs etc.
        List <Vector3> vectors = new List <Vector3>();
        List <Vector3> normals = new List <Vector3>();
        List <Vector2> uvs     = new List <Vector2>();
        List <int>     indices = new List <int>();

        // Call the child class' object creation code
        OnObjectCreated(mapInfo, way, localOrigin, vectors, normals, uvs, indices);

        // Apply the data to the mesh
        mf.mesh.vertices  = vectors.ToArray();
        mf.mesh.normals   = normals.ToArray();
        mf.mesh.triangles = indices.ToArray();
        mf.mesh.uv        = uvs.ToArray();
    }
示例#2
0
 public void DisplayWays(OSMapInfo mapInfo)
 {
     DrawWayCollection(mapInfo, mapInfo.Roads, HighwayColor);
     DrawWayCollection(mapInfo, mapInfo.ParkSpaces, ParkingColor);
     DrawWayCollection(mapInfo, mapInfo.Buildings, BuildingColor);
     DrawWayCollection(mapInfo, mapInfo.Unknown, UnknownColor);
 }
示例#3
0
 public void SpawnBuildings(OSMapInfo mapInfo)
 {
     foreach (OSMWay building in mapInfo.Buildings)
     {
         BuildingBuilder.CreateBuilding(building, mapInfo, BuildingMaterial);
     }
 }
示例#4
0
 public void SpawnRoads(OSMapInfo mapInfo)
 {
     foreach (OSMWay road in mapInfo.Roads)
     {
         RoadBuilder.CreateRoad(road, mapInfo, RoadMaterial);
     }
 }
示例#5
0
 public void SpawnParkingLotMarkers(OSMapInfo mapInfo)
 {
     foreach (OSMWay parkSpace in mapInfo.ParkSpaces)
     {
         BuildingBuilder.CreateBuilding(parkSpace, mapInfo, ParkingSpaceMaterial);
     }
 }
示例#6
0
    private static void OnObjectCreated(OSMapInfo mapInfo, OSMWay way, Vector3 origin, List <Vector3> vectors, List <Vector3> normals, List <Vector2> uvs, List <int> indices)
    {
        for (int i = 1; i < way.NodeIDs.Count; i++)
        {
            OSMNode p1 = mapInfo.Nodes[way.NodeIDs[i - 1]];
            OSMNode p2 = mapInfo.Nodes[way.NodeIDs[i]];

            Vector3 s1 = p1 - origin;
            Vector3 s2 = p2 - origin;

            Vector3 diff = (s2 - s1).normalized;
            // elongate the road a little bit
            s1 -= diff;
            s2 += diff;

            // https://en.wikipedia.org/wiki/Lane
            // According to the article, it's 3.7m in Canada
            var cross = Vector3.Cross(diff, Vector3.up) * 3.7f * way.Lanes;

            // Create points that represent the width of the road
            Vector3 v1 = s1 + cross;
            Vector3 v2 = s1 - cross;
            Vector3 v3 = s2 + cross;
            Vector3 v4 = s2 - cross;

            vectors.Add(v1);
            vectors.Add(v2);
            vectors.Add(v3);
            vectors.Add(v4);

            uvs.Add(new Vector2(0, 0));
            uvs.Add(new Vector2(1, 0));
            uvs.Add(new Vector2(0, 1));
            uvs.Add(new Vector2(1, 1));

            normals.Add(Vector3.up);
            normals.Add(Vector3.up);
            normals.Add(Vector3.up);
            normals.Add(Vector3.up);

            int idx1, idx2, idx3, idx4;
            idx4 = vectors.Count - 1;
            idx3 = vectors.Count - 2;
            idx2 = vectors.Count - 3;
            idx1 = vectors.Count - 4;

            // first triangle v1, v3, v2
            indices.Add(idx1);
            indices.Add(idx3);
            indices.Add(idx2);

            // second         v3, v4, v2
            indices.Add(idx3);
            indices.Add(idx4);
            indices.Add(idx2);
        }
    }
示例#7
0
 private void DrawWayCollection(OSMapInfo mapInfo, List <OSMWay> wayCollection, Color color)
 {
     foreach (OSMWay currentWay in wayCollection)
     {
         if (currentWay.Visible)
         {
             DrawWay(mapInfo, currentWay, color);
         }
     }
 }
示例#8
0
    private static Vector3 CalcWayCenter(OSMapInfo mapInfo, OSMWay pSpace)
    {
        Vector3 wayCenter = new Vector3();

        foreach (ulong nodeId in pSpace.NodeIDs)
        {
            wayCenter += mapInfo.Nodes[nodeId].WorldCoord;
        }
        wayCenter /= pSpace.NodeIDs.Count;
        return(wayCenter);
    }
示例#9
0
    private static Vector3 GetCenter(OSMWay way, OSMapInfo mapInfo)
    {
        Vector3 total = Vector3.zero;

        foreach (ulong nodeID in way.NodeIDs)
        {
            total += mapInfo.Nodes[nodeID];
        }

        return(total / way.NodeIDs.Count);
    }
示例#10
0
    private void DrawWay(OSMapInfo mapInfo, OSMWay way, Color color)
    {
        for (int i = 1; i < way.NodeIDs.Count; i++)
        {
            OSMNode p1 = mapInfo.Nodes[way.NodeIDs[i - 1]];
            OSMNode p2 = mapInfo.Nodes[way.NodeIDs[i]];

            Vector3 v1 = p1 - mapInfo.Bounds.Center;
            Vector3 v2 = p2 - mapInfo.Bounds.Center;

            Debug.DrawLine(v1, v2, color);
        }
    }
示例#11
0
    private static float GetNearestNodeDistanceFromCenter(OSMapInfo mapInfo, OSMWayFindingInfo pSpace)
    {
        float nearestNodeDistance = float.MaxValue;

        foreach (ulong nodeId in pSpace.Way.NodeIDs)
        {
            float distanceSqr = (mapInfo.Nodes[nodeId].WorldCoord - pSpace.WayCenter).sqrMagnitude;
            if (distanceSqr < nearestNodeDistance)
            {
                nearestNodeDistance = distanceSqr;
            }
        }
        return(Mathf.Sqrt(nearestNodeDistance));
    }
示例#12
0
 public static void SortParkingSpaces(Vector3 position, OSMapInfo mapInfo, out List <OSMWayFindingInfo> orderedParkingSpaces)
 {
     orderedParkingSpaces = new List <OSMWayFindingInfo>();
     foreach (OSMWay pSpace in mapInfo.ParkSpaces)
     {
         OSMWayFindingInfo info = new OSMWayFindingInfo();
         info.WayCenter           = CalcWayCenter(mapInfo, pSpace);
         info.Distance            = (info.WayCenter - position).magnitude;
         info.Way                 = pSpace;
         info.NearestNodeDistance = GetNearestNodeDistanceFromCenter(mapInfo, info);
         orderedParkingSpaces.Add(info);
     }
     orderedParkingSpaces.Sort(new OSMWayDistanceToPosDescendingSorter());
 }
示例#13
0
    /// <summary>
    /// Build the object using the data from the OsmWay instance.
    /// </summary>
    /// <param name="way">OsmWay instance</param>
    /// <param name="origin">The origin of the structure</param>
    /// <param name="vectors">The vectors (vertices) list</param>
    /// <param name="normals">The normals list</param>
    /// <param name="uvs">The UVs list</param>
    /// <param name="indices">The indices list</param>
    private static void OnObjectCreated(OSMapInfo mapInfo, OSMWay way, Vector3 origin, List <Vector3> vectors, List <Vector3> normals, List <Vector2> uvs, List <int> indices)
    {
        // Get the centre of the roof
        Vector3 oTop = new Vector3(0, way.Height, 0);

        // First vector is the middle point in the roof
        vectors.Add(oTop);
        normals.Add(Vector3.up);
        uvs.Add(new Vector2(0.5f, 0.5f));

        for (int i = 1; i < way.NodeIDs.Count; i++)
        {
            OSMNode p1 = mapInfo.Nodes[way.NodeIDs[i - 1]];
            OSMNode p2 = mapInfo.Nodes[way.NodeIDs[i]];

            Vector3 v1 = p1 - origin;
            Vector3 v2 = p2 - origin;
            Vector3 v3 = v1 + new Vector3(0, way.Height, 0);
            Vector3 v4 = v2 + new Vector3(0, way.Height, 0);

            vectors.Add(v1);
            vectors.Add(v2);
            vectors.Add(v3);
            vectors.Add(v4);

            uvs.Add(new Vector2(0, 0));
            uvs.Add(new Vector2(1, 0));
            uvs.Add(new Vector2(0, 1));
            uvs.Add(new Vector2(1, 1));

            normals.Add(-Vector3.forward);
            normals.Add(-Vector3.forward);
            normals.Add(-Vector3.forward);
            normals.Add(-Vector3.forward);

            int idx1, idx2, idx3, idx4;
            idx4 = vectors.Count - 1;
            idx3 = vectors.Count - 2;
            idx2 = vectors.Count - 3;
            idx1 = vectors.Count - 4;

            // first triangle v1, v3, v2
            indices.Add(idx1);
            indices.Add(idx3);
            indices.Add(idx2);

            // second         v3, v4, v2
            indices.Add(idx3);
            indices.Add(idx4);
            indices.Add(idx2);

            // third          v2, v3, v1
            indices.Add(idx2);
            indices.Add(idx3);
            indices.Add(idx1);

            // fourth         v2, v4, v3
            indices.Add(idx2);
            indices.Add(idx4);
            indices.Add(idx3);

            // And now the roof triangles
            indices.Add(0);
            indices.Add(idx3);
            indices.Add(idx4);

            // Don't forget the upside down one!
            indices.Add(idx4);
            indices.Add(idx3);
            indices.Add(0);
        }
    }