Skip to content

Commit

Permalink
IMPROVE: minor improvement to to_mesh performance. #15
Browse files Browse the repository at this point in the history
  • Loading branch information
spillerrec committed Mar 20, 2014
1 parent c31873f commit ff3e80b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 20 deletions.
28 changes: 16 additions & 12 deletions lib/MeshModel.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
part of ldraw;

abstract class Drawable3D{
void setColor( double r, double g, double b, double alpha );
void setColor( int r, int g, int b, int alpha );
void draw_triangles( Float32List vertices, int amount );
void draw_lines( Float32List vertices, int amount );
}
Expand Down Expand Up @@ -79,7 +79,7 @@ class DynamicFloat32List{
}

class MeshColor{
double r,g,b,a;
int r,g,b,a;
MeshColor( this.r, this.g, this.b, this.a );
void draw( Drawable3D canvas ) => canvas.setColor( r, g, b, a );

Expand All @@ -88,10 +88,10 @@ class MeshColor{
}
int get hashCode{
int result = 17;
result = 37 * result + (r*255).toInt();
result = 37 * result + (g*255).toInt();
result = 37 * result + (b*255).toInt();
result = 37 * result + (a*255).toInt();
result = 37 * result + r;
result = 37 * result + g;
result = 37 * result + b;
result = 37 * result + a;
return result;
}
}
Expand Down Expand Up @@ -178,14 +178,18 @@ class MeshModel{
return math.min( min_z, math.min( min_y, min_x ) );
}

void add_triangle( Float32List vertices, Matrix4 offset, double r, double g, double b, double a ){
MeshTriangles last_tri;
void add_triangle( Float32List vertices, Matrix4 offset, int r, int g, int b, int a ){
MeshColor color = new MeshColor( r, g, b, a );
MeshTriangles tri = triangles.putIfAbsent( color, () => new MeshTriangles(color) );
tri.vertices.addAll( vertices, offset );
if( last_tri == null || !(last_tri.color == color) )
last_tri = triangles.putIfAbsent( color, () => new MeshTriangles(color) );
last_tri.vertices.addAll( vertices, offset );
}
void add_lines( Float32List vertices, Matrix4 offset, double r, double g, double b, double a ){
MeshLines last_line;
void add_lines( Float32List vertices, Matrix4 offset, int r, int g, int b, int a ){
MeshColor color = new MeshColor( r, g, b, a );
MeshLines line = lines.putIfAbsent( color, () => new MeshLines(color) );
line.vertices.addAll( vertices, offset );
if( last_line == null || !(last_line.color == color) )
last_line = lines.putIfAbsent( color, () => new MeshLines(color) );
last_line.vertices.addAll( vertices, offset );
}
}
9 changes: 4 additions & 5 deletions lib/ldraw.dart
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,7 @@ class LDrawContext{
index = new LDrawColorIndex.officialColors();
color = index.lookUp(0);
}
LDrawContext.subpart( LDrawContext context, this.color, this.offset ){
index = context.index;
LDrawContext.subpart( this.index, this.color, this.offset ){
}
LDrawContext.subfile( LDrawContext context, LDrawColorIndex index ){
offset = context.offset;
Expand Down Expand Up @@ -424,7 +423,7 @@ class LDrawFile extends LDrawPrimitive{

void to_mesh( MeshModel model, LDrawContext context ){
Matrix4 new_pos = context.offset.clone().multiply(pos);
content.to_mesh( model, new LDrawContext.subpart( context, context.lookUp(color), new_pos ) );
content.to_mesh( model, new LDrawContext.subpart( context.index, context.lookUp(color), new_pos ) );
}
}

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

void to_mesh( MeshModel model, LDrawContext context ){
LDrawColor c = context.lookUp( color );
model.add_lines( vertices, context.offset, c.er/255, c.eg/255, c.eb/255, c.alpha/255 );
model.add_lines( vertices, context.offset, c.er, c.eg, c.eb, c.alpha );
}
}

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

void to_mesh( MeshModel model, LDrawContext context ){
LDrawColor c = context.lookUp( color );
model.add_triangle( vertices, context.offset, c.r/255, c.g/255, c.b/255, c.alpha/255 );
model.add_triangle( vertices, context.offset, c.r, c.g, c.b, c.alpha );
}
}

Expand Down
6 changes: 3 additions & 3 deletions lib/webgl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -191,11 +191,11 @@ class Canvas extends Drawable3D{
gl.uniformMatrix4fv( uMVMatrix, false, tmpList );
}

double old_r = 0.0, old_g = 0.0, old_b = 0.0, old_a = 0.0;
void setColor( double r, double g, double b, double a ){
int old_r = 0, old_g = 0, old_b = 0, old_a = 0;
void setColor( int r, int g, int b, int a ){
if( old_r != r || old_g != g || old_b != b || old_a != a ){
old_r = r; old_g = g; old_b = b; old_a = a;
gl.uniform4f( aColor, r, g, b, a );
gl.uniform4f( aColor, r/255, g/255, b/255, a/255 );
}
}

Expand Down

0 comments on commit ff3e80b

Please sign in to comment.