MeshGeometry.fromJson constructor

MeshGeometry.fromJson(
  1. Map<String, Object> json
)

Implementation

factory MeshGeometry.fromJson(Map<String, Object> json) {
  Float32List buffer;
  final jsonBuffer = json['buffer'];
  if (jsonBuffer is List<double>) {
    buffer = Float32List.fromList(jsonBuffer);
  } else {
    throw ArgumentError.value(
        jsonBuffer, 'json["buffer"]', 'Value type must be List<double>');
  }

  final jsonAttribs = json['attribs'];
  Map<String, Object> jsonAttribsMap;
  if (jsonAttribs is Map<String, Object>) {
    jsonAttribsMap = jsonAttribs;
  } else {
    throw ArgumentError.value(jsonBuffer, 'json["attribs"]',
        'Value type must be Map<String, Object>');
  }
  final attribs = <VertexAttrib>[];
  var stride = 0;
  for (var key in jsonAttribsMap.keys) {
    VertexAttrib attrib;
    final jsonAttrib = jsonAttribsMap[key];
    if (jsonAttrib is Map<String, Object>) {
      attrib = attribFromJson(key, jsonAttrib);
      attribs.add(attrib);
      if (stride == 0) {
        stride = attrib.stride;
      }
    }
  }

  final mesh = MeshGeometry._internal(
      buffer.lengthInBytes ~/ stride, stride, attribs, buffer);

  final jsonIndices = json['indices'];
  if (jsonIndices is List<int>) {
    mesh.indices = Uint16List.fromList(jsonIndices);
  }

  return mesh;
}