public Intersection(Vector3 position, IntersectionCustomization customization) { this.customization = customization; this.connectedRoads = new List <Road> (); _position = position; this.builder = GameObject.FindWithTag(GlobalTags.Builder).GetComponent <IntersectionBuilder> (); // TODO Performance optimizaiton: this always build an empty game object this.gameObject = this.builder.BuildIntersection(this); }
private GameObject createParentIntersectionWithChildIntersections(string name, IntersectionCustomization customization, params GameObject[] childs) { GameObject obj = new GameObject(name, typeof(Rigidbody)); obj.tag = GlobalTags.Intersection; obj.layer = customization.isTemporary ? GlobalLayers.IgnoreRaycast : GlobalLayers.Default; foreach (GameObject c in childs) { c.transform.parent = obj.transform; c.tag = GlobalTags.Untagged; c.layer = customization.isTemporary ? GlobalLayers.IgnoreRaycast : GlobalLayers.Default; } // Kinematic to prevent gravity obj.GetComponent <Rigidbody> ().isKinematic = true; return(obj); }
// this method will elevate intersection private GameObject createGameObjectWithPrefabPositionAndMesh(GameObject prefab, Vector3 position, Mesh mesh, IntersectionCustomization customization) { // move up a little bit // TODO Fix magic numbers Vector3 elevatedPos = position + new Vector3(0, 0.02f, 0); GameObject intersectionObj = Instantiate(prefab, elevatedPos, Quaternion.identity); MeshFilter objMeshFilter = intersectionObj.GetComponent <MeshFilter> (); objMeshFilter.mesh = mesh; //TODO : Change to Sphere Collider instead of Mesh Collider MeshCollider objMeshCollider = intersectionObj.GetComponent <MeshCollider> (); objMeshCollider.sharedMesh = mesh; intersectionObj.layer = customization.isTemporary ? GlobalLayers.IgnoreRaycast : GlobalLayers.Default; return(intersectionObj); }
// smoothness : number of triangles to create private GameObject buildOuter(Vector3 fromPoint, Vector3 toPoint, int smoothness, Vector3 position, IntersectionCustomization customization) { // swap from to ponit if necessary if (Vector3.Cross(fromPoint, toPoint).y < 0) { Vector3 temp = fromPoint; fromPoint = toPoint; toPoint = temp; } // Outer is mimicking to one way road end, // TODO delete one way road end, use this method // DEBUG HINT: /** * e.g. s = 3 * * 0 2 4 * | / + * | / + * |/+------ 6 * 1 3 5 * * Triangles : 0 2 1, 2 4 3, 4 6 5 * * * UV: * 0 2 4 6 * * 1 3 5 * * */ Mesh mesh = new Mesh(); Vector3[] vertices = new Vector3[2 * smoothness + 1]; Vector2[] uv = new Vector2[vertices.Length]; Vector3[] normals = new Vector3[vertices.Length]; for (int i = 0; i < smoothness + 1; i++) { vertices [2 * i] = Vector3.Slerp(fromPoint, toPoint, (float)i / smoothness); uv [2 * i] = Vector2.Lerp(new Vector2(0, 0), new Vector2(1, 0), (float)i / smoothness); normals [2 * i] = Vector3.up; if (i != smoothness) { vertices [2 * i + 1] = Vector3.zero; uv [2 * i + 1] = Vector2.Lerp(new Vector2(0, 1), new Vector2(1, 1), (float)i / smoothness) + new Vector2(1f, 0) / smoothness / 2; normals [2 * i + 1] = Vector3.up; } } mesh.vertices = vertices; mesh.uv = uv; mesh.normals = normals; int[] triangles = new int[smoothness * 3]; for (int i = 0; i < smoothness; i++) { triangles [3 * i] = 2 * i; triangles [3 * i + 1] = 2 * i + 2; triangles [3 * i + 2] = 2 * i + 1; } mesh.triangles = triangles; GameObject sectorRoad = createGameObjectWithPrefabPositionAndMesh(oneWayIntersectionPrefab, position, mesh, customization); // the outer is lower than normal intersection, same as roads sectorRoad.transform.Translate(new Vector3(0, -0.01f, 0)); return(sectorRoad); }
// this builds a quadrilateral shaped mesh, with four points /** * r * * * * f t * * z * * r -- refpoint * f -- frompoint * t -- topoint * z -- Vector3.zero */ private GameObject buildInner(Vector3 fromPoint, Vector3 toPoint, Vector3 refPoint, Vector3 position, IntersectionCustomization customization) { // swap from to ponit if necessary if (Vector3.Cross(fromPoint, toPoint).y < 0) { Vector3 temp = fromPoint; fromPoint = toPoint; toPoint = temp; } Mesh mesh = new Mesh(); Vector3[] vertices = { refPoint, fromPoint, toPoint, Vector3.zero }; int[] triangles = { 0, 2, 3, 3, 1, 0 }; Vector3[] normals = { Vector3.up, Vector3.up, Vector3.up, Vector3.up }; mesh.vertices = vertices; mesh.triangles = triangles; mesh.normals = normals; return(createGameObjectWithPrefabPositionAndMesh(oneWayIntersectionPrefab, position, mesh, customization)); }