/// <summary> /// Receive map style /// </summary> /// <param name="type"><see cref="MapStyleData.Type"/> of map style</param> /// <returns>Map style as <see cref="GameObjectOptions"/></returns> /// <exception cref="ArgumentNullException">Thrown when dataset does not contain <see cref="MapStyleData"/> /// of given <see cref="MapStyleData.Type"/></exception> public GameObjectOptions GetMapStyle(MapStyleData.Type type) { var styleData = mapStyleData.FirstOrDefault(element => element.StyleType == type); if (styleData == null) { throw new ArgumentNullException(nameof(styleData), $"Style of {type} type does not exist!"); } var style = styleData.GenerateStyle(); // Terrain style generation var terrainStyleBuilder = new TerrainStyle.Builder() { AlphaMapResolutionMetersPerPixel = alphaMapResolutionMetersPerPixel, BaseMapResolutionMetersPerPixel = baseMapResolutionMetersPerPixel, BaseMapDistance = baseMapDistance, TerrainLayers = new List <TerrainLayer>(terrainLayers) }; style.TerrainStyle = terrainStyleBuilder.Build(); // Segment style with adjustments to the terrain style var segmentStyleBuilder = new SegmentStyle.Builder(style.SegmentStyle) { GameObjectLayer = style.TerrainStyle.TerrainPaintingLayer }; style.SegmentStyle = segmentStyleBuilder.Build(); // Area water style with adjustments to the terrain style var areaWaterStyleBuilder = new AreaWaterStyle.Builder(style.AreaWaterStyle) { GameObjectLayer = style.TerrainStyle.TerrainPaintingLayer }; style.AreaWaterStyle = areaWaterStyleBuilder.Build(); // Region style with adjustments to the terrain style var regionStyleBuilder = new RegionStyle.Builder(style.RegionStyle) { GameObjectLayer = style.TerrainStyle.TerrainPaintingLayer }; style.RegionStyle = regionStyleBuilder.Build(); // Line water style with adjustments to the terrain style var lineWaterStyleBuilder = new LineWaterStyle.Builder(style.LineWaterStyle) { GameObjectLayer = style.TerrainStyle.TerrainPaintingLayer }; style.LineWaterStyle = lineWaterStyleBuilder.Build(); return(style); }
/// <summary> /// Configures the map. /// </summary> private void Start() { MapsService mapsService = GetComponent <MapsService>(); mapsService.InitFloatingOrigin(LoadingCenter); Material adminAreaMaterial = BaseMapMaterialUtils.CreateUniformColoredMaterial(AdminAreaColor); // Configure the map to only show prefectures and texture them with a solid color. mapsService.Events.RegionEvents.WillCreate.AddListener(args => { if (args.MapFeature.Metadata.Usage == RegionMetadata.UsageType.AdministrativeArea1) { RegionStyle.Builder style = args.Style.AsBuilder(); style.FillMaterial = adminAreaMaterial; args.Style = style.Build(); } else { args.Cancel = true; } }); // When a prefecture GameObject is created, create an extruded outline for it. Material borderMaterial = null; mapsService.Events.RegionEvents.DidCreate.AddListener(args => { if (borderMaterial == null) { // Set up the border material based on the prefecture material, but change the render queue // so that it renders on top. borderMaterial = new Material(args.GameObject.GetComponent <Renderer>().material); borderMaterial.color = BorderColor; borderMaterial.renderQueue++; } Extruder.AddAreaExternalOutline(args.GameObject, borderMaterial, args.MapFeature.Shape, BorderWidth); }); // Don't display water. mapsService.Events.AreaWaterEvents.WillCreate.AddListener(args => { args.Cancel = true; }); // Load the map around Japan. mapsService.MakeMapLoadRegion() .AddCircle(mapsService.Coords.FromLatLngToVector3(LoadingCenter), 2000000) // 2,000 km. .Load(ExampleDefaults.DefaultGameObjectOptions, 5); // Zoom level 5. }
/// <summary> /// Setup default <see cref="GameObjectOptions"/>. /// </summary> static ExampleDefaults() { // Find shaders that will be used to create required Materials for rendering geometry // generated by the Maps SDK for Unity. Shader standardShader = Shader.Find("Google/Maps/Shaders/Standard"); if (standardShader == null) { // Try to find the Unity Standard Shader as a backup. standardShader = Shader.Find("Standard"); if (standardShader == null) { // Try to find the Legacy Diffuse Shader as a backup-backup. standardShader = Shader.Find("Diffuse"); if (standardShader == null) { Debug.LogErrorFormat( "Unable to find Maps SDK for Unity Standard Shader (named " + "\"Google/Maps/Shaders/Standard\"), or as a backup the Unity Standard " + "Shader (named \"Standard\"), or as a backup-backup the Legacy Unity " + "Standard Shader (named \"Diffuse\"), so cannot setup default materials in {0}", typeof(ExampleDefaults)); return; } Debug.LogWarningFormat( "Unable to find Maps SDK for Unity Standard Shader (named " + "\"Google/Maps/Shaders/Standard\"), or as a backup the Unity Standard Shader " + "(named \"Standard\")\nDefaulting to Legacy Unity Standard Shader (named" + "\"Diffuse\") as a backup-backup for setting up default materials in {0}", typeof(ExampleDefaults)); } else { Debug.LogWarningFormat( "Unable to find Maps SDK for Unity Standard Shader (named " + "\"Google/Maps/Shaders/Standard\").\nDefaulting to the Unity Standard Shader " + "(named \"Standard\") as a backup for setting up default materials in {0}", typeof(ExampleDefaults)); } } // Find BaseMaps Shader. Note that this Shader does not have a backup, as it has unique // behaviour needed for BaseMap level geometry to show in the correct render order. Shader baseMapShader = Shader.Find("Google/Maps/Shaders/BaseMap Color"); if (baseMapShader == null) { Debug.LogErrorFormat( "Unable to find Maps SDK for Unity Base Map Shader (named " + "\"Google/Maps/Shaders/BaseMap Color\"), so unable to setup default materials in " + "{0}", typeof(ExampleDefaults)); return; } // Create default materials for use by buildings, as well as other materials for use by water, // ground, roads, etc. Material wallMaterial = new Material(standardShader) { color = new Color(1f, 0.75f, 0.5f) }; Material roofMaterial = new Material(standardShader) { color = new Color(1f, 0.8f, 0.6f) }; Material regionMaterial = new Material(baseMapShader) { color = new Color(0.5f, 0.7f, 0.5f), }; regionMaterial.SetFloat("_Glossiness", 1f); Material waterMaterial = new Material(baseMapShader) { color = new Color(0.0f, 1.0f, 1.0f), }; waterMaterial.SetFloat("_Glossiness", 1f); Material segmentMaterial = new Material(baseMapShader) { color = new Color(0.5f, 0.5f, 0.5f), }; segmentMaterial.SetFloat("_Glossiness", 0.5f); // Create style for buildings made from extruded shapes (most buildings). ExtrudedStructureStyle extrudedStructureStyle = new ExtrudedStructureStyle .Builder { WallMaterial = wallMaterial, RoofMaterial = roofMaterial } .Build(); // Create style for buildings with detailed vertex/triangle data (such as the Statue of // Liberty). ModeledStructureStyle modeledStructureStyle = new ModeledStructureStyle.Builder { Material = wallMaterial }.Build(); // Create style for regions (such as parks). RegionStyle regionStyle = new RegionStyle.Builder { FillMaterial = regionMaterial }.Build(); // Create style for bodies of water (such as oceans). AreaWaterStyle areaWaterStyle = new AreaWaterStyle.Builder { FillMaterial = waterMaterial }.Build(); // Create style for lines of water (such as narrow rivers). LineWaterStyle lineWaterStyle = new LineWaterStyle.Builder { Material = waterMaterial }.Build(); // Create style for segments (such as roads). SegmentStyle segmentStyle = new SegmentStyle.Builder { Material = segmentMaterial, Width = 7.0f }.Build(); // Collect styles into a form that can be given to map loading function. DefaultGameObjectOptions = new GameObjectOptions { ExtrudedStructureStyle = extrudedStructureStyle, ModeledStructureStyle = modeledStructureStyle, RegionStyle = regionStyle, AreaWaterStyle = areaWaterStyle, LineWaterStyle = lineWaterStyle, SegmentStyle = segmentStyle, }; }
/// <summary> /// Returns a new <see cref="GameObjectOptions"/> instance reflecting the properties on /// this component. /// </summary> private GameObjectOptions CreateGameObjectOptions() { GameObjectOptions gameObjectOptions = new GameObjectOptions(); // Terrain style TerrainStyle.Builder terrainStyleBuilder = new TerrainStyle.Builder() { AlphaMapResolutionMetersPerPixel = AlphaMapResolutionMetersPerPixel, BaseMapResolutionMetersPerPixel = BaseMapResolutionMetersPerPixel, BaseMapDistance = BaseMapDistance, TerrainLayers = new List <TerrainLayer>(TerrainLayers), }; gameObjectOptions.TerrainStyle = terrainStyleBuilder.Build(); // Segment style SegmentStyle.Builder segmentStyleBuilder = new SegmentStyle.Builder() { Material = SegmentMaterial, IntersectionMaterial = IntersectionMaterial, GameObjectLayer = gameObjectOptions.TerrainStyle.TerrainPaintingLayer }; gameObjectOptions.SegmentStyle = segmentStyleBuilder.Build(); // Area water style AreaWaterStyle.Builder areaWaterStyleBuilder = new AreaWaterStyle.Builder() { FillMaterial = AreaWaterMaterial, GameObjectLayer = gameObjectOptions.TerrainStyle.TerrainPaintingLayer }; gameObjectOptions.AreaWaterStyle = areaWaterStyleBuilder.Build(); // Region style RegionStyle.Builder regionStyleBuilder = new RegionStyle.Builder() { FillMaterial = RegionMaterial, GameObjectLayer = gameObjectOptions.TerrainStyle.TerrainPaintingLayer }; gameObjectOptions.RegionStyle = regionStyleBuilder.Build(); // Line water style LineWaterStyle.Builder lineWaterStyleBuilder = new LineWaterStyle.Builder() { Material = LineWaterMaterial, GameObjectLayer = gameObjectOptions.TerrainStyle.TerrainPaintingLayer }; gameObjectOptions.LineWaterStyle = lineWaterStyleBuilder.Build(); // Extruded structure style ExtrudedStructureStyle.Builder extrudedStructureStyleBuilder = new ExtrudedStructureStyle.Builder() { RoofMaterial = BuildingMaterial, WallMaterial = BuildingMaterial }; gameObjectOptions.ExtrudedStructureStyle = extrudedStructureStyleBuilder.Build(); // Modeled structure style ModeledStructureStyle.Builder modeledStructureStyleBuilder = new ModeledStructureStyle.Builder() { Material = BuildingMaterial }; gameObjectOptions.ModeledStructureStyle = modeledStructureStyleBuilder.Build(); return(gameObjectOptions); }
/// <summary> /// Connect to <see cref="FloatingOriginUpdater.OnFloatingOriginUpdate"/>, so that whenever the /// world's Floating Origin is moved, all controlled world space textured /// <see cref="WorldspaceMaterials"/> are realigned to the world's new Floating Origin. /// </summary> private void Start() { // Verify all required parameters are defined, skipping further setup if not. if (!VerifyParameters()) { enabled = false; return; } // Get the required Floating Origin component on this GameObject. FloatingOriginUpdater floatingOriginUpdater = GetComponent <FloatingOriginUpdater>(); // Make sure that whenever the Floating Origin is updated, Materials are updated in sync. floatingOriginUpdater.OnFloatingOriginUpdate.AddListener(UpdateWorldspaceMaterialOffsets); // Store all Materials that are to be updated. This function only stores Materials that have an // _Offset Vector, which will be used to offset the Material's world space coordinates to align // to the world's moved Floating Origin. TryAddMaterial(Buildings); TryAddMaterial(Roads); TryAddMaterial(Ground); TryAddMaterial(Water); // Because the Floating Origin has not yet been moved, there is no need to apply an _Offset to // these Materials yet. Instead we make sure these Material's _Offsets start at (0, 0, 0). UpdateWorldspaceMaterialOffsets(Vector3.zero); // Create styles to assign world space textured Materials to geometry as it is created. ExtrudedStructureStyle extrudedStructureStyle = new ExtrudedStructureStyle.Builder { WallMaterial = Buildings, RoofMaterial = Buildings }.Build(); ModeledStructureStyle modeledStructureStyle = new ModeledStructureStyle.Builder { Material = Buildings }.Build(); SegmentStyle roadsStyle = new SegmentStyle.Builder { Material = Roads, BorderMaterial = Borders, Width = 7.0f, BorderWidth = BorderWidth }.Build(); RegionStyle groundStyle = new RegionStyle.Builder { FillMaterial = Ground }.Build(); AreaWaterStyle areaWaterStyle = new AreaWaterStyle.Builder { FillMaterial = Water }.Build(); LineWaterStyle lineWaterStyle = new LineWaterStyle.Builder { Material = Water, Width = 7.0f }.Build(); GameObjectOptions renderingStyles = ExampleDefaults.DefaultGameObjectOptions; renderingStyles.ExtrudedStructureStyle = extrudedStructureStyle; renderingStyles.ModeledStructureStyle = modeledStructureStyle; renderingStyles.SegmentStyle = roadsStyle; renderingStyles.RegionStyle = groundStyle; renderingStyles.AreaWaterStyle = areaWaterStyle; renderingStyles.LineWaterStyle = lineWaterStyle; DynamicMapsService dynamicMapsService = floatingOriginUpdater.DynamicMapsService; dynamicMapsService.RenderingStyles = renderingStyles; // Make sure that if any new Materials are cloned by the Maps Service (which can occur to // resolve z-fighting issues), that these new cloned materials are added to the list of managed // Materials. MapsService mapsService = dynamicMapsService.MapsService; mapsService.Events.RegionEvents.DidCreate.AddListener( args => TryAddMaterialFrom(args.GameObject)); mapsService.Events.AreaWaterEvents.DidCreate.AddListener( args => TryAddMaterialFrom(args.GameObject)); mapsService.Events.LineWaterEvents.DidCreate.AddListener( args => TryAddMaterialFrom(args.GameObject)); mapsService.Events.SegmentEvents.DidCreate.AddListener( args => TryAddMaterialFrom(args.GameObject)); mapsService.Events.ModeledStructureEvents.DidCreate.AddListener( args => TryAddMaterialFrom(args.GameObject)); // For extruded buildings, also create borders around the edges of these buildings, to match // borders around roads. mapsService.Events.ExtrudedStructureEvents.DidCreate.AddListener(args => { Debug.Log(args.MapFeature.MapFeatureMetadata.PlaceId); TryAddMaterialFrom(args.GameObject); Extruder.AddBuildingBorder(args.GameObject, args.MapFeature.Shape, Borders, BorderWidth); }); }