Newer
Older
pixi.js / src / core / graphics / webgl / utils / buildPoly.js
@Steffen Bär Steffen Bär on 20 Sep 2016 2 KB some more es6 changes (#2969)
import buildLine from './buildLine';
import utils from '../../../utils';
import earcut from 'earcut';

/**
 * Builds a polygon to draw
 *
 * Ignored from docs since it is not directly exposed.
 *
 * @ignore
 * @private
 * @param graphicsData {PIXI.WebGLGraphicsData} The graphics object containing all the necessary properties
 * @param webGLData {object} an object containing all the webGL-specific information to create this shape
 */
const buildPoly = function (graphicsData, webGLData)
{
    graphicsData.points = graphicsData.shape.points.slice();

    let points = graphicsData.points;

    if(graphicsData.fill && points.length >= 6)
    {
        let holeArray = [];
             // Process holes..
        let holes = graphicsData.holes;

        for (let i = 0; i < holes.length; i++) {
            const hole = holes[i];

            holeArray.push(points.length/2);

            points = points.concat(hole.points);
        }

        // get first and last point.. figure out the middle!
        const verts = webGLData.points;
        const indices = webGLData.indices;

        const length = points.length / 2;

        // sort color
        const color = utils.hex2rgb(graphicsData.fillColor);
        const alpha = graphicsData.fillAlpha;
        const r = color[0] * alpha;
        const g = color[1] * alpha;
        const b = color[2] * alpha;

        const triangles = earcut(points, holeArray, 2);

        if (!triangles) {
            return;
        }

        let vertPos = verts.length / 6;

        for (let i = 0; i < triangles.length; i+=3)
        {
            indices.push(triangles[i] + vertPos);
            indices.push(triangles[i] + vertPos);
            indices.push(triangles[i+1] + vertPos);
            indices.push(triangles[i+2] +vertPos);
            indices.push(triangles[i+2] + vertPos);
        }

        for (let i = 0; i < length; i++)
        {
            verts.push(points[i * 2], points[i * 2 + 1],
                       r, g, b, alpha);
        }
    }

    if (graphicsData.lineWidth > 0)
    {
        buildLine(graphicsData, webGLData);
    }
};

export default buildPoly;