From b279308234407622b752af8a3896759c2de2127d Mon Sep 17 00:00:00 2001 From: Richard Davey Date: Thu, 14 Dec 2017 02:29:30 +0000 Subject: [PATCH] Backface culling support added to tris --- v3/src/geom/mesh/Mesh.js | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/v3/src/geom/mesh/Mesh.js b/v3/src/geom/mesh/Mesh.js index 01534fafa..6ea4e2470 100644 --- a/v3/src/geom/mesh/Mesh.js +++ b/v3/src/geom/mesh/Mesh.js @@ -26,6 +26,8 @@ var Mesh = new Class({ this.fillColor = 0x00ff00; this.fillAlpha = 1; + this.backfaceCull = true; + this.points = []; this._tempVec3 = new Vector3(); @@ -90,7 +92,10 @@ var Mesh = new Class({ this.project(graphics, b, verts[face.vertices[1].vertexIndex], world); this.project(graphics, c, verts[face.vertices[2].vertexIndex], world); - graphics.fillTriangle(a.x, a.y, b.x, b.y, c.x, c.y); + if (this.backfaceCull && !this.isBackFacing(a, b, c)) + { + graphics.fillTriangle(a.x, a.y, b.x, b.y, c.x, c.y); + } }, fillPoly: function (graphics, face) @@ -154,7 +159,10 @@ var Mesh = new Class({ this.project(graphics, b, verts[face.vertices[1].vertexIndex], world); this.project(graphics, c, verts[face.vertices[2].vertexIndex], world); - graphics.strokeTriangle(a.x, a.y, b.x, b.y, c.x, c.y); + if (this.backfaceCull && !this.isBackFacing(a, b, c)) + { + graphics.strokeTriangle(a.x, a.y, b.x, b.y, c.x, c.y); + } }, strokePoly: function (graphics, face) @@ -190,6 +198,19 @@ var Mesh = new Class({ local.y = -point.y * h + h / 2 >> 0; }, + isBackFacing: function (a, b, c) + { + var ax = c.x - a.x; + var ay = c.y - a.y; + + var bx = b.x - c.x; + var by = b.y - c.y; + + var result = ax * by - ay * bx; + + return (result >= 0); + }, + setPosition: function (x, y, z) { if (x === undefined) { x = 0; }