Skip to content

Commit c49f7de

Browse files
committed
Updated to use new ParseObj data format
1 parent 4a5f4a8 commit c49f7de

1 file changed

Lines changed: 109 additions & 45 deletions

File tree

v3/src/geom/mesh/Mesh.js

Lines changed: 109 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ var Mesh = new Class({
99

1010
function Mesh (data, x, y, z)
1111
{
12-
this.vertices = data.verts;
13-
this.faces = data.faces;
12+
// May contain multiple models
13+
this.data = data;
14+
this.vertices = data.vertices;
1415

1516
this.position = new Vector3(x, y, z);
1617
this.rotation = new Vector3();
@@ -25,14 +26,23 @@ var Mesh = new Class({
2526
this.fillColor = 0x00ff00;
2627
this.fillAlpha = 1;
2728

28-
this._pA = new Vector2();
29-
this._pB = new Vector2();
30-
this._pC = new Vector2();
31-
this._pD = new Vector2();
29+
this.points = [];
3230

3331
this._tempVec3 = new Vector3();
3432

3533
this.worldMatrix = new Matrix4();
34+
35+
this.createPoints();
36+
},
37+
38+
createPoints: function ()
39+
{
40+
var points = this.points;
41+
42+
for (var i = 0; i < this.data.maxVertices; i++)
43+
{
44+
points.push(new Vector2());
45+
}
3646
},
3747

3848
fill: function (graphics)
@@ -42,27 +52,62 @@ var Mesh = new Class({
4252
return;
4353
}
4454

45-
var pa = this._pA;
46-
var pb = this._pB;
47-
var pc = this._pC;
55+
this.worldMatrix.setWorldMatrix(this.rotation, this.position, this.scale, graphics.viewMatrix, graphics.projectionMatrix);
56+
57+
graphics.fillStyle(this.fillColor, this.fillAlpha);
58+
59+
for (var m = 0; m < this.data.models.length; m++)
60+
{
61+
var model = this.data.models[m];
62+
63+
for (var f = 0; f < model.faces.length; f++)
64+
{
65+
var face = model.faces[f];
66+
67+
if (face.type === 0)
68+
{
69+
this.fillTriangle(graphics, face);
70+
}
71+
else
72+
{
73+
this.fillPoly(graphics, face);
74+
}
75+
}
76+
}
77+
78+
},
4879

80+
fillTriangle: function (graphics, face)
81+
{
82+
var a = this.points[0];
83+
var b = this.points[1];
84+
var c = this.points[2];
85+
86+
var verts = this.vertices;
4987
var world = this.worldMatrix;
5088

51-
world.setWorldMatrix(this.rotation, this.position, this.scale, graphics.viewMatrix, graphics.projectionMatrix);
89+
this.project(graphics, a, verts[face.vertices[0].vertexIndex], world);
90+
this.project(graphics, b, verts[face.vertices[1].vertexIndex], world);
91+
this.project(graphics, c, verts[face.vertices[2].vertexIndex], world);
5292

53-
graphics.fillStyle(this.fillColor, this.fillAlpha);
93+
graphics.fillTriangle(a.x, a.y, b.x, b.y, c.x, c.y);
94+
},
5495

55-
for (var f = 0; f < this.faces.length; f++)
56-
{
57-
var face = this.faces[f];
58-
var verts = this.vertices;
96+
fillPoly: function (graphics, face)
97+
{
98+
var points = this.points;
99+
var verts = this.vertices;
100+
var world = this.worldMatrix;
59101

60-
this.project(graphics, pa, verts[face.A].pos, world);
61-
this.project(graphics, pb, verts[face.B].pos, world);
62-
this.project(graphics, pc, verts[face.C].pos, world);
102+
var size = face.vertices.length;
63103

64-
graphics.fillTriangle(pa.x, pa.y, pb.x, pb.y, pc.x, pc.y);
104+
// Project
105+
for (var i = 0; i < size; i++)
106+
{
107+
this.project(graphics, points[i], verts[face.vertices[i].vertexIndex], world);
65108
}
109+
110+
graphics.fillPoints(points, true, size);
66111
},
67112

68113
stroke: function (graphics)
@@ -72,45 +117,64 @@ var Mesh = new Class({
72117
return;
73118
}
74119

75-
var pa = this._pA;
76-
var pb = this._pB;
77-
var pc = this._pC;
78-
var pd = this._pD;
79-
80-
var world = this.worldMatrix;
81-
82-
world.setWorldMatrix(this.rotation, this.position, this.scale, graphics.viewMatrix, graphics.projectionMatrix);
120+
this.worldMatrix.setWorldMatrix(this.rotation, this.position, this.scale, graphics.viewMatrix, graphics.projectionMatrix);
83121

84122
graphics.lineStyle(this.thickness, this.strokeColor, this.strokeAlpha);
85-
graphics.beginPath();
86123

87-
for (var f = 0; f < this.faces.length; f++)
124+
for (var m = 0; m < this.data.models.length; m++)
88125
{
89-
var face = this.faces[f];
90-
var verts = this.vertices;
126+
var model = this.data.models[m];
127+
128+
for (var f = 0; f < model.faces.length; f++)
129+
{
130+
var face = model.faces[f];
131+
132+
if (face.type === 0)
133+
{
134+
this.strokeTriangle(graphics, face);
135+
}
136+
else
137+
{
138+
this.strokePoly(graphics, face);
139+
}
140+
}
141+
}
142+
},
91143

92-
this.project(graphics, pa, verts[face.A].pos, world);
93-
this.project(graphics, pb, verts[face.B].pos, world);
94-
this.project(graphics, pc, verts[face.C].pos, world);
95-
this.project(graphics, pd, verts[face.D].pos, world);
144+
strokeTriangle: function (graphics, face)
145+
{
146+
var a = this.points[0];
147+
var b = this.points[1];
148+
var c = this.points[2];
96149

97-
graphics.moveTo(pa.x, pa.y);
98-
graphics.lineTo(pb.x, pb.y);
150+
var verts = this.vertices;
151+
var world = this.worldMatrix;
99152

100-
graphics.moveTo(pb.x, pb.y);
101-
graphics.lineTo(pc.x, pc.y);
153+
this.project(graphics, a, verts[face.vertices[0].vertexIndex], world);
154+
this.project(graphics, b, verts[face.vertices[1].vertexIndex], world);
155+
this.project(graphics, c, verts[face.vertices[2].vertexIndex], world);
102156

103-
graphics.moveTo(pc.x, pc.y);
104-
graphics.lineTo(pd.x, pd.y);
157+
graphics.strokeTriangle(a.x, a.y, b.x, b.y, c.x, c.y);
158+
},
105159

106-
graphics.moveTo(pd.x, pd.y);
107-
graphics.lineTo(pa.x, pa.y);
160+
strokePoly: function (graphics, face)
161+
{
162+
var points = this.points;
163+
var verts = this.vertices;
164+
var world = this.worldMatrix;
165+
166+
var size = face.vertices.length;
167+
168+
// Project
169+
for (var i = 0; i < size; i++)
170+
{
171+
this.project(graphics, points[i], verts[face.vertices[i].vertexIndex], world);
108172
}
109173

110-
graphics.closePath();
111-
graphics.strokePath();
174+
graphics.strokePoints(points, true, size);
112175
},
113176

177+
// local is a Vec2 that is changed in place (so not returned)
114178
project: function (graphics, local, coord, transMat)
115179
{
116180
var w = graphics.viewportWidth;

0 commit comments

Comments
 (0)