toJson method

Map<String, dynamic> toJson({
  1. Object3dMeta? meta,
})
inherited

Convert the buffer geometry to three.js JSON Object/Scene format.

Implementation

Map<String, dynamic> toJson({Object3dMeta? meta}) {
  Map<String, dynamic> data = {
    "metadata": {
      "version": 4.5,
      "type": 'BufferGeometry',
      "generator": 'BufferGeometry.toJson'
    }
  };

  // standard BufferGeometry serialization

  data["uuid"] = uuid;
  data["type"] = type;
  if (name != '') data["name"] = name;
  if (userData.keys.isNotEmpty) data["userData"] = userData;

  if (parameters != null) {
    for (String key in parameters!.keys) {
      if (parameters![key] != null) data[key] = parameters![key];
    }

    return data;
  }

  // for simplicity the code assumes attributes are not shared across geometries, see #15811

  data["data"] = {};
  data["data"]["attributes"] = {};

  final index = this.index;

  if (index != null) {
    data["data"]["index"] = {
      "type": index.array.runtimeType.toString(),
      "array": index.array.sublist(0)
    };
  }

  final attributes = this.attributes;

  for (String key in attributes.keys) {
    final attribute = attributes[key];

    // data["data"]["attributes"][ key ] = attribute.toJson( data["data"] );
    data["data"]["attributes"][key] = attribute.toJson();
  }

  Map<String, List<BufferAttribute>> morphAttributes = {};
  bool hasMorphAttributes = false;

  for (String key in morphAttributes.keys) {
    final attributeArray = this.morphAttributes[key]!;

    List<BufferAttribute> array = [];

    for (int i = 0, il = attributeArray.length; i < il; i++) {
      final attribute = attributeArray[i];

      // final attributeData = attribute.toJson( data["data"] );
      // final attributeData = attribute.toJson();

      array.add(attribute);
    }

    if (array.isNotEmpty) {
      morphAttributes[key] = array;

      hasMorphAttributes = true;
    }
  }

  if (hasMorphAttributes) {
    data["data"].morphAttributes = morphAttributes;
    data["data"].morphTargetsRelative = morphTargetsRelative;
  }

  List<Map<String,dynamic>> groups = this.groups;

  if (groups.isNotEmpty) {
    data["data"]["groups"] = json.decode(json.encode(groups));
  }

  final boundingSphere = this.boundingSphere;

  if (boundingSphere != null) {
    List<double> l = List.filled(3, 0);
    boundingSphere.center.copyIntoArray(l);
    data["data"]["boundingSphere"] = {
      "center": l,
      "radius": boundingSphere.radius
    };
  }

  return data;
}