The purpose of this project is to provide the functionality to create custom meshes (polygons) in Unity3D based on a collection (array) of vertices directly at runtime. These 2D meshes are created along the x- and z-dimensions in the 3D space. Furthermore, the created custom mesh can be extruded (into a 3D prism) along the y-dimension in the 3D space.
Some of my research work required me to visualize country borders (geo-spatial coordinates; received from various open data sources) as individual meshes (2D polygons / 3D prisms) in the 3D space in Unity3D. Some examples of the usage of this project are provided beneath in the section Screenshots - SCB Kommun RT90, demonstrating the visualization of all individual municipalities in the country of Sweden.
The Triangulation.cs
class features a partial implementation of the original Triangle and Triangle.NET libraries in order to create render triangles for a custom mesh. The implemented triangulation supports holes in the mesh.
The PolyExtruder.cs
class is responsible for handling the input data and creating all Unity3D GameObjects (incl. the actual mesh; 2D polygon / 3D prism) using the features provided through Triangulation.cs
class in the process. Created 3D prisms (extruded 2D polygons) consist of three GameObjects, namely 1) the bottom mesh (y = 0), 2) the top mesh (y = dynamically assigned; greater than 0), and 3) the surrounding mesh connecting 1 and 2 on their outline accordingly.
Furthermore, the PolyExtruder.cs
class provides some quality-of-life features, such as:
- select whether the mesh should be 2D (polygon) or 3D (extruded, prism)
- calculation of the mesh's (2D polygon's) area
- calculation of the mesh's (2D polygon's) centroid
- set extrusion length ("height")
- select whether or not to visually display the polygon's outline
The main idea is to visualize the 2D input data along the x- and z-dimensions, while the (potential) extrusion is always conducted along the y-dimension.
This project has been built using the following specifications:
- OS X 10.14.6
- Unity3D 2019.2.17f1 Personal (OS X release)
Note: Generally, Unity source code should work also within their Windows counterparts. Please check out the above stated dependencies for troubleshooting.
Additional resources used to create this project have been accessed as follows:
- (Original) Triangle library implementation by Jonathan Richard Shewchuk (project web page)
- Triangle.NET by Christian Woltering (CodePlex Archive, GitHub snapshot)
- Using Triangle.NET in Unity (YouTube video; not available anymore as of 2019-06-04)
- Determine order (clockwise vs. counter-clockwise) of input vertices (StackOverflow)
- Polygons and meshes by Paul Bourke (project web page)
- Geo-spatial data about the island of Gotland (Sweden) (Swedish Statistiska centralbyrån (SCB); accessed 2019-02-06)
In order to add the features provided by this project to your Unity3D project, I recommend to add the assets by simply importing the pre-compiled nicoversity-unity_polyextruder.unitypackage
. Alternatively, the repository directory unity_src
features a directory titled UnityPolyExtruder
, which contains an already setup Unity3D project (that was used to export the provided .unitypackage
in the first place).
// prepare data and options
Vector2[] MyCustomMeshData = new Vector2[]
{
new Vector2(0.0f, 0.0f),
new Vector2(10.0f, 0.0f),
new Vector2(10.0f, 10.0f),
// ... and more vertices
};
float extrusionHeight = 10.0f;
bool is3D = true;
// create new GameObject (as a child), and further configuration
GameObject polyExtruderGO = new GameObject();
polyExtruderGO.transform.parent = this.transform;
polyExtruderGO.name = "MyCustomMeshName";
// add PolyExtruder script to newly created GameObject,
// keep track of its reference
PolyExtruder polyExtruder = polyExtruderGO.AddComponent<PolyExtruder>();
// configure display of outline (before running the poly extruder)
polyExtruder.isOutlineRendered = true; // default: false
polyExtruder.outlineWidth = 0.1f; // default: 0.01f
polyExtruder.outlineColor = Color.blue; // default: Color.black
// run poly extruder according to input data
polyExtruder.createPrism(polyExtruderGO.name, extrusionHeight, MyCustomMeshData, Color.grey, is3D);
// access calculated area and centroid
float area = polyExtruder.polygonArea;
Vector2 centroid = polyExtruder.polygonCentroid;
All Unity3D scripts in this project are well documented directly within the source code. Please refer directly to the individual scripts to get a better understanding of the implementation.
The imported Unity3D assets provide two demonstration scenes:
TriangleNET_Test.unity
, illustrating and testing the implementation of theTriangulation.cs
class viaTriangulationTest.cs
script.PolyExtruder_Demo.unity
, illustrating the usage of thePolyExtruder.cs
class viaPolyExtruderDemo.cs
script. ThePolyExtruderDemo.cs
script allows the user to make selections using the Unity3D Inspector accordingly to a) select an example data set for the custom mesh creation (Triangle, Square, Cross, SCB Kommun RT90 Gotland), b) indicate whether the custom mesh should be created in 2D (polygon) or 3D (prism), c) the length ("height") of the extrusion, and d) whether the extrusion length should be dynamically scaled at runtime (oscillated movement example).
Please refer to these scenes and scripts to learn more about the examples.
Following, some visual impressions of the included example data, visualized using the Triangulation.cs
and PolyExtruder.cs
classes.
Following, some visual impressions of the earlier stated use case of visualizing all municipalities in the country of Sweden (see Background) using the PolyExtruder.cs
class. The data has been received from the Swedish Statistiska centralbyrån (SCB) (accessed: 2019-02-06; Note: The data is not included as part of this project).
A random extrusion length ("height") for each municipality has been applied to emphasize the 3D scenario.
- The function
public static bool triangulate(...)
always returnstrue
. In the future, an error list could be implemented to capture errors that occur during the triangulation process.
- No holes-support for extrusion (3D prism) is implemented. Although the
Triangulation.cs
script supports holes in the 2D polygon mesh, the support for holes as part of thePolyExtruder.cs
class has not been implemented in this version.
- added feature to allow display of the polygon's outline
- upgrade to support Unity version 2019.2.17f1 Personal (from prior version 2019.1.5f1; no changes in the code required)
MIT License, see LICENSE.md