diff --git a/lib/MeshModel.dart b/lib/MeshModel.dart index 7bdcc67..495a3e2 100644 --- a/lib/MeshModel.dart +++ b/lib/MeshModel.dart @@ -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 ); } @@ -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 ); @@ -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; } } @@ -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 ); } } \ No newline at end of file diff --git a/lib/ldraw.dart b/lib/ldraw.dart index 0efe719..e2845af 100644 --- a/lib/ldraw.dart +++ b/lib/ldraw.dart @@ -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; @@ -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 ) ); } } @@ -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 ); } } @@ -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 ); } } diff --git a/lib/webgl.dart b/lib/webgl.dart index 4421426..24f74e6 100644 --- a/lib/webgl.dart +++ b/lib/webgl.dart @@ -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 ); } }