/// <summary> /// Constructs the scene from given parameters /// </summary> /// <param name="OSMfilename">Full path of OSM file</param> /// <param name="continent">Specify Continent to download correct Heightmap from Nasa Srtm Data</param> /// <param name="provider">Choose mapProvider to select Texture of Terrain</param> public void initializeScene(string OSMfilename, HeightmapContinent _continent, MapProvider _provider) { string[] subStr = OSMfilename.Split(new char[] { '/', '\\' }); sceneName = subStr[subStr.Length - 1]; OSMPath = OSMfilename; continent = _continent; provider = _provider; List <Way> WayListforHighway = new List <Way>(); List <Way> WayListforBuilding = new List <Way>(); InitialConfigLoader configloader = new InitialConfigLoader(); var stopwatch = new System.Diagnostics.Stopwatch(); stopwatch.Start(); OSMparser parser = new OSMparser(); scenebbox = parser.readBBox(OSMfilename); scenebbox = editbbox(scenebbox); config = configloader.loadInitialConfig(); HeightmapLoader heightMap = new HeightmapLoader(scenebbox, continent); terrain = new myTerrain(heightMap, scenebbox, OSMfilename, provider); stopwatch.Stop(); Debug.Log("<color=blue>TERRAIN RENDER TIME:</color>" + stopwatch.ElapsedMilliseconds); stopwatch.Reset(); stopwatch.Start(); osmxml = parser.parseOSM(OSMfilename); assignNodePositions(); stopwatch.Stop(); Debug.Log("<color=blue>OSM PARSING TIME:</color>" + stopwatch.ElapsedMilliseconds); stopwatch.Reset(); stopwatch.Start(); defaultObject3DList = DefaultObject3DHandler.drawDefaultObjects(osmxml.defaultobject3DList); stopwatch.Stop(); Debug.Log("<color=blue>3D OBJECT RENDER TIME:</color>" + stopwatch.ElapsedMilliseconds); stopwatch.Reset(); stopwatch.Start(); for (int k = 0; k < osmxml.wayList.Count; k++) { Way w = osmxml.wayList[k]; switch (w.type) { case ItemEnumerator.wayType.building: WayListforBuilding.Add(w); break; case ItemEnumerator.wayType.highway: WayListforHighway.Add(w); break; case ItemEnumerator.wayType.area: break; case ItemEnumerator.wayType.barrier: barrierList.Add(new Barrier(w, config.barrierConfig)); break; case ItemEnumerator.wayType.river: highwayList.Add(new Highway(w, config.highwayConfig, terrain)); break; case ItemEnumerator.wayType.none: break; } } stopwatch.Stop(); Debug.Log("<color=blue>ITEM ENUMERATING TIME:</color>" + stopwatch.ElapsedMilliseconds); stopwatch.Reset(); stopwatch.Start(); highwayModeller = new HighwayModeller(WayListforHighway, terrain, config.highwayConfig); highwayModeller.renderHighwayList(); highwayModeller.renderPavementList(); highwayList = highwayModeller.highwayList; pavementList = highwayModeller.pavementList; stopwatch.Stop(); Debug.Log("<color=blue>HIGHWAY RENDERING TIME:</color>" + stopwatch.ElapsedMilliseconds); stopwatch.Reset(); stopwatch.Start(); BuildingListModeller buildingListModeller = new BuildingListModeller(WayListforBuilding, osmxml.buildingRelations, config.buildingConfig); buildingListModeller.renderBuildingList(); buildingList = buildingListModeller.buildingList; stopwatch.Stop(); Debug.Log("<color=blue>BUILDING RENDERING TIME:</color>" + stopwatch.ElapsedMilliseconds); Debug.Log("<color=red>Scene Info:</color> BuildingCount:" + buildingList.Count.ToString() + " HighwayCount:" + highwayList.Count); }
//This will be used for generating initial vertexes public void generateInitial3Dway(myTerrain terrain) { leftSideVertexes = new List <Vector3>(); rightSideVertexes = new List <Vector3>(); if (way.nodes.Count == 2) { Vector3 up = new Vector3(0, 1, 0); Vector3 forward = way.nodes[1].meterPosition - way.nodes[0].meterPosition; forward.y = 0.0f; //Vector3 right = Vector3.Cross(forward ,up);********************************************** Vector3 right = Vector3.Cross(up, forward); right = right.normalized; Vector3 left = -1 * right; //LEFT SIDE Vector2 pointLeft1 = new Vector2(way.nodes[0].meterPosition.x, way.nodes[0].meterPosition.z) + new Vector2(left.x, left.z) * (waySize / 2.0f); Vector2 pointLeft2 = new Vector2(way.nodes[1].meterPosition.x, way.nodes[1].meterPosition.z) + new Vector2(left.x, left.z) * (waySize / 2.0f); Vector3 pointLeft1Pos = new Vector3(pointLeft1.x, terrain.getTerrainHeight2(pointLeft1.y + bbox.meterBottom, pointLeft1.x + bbox.meterLeft), pointLeft1.y); Vector3 pointLeft2Pos = new Vector3(pointLeft2.x, terrain.getTerrainHeight2(pointLeft2.y + bbox.meterBottom, pointLeft2.x + bbox.meterLeft), pointLeft2.y); leftSideVertexes.Add(pointLeft1Pos); leftSideVertexes.Add(pointLeft2Pos); if (way.nodes[0].type == ItemEnumerator.nodeType.StreetLamp) { streetLampList.Add(DefaultObject3DHandler.drawStreetLamp(pointLeft1Pos, way.nodes[0].id)); } if (way.nodes[1].type == ItemEnumerator.nodeType.StreetLamp) { streetLampList.Add(DefaultObject3DHandler.drawStreetLamp(pointLeft2Pos, way.nodes[1].id)); } //RIGHT SIDE Vector2 pointRight1 = new Vector2(way.nodes[0].meterPosition.x, way.nodes[0].meterPosition.z) + new Vector2(right.x, right.z) * (waySize / 2.0f); Vector2 pointRight2 = new Vector2(way.nodes[1].meterPosition.x, way.nodes[1].meterPosition.z) + new Vector2(right.x, right.z) * (waySize / 2.0f); Vector3 pointRight1Pos = new Vector3(pointRight1.x, terrain.getTerrainHeight2(pointRight1.y + bbox.meterBottom, pointRight1.x + bbox.meterLeft), pointRight1.y); Vector3 pointRight2Pos = new Vector3(pointRight2.x, terrain.getTerrainHeight2(pointRight2.y + bbox.meterBottom, pointRight2.x + bbox.meterLeft), pointRight2.y); rightSideVertexes.Add(pointRight1Pos); rightSideVertexes.Add(pointRight2Pos); if (way.nodes[0].type == ItemEnumerator.nodeType.StreetLamp) { streetLampList.Add(DefaultObject3DHandler.drawStreetLamp(pointRight1Pos, way.nodes[0].id)); } if (way.nodes[1].type == ItemEnumerator.nodeType.StreetLamp) { streetLampList.Add(DefaultObject3DHandler.drawStreetLamp(pointRight2Pos, way.nodes[1].id)); } } else { for (int i = 0; i < way.nodes.Count - 2; i++) { Vector3 up = new Vector3(0, 1, 0); Vector3 forward1 = way.nodes[i + 1].meterPosition - way.nodes[i].meterPosition; forward1.y = 0.0f; Vector3 right1 = Vector3.Cross(up, forward1); right1 = right1.normalized; Vector3 left1 = -1 * right1; Vector3 forward2 = way.nodes[i + 2].meterPosition - way.nodes[i + 1].meterPosition; forward2.y = 0.0f; Vector3 right2 = Vector3.Cross(up, forward2); right2 = right2.normalized; Vector3 left2 = -1 * right2; //INITIAL POINTS ARE ADDED TO NODES3D if (i == 0) { Vector2 pointLeft1 = new Vector2(way.nodes[i].meterPosition.x, way.nodes[i].meterPosition.z) + new Vector2(left1.x, left1.z) * (waySize / 2.0f); Vector3 pointLeft1Pos = new Vector3(pointLeft1.x, terrain.getTerrainHeight2(pointLeft1.y + bbox.meterBottom, pointLeft1.x + bbox.meterLeft), pointLeft1.y); leftSideVertexes.Add(pointLeft1Pos); Vector2 pointRight1 = new Vector2(way.nodes[i].meterPosition.x, way.nodes[i].meterPosition.z) + new Vector2(right1.x, right1.z) * (waySize / 2.0f); Vector3 pointRight1Pos = new Vector3(pointRight1.x, terrain.getTerrainHeight2(pointRight1.y + bbox.meterBottom, pointRight1.x + bbox.meterLeft), pointRight1.y); rightSideVertexes.Add(pointRight1Pos); } //1ST LINE LEFT Vector2 p0 = new Vector2(way.nodes[i].meterPosition.x, way.nodes[i].meterPosition.z) + new Vector2(left1.x, left1.z) * (waySize / 2.0f); Vector2 p1 = new Vector2(way.nodes[i + 1].meterPosition.x, way.nodes[i + 1].meterPosition.z) + new Vector2(left1.x, left1.z) * (waySize / 2.0f); //2ND LINE LEFT Vector2 p2 = new Vector2(way.nodes[i + 1].meterPosition.x, way.nodes[i + 1].meterPosition.z) + new Vector2(left2.x, left2.z) * (waySize / 2.0f); Vector2 p3 = new Vector2(way.nodes[i + 2].meterPosition.x, way.nodes[i + 2].meterPosition.z) + new Vector2(left2.x, left2.z) * (waySize / 2.0f); //INTERSECTION LEFT Vector2 iL = new Vector2(); Vector3 iLPos; if (!Geometry.getInfiniteLineIntersection(ref iL, p0, p1, p2, p3)) { iLPos = new Vector3(p1.x, terrain.getTerrainHeight2(p1.y + bbox.meterBottom, p1.x + bbox.meterLeft), p1.y); } else { iLPos = new Vector3(iL.x, terrain.getTerrainHeight2(iL.y + bbox.meterBottom, iL.x + bbox.meterLeft), iL.y); } leftSideVertexes.Add(iLPos); if (way.nodes[i + 1].type == ItemEnumerator.nodeType.StreetLamp) { streetLampList.Add(DefaultObject3DHandler.drawStreetLamp(iLPos, way.nodes[i + 1].id)); } //1ST LINE RIGHT p0 = new Vector2(way.nodes[i].meterPosition.x, way.nodes[i].meterPosition.z) + new Vector2(right1.x, right1.z) * (waySize / 2.0f); p1 = new Vector2(way.nodes[i + 1].meterPosition.x, way.nodes[i + 1].meterPosition.z) + new Vector2(right1.x, right1.z) * (waySize / 2.0f); //2ND LINE RIGHT p2 = new Vector2(way.nodes[i + 1].meterPosition.x, way.nodes[i + 1].meterPosition.z) + new Vector2(right2.x, right2.z) * (waySize / 2.0f); p3 = new Vector2(way.nodes[i + 2].meterPosition.x, way.nodes[i + 2].meterPosition.z) + new Vector2(right2.x, right2.z) * (waySize / 2.0f); //INTERSECTION RIGHT Vector2 iR = new Vector2(); Vector3 iRPos; if (!Geometry.getInfiniteLineIntersection(ref iR, p0, p1, p2, p3)) { iRPos = new Vector3(p1.x, terrain.getTerrainHeight2(p1.y + bbox.meterBottom, p1.x + bbox.meterLeft), p1.y); } else { iRPos = new Vector3(iR.x, terrain.getTerrainHeight2(iR.y + bbox.meterBottom, iR.x + bbox.meterLeft), iR.y); } rightSideVertexes.Add(iRPos); if (way.nodes[i + 1].type == ItemEnumerator.nodeType.StreetLamp) { streetLampList.Add(DefaultObject3DHandler.drawStreetLamp(iRPos, way.nodes[i + 1].id)); } //ENDING POINTS ARE ADDEDD TO NODES3D if (i == way.nodes.Count - 3) { Vector2 pointLeft1 = new Vector2(way.nodes[i + 2].meterPosition.x, way.nodes[i + 2].meterPosition.z) + new Vector2(left2.x, left2.z) * (waySize / 2.0f); leftSideVertexes.Add(new Vector3(pointLeft1.x, terrain.getTerrainHeight2(pointLeft1.y + bbox.meterBottom, pointLeft1.x + bbox.meterLeft), pointLeft1.y)); Vector2 pointRight1 = new Vector2(way.nodes[i + 2].meterPosition.x, way.nodes[i + 2].meterPosition.z) + new Vector2(right2.x, right2.z) * (waySize / 2.0f); rightSideVertexes.Add(new Vector3(pointRight1.x, terrain.getTerrainHeight2(pointRight1.y + bbox.meterBottom, pointRight1.x + bbox.meterLeft), pointRight1.y)); return; } } } }
/// <summary> /// Load urban scene using a save file /// </summary> /// <param name="save"> Save file object </param> public void loadProject(SceneSave save) { List <Way> WayListforHighway = new List <Way>(); List <Way> WayListforBuilding = new List <Way>(); InitialConfigLoader configloader = new InitialConfigLoader(); OSMPath = save.osmPath; OSMparser parser = new OSMparser(); scenebbox = parser.readBBox(save.osmPath); scenebbox = editbbox(scenebbox); config = configloader.loadInitialConfig(); //--> Maybe it is better to include config to SaveProject file HeightmapLoader heightMap = new HeightmapLoader(scenebbox, save.continent); terrain = new myTerrain(heightMap, scenebbox, save.osmPath, save.provider); osmxml = parser.parseOSM(save.osmPath); assignNodePositions(); defaultObject3DList = DefaultObject3DHandler.drawDefaultObjects(osmxml.defaultobject3DList); LoadExternalOBJ objloader = new LoadExternalOBJ(); //3D OBJECT LOAD for (int i = 0; i < save.objectSaveList.Count; i++) { Object3D obj = new Object3D(); obj.name = save.objectSaveList[i].name; if (save.objectSaveList[i].type == ObjectType.External) { obj.object3D = objloader.loadOBJ(save.objectSaveList[i].resourcePath); } else { obj.object3D = (GameObject)MonoBehaviour.Instantiate(Resources.Load(save.objectSaveList[i].resourcePath)); } obj.object3D.AddComponent <Object3dMouseHandler>(); obj.resourcePath = save.objectSaveList[i].resourcePath; obj.object3D.transform.position = save.objectSaveList[i].translate; obj.object3D.transform.localScale = save.objectSaveList[i].scale; Quaternion quat = new Quaternion(); quat.eulerAngles = save.objectSaveList[i].rotate; obj.object3D.transform.rotation = quat; obj.object3D.name = obj.name; obj.object3D.tag = "3DObject"; object3DList.Add(obj); } for (int k = 0; k < osmxml.wayList.Count; k++) { Way w = osmxml.wayList[k]; switch (w.type) { case ItemEnumerator.wayType.building: WayListforBuilding.Add(w); break; case ItemEnumerator.wayType.highway: WayListforHighway.Add(w); break; case ItemEnumerator.wayType.area: break; case ItemEnumerator.wayType.barrier: barrierList.Add(new Barrier(w, config.barrierConfig)); break; case ItemEnumerator.wayType.river: highwayList.Add(new Highway(w, config.highwayConfig, terrain)); break; case ItemEnumerator.wayType.none: break; } } highwayModeller = new HighwayModeller(WayListforHighway, terrain, config.highwayConfig, save.highwaySaveList); highwayModeller.renderHighwayList(); highwayModeller.renderPavementList(); highwayList = highwayModeller.highwayList; pavementList = highwayModeller.pavementList; BuildingListModeller buildingListModeller = new BuildingListModeller(WayListforBuilding, osmxml.buildingRelations, config.buildingConfig, save.buildingSaveList); buildingListModeller.renderBuildingList(); buildingList = buildingListModeller.buildingList; if (save.controller == null) { return; } if (save.controller.controllerType == ControllerSave.ControllerType.CameraVan) { Transform mainCamera = GameObject.Find("Main Camera").transform; CameraController camController = mainCamera.GetComponent <CameraController>(); controller = (GameObject)GameObject.Instantiate(Resources.Load("Prefabs/Car/PolimiCameraCar/CameraVan")); controller.AddComponent <CameraVanMouseHandler>(); controller.tag = "CameraVan"; controller.name = "Camera Van"; controller.transform.position = mainCamera.position + mainCamera.forward * 10.0f; controller.GetComponent <Rigidbody>().useGravity = false; camController.target = controller.transform; } else { Transform mainCamera = GameObject.Find("Main Camera").transform; CameraController camController = mainCamera.GetComponent <CameraController>(); controller = (GameObject)GameObject.Instantiate(Resources.Load("Prefabs/Ethan/ThirdPersonController")); controller.AddComponent <CameraVanMouseHandler>(); controller.tag = "CameraVan"; controller.name = "Third Person (Ethan)"; controller.transform.position = mainCamera.position + mainCamera.forward * 10.0f; camController.target = controller.transform; } controller.transform.position = save.controller.controllerPosition; Quaternion controllerQuat = new Quaternion(); controllerQuat.eulerAngles = save.controller.controllerRotation; controller.transform.rotation = controllerQuat; CameraVanEdit cve = GameObject.Find("Canvas").transform.Find("CameraVanEdit").GetComponent <CameraVanEdit>(); cve.cameraList = save.controller.convertBackToCamList(save.controller.cameraSettings); cve.laserScanner = save.controller.convertBackToLaser(save.controller.laserSetting); }