@@ -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,25 @@ 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 . backfaceCull = true ;
30+
31+ this . points = [ ] ;
3232
3333 this . _tempVec3 = new Vector3 ( ) ;
3434
3535 this . worldMatrix = new Matrix4 ( ) ;
36+
37+ this . createPoints ( ) ;
38+ } ,
39+
40+ createPoints : function ( )
41+ {
42+ var points = this . points ;
43+
44+ for ( var i = 0 ; i < this . data . maxVertices ; i ++ )
45+ {
46+ points . push ( new Vector2 ( ) ) ;
47+ }
3648 } ,
3749
3850 fill : function ( graphics )
@@ -42,27 +54,65 @@ var Mesh = new Class({
4254 return ;
4355 }
4456
45- var pa = this . _pA ;
46- var pb = this . _pB ;
47- var pc = this . _pC ;
57+ this . worldMatrix . setWorldMatrix ( this . rotation , this . position , this . scale , graphics . viewMatrix , graphics . projectionMatrix ) ;
4858
49- var world = this . worldMatrix ;
59+ graphics . fillStyle ( this . fillColor , this . fillAlpha ) ;
60+
61+ for ( var m = 0 ; m < this . data . models . length ; m ++ )
62+ {
63+ var model = this . data . models [ m ] ;
64+
65+ for ( var f = 0 ; f < model . faces . length ; f ++ )
66+ {
67+ var face = model . faces [ f ] ;
68+
69+ if ( face . type === 0 )
70+ {
71+ this . fillTriangle ( graphics , face ) ;
72+ }
73+ else
74+ {
75+ this . fillPoly ( graphics , face ) ;
76+ }
77+ }
78+ }
5079
51- world . setWorldMatrix ( this . rotation , this . position , this . scale , graphics . viewMatrix , graphics . projectionMatrix ) ;
80+ } ,
5281
53- graphics . fillStyle ( this . fillColor , this . fillAlpha ) ;
82+ fillTriangle : function ( graphics , face )
83+ {
84+ var a = this . points [ 0 ] ;
85+ var b = this . points [ 1 ] ;
86+ var c = this . points [ 2 ] ;
87+
88+ var verts = this . vertices ;
89+ var world = this . worldMatrix ;
5490
55- for ( var f = 0 ; f < this . faces . length ; f ++ )
91+ this . project ( graphics , a , verts [ face . vertices [ 0 ] . vertexIndex ] , world ) ;
92+ this . project ( graphics , b , verts [ face . vertices [ 1 ] . vertexIndex ] , world ) ;
93+ this . project ( graphics , c , verts [ face . vertices [ 2 ] . vertexIndex ] , world ) ;
94+
95+ if ( this . backfaceCull && ! this . isBackFacing ( a , b , c ) )
5696 {
57- var face = this . faces [ f ] ;
58- var verts = this . vertices ;
97+ graphics . fillTriangle ( a . x , a . y , b . x , b . y , c . x , c . y ) ;
98+ }
99+ } ,
100+
101+ fillPoly : function ( graphics , face )
102+ {
103+ var points = this . points ;
104+ var verts = this . vertices ;
105+ var world = this . worldMatrix ;
59106
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 ) ;
107+ var size = face . vertices . length ;
63108
64- graphics . fillTriangle ( pa . x , pa . y , pb . x , pb . y , pc . x , pc . y ) ;
109+ // Project
110+ for ( var i = 0 ; i < size ; i ++ )
111+ {
112+ this . project ( graphics , points [ i ] , verts [ face . vertices [ i ] . vertexIndex ] , world ) ;
65113 }
114+
115+ graphics . fillPoints ( points , true , size ) ;
66116 } ,
67117
68118 stroke : function ( graphics )
@@ -72,45 +122,67 @@ var Mesh = new Class({
72122 return ;
73123 }
74124
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 ) ;
125+ this . worldMatrix . setWorldMatrix ( this . rotation , this . position , this . scale , graphics . viewMatrix , graphics . projectionMatrix ) ;
83126
84127 graphics . lineStyle ( this . thickness , this . strokeColor , this . strokeAlpha ) ;
85- graphics . beginPath ( ) ;
86128
87- for ( var f = 0 ; f < this . faces . length ; f ++ )
129+ for ( var m = 0 ; m < this . data . models . length ; m ++ )
88130 {
89- var face = this . faces [ f ] ;
90- var verts = this . vertices ;
131+ var model = this . data . models [ m ] ;
132+
133+ for ( var f = 0 ; f < model . faces . length ; f ++ )
134+ {
135+ var face = model . faces [ f ] ;
136+
137+ if ( face . type === 0 )
138+ {
139+ this . strokeTriangle ( graphics , face ) ;
140+ }
141+ else
142+ {
143+ this . strokePoly ( graphics , face ) ;
144+ }
145+ }
146+ }
147+ } ,
91148
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 ) ;
149+ strokeTriangle : function ( graphics , face )
150+ {
151+ var a = this . points [ 0 ] ;
152+ var b = this . points [ 1 ] ;
153+ var c = this . points [ 2 ] ;
96154
97- graphics . moveTo ( pa . x , pa . y ) ;
98- graphics . lineTo ( pb . x , pb . y ) ;
155+ var verts = this . vertices ;
156+ var world = this . worldMatrix ;
99157
100- graphics . moveTo ( pb . x , pb . y ) ;
101- graphics . lineTo ( pc . x , pc . y ) ;
158+ this . project ( graphics , a , verts [ face . vertices [ 0 ] . vertexIndex ] , world ) ;
159+ this . project ( graphics , b , verts [ face . vertices [ 1 ] . vertexIndex ] , world ) ;
160+ this . project ( graphics , c , verts [ face . vertices [ 2 ] . vertexIndex ] , world ) ;
102161
103- graphics . moveTo ( pc . x , pc . y ) ;
104- graphics . lineTo ( pd . x , pd . 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+ }
166+ } ,
105167
106- graphics . moveTo ( pd . x , pd . y ) ;
107- graphics . lineTo ( pa . x , pa . y ) ;
168+ strokePoly : function ( graphics , face )
169+ {
170+ var points = this . points ;
171+ var verts = this . vertices ;
172+ var world = this . worldMatrix ;
173+
174+ var size = face . vertices . length ;
175+
176+ // Project
177+ for ( var i = 0 ; i < size ; i ++ )
178+ {
179+ this . project ( graphics , points [ i ] , verts [ face . vertices [ i ] . vertexIndex ] , world ) ;
108180 }
109181
110- graphics . closePath ( ) ;
111- graphics . strokePath ( ) ;
182+ graphics . strokePoints ( points , true , size ) ;
112183 } ,
113184
185+ // local is a Vec2 that is changed in place (so not returned)
114186 project : function ( graphics , local , coord , transMat )
115187 {
116188 var w = graphics . viewportWidth ;
@@ -126,6 +198,19 @@ var Mesh = new Class({
126198 local . y = - point . y * h + h / 2 >> 0 ;
127199 } ,
128200
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+
129214 setPosition : function ( x , y , z )
130215 {
131216 if ( x === undefined ) { x = 0 ; }
0 commit comments