Rendering a contour map from Marching Squares
Marching Squares is a fine algorithm for generating a contour map from a height map, but what's the best way to put the contour map on the screen? Unity seems to have limited options for drawing lines.
Here's a relevant question that includes an answer with a full implementation for drawing a contour map: Turn a Terrain into a Contoured Map. It works by calling SetPixel on a Texture2D, which probably works fine for what it's trying to do, but it's not doing Marching Squares. It's just examining each pixel of the height map and deciding if that pixel is on a contour or not, while Marching Squares creates much finer lines that are smaller than a height map pixel. Even so, it's certainly possible to draw lines through repeated calls to SetPixel and something like Bresenham's algorithm.
Another option would be to use GL library for immediate mode drawing. This is a nice, simple way to draw lines and it would seem like the perfect option, but immediate mode is supposed to be dreadfully slow. Even the GL scripting reference page recommends against using it. Certainly it wouldn't be as slow as doing Bresenham on every line, but the Bresenham drawing only needs to happen once, while GL line drawing needs to happen for every frame.
Finally, we could create a mesh for every contour height. Instead of showing the contours as a line drawing, we can fill in each contour line as a solid polygon with a color that represents its elevation, and then draw them with an orthographic camera with the lowest elevations furthest away so the contour polygons are stacked with the highest elevation on top.
Have I missed any good options? Are there any serious issues or benefits from using these options? I would really like to be able to use a line mesh, but it seems that's not an option in Unity.
Answer by Circool · Mar 02, 2018 at 08:44 AM
You can render contour map as Unity mesh with topology set as: MeshTopology.Lines or MeshTopology.LineStrip see here: https://docs.unity3d.com/ScriptReference/MeshTopology.html . I'm doing so in my asset "Wire Terrain": https://assetstore.unity.com/packages/tools/modeling/wireterrain-94895
Your answer
Follow this Question
Related Questions
Destroying 2D terrain at runtime 0 Answers
How to export Heightmap(Terrain) Raw.(Script) 0 Answers
How to spawn in loop like snail but in square 0 Answers
How can I make the character jump between the boxes? 0 Answers
Help with 2D Checkers game 0 Answers