SebLague/Procedural-Cave-Generation

Episode 4

Opened this issue · 0 comments

I dont get what your doing in this episode, but i'll do an "episode 4" update of issue #4

source code can be found here: https://gist.github.com/soraphis/a752a52a181f5612422765451202d6c9#file-meshgenerator_chapter4-cs-L100

Triangle struct

it bothers me really that you store everything twice, instead of using an switch-case for 0, 1, 2 and default in the indexer.
but this struct is not needed entirely, go on reading

TriangulateSquare() orientation changes

why do you switch the triangle order at the beginning for the swith-cases 1, 2 and 4?
the triangle ABC has the same orientation as BCA and CAB.

optimizations with "checkedVertices".

you can always add each Control Node to this, because and outline will never contain this.

thinking this further, leaves us with the following:

(method: TriangulateSquare)

  • an outline is always between two nodes (not control nodes)
    • these nodes are in the list give to "MeshFromPoints"
    • and per square can always be just 1 outline, except for the diagonals-case.

so we need a way to find those node pairs, which are connected with an outline.
so we need to check the control nodes which are inactive.

so for each inactive control node, check the previos node if its in the list -> it will become pair[0]
and check the next node if its in the list -> it will become pair[1]

with this way we can find every vertex the outline is made out of. we need to add them in an efficient way: dictionary<int, int> so we have an mapping from_vertex_index to to_vertex_index

this is all we need. just add the CreateWallMesh method, (mostly) like you did (just iterating a bit different cause we have a dict<int, int> instead of double nested lists and loops and checking in hashsets and so on.

100 lines of code less, no need for a Triangle struct. way less loops and whatever. this should be a huge performance increase

edit: just tested the performance
150 x 150 map, 49% fill chance

my script is in average 5 milliseconds faster, which are around 10%