toJson method

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

meta -- object containing metadata such as textures or images for the material.

Convert the material to three.js JSON Object/Scene format.

Implementation

Map<String, dynamic> toJson({Object3dMeta? meta}) {
  final isRoot = (meta == null || meta is String);

  if (isRoot) {
    meta = Object3dMeta();
  }

  Map<String, dynamic> data = {
    "metadata": {
      "version": 4.5,
      "type": 'Material',
      "generator": 'Material.toJson'
    }
  };

  // standard Material serialization
  data["uuid"] = uuid;
  data["type"] = type;

  if (name != '') data["name"] = name;

  data["color"] = color.getHex();
  data["roughness"] = roughness;
  data["metalness"] = metalness;

  data["sheen"] = sheen;
  if (sheenColor != null && sheenColor is Color) {
    data["sheenColor"] = sheenColor!.getHex();
  }
  data["sheenRoughness"] = sheenRoughness;

  if (emissive != null) {
    data["emissive"] = emissive!.getHex();
  }
  if (emissiveIntensity != 1) {
    data["emissiveIntensity"] = emissiveIntensity;
  }

  if (specular != null) {
    data["specular"] = specular!.getHex();
  }
  if (specularIntensity != null) {
    data["specularIntensity"] = specularIntensity;
  }
  if (specularColor != null) {
    data["specularColor"] = specularColor!.getHex();
  }
  if (shininess != null) data["shininess"] = shininess;
  data["clearcoat"] = clearcoat;
  if (clearcoatRoughness != null) {
    data["clearcoatRoughness"] = clearcoatRoughness;
  }

  if (clearcoatMap != null && clearcoatMap is Texture) {
    data["clearcoatMap"] = clearcoatMap!.toJson(meta)['uuid'];
  }

  if (clearcoatRoughnessMap != null && clearcoatRoughnessMap is Texture) {
    data["clearcoatRoughnessMap"] =
        clearcoatRoughnessMap!.toJson(meta)['uuid'];
  }

  if (clearcoatNormalMap != null && clearcoatNormalMap is Texture) {
    data["clearcoatNormalMap"] = clearcoatNormalMap!.toJson(meta)['uuid'];
    data["clearcoatNormalScale"] = clearcoatNormalScale!.storage;
  }

  if (map != null && map is Texture) {
    data["map"] = map!.toJson(meta)["uuid"];
  }
  if (matcap != null && matcap is Texture) {
    data["matcap"] = matcap!.toJson(meta)["uuid"];
  }
  if (alphaMap != null && alphaMap is Texture) {
    data["alphaMap"] = alphaMap!.toJson(meta)["uuid"];
  }
  if (lightMap != null && lightMap is Texture) {
    data["lightMap"] = lightMap!.toJson(meta)["uuid"];
  }

  if (lightMap != null && lightMap is Texture) {
    data["lightMap"] = lightMap!.toJson(meta)['uuid'];
    data["lightMapIntensity"] = lightMapIntensity;
  }

  if (aoMap != null && aoMap is Texture) {
    data["aoMap"] = aoMap!.toJson(meta)['uuid'];
    data["aoMapIntensity"] = aoMapIntensity;
  }

  if (bumpMap != null && bumpMap is Texture) {
    data["bumpMap"] = bumpMap!.toJson(meta)['uuid'];
    data["bumpScale"] = bumpScale;
  }

  if (normalMap != null && normalMap is Texture) {
    data["normalMap"] = normalMap!.toJson(meta)['uuid'];
    data["normalMapType"] = normalMapType;
    data["normalScale"] = normalScale!.storage;
  }

  if (displacementMap != null && displacementMap is Texture) {
    data["displacementMap"] = displacementMap!.toJson(meta)['uuid'];
    data["displacementScale"] = displacementScale;
    data["displacementBias"] = displacementBias;
  }

  if (roughnessMap != null && roughnessMap is Texture) {
    data["roughnessMap"] = roughnessMap!.toJson(meta)['uuid'];
  }
  if (metalnessMap != null && metalnessMap is Texture) {
    data["metalnessMap"] = metalnessMap!.toJson(meta)['uuid'];
  }

  if (emissiveMap != null && emissiveMap is Texture) {
    data["emissiveMap"] = emissiveMap!.toJson(meta)['uuid'];
  }
  if (specularMap != null && specularMap is Texture) {
    data["specularMap"] = specularMap!.toJson(meta)['uuid'];
  }
  if (specularIntensityMap != null && specularIntensityMap is Texture) {
    data["specularIntensityMap"] = specularIntensityMap!.toJson(meta)['uuid'];
  }
  if (specularColorMap != null && specularColorMap is Texture) {
    data["specularColorMap"] = specularColorMap!.toJson(meta)['uuid'];
  }

  if (envMap != null && envMap is Texture) {
    data["envMap"] = envMap!.toJson(meta)['uuid'];

    data["refractionRatio"] = refractionRatio;

    if (combine != null) data["combine"] = combine;
    if (envMapIntensity != null) {
      data["envMapIntensity"] = envMapIntensity;
    }
  }

  if (gradientMap != null && gradientMap is Texture) {
    data["gradientMap"] = gradientMap!.toJson(meta)['uuid'];
  }

  data["transmission"] = transmission;
  if (transmissionMap != null && transmissionMap is Texture) {
    data["transmissionMap"] = transmissionMap!.toJson(meta)['uuid'];
  }
  if (thickness != null) data["thickness"] = thickness;
  if (thicknessMap != null && thicknessMap is Texture) {
    data["thicknessMap"] = thicknessMap!.toJson(meta)['uuid'];
  }
  if (attenuationColor != null) {
    data["attenuationColor"] = attenuationColor!.getHex();
  }
  if (attenuationDistance != null) {
    data["attenuationDistance"] = attenuationDistance;
  }

  if (size != null) data["size"] = size;
  if (shadowSide != null) data["shadowSide"] = shadowSide;
  data["sizeAttenuation"] = sizeAttenuation;

  if (blending != NormalBlending) data["blending"] = blending;
  if (side != FrontSide) data["side"] = side;
  if (vertexColors) data["vertexColors"] = true;

  if (opacity < 1) data["opacity"] = opacity;
  if (transparent == true) data["transparent"] = transparent;

  data["depthFunc"] = depthFunc;
  data["depthTest"] = depthTest;
  data["depthWrite"] = depthWrite;
  data["colorWrite"] = colorWrite;

  data["stencilWrite"] = stencilWrite;
  data["stencilWriteMask"] = stencilWriteMask;
  data["stencilFunc"] = stencilFunc;
  data["stencilRef"] = stencilRef;
  data["stencilFuncMask"] = stencilFuncMask;
  data["stencilFail"] = stencilFail;
  data["stencilZFail"] = stencilZFail;
  data["stencilZPass"] = stencilZPass;

  if (rotation != 0) {
    data["rotation"] = rotation;
  }

  if (polygonOffset == true) data["polygonOffset"] = true;
  if (polygonOffsetFactor != 0) {
    data["polygonOffsetFactor"] = polygonOffsetFactor;
  }
  if (polygonOffsetUnits != 0) {
    data["polygonOffsetUnits"] = polygonOffsetUnits;
  }

  if (linewidth != null && linewidth != 1) {
    data["linewidth"] = linewidth;
  }
  if (dashSize != null) data["dashSize"] = dashSize;
  if (gapSize != null) data["gapSize"] = gapSize;
  if (scale != null) data["scale"] = scale;

  if (dithering == true) data["dithering"] = true;

  if (alphaTest > 0) data["alphaTest"] = alphaTest;
  if (alphaToCoverage == true) {
    data["alphaToCoverage"] = alphaToCoverage;
  }
  if (premultipliedAlpha == true) {
    data["premultipliedAlpha"] = premultipliedAlpha;
  }

  if (wireframe == true) data["wireframe"] = wireframe;
  if (wireframeLinewidth != null && wireframeLinewidth! > 1) {
    data["wireframeLinewidth"] = wireframeLinewidth;
  }
  if (wireframeLinecap != 'round') {
    data["wireframeLinecap"] = wireframeLinecap;
  }
  if (wireframeLinejoin != 'round') {
    data["wireframeLinejoin"] = wireframeLinejoin;
  }

  if (visible == false) data["visible"] = false;

  if (toneMapped == false) data["toneMapped"] = false;

  if (fog == false) data["fog"] = false;

  if (jsonEncode(userData) != '{}') data["userData"] = userData;

  extractFromCache(cache) {
    final values = [];

    cache.keys.forEach((key) {
      final data = cache[key];
      data.remove("metadata");
      values.add(data);
    });

    return values;
  }

  if (isRoot) {
    final textures = extractFromCache(meta.textures);
    final images = extractFromCache(meta.images);

    if (textures.isNotEmpty) data["textures"] = textures;
    if (images.isNotEmpty) data["images"] = images;
  }

  return data;
}