src/Tools/babylon.earcut.js
// main ear slicing loop which triangulates a polygon (given as a linked list)
main ear slicing loop which triangulates a polygon (given as a linked list)
main ear slicing loop which triangulates a polygon ( given as a linked list )
function earcutLinked(ear, triangles, dim, minX, minY, size, pass) {
        if (!ear)
            return;
        if (!pass && size)
            indexCurve(ear, minX, minY, size);
        var stop = ear, prev, next;
        while (ear.prev !== ear.next) {
            prev = ear.prev;
            next = ear.next;
            if (size ? isEarHashed(ear, minX, minY, size) : isEar(ear)) {
                triangles.push(prev.i / dim);
                triangles.push(ear.i / dim);
                triangles.push(next.i / dim);
                removeNode(ear);
                ear = next.next;
                stop = next.next;
                continue;
            }
            ear = next;
            if (ear === stop) {
                if (!pass) {
                    earcutLinked(filterPoints(ear, undefined), triangles, dim, minX, minY, size, 1);
                }
                else if (pass === 1) {
                    ear = cureLocalIntersections(ear, triangles, dim);
                    earcutLinked(ear, triangles, dim, minX, minY, size, 2);
                }
                else if (pass === 2) {
                    splitEarcut(ear, triangles, dim, minX, minY, size);
                }
                break;
            }
        }
    }
function earcutLinked ( ear , triangles , dim , minX , minY , size , pass ) { if ( ! ear ) return ; if ( ! pass && size ) indexCurve ( ear , minX , minY , size ) ; var stop = ear , prev , next ; while ( ear . prev !== ear . next ) { prev = ear . prev ; next = ear . next ; if ( size ? isEarHashed ( ear , minX , minY , size ) : isEar ( ear ) ) { triangles . push ( prev . i / dim ) ; triangles . push ( ear . i / dim ) ; triangles . push ( next . i / dim ) ; removeNode ( ear ) ; ear = next . next ; stop = next . next ; continue ; } ear = next ; if ( ear === stop ) { if ( ! pass ) { earcutLinked ( filterPoints ( ear , undefined ) , triangles , dim , minX , minY , size , 1 ) ; } else if ( pass === 1 ) { ear = cureLocalIntersections ( ear , triangles , dim ) ; earcutLinked ( ear , triangles , dim , minX , minY , size , 2 ) ; } else if ( pass === 2 ) { splitEarcut ( ear , triangles , dim , minX , minY , size ) ; } break ; } } }
main ear slicing loop which triangulates a polygon (given as a linked list)
main ear slicing loop which triangulates a polygon ( given as a linked list )
// interlink polygon nodes in z-order // iterate through ears, slicing them one by one // cut off the triangle // skipping the next vertice leads to less sliver triangles // if we looped through the whole remaining polygon and can't find any more ears // try filtering points and slicing again