Topography Project

  After taking graphics and having to render objects to a camera from scratch in C++ I wanted to do something similar in unity. After seeing a topography map I decided to create a program that can take a picture, analyze the colors of each pixel and generate a mesh based on the image. To load in the image, I use a selected path determined from a simple UI I quickly threw together (Still a work in progress). I then Load the File into a Texture2D and then use Sprite.Create() to generate the sprite image used by the program. I load the sprite into a preview box for the user to view the image before generating the mesh.

  The main challenges so far were in creating the meshes. To create the mesh, you need 2 main things. The Vertices used by the mesh, which are an array of Vector3’s and an array of Triangle points. For example, if you wanted to create one triangle on a mesh you would need and array size of 3. The numbers you list inside the Triangle array are the Vertices you want to connect. So one triangle would be an array of [1,3,2] and the array of Vertices would be [(0,0,0),(1,0,0),(1,0,1)]. Using the triangle array (0,0,0) would connect to (1,0,1) would connect to (1,0,0) which would connect back to (0,0,0). Because a single triangle needs 3 Vertices the triangle array is the largest allocation of memory in the script. A mesh made of 16 points(4x4) would need 18 triangles, 18*3 = 54.   

  If you try to use a single mesh for an image the biggest image you can generally use is around 250x250. Unity limits a signle Mesh to a max count of 65000 Vertices. To get around this first hurdle I split the image into multiple meshes. That way the Vertices array is split into sections rather than one large block. In my script I have a MasterGrid and a SubGrid script. The MasterGrid calculates the number of meshes needed based on a pre-determined width and height which the user can change. A larger WxH(WidthXHeight) will result in less sub meshes and a smaller WxH will result in more sub meshes. The MasterGrid also holds the array of pixel data that the meshes use to calculate the height of each Vertex, this way the memory is allocated once. That means that now the “limit” to how big the mesh image can be is placed on the pixelData[] array, or just the size of the image itself.

Missing Triangles between meshes

  Because there are multiple meshes in play now, there becomes a new problem. The meshes themselves need to be placed next to each other in a way that they “Connect” together. The further away from the first mesh the further the mesh needs to be moved. The second problem from splitting the mesh into pieces is that triangles between the meshes are now missing. As a result, it is easy to see the seams between the meshes, to fix this problem, the meshes need to also grab the last line of vertices from the mesh next to it to account for the seam. Because of this the meshes HxW are increased by 0,1 or 2 depending on how many neighbors that mesh has.