Intersections provide a way to identify and style road intersections in the Maps SDK for Unity. You can use the metadata provided by intersection features to place road markings and stop signs and to create a traffic simulation to make your game feel more alive.
Intersections are generated as separate game objects with their own mesh. They lie on top of the existing road segments. Working with intersections is similar to working with other map features. You can subscribe to creation events through the MapsService component and make modifications to the intersection as it makes its way through the pipeline.
A traffic simulation example scene demonstrating the use of intersections for both styling and game logic is available in Unity's Project pane in the GoogleMaps > Examples > 04_Advanced > Intersections folder.
Enable intersections
Intersections rely on the road lattice and will not function unless the road lattice is enabled. To enable intersections using the MapsService component:
- From the Unity inspector, open the MapsService script component.
- Select the Enable Road Lattice checkbox under Map Feature Options > Road Lattice.
- Select the Enable Intersections checkbox under Map Feature Options.
You must also provide a default material that will be assigned to the generated
intersection game objects. The simplest way to do this is by adding the
StyleAttachment
component to the game object with the MapsService component
and assigning it a SegmentStyleSettings
(Assets -> Create -> Google Maps ->
Style Settings) with the Intersection Material field set to your material.
Alternatively, you can set the material as intersections are generated. For more
information, see Styling intersections.
Configure intersection parameters
The following intersection parameters can be configured using the
SegmentStyleSettings
asset assigned to the StyleAttachment
on your
MapsService game object.
Intersection Material
The default material applied to the intersection.
Intersection Arm Length
The length of the intersection arms, which are the non-overlapping regions of the intersection along each incoming road.
Intersection Join Length
The join length of the intersection. Two intersections with arms along the same road are joined if the length of the road is shorter than the join length. These arms are UV-mapped like the inner polygon, and are extended to cover the entire road.
In most cases, the ideal join length is slightly greater than twice the arm length to prevent overlapping arms from different intersections.
Max Intersection Arm Distance
The maximum distance an intersection arm may extend from the intersection center. Two roads may overlap indefinitely (usually caused by a large road width). In these cases, it may not be possible to generate an acceptable intersection. Setting a maximum arm distance prevents abnormally large intersections from being generated, with a reasonable value being about five times the arm length.
Styling intersections
Intersections are meshes rendered on top of the existing road geometry, with
one submesh for each arm, and one for the inner polygon. By default, the
material for each submesh is set to the one specified in SegmentStyleSettings
,
but you can change this on a case-by-case basis by listening to
the IntersectionEvents.WillCreate
event in MapsService.Events
and changing
IntersectionArm.Material
, which you can access through
WillCreateIntersectionArgs.MapFeature.Shape.Arms
.
Texture mapping
Intersections use two sets of UV coordinates.
World-space UV coordinates (channel 0)
These are set to the world position of each vertex, and are suitable for applying tileable textures such as asphalt or solid colors. These are the only useable coordinates for the inner polygon submesh.
Arm UV coordinates (channel 1)
Arm submeshes have UV coordinates normalized between (0,1). The v-coordinate begins at 0 closest to the intersection center and reaches 1 at the end furthest from the intersection. The u-coordinate ranges from 0 on the left to 1 on the right side of each intersection arm, where the 'left' side is determined as the arm leaves the intersection.
Using intersections
Identifying intersections in the road lattice
Intersections aren't just for styling. New nodes and edges are inserted into
the road lattice to correspond to each intersection's entry points, with the
first edge to enter an intersection flagged with
RoadLatticeEdge.Flags.Intersection
in RoadLatticeEdge.EdgeFlags
as shown in
the image below. The example scene also demonstrates how these edges can be
used to identify points where vehicles should pause.
In the above image, circles represent existing nodes, while diamonds represent new nodes inserted at the start of the intersection arms. The edges from these diamonds along the thick, green line are the flagged intersection edges.
Accessing intersection metadata
Metadata relating to the roads connecting to an intersection is available
through IntersectionArm
, which can be accessed through Intersection.Arms
.