@deck.gl/layers
Version:
deck.gl core layers
467 lines (418 loc) • 16.1 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 _polygonTesselator = _interopRequireDefault(require("./polygon-tesselator"));
var _solidPolygonLayerVertexTop = _interopRequireDefault(require("./solid-polygon-layer-vertex-top.glsl"));
var _solidPolygonLayerVertexSide = _interopRequireDefault(require("./solid-polygon-layer-vertex-side.glsl"));
var _solidPolygonLayerFragment = _interopRequireDefault(require("./solid-polygon-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 = {
filled: true,
extruded: false,
wireframe: false,
_normalize: true,
_windingOrder: 'CW',
_full3d: false,
elevationScale: {
type: 'number',
min: 0,
value: 1
},
getPolygon: {
type: 'accessor',
value: function value(f) {
return f.polygon;
}
},
getElevation: {
type: 'accessor',
value: 1000
},
getFillColor: {
type: 'accessor',
value: DEFAULT_COLOR
},
getLineColor: {
type: 'accessor',
value: DEFAULT_COLOR
},
material: true
};
var ATTRIBUTE_TRANSITION = {
enter: function enter(value, chunk) {
return chunk.length ? chunk.subarray(chunk.length - value.length) : value;
}
};
var SolidPolygonLayer = function (_Layer) {
(0, _inherits2.default)(SolidPolygonLayer, _Layer);
var _super = _createSuper(SolidPolygonLayer);
function SolidPolygonLayer() {
var _this;
(0, _classCallCheck2.default)(this, SolidPolygonLayer);
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)(SolidPolygonLayer, [{
key: "getShaders",
value: function getShaders(type) {
return (0, _get2.default)((0, _getPrototypeOf2.default)(SolidPolygonLayer.prototype), "getShaders", this).call(this, {
vs: type === 'top' ? _solidPolygonLayerVertexTop.default : _solidPolygonLayerVertexSide.default,
fs: _solidPolygonLayerFragment.default,
defines: {
RING_WINDING_ORDER_CW: !this.props._normalize && this.props._windingOrder === 'CCW' ? 0 : 1
},
modules: [_core.project32, _core.gouraudLighting, _core.picking]
});
}
}, {
key: "wrapLongitude",
get: function get() {
return false;
}
}, {
key: "initializeState",
value: function initializeState() {
var _this2 = this;
var _this$context = this.context,
gl = _this$context.gl,
viewport = _this$context.viewport;
var coordinateSystem = this.props.coordinateSystem;
var _full3d = this.props._full3d;
if (viewport.isGeospatial && coordinateSystem === _core.COORDINATE_SYSTEM.DEFAULT) {
coordinateSystem = _core.COORDINATE_SYSTEM.LNGLAT;
}
var preproject;
if (coordinateSystem === _core.COORDINATE_SYSTEM.LNGLAT) {
if (_full3d) {
preproject = viewport.projectPosition.bind(viewport);
} else {
preproject = viewport.projectFlat.bind(viewport);
}
}
this.setState({
numInstances: 0,
polygonTesselator: new _polygonTesselator.default({
preproject: preproject,
fp64: this.use64bitPositions(),
IndexType: !gl || (0, _core2.hasFeatures)(gl, _core2.FEATURES.ELEMENT_INDEX_UINT32) ? Uint32Array : Uint16Array
})
});
var attributeManager = this.getAttributeManager();
var noAlloc = true;
attributeManager.remove(['instancePickingColors']);
attributeManager.add({
indices: {
size: 1,
isIndexed: true,
update: this.calculateIndices,
noAlloc: noAlloc
},
positions: {
size: 3,
type: 5130,
fp64: this.use64bitPositions(),
transition: ATTRIBUTE_TRANSITION,
accessor: 'getPolygon',
update: this.calculatePositions,
noAlloc: noAlloc,
shaderAttributes: {
positions: {
vertexOffset: 0,
divisor: 0
},
instancePositions: {
vertexOffset: 0,
divisor: 1
},
nextPositions: {
vertexOffset: 1,
divisor: 1
}
}
},
vertexValid: {
size: 1,
divisor: 1,
type: 5121,
update: this.calculateVertexValid,
noAlloc: noAlloc
},
elevations: {
size: 1,
transition: ATTRIBUTE_TRANSITION,
accessor: 'getElevation',
shaderAttributes: {
elevations: {
divisor: 0
},
instanceElevations: {
divisor: 1
}
}
},
fillColors: {
size: this.props.colorFormat.length,
type: 5121,
normalized: true,
transition: ATTRIBUTE_TRANSITION,
accessor: 'getFillColor',
defaultValue: DEFAULT_COLOR,
shaderAttributes: {
fillColors: {
divisor: 0
},
instanceFillColors: {
divisor: 1
}
}
},
lineColors: {
size: this.props.colorFormat.length,
type: 5121,
normalized: true,
transition: ATTRIBUTE_TRANSITION,
accessor: 'getLineColor',
defaultValue: DEFAULT_COLOR,
shaderAttributes: {
lineColors: {
divisor: 0
},
instanceLineColors: {
divisor: 1
}
}
},
pickingColors: {
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);
},
shaderAttributes: {
pickingColors: {
divisor: 0
},
instancePickingColors: {
divisor: 1
}
}
}
});
}
}, {
key: "getPickingInfo",
value: function getPickingInfo(params) {
var info = (0, _get2.default)((0, _getPrototypeOf2.default)(SolidPolygonLayer.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)(SolidPolygonLayer.prototype), "disablePickingIndex", this).call(this, objectIndex);
}
}
}, {
key: "draw",
value: function draw(_ref2) {
var uniforms = _ref2.uniforms;
var _this$props = this.props,
extruded = _this$props.extruded,
filled = _this$props.filled,
wireframe = _this$props.wireframe,
elevationScale = _this$props.elevationScale;
var _this$state = this.state,
topModel = _this$state.topModel,
sideModel = _this$state.sideModel,
polygonTesselator = _this$state.polygonTesselator;
var renderUniforms = _objectSpread(_objectSpread({}, uniforms), {}, {
extruded: Boolean(extruded),
elevationScale: elevationScale
});
if (sideModel) {
sideModel.setInstanceCount(polygonTesselator.instanceCount - 1);
sideModel.setUniforms(renderUniforms);
if (wireframe) {
sideModel.setDrawMode(3);
sideModel.setUniforms({
isWireframe: true
}).draw();
}
if (filled) {
sideModel.setDrawMode(6);
sideModel.setUniforms({
isWireframe: false
}).draw();
}
}
if (topModel) {
topModel.setVertexCount(polygonTesselator.vertexCount);
topModel.setUniforms(renderUniforms).draw();
}
}
}, {
key: "updateState",
value: function updateState(updateParams) {
(0, _get2.default)((0, _getPrototypeOf2.default)(SolidPolygonLayer.prototype), "updateState", this).call(this, updateParams);
this.updateGeometry(updateParams);
var props = updateParams.props,
oldProps = updateParams.oldProps,
changeFlags = updateParams.changeFlags;
var attributeManager = this.getAttributeManager();
var regenerateModels = changeFlags.extensionsChanged || props.filled !== oldProps.filled || props.extruded !== oldProps.extruded;
if (regenerateModels) {
var _this$state$models;
(_this$state$models = this.state.models) === null || _this$state$models === void 0 ? void 0 : _this$state$models.forEach(function (model) {
return model.delete();
});
this.setState(this._getModels(this.context.gl));
attributeManager.invalidateAll();
}
}
}, {
key: "updateGeometry",
value: function updateGeometry(_ref3) {
var props = _ref3.props,
oldProps = _ref3.oldProps,
changeFlags = _ref3.changeFlags;
var geometryConfigChanged = changeFlags.dataChanged || changeFlags.updateTriggersChanged && (changeFlags.updateTriggersChanged.all || changeFlags.updateTriggersChanged.getPolygon);
if (geometryConfigChanged) {
var polygonTesselator = this.state.polygonTesselator;
var buffers = props.data.attributes || {};
polygonTesselator.updateGeometry({
data: props.data,
normalize: props._normalize,
geometryBuffer: buffers.getPolygon,
buffers: buffers,
getGeometry: props.getPolygon,
positionFormat: props.positionFormat,
wrapLongitude: props.wrapLongitude,
resolution: this.context.viewport.resolution,
fp64: this.use64bitPositions(),
dataChanged: changeFlags.dataChanged,
full3d: props._full3d
});
this.setState({
numInstances: polygonTesselator.instanceCount,
startIndices: polygonTesselator.vertexStarts
});
if (!changeFlags.dataChanged) {
this.getAttributeManager().invalidateAll();
}
}
}
}, {
key: "_getModels",
value: function _getModels(gl) {
var _this$props2 = this.props,
id = _this$props2.id,
filled = _this$props2.filled,
extruded = _this$props2.extruded;
var topModel;
var sideModel;
if (filled) {
var shaders = this.getShaders('top');
shaders.defines.NON_INSTANCED_MODEL = 1;
topModel = new _core2.Model(gl, _objectSpread(_objectSpread({}, shaders), {}, {
id: "".concat(id, "-top"),
drawMode: 4,
attributes: {
vertexPositions: new Float32Array([0, 1])
},
uniforms: {
isWireframe: false,
isSideVertex: false
},
vertexCount: 0,
isIndexed: true
}));
}
if (extruded) {
sideModel = new _core2.Model(gl, _objectSpread(_objectSpread({}, this.getShaders('side')), {}, {
id: "".concat(id, "-side"),
geometry: new _core2.Geometry({
drawMode: 1,
vertexCount: 4,
attributes: {
vertexPositions: {
size: 2,
value: new Float32Array([1, 0, 0, 0, 0, 1, 1, 1])
}
}
}),
instanceCount: 0,
isInstanced: 1
}));
sideModel.userData.excludeAttributes = {
indices: true
};
}
return {
models: [sideModel, topModel].filter(Boolean),
topModel: topModel,
sideModel: sideModel
};
}
}, {
key: "calculateIndices",
value: function calculateIndices(attribute) {
var polygonTesselator = this.state.polygonTesselator;
attribute.startIndices = polygonTesselator.indexStarts;
attribute.value = polygonTesselator.get('indices');
}
}, {
key: "calculatePositions",
value: function calculatePositions(attribute) {
var polygonTesselator = this.state.polygonTesselator;
attribute.startIndices = polygonTesselator.vertexStarts;
attribute.value = polygonTesselator.get('positions');
}
}, {
key: "calculateVertexValid",
value: function calculateVertexValid(attribute) {
attribute.value = this.state.polygonTesselator.get('vertexValid');
}
}]);
return SolidPolygonLayer;
}(_core.Layer);
exports.default = SolidPolygonLayer;
(0, _defineProperty2.default)(SolidPolygonLayer, "defaultProps", defaultProps);
(0, _defineProperty2.default)(SolidPolygonLayer, "layerName", 'SolidPolygonLayer');
//# sourceMappingURL=solid-polygon-layer.js.map