Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ namespace Mapbox.Unity.MeshGeneration.Factories
using Mapbox.Unity.MeshGeneration.Data;
using Mapbox.Platform;

/// <summary>
/// Corresponds to Mapbox Api end points like raster image, vector tile or directions. Makes the web calls and handles the response.
/// </summary>
public class Factory : ScriptableObject
{
protected IFileSource FileSource;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ public enum MapImageType
None
}

/// <summary>
/// Uses raster image services to create materials & textures for terrain
/// </summary>
[CreateAssetMenu(menuName = "Mapbox/Factories/Map Image Factory")]
public class MapImageFactory : Factory
{
Expand Down Expand Up @@ -51,6 +54,11 @@ public override void Update()
}
}

/// <summary>
/// Fetches the image and applies it to tile material.
/// MapImage factory currently supports both new (RasterTile) and classic (ClassicRasterTile) Mapbox styles.
/// </summary>
/// <param name="tile"></param>
private void Run(UnityTile tile)
{
if (!string.IsNullOrEmpty(_mapId))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ namespace Mapbox.Unity.MeshGeneration.Factories
using Mapbox.Unity.MeshGeneration.Interfaces;
using Mapbox.Platform;

/// <summary>
/// Uses vector tile api to visualize vector data.
/// Fetches the vector data for given tile and passes layer data to layer visualizers.
/// </summary>
[CreateAssetMenu(menuName = "Mapbox/Factories/Mesh Factory")]
public class MeshFactory : Factory
{
Expand All @@ -17,6 +21,10 @@ public class MeshFactory : Factory
private Dictionary<Vector2, UnityTile> _tiles;
private Dictionary<string, List<LayerVisualizerBase>> _layerBuilder;

/// <summary>
/// Sets up the Mesh Factory
/// </summary>
/// <param name="fs"></param>
public override void Initialize(IFileSource fs)
{
base.Initialize(fs);
Expand All @@ -42,6 +50,10 @@ public override void Register(UnityTile tile)
Run(tile);
}

/// <summary>
/// Mesh Factory waits for both Height and Image data to be processed if they are requested
/// </summary>
/// <param name="tile"></param>
private void Run(UnityTile tile)
{
if (tile.HeightDataState == TilePropertyState.Loading ||
Expand All @@ -68,6 +80,12 @@ private void ImageDataChangedHandler(UnityTile t, object e)
CreateMeshes(t, e);
}


/// <summary>
/// Fetches the vector data and passes each layer to relevant layer visualizers
/// </summary>
/// <param name="tile"></param>
/// <param name="e"></param>
private void CreateMeshes(UnityTile tile, object e)
{
tile.HeightDataChanged -= HeightDataChangedHandler;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ public enum MapIdType
Custom
}

/// <summary>
/// Uses Mapbox Terrain api and creates terrain meshes.
/// </summary>
[CreateAssetMenu(menuName = "Mapbox/Factories/Terrain Factory")]
public class TerrainFactory : Factory
{
Expand Down Expand Up @@ -57,6 +60,9 @@ public override void Register(UnityTile tile)
Run(tile);
}

/// <summary>
/// Clears the mesh data and re-runs the terrain creation procedure using current settings. Clearing the old mesh data is important as terrain stitching function checks if the data exists or not.
/// </summary>
public override void Update()
{
base.Update();
Expand All @@ -69,7 +75,7 @@ public override void Update()
Run(tile);
}
}

private void Run(UnityTile tile)
{
if (_generationType == TerrainGenerationType.Height)
Expand All @@ -86,6 +92,11 @@ private void Run(UnityTile tile)
}
}

/// <summary>
/// Creates the non-flat terrain using a height multiplier
/// </summary>
/// <param name="tile"></param>
/// <param name="heightMultiplier">Multiplier for queried height value</param>
private void CreateTerrainHeight(UnityTile tile, float heightMultiplier = 1)
{
if (tile.HeightData == null)
Expand Down Expand Up @@ -120,6 +131,12 @@ private void CreateTerrainHeight(UnityTile tile, float heightMultiplier = 1)
}
}

/// <summary>
/// Creates the non-flat terrain mesh, using a grid by defined resolution (_sampleCount). Vertex order goes right & up. Normals are calculated manually and UV map is fitted/stretched 1-1.
/// Any additional scripts or logic, like MeshCollider or setting layer, can be done here.
/// </summary>
/// <param name="tile"></param>
/// <param name="heightMultiplier">Multiplier for queried height value</param>
private void GenerateTerrainMesh(UnityTile tile, float heightMultiplier)
{
var go = tile.gameObject;
Expand All @@ -144,7 +161,6 @@ private void GenerateTerrainMesh(UnityTile tile, float heightMultiplier)
(int)((1 - yrat) * 255)),
tile.RelativeScale),
(float)(yy - tile.Rect.Center.y)));
mesh.Normals.Add(Vector3.up);
mesh.UV[0].Add(new Vector2(x * step, 1 - (y * step)));
}
}
Expand Down Expand Up @@ -205,6 +221,11 @@ private void GenerateTerrainMesh(UnityTile tile, float heightMultiplier)
//go.layer = LayerMask.NameToLayer("terrain");
}

/// <summary>
/// Creates a basic quad to be used as flat base mesh. Normals are up and UV is fitted to quad.
/// A quad is enough for basic usage but the resolution should be increased if any mesh deformation, like bending, to work.
/// </summary>
/// <param name="tile"></param>
private void CreateFlatMesh(UnityTile tile)
{
var mesh = new Mesh();
Expand Down Expand Up @@ -236,6 +257,11 @@ private void CreateFlatMesh(UnityTile tile)
//go.layer = LayerMask.NameToLayer("terrain");
}

/// <summary>
/// Checkes all neighbours of the given tile and stitches the edges to achieve a smooth mesh surface.
/// </summary>
/// <param name="tile"></param>
/// <param name="tmesh"></param>
private void FixStitches(UnityTile tile, MeshData tmesh)
{
_stitchTarget.Set(tile.TileCoordinate.x, tile.TileCoordinate.y - 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ namespace Mapbox.Unity.MeshGeneration.Interfaces
using UnityEngine;
using Mapbox.Unity.MeshGeneration.Data;

/// <summary>
/// Layer visualizers contains sytling logic and processes features
/// </summary>
public abstract class LayerVisualizerBase : ScriptableObject
{
public bool Active = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ public class TypeVisualizerTuple
public ModifierStackBase Stack;
}


/// <summary>
/// VectorLayerVisualizer is a specialized layer visualizer working on polygon and line based vector data (i.e. building, road, landuse) using modifier stacks.
/// Each feature is preprocessed and passed down to a modifier stack, which will create and return a game object for that given feature.
/// Key is the name of the layer to be processed.
/// Classification Key is the property name to be used for stack selection.
/// It also supports filters; objects that goes over features and decides if it'll be visualized or not.
/// Default Stack is the stack that'll be used for any feature that passes the filters but isn't matched to any special stack.
///
/// </summary>
[CreateAssetMenu(menuName = "Mapbox/Layer Visualizer/Vector Layer Visualizer")]
public class VectorLayerVisualizer : LayerVisualizerBase
{
Expand Down Expand Up @@ -44,6 +54,11 @@ public override string Key

private GameObject _container;

/// <summary>
/// Creates an object for each layer, extract and filter in/out the features and runs Build method on them.
/// </summary>
/// <param name="layer"></param>
/// <param name="tile"></param>
public override void Create(VectorTileLayer layer, UnityTile tile)
{
_container = new GameObject(Key + " Container");
Expand Down Expand Up @@ -88,9 +103,14 @@ public override void Create(VectorTileLayer layer, UnityTile tile)
}
}

/// <summary>
/// Preprocess features, finds the relevant modifier stack and passes the feature to that stack
/// </summary>
/// <param name="feature"></param>
/// <param name="tile"></param>
/// <param name="parent"></param>
private void Build(VectorFeatureUnity feature, UnityTile tile, GameObject parent)
{

if (feature.Properties.ContainsKey("extrude") && !bool.Parse(feature.Properties["extrude"].ToString()))
return;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ namespace Mapbox.Unity.MeshGeneration
using Mapbox.Unity.Utilities;
using Utils;

/// <summary>
/// MapController is just an helper class imitating the game/app logic controlling the map. It creates and passes the tiles requests to MapVisualization.
/// </summary>
public class MapController : MonoBehaviour
{
public static RectD ReferenceTileRect { get; set; }
Expand All @@ -27,6 +30,9 @@ public class MapController : MonoBehaviour
private GameObject _root;
private Dictionary<Vector2, UnityTile> _tiles;

/// <summary>
/// Resets the map controller and initializes the map visualization
/// </summary>
public void Awake()
{
MapVisualization.Initialize(MapboxAccess.Instance);
Expand All @@ -38,6 +44,9 @@ public void Start()
Execute();
}

/// <summary>
/// Pulls the root world object to origin for ease of use/view
/// </summary>
public void Update()
{
if (_snapYToZero)
Expand All @@ -58,6 +67,13 @@ public void Execute()
Execute(double.Parse(parm[0]), double.Parse(parm[1]), Zoom, Range);
}

/// <summary>
/// World creation call used in the demos. Destroys and existing worlds and recreates another one.
/// </summary>
/// <param name="lat">Latitude of the requested point</param>
/// <param name="lng">Longitude of the requested point</param>
/// <param name="zoom">Zoom/Detail level of the world</param>
/// <param name="frame">Tiles to load around central tile in each direction; west-north-east-south</param>
public void Execute(double lat, double lng, int zoom, Vector4 frame)
{
//frame goes left-top-right-bottom here
Expand Down Expand Up @@ -104,6 +120,11 @@ public void Execute(double lat, double lng, int zoom, int range)
Execute(lat, lng, zoom, new Vector4(range, range, range, range));
}

/// <summary>
/// Used for loading new tiles on the existing world. Unlike Execute function, doesn't destroy the existing ones.
/// </summary>
/// <param name="pos">Tile coordinates of the requested tile</param>
/// <param name="zoom">Zoom/Detail level of the requested tile</param>
public void Request(Vector2 pos, int zoom)
{
if (!_tiles.ContainsKey(pos))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ public class MapVisualization : ScriptableObject
{
public List<Factory> Factories;

/// <summary>
/// Initializes the factories by passing the file source down, which's necessary for data (web/file) calls
/// </summary>
/// <param name="fs"></param>
public void Initialize(IFileSource fs)
{
foreach (Factory fac in Factories.Where(x => x != null))
Expand All @@ -20,6 +24,10 @@ public void Initialize(IFileSource fs)
}
}

/// <summary>
/// Registers requested tiles to the factories
/// </summary>
/// <param name="tile"></param>
public void ShowTile(UnityTile tile)
{
foreach (var fac in Factories.Where(x => x != null))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ namespace Mapbox.Unity.MeshGeneration.Modifiers
using UnityEngine;
using Mapbox.Unity.MeshGeneration.Components;

/// <summary>
/// Texture Modifier is a basic modifier which simply adds a TextureSelector script to the features.
/// Logic is all pushed into this TextureSelector mono behaviour to make it's easier to change it in runtime.
/// </summary>
[CreateAssetMenu(menuName = "Mapbox/Modifiers/Texture Modifier")]
public class TextureModifier : GameObjectModifier
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@

namespace Mapbox.Unity.MeshGeneration.Modifiers
{
/// <summary>
/// Merged Modifier Stack, just like regular Modifier stack, creates a game object from features. But the difference is, regular modifier stack creates a game object for each given faeture meanwhile Merged Modifier Stack merges meshes and creates one game object for all features (until the 65k vertex limit).
/// It has extremely higher performance compared to regular modifier stack but since it merged all entities together, it also loses all individual entity data & makes it harder to interact with them.
/// It pools and merges objects based on the tile contains them.
/// </summary>
[CreateAssetMenu(menuName = "Mapbox/Modifiers/Merged Modifier Stack")]
public class MergedModifierStack : ModifierStackBase
{
Expand Down Expand Up @@ -51,7 +56,6 @@ public override GameObject Execute(UnityTile tile, VectorFeatureUnity feature, M

private GameObject CreateGameObject(MeshData data, GameObject main)
{
//Debug.Log("building count " + _buildingCount[tile]);
var go = new GameObject();
var mesh = go.AddComponent<MeshFilter>().mesh;
var rend = go.AddComponent<MeshRenderer>();
Expand Down Expand Up @@ -84,7 +88,6 @@ private GameObject CreateGameObject(MeshData data, GameObject main)

public GameObject End(UnityTile tile, GameObject parent)
{

var md = new MeshData();
md.UV = new List<List<Vector2>>() { new List<Vector2>(), new List<Vector2>() };
md.Triangles = new List<List<int>>() { new List<int>(), new List<int>() };
Expand All @@ -111,7 +114,6 @@ public GameObject End(UnityTile tile, GameObject parent)
_cached[tile].Clear();
return go;
}

}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ namespace Mapbox.Unity.MeshGeneration.Modifiers
using UnityEngine;
using Mapbox.Unity.MeshGeneration.Data;

/// <summary>
/// Chamfer modifiers adds an extra vertex and a line segmet at each corner, making corners and line smoother.
/// Generally used for smoother building meshes and should be used before Polygon Mesh Modifier.
/// </summary>
[CreateAssetMenu(menuName = "Mapbox/Modifiers/Chamfer Modifier")]
public class ChamferModifier : MeshModifier
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ public enum ExtrusionType
FirstMidTopFloor
}

/// <summary>
/// Height Modifier is responsible for the y axis placement of the feature. It pushes the original vertices upwards by "height" value and creates side walls around that new polygon down to "min_height" value.
/// It also checkes for "ele" (elevation) value used for contour lines in Mapbox Terrain data.
/// Height Modifier also creates a continuous UV mapping for side walls.
/// </summary>
[CreateAssetMenu(menuName = "Mapbox/Modifiers/Height Modifier")]
public class HeightModifier : MeshModifier
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ namespace Mapbox.Unity.MeshGeneration.Modifiers
using UnityEngine;
using Mapbox.Unity.MeshGeneration.Data;

/// <summary>
/// Line Mesh Modifier creates line polygons from a list of vertices. It offsets the original vertices to both sides using Width parameter and triangulates them manually.
/// It also creates tiled UV mapping using the line length.
/// MergeStartEnd parameter connects both edges of the line segment and creates a closed loop which is useful for some cases like pavements around a building block.
/// </summary>
[CreateAssetMenu(menuName = "Mapbox/Modifiers/Line Mesh Modifier")]
public class LineMeshModifier : MeshModifier
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ namespace Mapbox.Unity.MeshGeneration.Modifiers
using UnityEngine;
using Mapbox.Unity.MeshGeneration.Data;

/// <summary>
/// Polygon modifier creates the polygon (vertex&triangles) using the original vertex list.
/// Currently uses Triangle.Net for triangulation, which occasionally adds extra vertices to maintain a good triangulation so output vertex list might not be exactly same as the original vertex list.
/// </summary>
[CreateAssetMenu(menuName = "Mapbox/Modifiers/Polygon Mesh Modifier")]
public class PolygonMeshModifier : MeshModifier
{
Expand Down
Loading