@deck.gl/layers
Version:
deck.gl core layers
334 lines (298 loc) • 11.8 kB
JavaScript
"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 _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized"));
var _get2 = _interopRequireDefault(require("@babel/runtime/helpers/get"));
var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _core = require("@deck.gl/core");
var _core2 = require("@luma.gl/core");
var _pathTesselator = _interopRequireDefault(require("./path-tesselator"));
var _pathLayerVertex = _interopRequireDefault(require("./path-layer-vertex.glsl"));
var _pathLayerFragment = _interopRequireDefault(require("./path-layer-fragment.glsl"));
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { (0, _defineProperty2.default)(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = (0, _getPrototypeOf2.default)(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = (0, _getPrototypeOf2.default)(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return (0, _possibleConstructorReturn2.default)(this, result); }; }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
var DEFAULT_COLOR = [0, 0, 0, 255];
var defaultProps = {
widthUnits: 'meters',
widthScale: {
type: 'number',
min: 0,
value: 1
},
widthMinPixels: {
type: 'number',
min: 0,
value: 0
},
widthMaxPixels: {
type: 'number',
min: 0,
value: Number.MAX_SAFE_INTEGER
},
jointRounded: false,
capRounded: false,
miterLimit: {
type: 'number',
min: 0,
value: 4
},
billboard: false,
_pathType: null,
getPath: {
type: 'accessor',
value: function value(object) {
return object.path;
}
},
getColor: {
type: 'accessor',
value: DEFAULT_COLOR
},
getWidth: {
type: 'accessor',
value: 1
},
rounded: {
deprecatedFor: ['jointRounded', 'capRounded']
}
};
var ATTRIBUTE_TRANSITION = {
enter: function enter(value, chunk) {
return chunk.length ? chunk.subarray(chunk.length - value.length) : value;
}
};
var PathLayer = function (_Layer) {
(0, _inherits2.default)(PathLayer, _Layer);
var _super = _createSuper(PathLayer);
function PathLayer() {
var _this;
(0, _classCallCheck2.default)(this, PathLayer);
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
_this = _super.call.apply(_super, [this].concat(args));
(0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "state", void 0);
return _this;
}
(0, _createClass2.default)(PathLayer, [{
key: "getShaders",
value: function getShaders() {
return (0, _get2.default)((0, _getPrototypeOf2.default)(PathLayer.prototype), "getShaders", this).call(this, {
vs: _pathLayerVertex.default,
fs: _pathLayerFragment.default,
modules: [_core.project32, _core.picking]
});
}
}, {
key: "wrapLongitude",
get: function get() {
return false;
}
}, {
key: "initializeState",
value: function initializeState() {
var _this2 = this;
var noAlloc = true;
var attributeManager = this.getAttributeManager();
attributeManager.addInstanced({
positions: {
size: 3,
vertexOffset: 1,
type: 5130,
fp64: this.use64bitPositions(),
transition: ATTRIBUTE_TRANSITION,
accessor: 'getPath',
update: this.calculatePositions,
noAlloc: noAlloc,
shaderAttributes: {
instanceLeftPositions: {
vertexOffset: 0
},
instanceStartPositions: {
vertexOffset: 1
},
instanceEndPositions: {
vertexOffset: 2
},
instanceRightPositions: {
vertexOffset: 3
}
}
},
instanceTypes: {
size: 1,
type: 5121,
update: this.calculateSegmentTypes,
noAlloc: noAlloc
},
instanceStrokeWidths: {
size: 1,
accessor: 'getWidth',
transition: ATTRIBUTE_TRANSITION,
defaultValue: 1
},
instanceColors: {
size: this.props.colorFormat.length,
type: 5121,
normalized: true,
accessor: 'getColor',
transition: ATTRIBUTE_TRANSITION,
defaultValue: DEFAULT_COLOR
},
instancePickingColors: {
size: 3,
type: 5121,
accessor: function accessor(object, _ref) {
var index = _ref.index,
value = _ref.target;
return _this2.encodePickingColor(object && object.__source ? object.__source.index : index, value);
}
}
});
this.setState({
pathTesselator: new _pathTesselator.default({
fp64: this.use64bitPositions()
})
});
}
}, {
key: "updateState",
value: function updateState(params) {
(0, _get2.default)((0, _getPrototypeOf2.default)(PathLayer.prototype), "updateState", this).call(this, params);
var props = params.props,
changeFlags = params.changeFlags;
var attributeManager = this.getAttributeManager();
var geometryChanged = changeFlags.dataChanged || changeFlags.updateTriggersChanged && (changeFlags.updateTriggersChanged.all || changeFlags.updateTriggersChanged.getPath);
if (geometryChanged) {
var pathTesselator = this.state.pathTesselator;
var buffers = props.data.attributes || {};
pathTesselator.updateGeometry({
data: props.data,
geometryBuffer: buffers.getPath,
buffers: buffers,
normalize: !props._pathType,
loop: props._pathType === 'loop',
getGeometry: props.getPath,
positionFormat: props.positionFormat,
wrapLongitude: props.wrapLongitude,
resolution: this.context.viewport.resolution,
dataChanged: changeFlags.dataChanged
});
this.setState({
numInstances: pathTesselator.instanceCount,
startIndices: pathTesselator.vertexStarts
});
if (!changeFlags.dataChanged) {
attributeManager.invalidateAll();
}
}
if (changeFlags.extensionsChanged) {
var _this$state$model;
var gl = this.context.gl;
(_this$state$model = this.state.model) === null || _this$state$model === void 0 ? void 0 : _this$state$model.delete();
this.state.model = this._getModel(gl);
attributeManager.invalidateAll();
}
}
}, {
key: "getPickingInfo",
value: function getPickingInfo(params) {
var info = (0, _get2.default)((0, _getPrototypeOf2.default)(PathLayer.prototype), "getPickingInfo", this).call(this, params);
var index = info.index;
var data = this.props.data;
if (data[0] && data[0].__source) {
info.object = data.find(function (d) {
return d.__source.index === index;
});
}
return info;
}
}, {
key: "disablePickingIndex",
value: function disablePickingIndex(objectIndex) {
var data = this.props.data;
if (data[0] && data[0].__source) {
for (var i = 0; i < data.length; i++) {
if (data[i].__source.index === objectIndex) {
this._disablePickingIndex(i);
}
}
} else {
(0, _get2.default)((0, _getPrototypeOf2.default)(PathLayer.prototype), "disablePickingIndex", this).call(this, objectIndex);
}
}
}, {
key: "draw",
value: function draw(_ref2) {
var uniforms = _ref2.uniforms;
var _this$props = this.props,
jointRounded = _this$props.jointRounded,
capRounded = _this$props.capRounded,
billboard = _this$props.billboard,
miterLimit = _this$props.miterLimit,
widthUnits = _this$props.widthUnits,
widthScale = _this$props.widthScale,
widthMinPixels = _this$props.widthMinPixels,
widthMaxPixels = _this$props.widthMaxPixels;
this.state.model.setUniforms(uniforms).setUniforms({
jointType: Number(jointRounded),
capType: Number(capRounded),
billboard: billboard,
widthUnits: _core.UNIT[widthUnits],
widthScale: widthScale,
miterLimit: miterLimit,
widthMinPixels: widthMinPixels,
widthMaxPixels: widthMaxPixels
}).draw();
}
}, {
key: "_getModel",
value: function _getModel(gl) {
var SEGMENT_INDICES = [0, 1, 2, 1, 4, 2, 1, 3, 4, 3, 5, 4];
var SEGMENT_POSITIONS = [0, 0, 0, -1, 0, 1, 1, -1, 1, 1, 1, 0];
return new _core2.Model(gl, _objectSpread(_objectSpread({}, this.getShaders()), {}, {
id: this.props.id,
geometry: new _core2.Geometry({
drawMode: 4,
attributes: {
indices: new Uint16Array(SEGMENT_INDICES),
positions: {
value: new Float32Array(SEGMENT_POSITIONS),
size: 2
}
}
}),
isInstanced: true
}));
}
}, {
key: "calculatePositions",
value: function calculatePositions(attribute) {
var pathTesselator = this.state.pathTesselator;
attribute.startIndices = pathTesselator.vertexStarts;
attribute.value = pathTesselator.get('positions');
}
}, {
key: "calculateSegmentTypes",
value: function calculateSegmentTypes(attribute) {
var pathTesselator = this.state.pathTesselator;
attribute.startIndices = pathTesselator.vertexStarts;
attribute.value = pathTesselator.get('segmentTypes');
}
}]);
return PathLayer;
}(_core.Layer);
exports.default = PathLayer;
(0, _defineProperty2.default)(PathLayer, "defaultProps", defaultProps);
(0, _defineProperty2.default)(PathLayer, "layerName", 'PathLayer');
//# sourceMappingURL=path-layer.js.map