Mapbox Unity SDK Terraced terrain style

Lines and polygons in Mapbox Unity SDK

It has been…..8 months since that last post?? So 8 months since I started working at Mapbox. I can’t believe it, time sure flies. That was an extremely busy 8 months and I, being a lazy blogger, couldn’t blog at all. But I’ll try to change that, I think I even found an awesome subject for this first blog post; how we treat line and polygons in Mapbox Unity SDK. And it’s not only that, I’ll try to explain it over contour lines data showing how to recreate meshes for two different styles; scifi contour lines and terraced terrain style with this.

Mapbox Unity SDK Scifi terrain style
Scifi contour lines

We can classify geometry types in Mapbox vector api in three groups; points (pois, labels etc), lines (roads, borders etc) and polygons (buildings, landuse, water etc). We’ll leave points aside for this post. They are quite interesting as well so might come back to that in another post though.

And actually let me just take a small step back and describe how these types are used. So we have this vector data right? It’s all hierarchical so you can visualizer it as xml or json in your head.

Root of the vector data contains a list of layers; like building layer and road layer. They have such names but there’s nothing else indicating what kind of layer it is and how it should be visualized so it’s all abstract. Inside these layers, we have “features”, individual buildings or road segments and these feature actually have lots of meta data with them, most importantly (for this blog post) the “GeometryType” of that feature. So shortly, we have bunch of layers containing features with “GeometryType” property.

You probably noticed how I’m emphasizing “GeometryType” right? That’s mainly because we don’t use this property anywhere at all. Sounds weird huh? Maybe a little, and we probably will use it at some point for certain special cases but one of our main goals with Unity SDK is to make it flexible so there are no restrictions or anything on what to use. So for example, you can render building data as lines to create walls or lakes data as lines to create shore.

mapbox unity sdk terraced terrain game shot
Just a random shot to break the wall of text

Another interesting thing to note here is that, GeometryType is just a property under feature. Other than that, both line and polygon features (even points actually) uses same structure to keep their data; list of vertices. Only difference between two is that polygon vertex list is closed (first and last vertices are same) and line vertex list is open.

Now let me show you the difference data&modifier makes. I’ll use the terraced terrain demo in the latest version of SDK and chamge some properties and stuff there. You can create similar stuff using that as well.

contour line mapbox sdk

What you see in that image is just one contour line feature, at the corner of a tile. So this tile has that a little elevation at the south west corner. That polygon is created using a mesh factory with “mapbox.mapbox-terrain-v2” source id (you can find its detailes here;  Vector Data page), which comes as default in that demo scene. I’m saying that because v2 of that api has contour lines defined as polygons. And then I used PolygonMeshModifier to visualize it as a polygon. Nothing fancy so far. But for example, what happens when you use line mesh modifier on a polygon?

contour line mapbox sdk

If you can ignore how ugly it looks on sharp edges, you’ll see it creates an outline. It’s kind of useless for contour I guess…. but maybe not. It’s all up to your imagination in the end. But as I mentioned before, you can create borders of a lake or a park with this. And I promise I’ll improve that corners thing soon! 🙂

Now I’ll switch source id to “mapbox.mapbox-terrain-v1”. So this version has contour feature as lines. It doesn’t mean much though, just that first&last vertices in the list are not the same. But let’s have a look at how both modifiers work with that;

contour line mapbox sdk

This is the v1 data with line mesh modifier. Important thing to note here, since v2 actually filled the whole space adding an extra vertex at the bottom left corner so it looks like a big triangular area. But real contour line is actually just this line, and without a need to fill empty space to create a polygon, we get a nice clean contour line using this.

Finally, what happens when you use polygon mesh modifier on a line;

contour line mapbox sdk

Yep. It’s not surprising I guess but polygon modifier normally created a polygon connecting the first and last vertices, and ended up with this totally wrong output. I think it’s safe to say that this is flat out wrong as I can’t think of a single use case.

To summarize it quickly, we have two geometry types (line and polygon) and two core mesh modifiers (line mesh and polygon mesh) which you can use in bunch of different ways to create different outputs.

Admittedly system doesn’t help you with these either , like saying that it would be better to use polygon on buildings etc. We’ll look into that soon and try to add some guidance&help beyond the demo scenes though.


Finally, I’ll show you the mesh gen structure to create those scifi and terrace styles using the brand new node editor I’m working on these days!

contour line mapbox sdk
This is the terraced terrain style, actually what you already have in that demo scene. Still wanted to post this so you can see how it actually looks on a graph and understand that demo better. This is using “mapbox.mapbox-terrain-v2”.

And for the scifi style;

contour line mapbox sdk

It’s almost exactly same really, just have a line mesh modifier instead of a polygon mesh modifier. And “Scifi Mesh Factory” there is using “mapbox.mapbox-terrain-v1” of course. We probably will also have a walkthrough/tutorial for this at Mapbox blog soon as well.

And this is it. Hope you helps you understand what’s going on behind the scenes a little better. Let me know if you have any questions or feedback about this blog post or SDK in general.


6 thoughts on “Lines and polygons in Mapbox Unity SDK

  1. Hello! I encourage you to resume the project “pokemon go”. How compatible is the current SDK with previous tutorials? I can not update the position using GPS on Android, I do not know if I missed any features in the mapbox SDK

    I’m glad you post again on your blog 🙂 greetings from Chile

    1. Thanks Philippe!
      My previous tutorials were based on my own MapzenGo project so this isn’t compatible with that. But Mapbox SDK is so much better at this stage that it might be a nice idea to make something similar with Mapbox SDK this time!
      I don’t know much about mobile&gps stuff but have you checked the LocationProvider demo scene inside the sdk?

  2. I just tried on Android(huawei p9) with Unity 5.6.1f1 and it works the LocationProvider! Do you know if the location is updated?

    Any possibility of new tutorials? the new SDK is great!

    1. Ouch sorry for the very late reply, missed this somehow 🙁
      I think it sets the location once at initial load but you probably discovered that already 🙂
      And thanks! I’ll try to write new one soon!

  3. I would like to know how powerful is the unity SDK when you have to load a large number of polygons on the scene, for example showing the buildings, I mean maps with vector data, not image tiles.

    1. We don’t really do anything fancy with polygons, no special shader or anything for example. Their creating is rather simple as well (basic triangulation) so you should be only limited by Unity’s engine with that.

Leave a Reply