From b4dcbc428fd33d45e6421a1131913f3a16be1626 Mon Sep 17 00:00:00 2001 From: Richard Davey Date: Fri, 15 Dec 2017 04:08:55 +0000 Subject: [PATCH] Backface cull can be enabled per type --- v3/src/geom/mesh/Mesh.js | 56 +++++++++++++++++++++++++++++++++++----- 1 file changed, 50 insertions(+), 6 deletions(-) diff --git a/v3/src/geom/mesh/Mesh.js b/v3/src/geom/mesh/Mesh.js index 6ea4e2470..1c23bfb62 100644 --- a/v3/src/geom/mesh/Mesh.js +++ b/v3/src/geom/mesh/Mesh.js @@ -26,7 +26,8 @@ var Mesh = new Class({ this.fillColor = 0x00ff00; this.fillAlpha = 1; - this.backfaceCull = true; + this.backfaceCullStroke = false; + this.backfaceCullFill = false; this.points = []; @@ -58,6 +59,8 @@ var Mesh = new Class({ graphics.fillStyle(this.fillColor, this.fillAlpha); + // Depth Sort + for (var m = 0; m < this.data.models.length; m++) { var model = this.data.models[m]; @@ -77,6 +80,18 @@ var Mesh = new Class({ } } + + // if (f % 2) + // { + // graphics.fillStyle(0xff0000, this.fillAlpha); + // } + // else + // { + // graphics.fillStyle(0xffffff, this.fillAlpha); + // } + + + }, fillTriangle: function (graphics, face) @@ -92,7 +107,7 @@ var Mesh = new Class({ this.project(graphics, b, verts[face.vertices[1].vertexIndex], world); this.project(graphics, c, verts[face.vertices[2].vertexIndex], world); - if (this.backfaceCull && !this.isBackFacing(a, b, c)) + if (!this.backfaceCullFill || (this.backfaceCullFill && !this.isBackFaceTriangle(a, b, c))) { graphics.fillTriangle(a.x, a.y, b.x, b.y, c.x, c.y); } @@ -112,7 +127,10 @@ var Mesh = new Class({ this.project(graphics, points[i], verts[face.vertices[i].vertexIndex], world); } - graphics.fillPoints(points, true, size); + if (!this.backfaceCullFill || (this.backfaceCullFill && this.isBackFacePoly(points, size))) + { + graphics.fillPoints(points, true, size); + } }, stroke: function (graphics) @@ -159,7 +177,7 @@ var Mesh = new Class({ this.project(graphics, b, verts[face.vertices[1].vertexIndex], world); this.project(graphics, c, verts[face.vertices[2].vertexIndex], world); - if (this.backfaceCull && !this.isBackFacing(a, b, c)) + if (!this.backfaceCullStroke || (this.backfaceCullStroke && !this.isBackFaceTriangle(a, b, c))) { graphics.strokeTriangle(a.x, a.y, b.x, b.y, c.x, c.y); } @@ -179,7 +197,10 @@ var Mesh = new Class({ this.project(graphics, points[i], verts[face.vertices[i].vertexIndex], world); } - graphics.strokePoints(points, true, size); + if (!this.backfaceCullStroke || (this.backfaceCullStroke && this.isBackFacePoly(points, size))) + { + graphics.strokePoints(points, true, size); + } }, // local is a Vec2 that is changed in place (so not returned) @@ -198,7 +219,7 @@ var Mesh = new Class({ local.y = -point.y * h + h / 2 >> 0; }, - isBackFacing: function (a, b, c) + isBackFaceTriangle: function (a, b, c) { var ax = c.x - a.x; var ay = c.y - a.y; @@ -211,6 +232,29 @@ var Mesh = new Class({ return (result >= 0); }, + isBackFacePoly: function (points, endIndex) + { + var area = 0; + + for (var i = 0; i < endIndex; i++) + { + j = (i + 1) % endIndex; + + area += points[i].x * points[j].y; + area -= points[j].x * points[i].y; + } + + return (area / 2); + }, + + setBackfaceCull: function (stroke, fill) + { + this.backfaceCullStroke = stroke; + this.backfaceCullFill = fill; + + return this; + }, + setPosition: function (x, y, z) { if (x === undefined) { x = 0; }