sniffModelFormat function

ModelFormat sniffModelFormat(
  1. Uint8List bytes
)

Guesses a format from the leading bytes.

Implementation

ModelFormat sniffModelFormat(Uint8List bytes) {
  if (isF3dFile(bytes)) return ModelFormat.f3d;

  // 'glTF' little-endian magic marks a GLB container.
  if (bytes.length >= 4 &&
      bytes[0] == 0x67 &&
      bytes[1] == 0x6C &&
      bytes[2] == 0x54 &&
      bytes[3] == 0x46) {
    return ModelFormat.gltf;
  }

  // Skip whitespace, then a '{' means JSON, i.e. a .gltf document.
  for (var i = 0; i < bytes.length && i < 64; i++) {
    final byte = bytes[i];
    if (byte == 0x20 || byte == 0x09 || byte == 0x0A || byte == 0x0D) continue;
    if (byte == 0x7B) return ModelFormat.gltf;
    break;
  }

  // Checked ahead of the OBJ default, and after everything above: a binary
  // STL's own size is decisive regardless of what its header text says (see
  // `isBinaryStl`), and an ASCII one is the one format here that looks like
  // plain text the way OBJ does, so it needs its own check rather than
  // falling into that default by looking similar enough.
  if (isBinaryStl(bytes) || looksLikeAsciiStl(bytes)) return ModelFormat.stl;

  return ModelFormat.obj;
}