All files / engine/Source/Scene I3SSublayer.js

100% Statements 50/50
91.66% Branches 11/12
100% Functions 10/10
100% Lines 47/47

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 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176                              42x 42x 42x 42x 42x 42x 42x 42x 42x     1x                 18x                       4x                       20x                       6x                       55x                     10x       2x     2x 1x 1x 1x                           24x               1x           42x 42x 18x 18x 12x 12x 30x           30x   12x 12x 30x 30x 30x     24x 18x     18x 18x 18x 18x   18x 18x 18x     6x       42x        
import Check from "../Core/Check.js";
import defined from "../Core/defined.js";
import I3SDataProvider from "./I3SDataProvider.js";
import I3SLayer from "./I3SLayer.js";
import Resource from "../Core/Resource.js";
 
/**
 * This class implements an I3S sublayer for Building Scene Layer.
 * <p>
 * This object is normally not instantiated directly, use {@link I3SSublayer.fromData}.
 * </p>
 * @alias I3SSublayer
 * @internalConstructor
 */
function I3SSublayer(dataProvider, parent, sublayerData) {
  this._dataProvider = dataProvider;
  this._parent = parent;
  this._data = sublayerData;
  this._name = sublayerData.name;
  this._modelName = sublayerData.modelName;
  this._visibility = sublayerData.visibility ?? true;
  this._resource = undefined;
  this._sublayers = [];
  this._i3sLayers = [];
}
 
Object.defineProperties(I3SSublayer.prototype, {
  /**
   * Gets the resource for the sublayer
   * @memberof I3SSublayer.prototype
   * @type {Resource}
   * @readonly
   */
  resource: {
    get: function () {
      return this._resource;
    },
  },
 
  /**
   * Gets the I3S data for this object.
   * @memberof I3SSublayer.prototype
   * @type {object}
   * @readonly
   */
  data: {
    get: function () {
      return this._data;
    },
  },
 
  /**
   * Gets the name for the sublayer.
   * @memberof I3SSublayer.prototype
   * @type {string}
   * @readonly
   */
  name: {
    get: function () {
      return this._name;
    },
  },
 
  /**
   * Gets the model name for the sublayer.
   * @memberof I3SSublayer.prototype
   * @type {string}
   * @readonly
   */
  modelName: {
    get: function () {
      return this._modelName;
    },
  },
 
  /**
   * Gets the collection of child sublayers.
   * @memberof I3SSublayer.prototype
   * @type {I3SSublayer[]}
   * @readonly
   */
  sublayers: {
    get: function () {
      return this._sublayers;
    },
  },
 
  /**
   * Gets or sets the sublayer visibility.
   * @memberof I3SSublayer.prototype
   * @type {boolean}
   */
  visibility: {
    get: function () {
      return this._visibility;
    },
    set: function (value) {
      //>>includeStart('debug', pragmas.debug);
      Check.defined("value", value);
      //>>includeEnd('debug');
 
      if (this._visibility !== value) {
        this._visibility = value;
        for (let i = 0; i < this._i3sLayers.length; i++) {
          this._i3sLayers[i]._updateVisibility();
        }
      }
    },
  },
 
  /**
   * Determines if the sublayer will be shown.
   * @memberof I3SSublayer.prototype
   * @type {boolean}
   * @readonly
   */
  show: {
    get: function () {
      return this._visibility && this._parent.show;
    },
  },
});
 
/**
 * @private
 */
I3SSublayer._fromData = async function (
  dataProvider,
  buildingLayerUrl,
  sublayerData,
  parent,
) {
  const sublayer = new I3SSublayer(dataProvider, parent, sublayerData);
  if (sublayer._data.layerType === "group") {
    const sublayers = sublayer._data.sublayers;
    if (defined(sublayers)) {
      const promises = [];
      for (let i = 0; i < sublayers.length; i++) {
        const promise = I3SSublayer._fromData(
          dataProvider,
          buildingLayerUrl,
          sublayers[i],
          sublayer,
        );
        promises.push(promise);
      }
      const childSublayers = await Promise.all(promises);
      for (let i = 0; i < childSublayers.length; i++) {
        const childSublayer = childSublayers[i];
        sublayer._sublayers.push(childSublayer);
        sublayer._i3sLayers.push(...childSublayer._i3sLayers);
      }
    }
  } else if (sublayer._data.layerType === "3DObject") {
    const sublayerUrl = buildingLayerUrl.concat(
      `/sublayers/${sublayer._data.id}`,
    );
    const resource = new Resource({ url: sublayerUrl });
    resource.setQueryParameters(dataProvider.resource.queryParameters);
    resource.appendForwardSlash();
    sublayer._resource = resource;
 
    const layerData = await I3SDataProvider.loadJson(sublayer._resource);
    const layer = new I3SLayer(dataProvider, layerData, sublayer);
    sublayer._i3sLayers.push(layer);
  } else {
    // Filter other scene layer types out
    console.log(
      `${sublayer._data.layerType} layer ${sublayer._data.name} is skipped as not supported.`,
    );
  }
  return sublayer;
};
 
export default I3SSublayer;