Skip to content

Commit b4dcbc4

Browse files
committed
Backface cull can be enabled per type
1 parent d999151 commit b4dcbc4

1 file changed

Lines changed: 50 additions & 6 deletions

File tree

v3/src/geom/mesh/Mesh.js

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

29-
this.backfaceCull = true;
29+
this.backfaceCullStroke = false;
30+
this.backfaceCullFill = false;
3031

3132
this.points = [];
3233

@@ -58,6 +59,8 @@ var Mesh = new Class({
5859

5960
graphics.fillStyle(this.fillColor, this.fillAlpha);
6061

62+
// Depth Sort
63+
6164
for (var m = 0; m < this.data.models.length; m++)
6265
{
6366
var model = this.data.models[m];
@@ -77,6 +80,18 @@ var Mesh = new Class({
7780
}
7881
}
7982

83+
84+
// if (f % 2)
85+
// {
86+
// graphics.fillStyle(0xff0000, this.fillAlpha);
87+
// }
88+
// else
89+
// {
90+
// graphics.fillStyle(0xffffff, this.fillAlpha);
91+
// }
92+
93+
94+
8095
},
8196

8297
fillTriangle: function (graphics, face)
@@ -92,7 +107,7 @@ var Mesh = new Class({
92107
this.project(graphics, b, verts[face.vertices[1].vertexIndex], world);
93108
this.project(graphics, c, verts[face.vertices[2].vertexIndex], world);
94109

95-
if (this.backfaceCull && !this.isBackFacing(a, b, c))
110+
if (!this.backfaceCullFill || (this.backfaceCullFill && !this.isBackFaceTriangle(a, b, c)))
96111
{
97112
graphics.fillTriangle(a.x, a.y, b.x, b.y, c.x, c.y);
98113
}
@@ -112,7 +127,10 @@ var Mesh = new Class({
112127
this.project(graphics, points[i], verts[face.vertices[i].vertexIndex], world);
113128
}
114129

115-
graphics.fillPoints(points, true, size);
130+
if (!this.backfaceCullFill || (this.backfaceCullFill && this.isBackFacePoly(points, size)))
131+
{
132+
graphics.fillPoints(points, true, size);
133+
}
116134
},
117135

118136
stroke: function (graphics)
@@ -159,7 +177,7 @@ var Mesh = new Class({
159177
this.project(graphics, b, verts[face.vertices[1].vertexIndex], world);
160178
this.project(graphics, c, verts[face.vertices[2].vertexIndex], world);
161179

162-
if (this.backfaceCull && !this.isBackFacing(a, b, c))
180+
if (!this.backfaceCullStroke || (this.backfaceCullStroke && !this.isBackFaceTriangle(a, b, c)))
163181
{
164182
graphics.strokeTriangle(a.x, a.y, b.x, b.y, c.x, c.y);
165183
}
@@ -179,7 +197,10 @@ var Mesh = new Class({
179197
this.project(graphics, points[i], verts[face.vertices[i].vertexIndex], world);
180198
}
181199

182-
graphics.strokePoints(points, true, size);
200+
if (!this.backfaceCullStroke || (this.backfaceCullStroke && this.isBackFacePoly(points, size)))
201+
{
202+
graphics.strokePoints(points, true, size);
203+
}
183204
},
184205

185206
// local is a Vec2 that is changed in place (so not returned)
@@ -198,7 +219,7 @@ var Mesh = new Class({
198219
local.y = -point.y * h + h / 2 >> 0;
199220
},
200221

201-
isBackFacing: function (a, b, c)
222+
isBackFaceTriangle: function (a, b, c)
202223
{
203224
var ax = c.x - a.x;
204225
var ay = c.y - a.y;
@@ -211,6 +232,29 @@ var Mesh = new Class({
211232
return (result >= 0);
212233
},
213234

235+
isBackFacePoly: function (points, endIndex)
236+
{
237+
var area = 0;
238+
239+
for (var i = 0; i < endIndex; i++)
240+
{
241+
j = (i + 1) % endIndex;
242+
243+
area += points[i].x * points[j].y;
244+
area -= points[j].x * points[i].y;
245+
}
246+
247+
return (area / 2);
248+
},
249+
250+
setBackfaceCull: function (stroke, fill)
251+
{
252+
this.backfaceCullStroke = stroke;
253+
this.backfaceCullFill = fill;
254+
255+
return this;
256+
},
257+
214258
setPosition: function (x, y, z)
215259
{
216260
if (x === undefined) { x = 0; }

0 commit comments

Comments
 (0)