UNPKG

@deck.gl/experimental-layers

Version:

Experimental layers for deck.gl

268 lines (229 loc) 9.8 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _core = require("@deck.gl/core"); var _constants = _interopRequireDefault(require("luma.gl/constants")); var _luma = require("luma.gl"); var _bitmapLayerVertex = _interopRequireDefault(require("./bitmap-layer-vertex")); var _bitmapLayerFragment = _interopRequireDefault(require("./bitmap-layer-fragment")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); } function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } function _iterableToArrayLimit(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); } function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); } function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } // Note: needs to match vertex shader var MAX_BITMAPS = 11; var defaultProps = { images: { type: 'array', value: [] }, desaturate: { type: 'number', min: 0, max: 1, value: 0 }, blendMode: null, // More context: because of the blending mode we're using for ground imagery, // alpha is not effective when blending the bitmap layers with the base map. // Instead we need to manually dim/blend rgb values with a background color. transparentColor: { type: 'color', value: [0, 0, 0, 0] }, tintColor: { type: 'color', value: [255, 255, 255] }, // accessors getCenter: { type: 'accessor', value: function value(x) { return x.center; } }, getRotation: { type: 'accessor', value: function value(x) { return x.rotation; } } }; /* * @class * @param {object} props * @param {number} props.transparentColor - color to interpret transparency to * @param {number} props.tintColor - color bias */ var BitmapLayer = /*#__PURE__*/ function (_Layer) { _inherits(BitmapLayer, _Layer); function BitmapLayer() { _classCallCheck(this, BitmapLayer); return _possibleConstructorReturn(this, _getPrototypeOf(BitmapLayer).apply(this, arguments)); } _createClass(BitmapLayer, [{ key: "initializeState", value: function initializeState() { var gl = this.context.gl; this.setState({ model: this.getModel(gl) }); var attributeManager = this.state.attributeManager; attributeManager.addInstanced({ instanceCenter: { size: 3, accessor: 'getCenter' }, instanceRotation: { size: 3, accessor: 'getRotation' }, instanceBitmapIndex: { size: 1, update: this.calculateInstanceBitmapIndex } }); } }, { key: "updateState", value: function updateState(_ref) { var props = _ref.props, oldProps = _ref.oldProps; if (props.images !== oldProps.images) { var changed = !oldProps.images || props.images.length !== oldProps.images.length; if (!changed) { for (var i = 0; i < props.images.length; ++i) { changed = changed || props.images[i] !== oldProps.images[i]; } } if (changed) { this.loadMapImagesToTextures(); } } var desaturate = props.desaturate; this.state.model.setUniforms({ desaturate: desaturate }); } }, { key: "getModel", value: function getModel(gl) { // Two triangles making up a square to render the bitmap texture on var verts = [[1, 1, 0], [-1, 1, 0], [1, -1, 0], [-1, 1, 0], [1, -1, 0], [-1, -1, 0]]; var positions = []; var texCoords = []; verts.forEach(function (vertex) { // geometry: unit square centered on origin positions.push(vertex[0] / 2, vertex[1] / 2, vertex[2] / 2); // texture: unit square with bottom left in origin texCoords.push(vertex[0] / 2 + 0.5, -vertex[1] / 2 + 0.5); }); var model = new _luma.Model(gl, { id: this.props.id, vs: _bitmapLayerVertex.default, fs: _bitmapLayerFragment.default, shaderCache: this.context.shaderCache, geometry: new _luma.Geometry({ drawMode: _constants.default.TRIANGLES, vertexCount: 6, attributes: { positions: new Float32Array(positions), texCoords: new Float32Array(texCoords) } }), isInstanced: true }); return model; } }, { key: "draw", value: function draw(_ref2) { var uniforms = _ref2.uniforms; var _this$props = this.props, transparentColor = _this$props.transparentColor, tintColor = _this$props.tintColor; // TODO fix zFighting // Render the image this.state.model.render(Object.assign({}, uniforms, { transparentColor: transparentColor, tintColor: tintColor })); } }, { key: "loadMapImagesToTextures", value: function loadMapImagesToTextures() { var _this = this; var model = this.state.model; var images = this.props.images; var _loop = function _loop(i) { (0, _luma.loadTextures)(_this.context.gl, { urls: [images[i]] }).then(function (_ref3) { var _ref4 = _slicedToArray(_ref3, 1), texture = _ref4[0]; return model.setUniforms(_defineProperty({}, "uBitmap".concat(i), texture)); }); }; for (var i = 0; i < Math.min(images.length, MAX_BITMAPS); i++) { _loop(i); } } }, { key: "getBitmapIndex", value: function getBitmapIndex(point) { var url = point.imageUrl; var idx = Math.max(this.props.images.indexOf(url), 0); return idx >= MAX_BITMAPS ? 0 : idx; } }, { key: "calculateInstanceBitmapIndex", value: function calculateInstanceBitmapIndex(attribute) { var data = this.props.data; var value = attribute.value, size = attribute.size; var i = 0; var _iteratorNormalCompletion = true; var _didIteratorError = false; var _iteratorError = undefined; try { for (var _iterator = data[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { var point = _step.value; var bitmapIndex = Number.isFinite(point.bitmapIndex) ? point.bitmapIndex : this.getBitmapIndex(point); value[i] = bitmapIndex; i += size; } } catch (err) { _didIteratorError = true; _iteratorError = err; } finally { try { if (!_iteratorNormalCompletion && _iterator.return != null) { _iterator.return(); } } finally { if (_didIteratorError) { throw _iteratorError; } } } } }]); return BitmapLayer; }(_core.Layer); exports.default = BitmapLayer; BitmapLayer.layerName = 'BitmapLayer'; BitmapLayer.defaultProps = defaultProps; //# sourceMappingURL=bitmap-layer.js.map