toJSON method

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

Implementation

Map<String, dynamic> toJSON({Object3dMeta? meta}) {
  // meta is a string when called from JSON.stringify
  var isRootObject = (meta == null || meta is String);

  Map<String, dynamic> output = <String, dynamic>{};

  // meta is a hash used to collect geometries, materials.
  // not providing it implies that this is the root object
  // being serialized.
  if (isRootObject) {
    // initialize meta obj
    meta = Object3dMeta();

    output["metadata"] = {"version": 4.5, "type": 'Object', "generator": 'Object3D.toJSON'};
  }

  // standard Object3D serialization

  Map<String, dynamic> object = <String, dynamic>{};

  object["uuid"] = uuid;
  object["type"] = type;

  if (name != "") object["name"] = name;
  if (castShadow == true) object["castShadow"] = true;
  if (receiveShadow == true) object["receiveShadow"] = true;
  if (visible == false) object["visible"] = false;
  if (frustumCulled == false) object["frustumCulled"] = false;
  if (renderOrder != 0) object["renderOrder"] = renderOrder;
  if (userData.isNotEmpty) object["userData"] = userData;

  object["layers"] = layers.mask;
  object["matrix"] = matrix.toArray(List<num>.filled(16, 0.0));

  if (matrixAutoUpdate == false) object["matrixAutoUpdate"] = false;

  // object specific properties

  if (type == "InstancedMesh") {
    InstancedMesh instanceMesh = this as InstancedMesh;

    object["type"] = 'InstancedMesh';
    object["count"] = instanceMesh.count;
    object["instanceMatrix"] = instanceMesh.instanceMatrix!.toJSON();

    if (instanceMesh.instanceColor != null) {
      object["instanceColor"] = instanceMesh.instanceColor!.toJSON();
    }
  }

  if (this is Scene) {
    if (background != null) {
      if (background is Color) {
        object["background"] = background!.getHex();
      } else if (background is Texture) {
        object["background"] = background.toJSON(meta).uuid;
      }
    }

    if (environment != null && environment is Texture) {
      object["environment"] = environment!.toJSON(meta)['uuid'];
    }
  } else if (this is Mesh || this is Line || this is Points) {
    object["geometry"] = serialize(meta!.geometries, geometry, meta);

    var parameters = geometry!.parameters;

    if (parameters != null && parameters["shapes"] != null) {
      var shapes = parameters["shapes"];

      if (shapes is List) {
        for (var i = 0, l = shapes.length; i < l; i++) {
          var shape = shapes[i];

          serialize(meta.shapes, shape, meta);
        }
      } else {
        serialize(meta.shapes, shapes, meta);
      }
    }
  }

  // TODO
  // if ( this.type == "SkinnedMesh" ) {

  //   SkinnedMesh _skinnedMesh = this;

  // 	object["bindMode"] = _skinnedMesh.bindMode;
  // 	object["bindMatrix"] = _skinnedMesh.bindMatrix.toArray();

  // 	if ( _skinnedMesh.skeleton != null ) {

  // 		serialize( meta.skeletons, _skinnedMesh.skeleton );

  // 		object.skeleton = _skinnedMesh.skeleton.uuid;

  // 	}

  // }

  if (material != null) {
    List<String> uuids = [];

    if (material is List) {
      for (var i = 0, l = material.length; i < l; i++) {
        uuids.add(serialize(meta!.materials, material[i], meta));
      }

      object["material"] = uuids;
    } else {
      object["material"] = serialize(meta!.materials, material, meta);
    }
  }

  if (children.isNotEmpty) {
    List<Map<String, dynamic>> childrenJSON = [];

    for (var i = 0; i < children.length; i++) {
      childrenJSON.add(children[i].toJSON(meta: meta)["object"]);
    }

    object["children"] = childrenJSON;
  }

  // //
  // TODO
  // if ( this.animations.length > 0 ) {

  // 	List<Map<String, dynamic>> _animationJSON = [];

  // 	for ( var i = 0; i < this.animations.length; i ++ ) {

  // 		const animation = this.animations[ i ];

  // 		_animationJSON.add( serialize( meta.animations, animation ) );

  // 	}

  //   object["animations"] = _animationJSON;

  // }

  if (isRootObject) {
    var geometries = extractFromCache(meta!.geometries);
    var materials = extractFromCache(meta.materials);
    var textures = extractFromCache(meta.textures);
    var images = extractFromCache(meta.images);
    var shapes = extractFromCache(meta.shapes);
    var skeletons = extractFromCache(meta.skeletons);
    var animations = extractFromCache(meta.animations);

    print(textures);
    print(" isRootObject: $isRootObject ");

    if (geometries.isNotEmpty) output["geometries"] = geometries;
    if (materials.isNotEmpty) output["materials"] = materials;
    if (textures.isNotEmpty) output["textures"] = textures;
    if (images.isNotEmpty) output["images"] = images;
    if (shapes.isNotEmpty) output["shapes"] = shapes;
    if (skeletons.isNotEmpty) output["skeletons"] = skeletons;
    if (animations.isNotEmpty) output["animations"] = animations;
  }

  output["object"] = object;

  return output;
}