toJson method
meta -- object containing metadata such as materials, textures or images for the object.
Convert the object to three.js JSON Object/Scene format.
Implementation
Map<String, dynamic> toJson({Object3dMeta? meta}) {
// meta is a string when called from JSON.stringify
final 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.storage;//toArray(List<double>.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);
final parameters = geometry!.parameters;
if (parameters != null && parameters["shapes"] != null) {
final shapes = parameters["shapes"];
if (shapes is List) {
for (int i = 0, l = shapes.length; i < l; i++) {
final shape = shapes[i];
serialize(meta.shapes, shape, meta);
}
} else {
serialize(meta.shapes, shapes, meta);
}
}
}
// 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 GroupMaterial) {
for (int i = 0, l = (material as GroupMaterial).children.length; i < l; i++) {
uuids.add(serialize(meta.materials, (material as GroupMaterial).children[i], meta));
}
object["material"] = uuids;
} else {
object["material"] = serialize(meta.materials, material, meta);
}
}
if (children.isNotEmpty) {
List<Map<String, dynamic>> childrenJSON = [];
for (int i = 0; i < children.length; i++) {
childrenJSON.add(children[i].toJson(meta: meta)["object"]);
}
object["children"] = childrenJSON;
}
// if ( this.animations.length > 0 ) {
// List<Map<String, dynamic>> _animationJSON = [];
// for ( int i = 0; i < this.animations.length; i ++ ) {
// const animation = this.animations[ i ];
// _animationJSON.add( serialize( meta.animations, animation ) );
// }
// object["animations"] = _animationJSON;
// }
if (isRootObject) {
final geometries = extractFromCache(meta.geometries);
final materials = extractFromCache(meta.materials);
final textures = extractFromCache(meta.textures);
final images = extractFromCache(meta.images);
final shapes = extractFromCache(meta.shapes);
final skeletons = extractFromCache(meta.skeletons);
final animations = extractFromCache(meta.animations);
console.info("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;
}