Skip to content

Commit ff3e80b

Browse files
committed
IMPROVE: minor improvement to to_mesh performance. #15
1 parent c31873f commit ff3e80b

File tree

3 files changed

+23
-20
lines changed

3 files changed

+23
-20
lines changed

lib/MeshModel.dart

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
part of ldraw;
22

33
abstract class Drawable3D{
4-
void setColor( double r, double g, double b, double alpha );
4+
void setColor( int r, int g, int b, int alpha );
55
void draw_triangles( Float32List vertices, int amount );
66
void draw_lines( Float32List vertices, int amount );
77
}
@@ -79,7 +79,7 @@ class DynamicFloat32List{
7979
}
8080

8181
class MeshColor{
82-
double r,g,b,a;
82+
int r,g,b,a;
8383
MeshColor( this.r, this.g, this.b, this.a );
8484
void draw( Drawable3D canvas ) => canvas.setColor( r, g, b, a );
8585

@@ -88,10 +88,10 @@ class MeshColor{
8888
}
8989
int get hashCode{
9090
int result = 17;
91-
result = 37 * result + (r*255).toInt();
92-
result = 37 * result + (g*255).toInt();
93-
result = 37 * result + (b*255).toInt();
94-
result = 37 * result + (a*255).toInt();
91+
result = 37 * result + r;
92+
result = 37 * result + g;
93+
result = 37 * result + b;
94+
result = 37 * result + a;
9595
return result;
9696
}
9797
}
@@ -178,14 +178,18 @@ class MeshModel{
178178
return math.min( min_z, math.min( min_y, min_x ) );
179179
}
180180

181-
void add_triangle( Float32List vertices, Matrix4 offset, double r, double g, double b, double a ){
181+
MeshTriangles last_tri;
182+
void add_triangle( Float32List vertices, Matrix4 offset, int r, int g, int b, int a ){
182183
MeshColor color = new MeshColor( r, g, b, a );
183-
MeshTriangles tri = triangles.putIfAbsent( color, () => new MeshTriangles(color) );
184-
tri.vertices.addAll( vertices, offset );
184+
if( last_tri == null || !(last_tri.color == color) )
185+
last_tri = triangles.putIfAbsent( color, () => new MeshTriangles(color) );
186+
last_tri.vertices.addAll( vertices, offset );
185187
}
186-
void add_lines( Float32List vertices, Matrix4 offset, double r, double g, double b, double a ){
188+
MeshLines last_line;
189+
void add_lines( Float32List vertices, Matrix4 offset, int r, int g, int b, int a ){
187190
MeshColor color = new MeshColor( r, g, b, a );
188-
MeshLines line = lines.putIfAbsent( color, () => new MeshLines(color) );
189-
line.vertices.addAll( vertices, offset );
191+
if( last_line == null || !(last_line.color == color) )
192+
last_line = lines.putIfAbsent( color, () => new MeshLines(color) );
193+
last_line.vertices.addAll( vertices, offset );
190194
}
191195
}

lib/ldraw.dart

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,7 @@ class LDrawContext{
208208
index = new LDrawColorIndex.officialColors();
209209
color = index.lookUp(0);
210210
}
211-
LDrawContext.subpart( LDrawContext context, this.color, this.offset ){
212-
index = context.index;
211+
LDrawContext.subpart( this.index, this.color, this.offset ){
213212
}
214213
LDrawContext.subfile( LDrawContext context, LDrawColorIndex index ){
215214
offset = context.offset;
@@ -424,7 +423,7 @@ class LDrawFile extends LDrawPrimitive{
424423

425424
void to_mesh( MeshModel model, LDrawContext context ){
426425
Matrix4 new_pos = context.offset.clone().multiply(pos);
427-
content.to_mesh( model, new LDrawContext.subpart( context, context.lookUp(color), new_pos ) );
426+
content.to_mesh( model, new LDrawContext.subpart( context.index, context.lookUp(color), new_pos ) );
428427
}
429428
}
430429

@@ -434,7 +433,7 @@ class LDrawLine extends LDrawPrimitive{
434433

435434
void to_mesh( MeshModel model, LDrawContext context ){
436435
LDrawColor c = context.lookUp( color );
437-
model.add_lines( vertices, context.offset, c.er/255, c.eg/255, c.eb/255, c.alpha/255 );
436+
model.add_lines( vertices, context.offset, c.er, c.eg, c.eb, c.alpha );
438437
}
439438
}
440439

@@ -444,7 +443,7 @@ class LDrawTriangle extends LDrawPrimitive{
444443

445444
void to_mesh( MeshModel model, LDrawContext context ){
446445
LDrawColor c = context.lookUp( color );
447-
model.add_triangle( vertices, context.offset, c.r/255, c.g/255, c.b/255, c.alpha/255 );
446+
model.add_triangle( vertices, context.offset, c.r, c.g, c.b, c.alpha );
448447
}
449448
}
450449

lib/webgl.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,11 +191,11 @@ class Canvas extends Drawable3D{
191191
gl.uniformMatrix4fv( uMVMatrix, false, tmpList );
192192
}
193193

194-
double old_r = 0.0, old_g = 0.0, old_b = 0.0, old_a = 0.0;
195-
void setColor( double r, double g, double b, double a ){
194+
int old_r = 0, old_g = 0, old_b = 0, old_a = 0;
195+
void setColor( int r, int g, int b, int a ){
196196
if( old_r != r || old_g != g || old_b != b || old_a != a ){
197197
old_r = r; old_g = g; old_b = b; old_a = a;
198-
gl.uniform4f( aColor, r, g, b, a );
198+
gl.uniform4f( aColor, r/255, g/255, b/255, a/255 );
199199
}
200200
}
201201

0 commit comments

Comments
 (0)