Ejemplo n.º 1
0
    /// <summary>
    /// Get <see cref="MapsService"/> and use it to load geometry.
    /// </summary>
    private void Start()
    {
        // Get required Maps Service component on this GameObject.
        MapsService mapsService = GetComponent <MapsService>();

        // Set location to load.
        mapsService.InitFloatingOrigin(LatLng);

        // Create a roads style that defines a material for roads and for borders of roads. The specific
        // border material used is chosen to look just a little darker than the material of the ground
        // plane (helping the roads to visually blend into the surrounding ground).
        SegmentStyle roadsStyle = new SegmentStyle.Builder {
            Material       = Roads,
            BorderMaterial = RoadEdges,
            Width          = 7.0f,
            BorderWidth    = 1.0f
        }.Build();

        // Get default style options.
        GameObjectOptions renderingStyles = ExampleDefaults.DefaultGameObjectOptions;

        // Replace default roads style with new, just created roads style.
        renderingStyles.SegmentStyle = roadsStyle;

        // Load map with desired options.
        mapsService.LoadMap(ExampleDefaults.DefaultBounds, renderingStyles);
    }
Ejemplo n.º 2
0
        /// <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);
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Checks Metadata, and assigns a new material to any road that is marked as private.
 /// </summary>
 /// <param name="args">Segment creation event arguments.</param>
 private void WillCreateHandler(WillCreateSegmentArgs args)
 {
     if (args.MapFeature.Metadata.IsPrivate)
     {
         // If the segment that is being created is marked private, change the material in the style
         // used to display the segment.
         SegmentStyle.Builder style = args.Style.AsBuilder();
         style.Material = PrivateRoadMaterial;
         args.Style     = style.Build();
     }
 }
Ejemplo n.º 4
0
    /// <summary>
    /// Create a <see cref="MapsService"/> to load buildings, then add borders around their bases and
    /// around the edges of roads.
    /// </summary>
    private void Start()
    {
        // Verify a Building Base Material has been given.
        if (BuildingAndRoadBorder == null)
        {
            Debug.LogError(ExampleErrors.MissingParameter(this, BuildingAndRoadBorder,
                                                          "Building And Road Border", "to apply around the bases of buildings"));
            return;
        }

        // Verify a Roads Material has been given.
        if (Roads == null)
        {
            Debug.LogError(ExampleErrors.MissingParameter(this, Roads, "Roads", "to apply to roads"));
            return;
        }

        // Get the required Dynamic Maps Service on this GameObject.
        DynamicMapsService dynamicMapsService = GetComponent <DynamicMapsService>();

        // Create a roads style that defines a material for roads and for borders of roads. The specific
        // border material used is chosen to look just a little darker than the material of the ground
        // plane (helping the roads to visually blend into the surrounding ground).
        SegmentStyle roadsStyle = new SegmentStyle.Builder {
            Material       = Roads,
            BorderMaterial = BuildingAndRoadBorder,
            Width          = 7.0f,
            BorderWidth    = 1.0f
        }.Build();

        // Get default style options.
        GameObjectOptions renderingStyles = ExampleDefaults.DefaultGameObjectOptions;

        // Replace default roads style with new, just created roads style.
        renderingStyles.SegmentStyle = roadsStyle;

        // Get required BuildingTexturer component on this GameObject.
        BuildingTexturer buildingTexturer = GetComponent <BuildingTexturer>();

        // Sign up to event called after each new building is loaded, so can assign Materials to this
        // new building, and add an extruded base around the building to fake an Ambient Occlusion
        // contact shadow. Note that:
        // - DynamicMapsService.MapsService is auto-found on first access (so will not be null).
        // - This event must be set now during Awake, so that when Dynamic Maps Service starts loading
        //   the map during Start, this event will be triggered for all Extruded Structures.
        dynamicMapsService.MapsService.Events.ExtrudedStructureEvents.DidCreate.AddListener(args => {
            // Apply nine sliced wall and roof materials to this building.
            //  buildingTexturer.AssignNineSlicedMaterials(args.GameObject);

            // Add a border around base to building using Building Border Builder class, coloring it using
            // the given border Material.
            Extruder.AddBuildingBorder(args.GameObject, args.MapFeature.Shape, BuildingAndRoadBorder);
        });
    }
Ejemplo n.º 5
0
        /// <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,
            };
        }
Ejemplo n.º 6
0
    /// <summary>
    /// Use <see cref="MapsService"/> to load geometry, setting the widths of all roads by their type.
    /// </summary>
    private void Awake()
    {
        // Get the required Dynamic Maps Service on this GameObject.
        DynamicMapsService dynamicMapsService = GetComponent <DynamicMapsService>();
        SegmentStyle       defaultSegmentStyle = new SegmentStyle.Builder {
            Width = DefaultWidth
        }.Build();

        // Create a Dictionary of separate styles, one for each type of road. The only difference
        // between each of these styles is that each of them has a different width applied to it,
        // resulting in a different road width per style.
        var roadWidths = new Dictionary <SegmentMetadata.UsageType, SegmentStyle> {
            { SegmentMetadata.UsageType.Unspecified, defaultSegmentStyle },
            { SegmentMetadata.UsageType.Road, defaultSegmentStyle },
            { SegmentMetadata.UsageType.LocalRoad,
              new SegmentStyle.Builder {
                  Width = LocalRoadWidth
              }.Build() },
            { SegmentMetadata.UsageType.ArterialRoad,
              new SegmentStyle.Builder {
                  Width = MajorRoadWidth
              }.Build() },
            { SegmentMetadata.UsageType.Highway,
              new SegmentStyle.Builder {
                  Width = HighwayWidth
              }.Build() },
            { SegmentMetadata.UsageType.ControlledAccessHighway,
              new SegmentStyle.Builder {
                  Width = ControlledAccessHighwayWidth
              }.Build() },
            { SegmentMetadata.UsageType.Footpath,
              new SegmentStyle.Builder {
                  Width = FootpathWidth
              }.Build() },
            { SegmentMetadata.UsageType.Rail,
              new SegmentStyle.Builder {
                  Width = RailWidth
              }.Build() },
            { SegmentMetadata.UsageType.Ferry,
              new SegmentStyle.Builder {
                  Width = FerryLaneWidth
              }.Build() },
        };

        // Sign up to event called just before any new road (segment) is loaded, so can assign it a
        // width based on its type. Note that:
        // - DynamicMapsService.MapsService is auto-found on first access (so will not be null).
        // - This event must be set now during Awake, so that when Dynamic Maps Service starts loading
        //   the map during Start, this event will be triggered for all Roads.
        dynamicMapsService.MapsService.Events.SegmentEvents.WillCreate.AddListener(args => {
            // Make sure we have defined a width for this specific type of road. This should be the case,
            // as the above Dictionary includes all currently available road types.
            SegmentMetadata.UsageType roadType = args.MapFeature.Metadata.Usage;
            if (roadWidths.ContainsKey(roadType))
            {
                // Tell the SDK to build this specific road using the style containing the width for this
                // type of road.
                args.Style = roadWidths[roadType];
            }
            else
            {
                // If a style has not been defined for this specific type of road, warn the developer.
                Debug.LogWarningFormat(
                    "No road width defined for road of type {0}, using the default road width.\n" +
                    roadType);
                args.Style = defaultSegmentStyle;
            }
        });
    }
Ejemplo n.º 7
0
        /// <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);
        }
Ejemplo n.º 8
0
    /// <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);
        });
    }