UNPKG

@deck.gl/experimental-layers

Version:

Experimental layers for deck.gl

224 lines (199 loc) 9.05 kB
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 _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 _get(target, property, receiver) { if (typeof Reflect !== "undefined" && Reflect.get) { _get = Reflect.get; } else { _get = function _get(target, property, receiver) { var base = _superPropBase(target, property); if (!base) return; var desc = Object.getOwnPropertyDescriptor(base, property); if (desc.get) { return desc.get.call(receiver); } return desc.value; }; } return _get(target, property, receiver || target); } function _superPropBase(object, property) { while (!Object.prototype.hasOwnProperty.call(object, property)) { object = _getPrototypeOf(object); if (object === null) break; } return object; } 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); } // Copyright (c) 2015 - 2017 Uber Technologies, Inc. // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. import { Layer } from '@deck.gl/core'; import GL from 'luma.gl/constants'; import { Model, CubeGeometry, fp64 } from 'luma.gl'; var fp64LowPart = fp64.fp64LowPart; import vs from './gpu-grid-cell-layer-vertex.glsl'; import fs from './gpu-grid-cell-layer-fragment.glsl'; var DEFAULT_MINCOLOR = [0, 0, 0, 255]; var DEFAULT_MAXCOLOR = [0, 255, 0, 255]; var AGGREGATION_DATA_UBO_INDEX = 0; var defaultProps = { cellSize: { type: 'number', min: 0, max: 1000, value: 1000 }, coverage: { type: 'number', min: 0, max: 1, value: 1 }, elevationScale: { type: 'number', min: 0, value: 1 }, extruded: true, fp64: false, pickable: false, // TODO: add picking support (read from aggregated texture) minColor: { type: 'color', value: DEFAULT_MINCOLOR }, maxColor: { type: 'color', value: DEFAULT_MAXCOLOR }, lightSettings: {} }; var GPUGridCellLayer = /*#__PURE__*/ function (_Layer) { _inherits(GPUGridCellLayer, _Layer); function GPUGridCellLayer() { _classCallCheck(this, GPUGridCellLayer); return _possibleConstructorReturn(this, _getPrototypeOf(GPUGridCellLayer).apply(this, arguments)); } _createClass(GPUGridCellLayer, [{ key: "getShaders", value: function getShaders() { return { vs: vs, fs: fs, modules: ['project32', 'lighting', 'picking', 'fp64'] }; } }, { key: "initializeState", value: function initializeState() { var attributeManager = this.getAttributeManager(); attributeManager.addInstanced({ instanceCounts: { size: 4, update: this.calculateInstanceCounts, noAlloc: true } }); } }, { key: "updateState", value: function updateState(_ref) { var props = _ref.props, oldProps = _ref.oldProps, changeFlags = _ref.changeFlags; _get(_getPrototypeOf(GPUGridCellLayer.prototype), "updateState", this).call(this, { props: props, oldProps: oldProps, changeFlags: changeFlags }); // Re-generate model if geometry changed if (props.fp64 !== oldProps.fp64) { var gl = this.context.gl; if (this.state.model) { this.state.model.delete(); } var model = this._getModel(gl); this._setupUniformBuffer(model); this.setState({ model: model }); this.state.attributeManager.invalidate('instanceCounts'); } if (props.countsBuffer !== oldProps.countsBuffer) { this.state.attributeManager.invalidate('instanceCounts'); } } }, { key: "_getModel", value: function _getModel(gl) { return new Model(gl, Object.assign({}, this.getShaders(), { id: this.props.id, geometry: new CubeGeometry(), isInstanced: true, shaderCache: this.context.shaderCache })); } }, { key: "draw", value: function draw(_ref2) { var uniforms = _ref2.uniforms; var _this$props = this.props, cellSize = _this$props.cellSize, extruded = _this$props.extruded, elevationScale = _this$props.elevationScale, coverage = _this$props.coverage, gridSize = _this$props.gridSize, gridOrigin = _this$props.gridOrigin, gridOffset = _this$props.gridOffset, minColor = _this$props.minColor, maxColor = _this$props.maxColor, maxCountBuffer = _this$props.maxCountBuffer; var gridOriginLow = [fp64LowPart(gridOrigin[0]), fp64LowPart(gridOrigin[1])]; var gridOffsetLow = [fp64LowPart(gridOffset[0]), fp64LowPart(gridOffset[1])]; maxCountBuffer.bind({ target: GL.UNIFORM_BUFFER, index: AGGREGATION_DATA_UBO_INDEX }); this.state.model.render(Object.assign({}, uniforms, { cellSize: cellSize, extruded: extruded, elevationScale: elevationScale, coverage: coverage, gridSize: gridSize, gridOrigin: gridOrigin, gridOriginLow: gridOriginLow, gridOffset: gridOffset, gridOffsetLow: gridOffsetLow, minColor: minColor, maxColor: maxColor })); maxCountBuffer.unbind({ target: GL.UNIFORM_BUFFER, index: AGGREGATION_DATA_UBO_INDEX }); } }, { key: "calculateInstanceCounts", value: function calculateInstanceCounts(attribute) { var countsBuffer = this.props.countsBuffer; attribute.update({ buffer: countsBuffer }); } }, { key: "_setupUniformBuffer", value: function _setupUniformBuffer(model) { var gl = this.context.gl; var programHandle = model.program.handle; var uniformBlockIndex = gl.getUniformBlockIndex(programHandle, 'AggregationData'); gl.uniformBlockBinding(programHandle, uniformBlockIndex, AGGREGATION_DATA_UBO_INDEX); } }]); return GPUGridCellLayer; }(Layer); export { GPUGridCellLayer as default }; GPUGridCellLayer.layerName = 'GridCellLayer'; GPUGridCellLayer.defaultProps = defaultProps; //# sourceMappingURL=gpu-grid-cell-layer.js.map