@@ -212,6 +212,92 @@ Phaser.Graphics.prototype.drawPolygon = function (poly) {
212212
213213} ;
214214
215+ /*
216+ * Draws a single {Phaser.Polygon} triangle from a {Phaser.Point} array
217+ *
218+ * @param {Array<Phaser.Point> } points - An array of Phaser.Points that make up the three vertices of this triangle
219+ * @param {boolean } [cull=false] - Should we check if the triangle is back-facing
220+ * @method Phaser.Graphics.prototype.drawTriangle
221+ */
222+
223+ Phaser . Graphics . prototype . drawTriangle = function ( points , cull ) {
224+ var triangle = new Phaser . Polygon ( points ) ;
225+ if ( cull ) {
226+ var cameraToFace = new Phaser . Point ( this . game . camera . x - points [ 0 ] . x , this . game . camera . y - points [ 0 ] . y ) ;
227+ var ab = new Phaser . Point ( points [ 1 ] . x - points [ 0 ] . x , points [ 1 ] . y - points [ 0 ] . y ) ;
228+ var cb = new Phaser . Point ( points [ 1 ] . x - points [ 2 ] . x , points [ 1 ] . y - points [ 2 ] . y ) ;
229+ var faceNormal = cb . cross ( ab ) ;
230+ if ( cameraToFace . dot ( faceNormal ) > 0 ) {
231+ this . drawPolygon ( triangle ) ;
232+ }
233+ } else {
234+ this . drawPolygon ( triangle ) ;
235+ }
236+ return ;
237+ } ;
238+
239+ /*
240+ * Draws {Phaser.Polygon} triangles
241+ *
242+ * @param {Array<Phaser.Point>|Array<number> } vertices - An array of Phaser.Points or numbers that make up the vertices of the triangles
243+ * @param {Array<number> } {indices=null } - An array of numbers that describe what order to draw the vertices in
244+ * @param {boolean } [cull=false] - Should we check if the triangle is back-facing
245+ * @method Phaser.Graphics.prototype.drawTriangles
246+ */
247+
248+ Phaser . Graphics . prototype . drawTriangles = function ( vertices , indices , cull ) {
249+
250+ var point1 = new Phaser . Point ( ) ,
251+ point2 = new Phaser . Point ( ) ,
252+ point3 = new Phaser . Point ( ) ,
253+ points = [ ] ,
254+ i ;
255+
256+ if ( ! indices ) {
257+ if ( vertices [ 0 ] instanceof Phaser . Point ) {
258+ for ( i = 0 ; i < vertices . length / 3 ; i ++ ) {
259+ this . drawTriangle ( [ vertices [ i * 3 ] , vertices [ i * 3 + 1 ] , vertices [ i * 3 + 2 ] ] , cull ) ;
260+ }
261+ } else {
262+ for ( i = 0 ; i < vertices . length / 6 ; i ++ ) {
263+ point1 . x = vertices [ i * 6 + 0 ] ;
264+ point1 . y = vertices [ i * 6 + 1 ] ;
265+ point2 . x = vertices [ i * 6 + 2 ] ;
266+ point2 . y = vertices [ i * 6 + 3 ] ;
267+ point3 . x = vertices [ i * 6 + 4 ] ;
268+ point3 . y = vertices [ i * 6 + 5 ] ;
269+ this . drawTriangle ( [ point1 , point2 , point3 ] , cull ) ;
270+ }
271+
272+ }
273+ } else {
274+ if ( vertices [ 0 ] instanceof Phaser . Point ) {
275+ for ( i = 0 ; i < indices . length / 3 ; i ++ ) {
276+ points . push ( vertices [ indices [ i * 3 ] ] ) ;
277+ points . push ( vertices [ indices [ i * 3 + 1 ] ] ) ;
278+ points . push ( vertices [ indices [ i * 3 + 2 ] ] ) ;
279+ if ( points . length === 3 ) {
280+ this . drawTriangle ( points , cull ) ;
281+ points = [ ] ;
282+ }
283+
284+ }
285+ } else {
286+ for ( i = 0 ; i < indices . length ; i ++ ) {
287+ point1 . x = vertices [ indices [ i ] * 2 ] ;
288+ point1 . y = vertices [ indices [ i ] * 2 + 1 ] ;
289+ points . push ( point1 . copyTo ( { } ) ) ;
290+ if ( points . length === 3 ) {
291+ this . drawTriangle ( points , cull ) ;
292+ points = [ ] ;
293+ }
294+ }
295+ }
296+ }
297+ } ;
298+
299+
300+
215301/**
216302* Indicates the rotation of the Graphics, in degrees, from its original orientation. Values from 0 to 180 represent clockwise rotation; values from 0 to -180 represent counterclockwise rotation.
217303* Values outside this range are added to or subtracted from 360 to obtain a value within the range. For example, the statement player.angle = 450 is the same as player.angle = 90.
0 commit comments