ExampleDefaults.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. using Google.Maps.Feature.Style;
  2. using Google.Maps.Terrain;
  3. using UnityEngine;
  4. namespace Google.Maps.Examples.Shared {
  5. /// <summary>
  6. /// Default values for use in Maps SDK for Unity example scenes.
  7. /// </summary>
  8. internal static class ExampleDefaults {
  9. /// <summary>
  10. /// Default <see cref="GameObjectOptions"/> to put into
  11. /// <see cref="MapsService.LoadMap(Bounds,GameObjectOptions)"/> function.
  12. /// </summary>
  13. public static readonly GameObjectOptions DefaultGameObjectOptions;
  14. /// <summary>
  15. /// Default <see cref="Bounds"/> of area to load (specifically for an area of 500 by 500
  16. /// meters).
  17. /// </summary>
  18. public static readonly Bounds DefaultBounds =
  19. new Bounds(Vector3.zero, new Vector3(500, 0, 500));
  20. /// <summary>
  21. /// Setup default <see cref="GameObjectOptions"/>.
  22. /// </summary>
  23. static ExampleDefaults() {
  24. // Find shaders that will be used to create required Materials for rendering geometry
  25. // generated by the Maps SDK for Unity.
  26. Shader standardShader = Shader.Find("Google/Maps/Shaders/Standard");
  27. if (standardShader == null) {
  28. // Try to find the Unity Standard Shader as a backup.
  29. standardShader = Shader.Find("Standard");
  30. if (standardShader == null) {
  31. // Try to find the Legacy Diffuse Shader as a backup-backup.
  32. standardShader = Shader.Find("Diffuse");
  33. if (standardShader == null) {
  34. Debug.LogErrorFormat(
  35. "Unable to find Maps SDK for Unity Standard Shader (named " +
  36. "\"Google/Maps/Shaders/Standard\"), or as a backup the Unity Standard " +
  37. "Shader (named \"Standard\"), or as a backup-backup the Legacy Unity " +
  38. "Standard Shader (named \"Diffuse\"), so cannot setup default materials in {0}",
  39. typeof(ExampleDefaults));
  40. return;
  41. }
  42. Debug.LogWarningFormat(
  43. "Unable to find Maps SDK for Unity Standard Shader (named " +
  44. "\"Google/Maps/Shaders/Standard\"), or as a backup the Unity Standard Shader " +
  45. "(named \"Standard\")\nDefaulting to Legacy Unity Standard Shader (named" +
  46. "\"Diffuse\") as a backup-backup for setting up default materials in {0}",
  47. typeof(ExampleDefaults));
  48. } else {
  49. Debug.LogWarningFormat(
  50. "Unable to find Maps SDK for Unity Standard Shader (named " +
  51. "\"Google/Maps/Shaders/Standard\").\nDefaulting to the Unity Standard Shader " +
  52. "(named \"Standard\") as a backup for setting up default materials in {0}",
  53. typeof(ExampleDefaults));
  54. }
  55. }
  56. // Find BaseMaps Shader. Note that this Shader does not have a backup, as it has unique
  57. // behaviour needed for BaseMap level geometry to show in the correct render order.
  58. Shader baseMapShader = Shader.Find("Google/Maps/Shaders/BaseMap Color");
  59. if (baseMapShader == null) {
  60. Debug.LogErrorFormat(
  61. "Unable to find Maps SDK for Unity Base Map Shader (named " +
  62. "\"Google/Maps/Shaders/BaseMap Color\"), so unable to setup default materials in " +
  63. "{0}",
  64. typeof(ExampleDefaults));
  65. return;
  66. }
  67. // Create default materials for use by buildings, as well as other materials for use by water,
  68. // ground, roads, etc.
  69. Material wallMaterial = new Material(standardShader) { color = new Color(1f, 0.75f, 0.5f) };
  70. Material roofMaterial = new Material(standardShader) { color = new Color(1f, 0.8f, 0.6f) };
  71. Material regionMaterial = new Material(baseMapShader) {
  72. color = new Color(0.5f, 0.7f, 0.5f),
  73. };
  74. regionMaterial.SetFloat("_Glossiness", 1f);
  75. Material waterMaterial = new Material(baseMapShader) {
  76. color = new Color(0.0f, 1.0f, 1.0f),
  77. };
  78. waterMaterial.SetFloat("_Glossiness", 1f);
  79. Material segmentMaterial = new Material(baseMapShader) {
  80. color = new Color(0.5f, 0.5f, 0.5f),
  81. };
  82. segmentMaterial.SetFloat("_Glossiness", 0.5f);
  83. Material intersectionMaterial = new Material(baseMapShader) {
  84. color = new Color(0.4f, 0.4f, 0.4f),
  85. };
  86. intersectionMaterial.SetFloat("_Glossiness", 0.5f);
  87. // Create style for buildings made from extruded shapes (most buildings).
  88. ExtrudedStructureStyle extrudedStructureStyle =
  89. new ExtrudedStructureStyle
  90. .Builder { WallMaterial = wallMaterial, RoofMaterial = roofMaterial }
  91. .Build();
  92. // Create style for buildings with detailed vertex/triangle data (such as the Statue of
  93. // Liberty).
  94. ModeledStructureStyle modeledStructureStyle =
  95. new ModeledStructureStyle.Builder { Material = wallMaterial }.Build();
  96. // Create style for regions (such as parks).
  97. RegionStyle regionStyle = new RegionStyle.Builder { FillMaterial = regionMaterial }.Build();
  98. // Create style for bodies of water (such as oceans).
  99. AreaWaterStyle areaWaterStyle =
  100. new AreaWaterStyle.Builder { FillMaterial = waterMaterial }.Build();
  101. // Create style for lines of water (such as narrow rivers).
  102. LineWaterStyle lineWaterStyle =
  103. new LineWaterStyle.Builder { Material = waterMaterial }.Build();
  104. // Create style for segments (such as roads).
  105. SegmentStyle segmentStyle =
  106. new SegmentStyle.Builder { Material = segmentMaterial,
  107. IntersectionMaterial = intersectionMaterial, Width = 7.0f }.Build();
  108. // Collect styles into a form that can be given to map loading function.
  109. DefaultGameObjectOptions = new GameObjectOptions {
  110. ExtrudedStructureStyle = extrudedStructureStyle,
  111. ModeledStructureStyle = modeledStructureStyle,
  112. RegionStyle = regionStyle,
  113. AreaWaterStyle = areaWaterStyle,
  114. LineWaterStyle = lineWaterStyle,
  115. SegmentStyle = segmentStyle,
  116. };
  117. }
  118. }
  119. }