Skip to content

Commit b279308

Browse files
committed
Backface culling support added to tris
1 parent c49f7de commit b279308

1 file changed

Lines changed: 23 additions & 2 deletions

File tree

v3/src/geom/mesh/Mesh.js

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ var Mesh = new Class({
2626
this.fillColor = 0x00ff00;
2727
this.fillAlpha = 1;
2828

29+
this.backfaceCull = true;
30+
2931
this.points = [];
3032

3133
this._tempVec3 = new Vector3();
@@ -90,7 +92,10 @@ var Mesh = new Class({
9092
this.project(graphics, b, verts[face.vertices[1].vertexIndex], world);
9193
this.project(graphics, c, verts[face.vertices[2].vertexIndex], world);
9294

93-
graphics.fillTriangle(a.x, a.y, b.x, b.y, c.x, c.y);
95+
if (this.backfaceCull && !this.isBackFacing(a, b, c))
96+
{
97+
graphics.fillTriangle(a.x, a.y, b.x, b.y, c.x, c.y);
98+
}
9499
},
95100

96101
fillPoly: function (graphics, face)
@@ -154,7 +159,10 @@ var Mesh = new Class({
154159
this.project(graphics, b, verts[face.vertices[1].vertexIndex], world);
155160
this.project(graphics, c, verts[face.vertices[2].vertexIndex], world);
156161

157-
graphics.strokeTriangle(a.x, a.y, b.x, b.y, c.x, c.y);
162+
if (this.backfaceCull && !this.isBackFacing(a, b, c))
163+
{
164+
graphics.strokeTriangle(a.x, a.y, b.x, b.y, c.x, c.y);
165+
}
158166
},
159167

160168
strokePoly: function (graphics, face)
@@ -190,6 +198,19 @@ var Mesh = new Class({
190198
local.y = -point.y * h + h / 2 >> 0;
191199
},
192200

201+
isBackFacing: function (a, b, c)
202+
{
203+
var ax = c.x - a.x;
204+
var ay = c.y - a.y;
205+
206+
var bx = b.x - c.x;
207+
var by = b.y - c.y;
208+
209+
var result = ax * by - ay * bx;
210+
211+
return (result >= 0);
212+
},
213+
193214
setPosition: function (x, y, z)
194215
{
195216
if (x === undefined) { x = 0; }

0 commit comments

Comments
 (0)