toJSON method

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

Implementation

Map<String, dynamic> toJSON({Object3dMeta? meta}) {
  var 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;

  if (color is Color) {
    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 && emissive!.isColor) {
    data["emissive"] = emissive!.getHex();
  }
  if (emissiveIntensity != 1) {
    data["emissiveIntensity"] = emissiveIntensity;
  }

  if (specular != null && specular!.isColor) {
    data["specular"] = specular!.getHex();
  }
  if (specularIntensity != null) {
    data["specularIntensity"] = specularIntensity;
  }
  if (specularColor != null && specularColor!.isColor) {
    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!.toArray();
  }

  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!.toArray();
  }

  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;

  // TODO: Copied from Object3D.toJSON

  extractFromCache(cache) {
    var values = [];

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

    return values;
  }

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

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

  return data;
}