// Receive the JSON results from the Mapzen download // Parse the JSON to create road objects using EasyRoads3D public void ReceiveDownloadResults(string results) { var json = JSON.Parse(results); JSONArray roadFeatures = json ["roads"] ["features"].AsArray; currentThreadRoads = new List <ERRoad> (); for (int i = 0; i < roadFeatures.Count; i++) { string type = roadFeatures [i] ["geometry"] ["type"].Value; string kindOfRoad = roadFeatures [i] ["properties"] ["kind"].Value; string nameOfRoad = roadFeatures [i] ["properties"] ["name"].Value; if (kindOfRoad != "path") { if (type == "LineString") { JSONArray coordinates = roadFeatures [i] ["geometry"] ["coordinates"].AsArray; Vector3[] roadMarkers = parseLineString(coordinates); ERRoad road = roadNetwork.CreateRoad(nameOfRoad, roadType, roadMarkers); addRoad(road); } else if (type == "MultiLineString") { JSONArray coordinates = roadFeatures [i] ["geometry"] ["coordinates"].AsArray; for (int j = 0; j < coordinates.Count; j++) { JSONArray coords = coordinates [j].AsArray; Vector3[] roadMarkers = parseLineString(coords); ERRoad road = roadNetwork.CreateRoad(nameOfRoad, roadType, roadMarkers); addRoad(road); } } } } }
void Start() { Debug.Log("Please read the comments at the top before using the runtime API!"); // Create Road Network object roadNetwork = new ERRoadNetwork(); // Create road // ERRoad road = roadNetwork.CreateRoad(string name); // ERRoad road = roadNetwork.CreateRoad(string name, Vector3[] markers); // ERRoad road = roadNetwork.CreateRoad(string name, ERRoadType roadType); // ERRoad road = roadNetwork.CreateRoad(string name, ERRoadType roadType, Vector3[] markers); ERRoadType roadType = new ERRoadType(); roadType.roadWidth = 6; roadType.roadMaterial = Resources.Load("Materials/roads/single lane") as Material; Vector3[] markers = new Vector3[4]; markers[0] = new Vector3(200, 5, 200); markers[1] = new Vector3(250, 5, 200); markers[2] = new Vector3(250, 5, 250); markers[3] = new Vector3(300, 5, 250); road = roadNetwork.CreateRoad("road 1", roadType, markers); // Add Marker: ERRoad.AddMarker(Vector3); road.AddMarker(new Vector3(300, 5, 300)); // Add Marker: ERRoad.InsertMarker(Vector3); road.InsertMarker(new Vector3(275, 5, 235)); // Delete Marker: ERRoad.DeleteMarker(int index); road.DeleteMarker(2); // Set the road width : ERRoad.SetWidth(float); // road.SetWidth(10); // Set the road material : ERRoad.SetMaterial(Material); // Material mat = Resources.Load("Materials/roads/single lane") as Material; // road.SetMaterial(mat); // Build Road Network roadNetwork.BuildRoadNetwork(); // Restore Road Network // roadNetwork.RestoreRoadNetwork(); // create dummy object go = GameObject.CreatePrimitive(PrimitiveType.Cube); }
/// <summary> /// Methode zum Zeichnen einer geraden Straße. /// </summary> /// <param name="length">Die Länge der Straße.</param> /// <param name="minCars">Die minimale Anzahl der Autos auf dem Straßenteil.</param> /// <param name="maxCars">Die maximale Anzahl der Autos auf dem Straßenteil.</param> /// <param name="heightDifference">Der Höhenunterschied.</param> /// <param name="seed">Der Seed.</param> /// <returns>Die Straße.</returns> public ERRoad CreateStraight(float length, int minCars, int maxCars, float?heightDifference, string seed) { // Die Strecke neu holen this.network = new ERRoadNetwork(); this.network.BuildRoadNetwork(); // Den RoadType holen ERRoadType roadType = this.GetRandomRoadType(); // Hole die akutellen Streckenteile ERRoad[] currentRoads = network.GetRoads(); // Hole die Höhe der Strecke float fixHeightDifference = heightDifference ?? 0; // Lege die Positionen der Strecke an Vector3 startPosition = new Vector3(0, 0, 0); Vector3 middlePosition = new Vector3(0, fixHeightDifference / 2, length / 2); Vector3 endPosition = new Vector3(0, fixHeightDifference, length); ERRoad lastRoad = null; ERRoad road = null; if (currentRoads.Length > 0) { // Hole die letzte Strecke lastRoad = currentRoads.Last(); // Hole den letzten Punkt der Strecke Vector3[] markers = lastRoad.GetMarkerPositions(); Vector3 lastMarker = markers.Last(); // Die richtige Rotation ausrechnen Vector3 heading = (lastMarker - markers[markers.Length - 2]); Vector3 direction = heading / heading.magnitude; direction.y = 0; // Das Verhältnis zwischen x und z-Achse ausrechnen float x = direction.x / (direction.magnitude); float z = direction.z / (direction.magnitude); Vector3[] streetVectors = new Vector3[(int)length]; float heightPart = fixHeightDifference / length; for (int lengthPart = 0; lengthPart < length; lengthPart++) { streetVectors[lengthPart] = lastMarker + new Vector3(x * lengthPart, heightPart * lengthPart, z * lengthPart); } // Generiere Straße road = network.CreateRoad("Straight" + currentRoads.Length, roadType, streetVectors); } else { // Generiere erste Straße road = network.CreateRoad("Straight" + currentRoads.Length, roadType, new Vector3[] { startPosition, middlePosition, endPosition }); } // Erstelle die Strecke mit einem eindeutigen Namen customEasyRoads.Add(new CustomEasyRoad(car, road, minCars, maxCars, numberOfTracks)); return(road); }
void Start() { Debug_Log.Call_WriteLog("Please read the comments at the top of the runtime script (/Assets/EasyRoads3D/Scripts/runtimeScript) before using the runtime API!"); // Create Road Network object roadNetwork = new ERRoadNetwork(); // Create road // ERRoad road = roadNetwork.CreateRoad(string name); // ERRoad road = roadNetwork.CreateRoad(string name, Vector3[] markers); // ERRoad road = roadNetwork.CreateRoad(string name, ERRoadType roadType); // ERRoad road = roadNetwork.CreateRoad(string name, ERRoadType roadType, Vector3[] markers); // get exisiting road types // ERRoadType[] roadTypes = roadNetwork.GetRoadTypes(); // ERRoadType roadType = roadNetwork.GetRoadTypeByName(string name); // create a new road type ERRoadType roadType = new ERRoadType(); roadType.roadWidth = 6; roadType.roadMaterial = Resources.Load("Materials/roads/road material") as Material; // optional roadType.layer = 1; roadType.tag = "Untagged"; // roadType.hasMeshCollider = false; // default is true // roadType = roadNetwork.GetRoadTypeByName("Train Rail"); // Debug_Log.Call_WriteLog(roadType.roadMaterial); // create a new road Vector3[] markers = new Vector3[4]; markers[0] = new Vector3(200, 5, 200); markers[1] = new Vector3(250, 5, 200); markers[2] = new Vector3(250, 5, 250); markers[3] = new Vector3(300, 5, 250); road = roadNetwork.CreateRoad("road 1", roadType, markers); // road.SetResolution(float value):void; // Add Marker: ERRoad.AddMarker(Vector3); road.AddMarker(new Vector3(300, 5, 300)); // Add Marker: ERRoad.InsertMarker(Vector3); road.InsertMarker(new Vector3(275, 5, 235)); // road.InsertMarkerAt(Vector3 pos, int index): void; // Delete Marker: ERRoad.DeleteMarker(int index); road.DeleteMarker(2); // Set the road width : ERRoad.SetWidth(float width); // road.SetWidth(10); // Set the road material : ERRoad.SetMaterial(Material path); // Material mat = Resources.Load("Materials/roads/single lane") as Material; // road.SetMaterial(mat); // add / remove a meshCollider component // road.SetMeshCollider(bool value):void; // set the position of a marker // road.SetMarkerPosition(int index, Vector3 position):void; // road.SetMarkerPositions(Vector3[] position):void; // road.SetMarkerPositions(Vector3[] position, int index):void; // get the position of a marker // road.GetMarkerPosition(int index):Vector3; // get the position of a marker // road.GetMarkerPositions():Vector3[]; // Set the layer // road.SetLayer(int value):void; // Set the tag // road.SetTag(string value):void; // set marker control type // road.SetMarkerControlType(int marker, ERMarkerControlType type) : bool; // Spline, StraightXZ, StraightXZY, Circular // find a road // public static function ERRoadNetwork.GetRoadByName(string name) : ERRoad; // get all roads // public static function ERRoadNetwork.GetRoads() : ERRoad[]; // snap vertices to the terrain (no terrain deformation) // road.SnapToTerrain(true); // Build Road Network roadNetwork.BuildRoadNetwork(); // remove script components // roadNetwork.Finalize(); // Restore Road Network // roadNetwork.RestoreRoadNetwork(); // Show / Hide the white surfaces surrounding roads // public function roadNetwork.HideWhiteSurfaces(bool value) : void; // road.GetConnectionAtStart(): GameObject; // road.GetConnectionAtStart(out int connection): GameObject; // connections: 0 = bottom, 1= tip, 2 = left, 3 = right (the same for T crossings) // road.GetConnectionAtEnd(): GameObject; // road.GetConnectionAtEnd(out int connection): GameObject; // connections: 0 = bottom, 1= tip, 2 = left, 3 = right (the same for T crossings) // Snap the road vertices to the terrain following the terrain shape (no terrain deformation) // road.SnapToTerrain(bool value): void; // road.SnapToTerrain(bool value, float yOffset): void; // get the road length // road.GetLength() : float; // create dummy object go = GameObject.CreatePrimitive(PrimitiveType.Cube); }