parseJson method

BufferGeometry parseJson(
  1. Map<String, dynamic> json
)

Implementation

BufferGeometry parseJson(Map<String,dynamic> json) {
  final interleavedBufferMap = {};
  final arrayBufferMap = {};

  getArrayBuffer(json, uuid) {
    if (arrayBufferMap[uuid] != null) return arrayBufferMap[uuid];

    final arrayBuffers = json.arrayBuffers;
    final arrayBuffer = arrayBuffers[uuid];

    final ab = Uint32Array(arrayBuffer).buffer;

    arrayBufferMap[uuid] = ab;

    return ab;
  }

  getInterleavedBuffer(json, uuid) {
    if (interleavedBufferMap[uuid] != null) return interleavedBufferMap[uuid];

    final interleavedBuffers = json.interleavedBuffers;
    final interleavedBuffer = interleavedBuffers[uuid];

    final buffer = getArrayBuffer(json, interleavedBuffer.buffer);

    final array = getTypedArray(interleavedBuffer.type, buffer);
    final ib = InterleavedBuffer(array, interleavedBuffer.stride);
    ib.uuid = interleavedBuffer.uuid;

    interleavedBufferMap[uuid] = ib;

    return ib;
  }

  final geometry = json["isInstancedBufferGeometry"] == true
      ? InstancedBufferGeometry()
      : BufferGeometry();

  final index = json["data"]["index"];

  if (index != null) {
    final typedArray = getTypedArray(index["type"], index["array"]);
    geometry.setIndex(getTypedAttribute(typedArray, 1, false));
  }

  final attributes = json["data"]["attributes"];

  for (final key in attributes.keys) {
    final attribute = attributes[key];
    BaseBufferAttribute bufferAttribute;

    if (attribute["isInterleavedBufferAttribute"] == true) {
      final interleavedBuffer =
          getInterleavedBuffer(json["data"], attribute["data"]);
      bufferAttribute = InterleavedBufferAttribute(
          interleavedBuffer,
          attribute["itemSize"],
          attribute["offset"],
          attribute["normalized"]);
    } else {
      final typedArray = getTypedArray(attribute["type"], attribute["array"]);
      // final bufferAttributeConstr = attribute.isInstancedBufferAttribute ? InstancedBufferAttribute : BufferAttribute;
      if (attribute["isInstancedBufferAttribute"] == true) {
        bufferAttribute = InstancedBufferAttribute(
            typedArray, attribute["itemSize"], attribute["normalized"]);
      } else {
        bufferAttribute = getTypedAttribute(typedArray, attribute["itemSize"],attribute["normalized"] == true);
      }
    }

    if (attribute["name"] != null) bufferAttribute.name = attribute["name"];
    if (attribute["usage"] != null) {
      if (bufferAttribute is InstancedBufferAttribute) {
        bufferAttribute.setUsage(attribute["usage"]);
      }
    }

    if (attribute["updateRange"] != null) {
      if (bufferAttribute is InterleavedBufferAttribute) {
        bufferAttribute.updateRange?['offset'] =
            attribute["updateRange"]["offset"];
        bufferAttribute.updateRange?['count'] =
            attribute["updateRange"]["count"];
      }
    }

    geometry.setAttributeFromString(key, bufferAttribute);
  }

  final morphAttributes = json["data"]["morphAttributes"];

  if (morphAttributes != null) {
    for (final key in morphAttributes.keys) {
      final attributeArray = morphAttributes[key];

      final array = <BufferAttribute>[];

      for (int i = 0, il = attributeArray.length; i < il; i++) {
        final attribute = attributeArray[i];
        BufferAttribute bufferAttribute;

        if (attribute is InterleavedBufferAttribute) {
          final interleavedBuffer = getInterleavedBuffer(json["data"], attribute.data);
          bufferAttribute = InterleavedBufferAttribute(interleavedBuffer, attribute.itemSize, attribute.offset, attribute.normalized);
        } else {
          final typedArray = getTypedArray(attribute.type, attribute.array);
          bufferAttribute = getTypedAttribute(typedArray, attribute.itemSize, attribute.normalized);
        }

        if (attribute.name != null) bufferAttribute.name = attribute.name;
        array.add(bufferAttribute);
      }

      geometry.morphAttributes[key] = array;
    }
  }

  final morphTargetsRelative = json["data"]["morphTargetsRelative"];

  if (morphTargetsRelative == true) {
    geometry.morphTargetsRelative = true;
  }

  final groups = json["data"]["groups"] ??
      json["data"]["drawcalls"] ??
      json["data"]["offsets"];

  if (groups != null) {
    for (int i = 0, n = groups.length; i != n; ++i) {
      final group = groups[i];

      geometry.addGroup(
          group["start"], group["count"], group["materialIndex"]);
    }
  }

  final boundingSphere = json["data"]["boundingSphere"];

  if (boundingSphere != null) {
    final center = Vector3(0, 0, 0);

    if (boundingSphere["center"] != null) {
      center.copyFromArray(boundingSphere["center"]);
    }

    geometry.boundingSphere = BoundingSphere(center, boundingSphere["radius"]);
  }

  if (json["name"] != null) geometry.name = json["name"];
  if (json["userData"] != null) geometry.userData = json["userData"];

  return geometry;
}