parseASCII method

BufferGeometry parseASCII(
  1. String data
)

Implementation

BufferGeometry parseASCII(String data) {
  final geometry = BufferGeometry();
  int faceCounter = 0;

  final List<double> vertices = [];
  final List<double> normals = [];
  final List<int> indices = [];
  final groupNames = [];

  int groupCount = 0;
  int startVertex = 0;
  int endVertex = 0;

  final lines = data.split('\n');
  int vertexCountPerFace = 0;
  int normalCountPerFace = 0;
  for (var line in lines) {
    List<String> parts = line.trim().split(RegExp(r"\s+"));
    switch (parts[0]) {
      case 'solid':
        groupNames.add(parts[0].replaceAll('solid ', ''));
        startVertex = endVertex;
        break;
      case 'endsolid':
        int start = startVertex;
        int count = endVertex - startVertex;

        geometry.userData['groupNames'] = groupNames;

        geometry.addGroup( start, count, groupCount );
        groupCount++;
        break;
      case 'facet':
        if(parts[1].contains('normal')){
          normals.addAll([double.parse(parts[2]),double.parse(parts[3]),double.parse(parts[4])]);
          normalCountPerFace++;
        }
        faceCounter++;
        break;
      case 'endfacet':
        // every face have to own ONE valid normal
        if(normalCountPerFace > 1){
          throw( 'THREE.STLLoader: Something isn\'t right with the normal of face number $faceCounter');
        }

        // each face have to own THREE valid vertices
        if(vertexCountPerFace != 3){
          print(vertexCountPerFace);
          throw( 'THREE.STLLoader: Something isn\'t right with the vertices of face number $faceCounter');
        }
        vertexCountPerFace = 0;
        normalCountPerFace = 0;
        break;
      case 'vertex':
        // the name for the group. eg: g front cube
        vertices.addAll([double.parse(parts[1]),double.parse(parts[2]),double.parse(parts[3])]);
        vertexCountPerFace++;
        endVertex++;
        int k = vertices.length;
        if(k%3 == 0 && k != 0){
          indices.addAll([k-3,k-2,k-1,k-3,k-2,k-1]);
        }
        break;
      default:
    }
  }

  geometry.setAttribute('position', Float32BufferAttribute(Float32Array.fromList(vertices),3));
  geometry.setAttribute('normal', Float32BufferAttribute(Float32Array.fromList(normals),3));
  geometry.setIndex(indices);

  return geometry;
}