Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Removing unnecessary faces on a Mesh

Discussion in 'Scripting' started by CallumKent, Sep 27, 2013.

  1. CallumKent

    CallumKent

    Joined:
    Jul 14, 2013
    Posts:
    8
    Hello all! I've been playing around with procedural generated mesh terrain, such as the type used in Minecraft (I'm also using cubes) and have come across the problem whilst I was attempting to optimize the generation. My problem being, I have a huge amount of unnecessary faces hidden under the blocks, which I believe is the cause of a 45 second load time for a 500x500 cube map. I've reduced the time from 1 minute 15 seconds by removing any faces that are below a certain point, which gives me the indication that the faces are the major reason behind it. Here's a picture of what's going on.

    View attachment 68991

    Here's also the code that I've been using to generate the terrain with mostly brushes. (I've used pastebin because it didn't seem to work with the in built code viewing thing).

    TERRAIN SCRIPT
    http://pastebin.com/hcXnyfdc

    CHUNK SCRIPT
    http://pastebin.com/cjG0KLum

    I've been thinking for a while now and used google to try and find a solution but to no prevail. Any help would be appreciated
    NOTE: With the Terrain script, the section of code commented out with /* */ is to generate the world as you move around, and the code for a preset world size is in the start function, I've not yet decided which I will use as of yet.

    The Unity Attachment image seems to have broken, so here's an alternative;
    http://imgur.com/OlWHeUv
     
    Last edited: Sep 30, 2013
  2. AlwaysSunny

    AlwaysSunny

    Joined:
    Sep 15, 2011
    Posts:
    260
    I tried tracking down an article about this very subject, but I've thus far failed to relocate it for you. It was part of a series of blog posts by someone who had developed a voxel mesh generator, generously walking readers through the finer points and hiccups. It may have been by the [a?] creator of Cube World, if that helps you know it when you see it.

    Long story short, in every impressive cubic voxel implementation I've personally seen, the data set isn't represented by instantiating literal cubes. You should investigate a voxel mesh creation algorithm to build your meshes procedurally. Marching Cubes is the most famous I think, but isn't designed for creating discrete cube based meshes. There are plenty of resources to be found related to the process, though hunting for the gems takes some serious investigating. Hey, accidental joke!

    To directly answer the question, I imagine the most efficient method for culling invisible faces would be entirely programmatic, somehow searching each triangle of the mesh to ensure drawing it is necessary to achieve a "whole" object, and destroying any verts belonging to non-visible tris. A good voxel mesh creation algorithm rolls this step into the creation process!

    If I locate anything helpful, I'll share a link!
    Edit - I didn't find the aforementioned article, but these three were useful when I was investigating this stuff
    http://mattbolt.blogspot.com/2012/11/a-heap-of-voxels.html
    http://accidentalnoise.sourceforge.net/minecraftworlds.html
    http://www.sauropodstudio.com/how-to-make-a-voxel-base-game-in-unity/
     
    Last edited: Sep 28, 2013
  3. CallumKent

    CallumKent

    Joined:
    Jul 14, 2013
    Posts:
    8
    I've read all three of the blogs, searched even more on google, but still no prevail, I'm still as clueless as before on how to remove the faces.
    If anyone else has any way to fix this issue, please let me know.
     
  4. Wobak

    Wobak

    Joined:
    May 21, 2013
    Posts:
    54
    i just have a loose idea how to remove the top faces:
    -filter out the top triangles pointing upwards through their normals.
    -get the two triangles that form the top quadrangle
    -then make a two dimensional array the size of the map.
    -iterate through the top quadrangles, compare their heigth with the heigth of the quadrangle in the maparray in that position
    -if it is greater, write the quadrangle index in the array under that position
    -delete all triangles which arent part of a quadrangle whose index is found in the map array
     
  5. HEATH3N

    HEATH3N

    Joined:
    Jul 19, 2014
    Posts:
    17
  6. Norite113

    Norite113

    Joined:
    Jul 14, 2016
    Posts:
    2
    Debuger for vertices
    do not test on large Mesh!!


    Code (CSharp):
    1. private void OnDrawGizmos()
    2.     {
    3.         if (transform.GetComponent<MeshFilter>() != null)
    4.         {
    5.             if (transform.GetComponent<MeshFilter>().sharedMesh != null)
    6.             {
    7.                 var mesh = transform.GetComponent<MeshFilter>().sharedMesh;
    8.                 for (int i = 0; i < vertices.Length; i++)
    9.                 {
    10.                     Handles.Label(mesh.vertices[i] + transform.position, i.ToString());
    11.                 }
    12.             }
    13.         }
    14.     }
     
    Last edited: Aug 6, 2019