All files / engine/Source/Scene preprocess3DTileContent.js

86.36% Statements 19/22
78.57% Branches 11/14
100% Functions 2/2
86.36% Lines 19/22

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103                                                      1686x 1686x       1686x 41x     1686x 1410x             276x 270x   47x           223x     149x           74x   57x           17x   17x                                       276x 276x   6x     270x        
import defined from "../Core/defined.js";
import getJsonFromTypedArray from "../Core/getJsonFromTypedArray.js";
import getMagic from "../Core/getMagic.js";
import RuntimeError from "../Core/RuntimeError.js";
import Cesium3DTileContentType from "./Cesium3DTileContentType.js";
 
/**
 * Results of the preprocess3DTileContent() function. This includes the
 * {@link Cesium3DTileContentType} and the payload. The payload is either
 * binary or JSON depending on the content type.
 *
 * @typedef {object} PreprocessedContent
 * @property {Cesium3DTileContentType} contentType The type of the content
 * @property {Uint8Array} [binaryPayload] For binary files, the payload is returned as a typed array with byteOffset of 0
 * @property {object} [jsonPayload] For JSON files, the results are returned as a JSON object.
 * @private
 */
 
/**
 * Preprocess a {@link Cesium3DTileContent}, to determine the type of content
 * and to parse JSON files into objects.
 *
 * @param {ArrayBuffer} arrayBuffer The raw binary payload
 * @return {PreprocessedContent}
 * @private
 */
function preprocess3DTileContent(arrayBuffer) {
  const uint8Array = new Uint8Array(arrayBuffer);
  let contentType = getMagic(uint8Array);
 
  // We use glTF for JSON glTF files. For binary glTF, we rename this
  // to glb to disambiguate
  if (contentType === "glTF") {
    contentType = "glb";
  }
 
  if (Cesium3DTileContentType.isBinaryFormat(contentType)) {
    return {
      // For binary files, the enum value is the magic number
      contentType: contentType,
      binaryPayload: uint8Array,
    };
  }
 
  const json = getJsonContent(uint8Array);
  if (defined(json.root)) {
    // Most likely a tileset JSON
    return {
      contentType: Cesium3DTileContentType.EXTERNAL_TILESET,
      jsonPayload: json,
    };
  }
 
  if (defined(json.asset)) {
    // Most likely a glTF. Tileset JSON also has an "asset" property
    // so this check needs to happen second
    return {
      contentType: Cesium3DTileContentType.GLTF,
      jsonPayload: json,
    };
  }
 
  if (defined(json.tileAvailability)) {
    // Most likely a subtree JSON.
    return {
      contentType: Cesium3DTileContentType.IMPLICIT_SUBTREE_JSON,
      jsonPayload: json,
    };
  }
 
  Eif (defined(json.type)) {
    // Most likely a GeoJSON
    return {
      contentType: Cesium3DTileContentType.GEOJSON,
      jsonPayload: json,
    };
  }
 
  if (defined(json.voxelTable)) {
    // Most likely a voxel JSON
    return {
      contentType: Cesium3DTileContentType.VOXEL_JSON,
      jsonPayload: json,
    };
  }
 
  throw new RuntimeError("Invalid tile content.");
}
 
function getJsonContent(uint8Array) {
  let json;
 
  try {
    json = getJsonFromTypedArray(uint8Array);
  } catch (error) {
    throw new RuntimeError("Invalid tile content.");
  }
 
  return json;
}
 
export default preprocess3DTileContent;