UNPKG

itowns

Version:

A JS/WebGL framework for 3D geospatial data visualization

337 lines (294 loc) 11.1 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck")); var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass")); var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn")); var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf")); var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized")); var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits")); var _Layer2 = _interopRequireDefault(require("./Layer")); var _Picking = _interopRequireDefault(require("../Core/Picking")); function disposeMesh(obj) { if (obj.dispose) { obj.dispose(); } else { if (obj.geometry) { obj.geometry.dispose(); } if (obj.material) { if (Array.isArray(obj.material)) { var _iteratorNormalCompletion = true; var _didIteratorError = false; var _iteratorError = undefined; try { for (var _iterator = obj.material[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { var material = _step.value; material.dispose(); } } catch (err) { _didIteratorError = true; _iteratorError = err; } finally { try { if (!_iteratorNormalCompletion && _iterator["return"] != null) { _iterator["return"](); } } finally { if (_didIteratorError) { throw _iteratorError; } } } } else { obj.material.dispose(); } } } } function traverse(obj, callback) { var _iteratorNormalCompletion2 = true; var _didIteratorError2 = false; var _iteratorError2 = undefined; try { for (var _iterator2 = obj.children[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) { var child = _step2.value; traverse(child, callback); } } catch (err) { _didIteratorError2 = true; _iteratorError2 = err; } finally { try { if (!_iteratorNormalCompletion2 && _iterator2["return"] != null) { _iterator2["return"](); } } finally { if (_didIteratorError2) { throw _iteratorError2; } } } callback(obj); } /** * Fires when the opacity of the layer has changed. * @event GeometryLayer#opacity-property-changed */ /** * @property {boolean} isGeometryLayer - Used to checkout whether this layer is * a GeometryLayer. Default is true. You should not change this, as it is used * internally for optimisation. */ var GeometryLayer = /*#__PURE__*/ function (_Layer) { (0, _inherits2["default"])(GeometryLayer, _Layer); /** * A layer usually managing a geometry to display on a view. For example, it * can be a layer of buildings extruded from a a WFS stream. * * @constructor * @extends Layer * * @param {string} id - The id of the layer, that should be unique. It is * not mandatory, but an error will be emitted if this layer is added a * {@link View} that already has a layer going by that id. * @param {THREE.Object3d} object3d - The object3d used to contain the * geometry of the GeometryLayer. It is usually a `THREE.Group`, but it can * be anything inheriting from a `THREE.Object3d`. * @param {Object} [config] - Optional configuration, all elements in it * will be merged as is in the layer. For example, if the configuration * contains three elements `name, protocol, extent`, these elements will be * available using `layer.name` or something else depending on the property * name. * @param {Source} [config.source] - Description and options of the source. * * @throws {Error} `object3d` must be a valid `THREE.Object3d`. * * @example * // Create a GeometryLayer * const geometry = new GeometryLayer('buildings', { * source: { * url: 'http://server.geo/wfs?', * protocol: 'wfs', * format: 'application/json' * }, * }); * * // Add the layer * view.addLayer(geometry); */ function GeometryLayer(id, object3d) { var _this; var config = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; (0, _classCallCheck2["default"])(this, GeometryLayer); _this = (0, _possibleConstructorReturn2["default"])(this, (0, _getPrototypeOf2["default"])(GeometryLayer).call(this, id, config)); _this.isGeometryLayer = true; if (!object3d || !object3d.isObject3D) { throw new Error("Missing/Invalid object3d parameter (must be a\n three.js Object3D instance)"); } if (object3d.type === 'Group' && object3d.name === '') { object3d.name = id; } Object.defineProperty((0, _assertThisInitialized2["default"])(_this), 'object3d', { value: object3d, writable: false, configurable: true }); _this.defineLayerProperty('opacity', 1.0, function () { var root = _this.parent ? _this.parent.object3d : _this.object3d; root.traverse(function (object) { if (object.layer == (0, _assertThisInitialized2["default"])(_this)) { _this.changeOpacity(object); } else if (object.content && object.content.layer == (0, _assertThisInitialized2["default"])(_this)) { object.content.traverse(_this.changeOpacity); } }); }); _this.defineLayerProperty('wireframe', false, function () { var root = _this.parent ? _this.parent.object3d : _this.object3d; root.traverse(function (object) { if (object.layer == (0, _assertThisInitialized2["default"])(_this) && object.material) { object.material.wireframe = _this.wireframe; } else if (object.content && object.content.layer == (0, _assertThisInitialized2["default"])(_this)) { object.content.traverse(function (o) { if (o.material && o.layer == (0, _assertThisInitialized2["default"])(_this)) { o.material.wireframe = _this.wireframe; } }); } }); }); _this.attachedLayers = []; _this.visible = config.visible == undefined ? true : config.visible; // Attached layers expect to receive the visual representation of a // layer (= THREE object with a material). So if a layer's update // function don't process this kind of object, the layer must provide a // getObjectToUpdateForAttachedLayers function that returns the correct // object to update for attached layer. // See 3dtilesProvider or PointCloudProvider for examples. // eslint-disable-next-line arrow-body-style _this.getObjectToUpdateForAttachedLayers = function (obj) { if (obj.parent && obj.material) { return { element: obj, parent: obj.parent }; } }; return _this; } // Placeholder // eslint-disable-next-line (0, _createClass2["default"])(GeometryLayer, [{ key: "postUpdate", value: function postUpdate() {} // Placeholder // eslint-disable-next-line }, { key: "culling", value: function culling() { return true; } /** * Attach another layer to this one. Layers attached to a GeometryLayer will * be available in `geometryLayer.attachedLayers`. * * @param {Layer} layer - The layer to attach, that must have an `update` * method. */ }, { key: "attach", value: function attach(layer) { if (!layer.update) { throw new Error("Missing 'update' function -> can't attach layer\n ".concat(layer.id)); } this.attachedLayers.push(layer); // To traverse GeometryLayer object3d attached layer.parent = this; } /** * Detach a layer attached to this one. See {@link attach} to learn how to * attach a layer. * * @param {Layer} layer - The layer to detach. * * @return {boolean} Confirmation of the detachment of the layer. */ }, { key: "detach", value: function detach(layer) { var count = this.attachedLayers.length; this.attachedLayers = this.attachedLayers.filter(function (attached) { return attached.id != layer.id; }); layer.parent = undefined; return this.attachedLayers.length < count; } /** * All layer's meshs are removed from scene and disposed from video device. */ }, { key: "delete", value: function _delete() { var _this2 = this; // if Layer is attached if (this.parent) { traverse(this.parent.object3d, function (obj) { if (obj.layer && obj.layer.id == _this2.id) { obj.parent.remove(obj); disposeMesh(obj); } }); } else { if (this.object3d.parent) { this.object3d.parent.remove(this.object3d); } this.object3d.traverse(disposeMesh); } } /** * Picking method for this layer. It uses the {@link Picking#pickObjectsAt} * method. * * @param {View} view - The view instance. * @param {Object} coordinates - The coordinates to pick in the view. It * should have at least `x` and `y` properties. * @param {number} radius - Radius of the picking circle. * * @return {Array} An array containing all targets picked under the * specified coordinates. */ }, { key: "pickObjectsAt", value: function pickObjectsAt(view, coordinates) { var radius = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : this.options.defaultPickingRadius; return _Picking["default"].pickObjectsAt(view, coordinates, radius, this.object3d); } /** * Change the opacity of an object, according to the value of the `opacity` * property of this layer. * * @param {Object} object - The object to change the opacity from. It is * usually a `THREE.Object3d` or an implementation of it. */ }, { key: "changeOpacity", value: function changeOpacity(object) { if (object.material) { // != undefined: we want the test to pass if opacity is 0 if (object.material.opacity != undefined) { object.material.transparent = this.opacity < 1.0; object.material.opacity = this.opacity; } if (object.material.uniforms && object.material.uniforms.opacity != undefined) { object.material.transparent = this.opacity < 1.0; object.material.uniforms.opacity.value = this.opacity; } } } }]); return GeometryLayer; }(_Layer2["default"]); var _default = GeometryLayer; exports["default"] = _default;