flix3dExportObj function

Uint8List flix3dExportObj(
  1. Flix3DAsset asset
)

Writes an imported or procedural mesh collection as a Wavefront OBJ file.

Implementation

Uint8List flix3dExportObj(Flix3DAsset asset) {
  final output = StringBuffer('# Flix3DEngine OBJ export: ${asset.name}\n');
  var offset = 1;
  for (final mesh in asset.meshes) {
    output.writeln('o ${mesh.name.replaceAll(RegExp(r'\s+'), '_')}');
    output.writeln(
      'usemtl ${mesh.material.name.replaceAll(RegExp(r'\s+'), '_')}',
    );
    for (final point in mesh.positions) {
      output.writeln('v ${point.x} ${point.y} ${point.z}');
    }
    for (var index = 0; index < mesh.indices.length; index += 3) {
      output.writeln(
        'f ${mesh.indices[index] + offset} '
        '${mesh.indices[index + 1] + offset} '
        '${mesh.indices[index + 2] + offset}',
      );
    }
    offset += mesh.positions.length;
  }
  return Uint8List.fromList(utf8.encode(output.toString()));
}