ArrayMesh grid

DschoonmakerDschoonmaker Posts: 274Member

I'm trying to make a grid mesh using ArrayMesh, kind of like a subdivided PlaneMesh. I found I can use indices to merge triangle vertices, to make a quad with only 4 vertices instead of 6. But it requires a particular order. My question is, how can I procedurally create a grid mesh, while merging unnecessary vertices? This is my grid, all the vertices should be connected, but right now each quad is separate:

Best Answer

  • DschoonmakerDschoonmaker Posts: 274
    Accepted Answer

    I got it, if anyone has the same problem I can explain it.

Answers

  • DschoonmakerDschoonmaker Posts: 274Member
    Accepted Answer

    I got it, if anyone has the same problem I can explain it.

  • SIsilicon28SIsilicon28 Posts: 769Moderator

    I don't have a problem, but I'd like to here how you solved it. :)

  • DschoonmakerDschoonmaker Posts: 274Member

    I'm not entirely sure why it works myself, but I'll explain as best I can.

    First, I create a grid of vertices, constructed row by row, left to right.

    For indices, I have this code for a loop:

    var i = 0
    var y = 0
    var x = 0
    while y < gridSize-1:
        while x < gridSize-1:
            indices.append(i+gridSize)
            indices.append(i+1)
            indices.append(i)
            indices.append(i+gridSize)
            indices.append(i+gridSize+1)
            indices.append(i+1)
            x+=1
            i+=1
        x = 0
        y+=1
        i+=1
    

    I have a specific order that worked for indices(for a single plane), it goes:

    • bottomleft
    • topright
    • topleft
    • bottomleft
    • bottomright
    • topright

    And this creates two triangles out of four vertices(top and bottom may be reversed). For the index code, I adapted this to work in a loop(with the variable i). The loop just loops over X and Y directions, adding vertices based on the variable i, which is increased every quad. Because the index code to make a quad assumes that there's vertices to the right and bottom, I subtract 1 from gridSize in the loop. Last, I have to increase i by 2 at the end of a row. I'm not entirely sure why, but it works when I do.

  • DschoonmakerDschoonmaker Posts: 274Member
    edited August 2020

    I'm trying to figure out how to leave out one vertex, to create a hole. Do you know how I can do that, without messing up indices?

    Edit: this is for a terrain mesh. Terrain normals are always facing up, with a normalmap to simulate shading. I could use a flat shaded mesh instead(like in the screenshot), and simply leaving out quads where holes should be, but that doesn't seem as neat....

  • MegalomaniakMegalomaniak Posts: 3,792Admin

    Maybe a discard of fragments based on a shared vertex? I haven't looked into this previously though.

  • DschoonmakerDschoonmaker Posts: 274Member

    I tried discarding fragments in a shader. It looks good, but doesn't do anything to the collisionShape.

    Maybe it would be best to switch back to disconnected/flat-shaded quads, I can't think of any disadvantages to them.

  • MegalomaniakMegalomaniak Posts: 3,792Admin
    edited August 2020

    Ah yeah, you'd probably need to have a separate collision mesh with a hole already in the spot. I'm pretty sure that games such as Witcher 3 that have such terrain systems probably do use custom manually created or tweaked collision meshes.

    On that note, you might find this interesting:
    https://www.gdcvault.com/play/1020197/Landscape-Creation-and-Rendering-in

    They used clipmap terrain.
    edit: and apparently they found discard quite useless as well, since it disables early-z / depth-prepass.

  • DschoonmakerDschoonmaker Posts: 274Member

    That article is very interesting(and their terrain system is very complicated!).

    I switched back to flat shading, and get heightmap values for each vertex; because I shade it with a normalmap it looks exactly the same. Flat shading allows me to create holes by leaving out quads without needing to do anything special about indices. Holes are stored in the alpha texture of the heightmap.

    The collisionShape is generated from the mesh, so physics work correctly. I modified my grass shader to make grass in holes transparent, too.

    I also took advantage of flat-shading to arrange the triangles differently:

    I've heard it looks better.

  • MegalomaniakMegalomaniak Posts: 3,792Admin

    Yeah, diamond-square is better especially for (heightmap) terrains. And normalmaps, if available, is how I'd go with the shading too so all of that makes sense.

  • DschoonmakerDschoonmaker Posts: 274Member

    The main reason I chose to use a normal map instead of calculating vertex normals is because there will be a loss of detail at lower LODs, when I implement LOD(per-vertex vs. per-pixel normals).

    I don't know how to calculate vertex normals anyway, and now I have a flat shaded terrain mesh, so normalmaps definitely work best.

Leave a Comment

BoldItalicStrikethroughOrdered listUnordered list
Emoji
Image
Align leftAlign centerAlign rightToggle HTML viewToggle full pageToggle lights
Drop image/file