parseObject method

Object3D parseObject(
  1. Map<String, dynamic> data,
  2. dynamic geometries,
  3. dynamic materials,
  4. dynamic textures,
  5. dynamic animations,
)

Implementation

Object3D parseObject(
    Map<String, dynamic> data, geometries, materials, textures, animations) {
  dynamic object;

  getGeometry(name) {
    if (geometries[name] == null) {
      print('THREE.ObjectLoader: Undefined geometry $name');
    }

    return geometries[name];
  }

  getMaterial(name) {
    if (name == null) return null;

    if (name is List) {
      var array = [];

      for (var i = 0, l = name.length; i < l; i++) {
        var uuid = name[i];

        if (materials[uuid] == null) {
          print('THREE.ObjectLoader: Undefined material $uuid');
        }

        array.add(materials[uuid]);
      }

      return array;
    }

    if (materials[name] == null) {
      print('THREE.ObjectLoader: Undefined material $name');
    }

    return materials[name];
  }

  getTexture(uuid) {
    if (textures[uuid] == null) {
      print('THREE.ObjectLoader: Undefined texture $uuid');
    }

    return textures[uuid];
  }

  var geometry, material;

  switch (data["type"]) {
    case 'Scene':
      object = Scene();

      if (data["background"] != null) {
        if (data["background"] is int) {
          object.background = Color.fromHex(data["background"]);
        } else {
          object.background = getTexture(data["background"]);
        }
      }

      if (data["environment"] != null) {
        object.environment = getTexture(data["environment"]);
      }

      if (data["fog"] != null) {
        if (data["fog"]["type"] == 'Fog') {
          object.fog = Fog(
              data["fog"]["color"], data["fog"]["near"], data["fog"]["far"]);
        } else if (data["fog"]["type"] == 'FogExp2') {
          object.fog = FogExp2(data["fog"]["color"], data["fog"]["density"]);
        }
      }

      break;

    case 'PerspectiveCamera':
      object = PerspectiveCamera(
          data["fov"], data["aspect"], data["near"], data["far"]);

      if (data["focus"] != null) object.focus = data["focus"];
      if (data["zoom"] != null) object.zoom = data["zoom"];
      if (data["filmGauge"] != null) object.filmGauge = data["filmGauge"];
      if (data["filmOffset"] != null) object.filmOffset = data["filmOffset"];
      if (data["view"] != null) {
        object.view = convert.jsonDecode(convert.jsonEncode(data["view"]));
      }

      break;

    case 'OrthographicCamera':
      object = OrthographicCamera(data["left"], data["right"], data["top"],
          data["bottom"], data["near"], data["far"]);

      if (data["zoom"] != null) object.zoom = data["zoom"];
      if (data["view"] != null) {
        object.view = convert.jsonDecode(convert.jsonEncode(data["view"]));
      }

      break;

    case 'AmbientLight':
      object = AmbientLight(data["color"], data["intensity"]);

      break;

    case 'DirectionalLight':
      object = DirectionalLight(data["color"], data["intensity"]);

      break;

    case 'PointLight':
      object = PointLight(
          data["color"], data["intensity"], data["distance"], data["decay"]);

      break;

    case 'RectAreaLight':
      object = RectAreaLight(
          data["color"], data["intensity"], data["width"], data["height"]);

      break;

    case 'SpotLight':
      object = SpotLight(data["color"], data["intensity"], data["distance"],
          data["angle"], data["penumbra"], data["decay"]);

      break;

    case 'HemisphereLight':
      object = HemisphereLight(
          data["color"], data["groundColor"], data["intensity"]);

      break;

    case 'LightProbe':
      object = LightProbe(null, null).fromJSON(data);

      break;

    case 'SkinnedMesh':
      geometry = getGeometry(data["geometry"]);
      material = getMaterial(data["material"]);

      object = SkinnedMesh(geometry, material);

      if (data["bindMode"] != null) object.bindMode = data["bindMode"];
      if (data["bindMatrix"] != null) {
        object.bindMatrix.fromArray(data["bindMatrix"]);
      }
      if (data["skeleton"] != null) object.skeleton = data["skeleton"];

      break;

    case 'Mesh':
      geometry = getGeometry(data["geometry"]);
      material = getMaterial(data["material"]);

      object = Mesh(geometry, material);

      break;

    case 'InstancedMesh':
      geometry = getGeometry(data["geometry"]);
      material = getMaterial(data["material"]);
      var count = data["count"];
      var instanceMatrix = data["instanceMatrix"];
      var instanceColor = data["instanceColor"];

      object = InstancedMesh(geometry, material, count);
      object.instanceMatrix = InstancedBufferAttribute(
          Float32Array(instanceMatrix.array), 16, false);
      if (instanceColor != null) {
        object.instanceColor = InstancedBufferAttribute(
            Float32Array(instanceColor.array), instanceColor.itemSize, false);
      }

      break;

    // case 'LOD':

    // 	object = new LOD();

    // 	break;

    case 'Line':
      object =
          Line(getGeometry(data["geometry"]), getMaterial(data["material"]));

      break;

    case 'LineLoop':
      object = LineLoop(
          getGeometry(data["geometry"]), getMaterial(data["material"]));

      break;

    case 'LineSegments':
      object = LineSegments(
          getGeometry(data["geometry"]), getMaterial(data["material"]));

      break;

    case 'PointCloud':
    case 'Points':
      object = Points(
          getGeometry(data["geometry"]), getMaterial(data["material"]));

      break;

    case 'Sprite':
      object = Sprite(getMaterial(data["material"]));

      break;

    case 'Group':
      object = Group();

      break;

    case 'Bone':
      object = Bone();

      break;

    default:
      object = Object3D();
  }

  object.uuid = data["uuid"];

  if (data["name"] != null) object.name = data["name"];

  if (data["matrix"] != null) {
    object.matrix.fromArray(data["matrix"]);

    if (data["matrixAutoUpdate"] != null) {
      object.matrixAutoUpdate = data["matrixAutoUpdate"];
    }
    if (object.matrixAutoUpdate) {
      object.matrix
          .decompose(object.position, object.quaternion, object.scale);
    }
  } else {
    if (data["position"] != null) object.position.fromArray(data["position"]);
    if (data["rotation"] != null) object.rotation.fromArray(data["rotation"]);
    if (data["quaternion"] != null) {
      object.quaternion.fromArray(data["quaternion"]);
    }
    if (data["scale"] != null) object.scale.fromArray(data["scale"]);
  }

  if (data["castShadow"] != null) object.castShadow = data["castShadow"];
  if (data["receiveShadow"] != null) {
    object.receiveShadow = data["receiveShadow"];
  }

  if (data["shadow"] != null) {
    if (data["shadow"]["bias"] != null) {
      object.shadow.bias = data["shadow"]["bias"];
    }
    if (data["shadow"]["normalBias"] != null) {
      object.shadow.normalBias = data["shadow"]["normalBias"];
    }
    if (data["shadow"]["radius"] != null) {
      object.shadow.radius = data["shadow"]["radius"];
    }
    if (data["shadow"]["mapSize"] != null) {
      object.shadow.mapSize.fromArray(data["shadow"]["mapSize"]);
    }
    if (data["shadow"]["camera"] != null) {
      object.shadow.camera =
          parseObject(data["shadow"]["camera"], null, null, null, null);
    }
  }

  if (data["visible"] != null) object.visible = data["visible"];
  if (data["frustumCulled"] != null) {
    object.frustumCulled = data["frustumCulled"];
  }
  if (data["renderOrder"] != null) object.renderOrder = data["renderOrder"];
  if (data["userData"] != null) object.userData = data["userData"];
  if (data["layers"] != null) object.layers.mask = data["layers"];

  if (data["children"] != null) {
    var children = data["children"];

    for (var i = 0; i < children.length; i++) {
      object.add(parseObject(
          children[i], geometries, materials, textures, animations));
    }
  }

  if (data["animations"] != null) {
    var objectAnimations = data["animations"];

    for (var i = 0; i < objectAnimations.length; i++) {
      var uuid = objectAnimations[i];

      object.animations.push(animations[uuid]);
    }
  }

  if (data["type"] == 'LOD') {
    if (data["autoUpdate"] != null) object.autoUpdate = data["autoUpdate"];

    var levels = data["levels"];

    for (var l = 0; l < levels.length; l++) {
      var level = levels[l];
      var child = object.getObjectByProperty('uuid', level.object);

      if (child != null) {
        object.addLevel(child, level.distance);
      }
    }
  }

  return object;
}