@deck.gl/layers
Version:
deck.gl core layers
283 lines (262 loc) • 6.86 kB
JavaScript
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
import { Layer, project32, picking, UNIT } from '@deck.gl/core';
import { Model, Geometry } from '@luma.gl/core';
import PathTesselator from './path-tesselator';
import vs from './path-layer-vertex.glsl';
import fs from './path-layer-fragment.glsl';
const DEFAULT_COLOR = [0, 0, 0, 255];
const 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: object => object.path
},
getColor: {
type: 'accessor',
value: DEFAULT_COLOR
},
getWidth: {
type: 'accessor',
value: 1
},
rounded: {
deprecatedFor: ['jointRounded', 'capRounded']
}
};
const ATTRIBUTE_TRANSITION = {
enter: (value, chunk) => {
return chunk.length ? chunk.subarray(chunk.length - value.length) : value;
}
};
export default class PathLayer extends Layer {
constructor(...args) {
super(...args);
_defineProperty(this, "state", void 0);
}
getShaders() {
return super.getShaders({
vs,
fs,
modules: [project32, picking]
});
}
get wrapLongitude() {
return false;
}
initializeState() {
const noAlloc = true;
const attributeManager = this.getAttributeManager();
attributeManager.addInstanced({
positions: {
size: 3,
vertexOffset: 1,
type: 5130,
fp64: this.use64bitPositions(),
transition: ATTRIBUTE_TRANSITION,
accessor: 'getPath',
update: this.calculatePositions,
noAlloc,
shaderAttributes: {
instanceLeftPositions: {
vertexOffset: 0
},
instanceStartPositions: {
vertexOffset: 1
},
instanceEndPositions: {
vertexOffset: 2
},
instanceRightPositions: {
vertexOffset: 3
}
}
},
instanceTypes: {
size: 1,
type: 5121,
update: this.calculateSegmentTypes,
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: (object, {
index,
target: value
}) => this.encodePickingColor(object && object.__source ? object.__source.index : index, value)
}
});
this.setState({
pathTesselator: new PathTesselator({
fp64: this.use64bitPositions()
})
});
}
updateState(params) {
super.updateState(params);
const {
props,
changeFlags
} = params;
const attributeManager = this.getAttributeManager();
const geometryChanged = changeFlags.dataChanged || changeFlags.updateTriggersChanged && (changeFlags.updateTriggersChanged.all || changeFlags.updateTriggersChanged.getPath);
if (geometryChanged) {
const {
pathTesselator
} = this.state;
const buffers = props.data.attributes || {};
pathTesselator.updateGeometry({
data: props.data,
geometryBuffer: buffers.getPath,
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;
const {
gl
} = this.context;
(_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();
}
}
getPickingInfo(params) {
const info = super.getPickingInfo(params);
const {
index
} = info;
const {
data
} = this.props;
if (data[0] && data[0].__source) {
info.object = data.find(d => d.__source.index === index);
}
return info;
}
disablePickingIndex(objectIndex) {
const {
data
} = this.props;
if (data[0] && data[0].__source) {
for (let i = 0; i < data.length; i++) {
if (data[i].__source.index === objectIndex) {
this._disablePickingIndex(i);
}
}
} else {
super.disablePickingIndex(objectIndex);
}
}
draw({
uniforms
}) {
const {
jointRounded,
capRounded,
billboard,
miterLimit,
widthUnits,
widthScale,
widthMinPixels,
widthMaxPixels
} = this.props;
this.state.model.setUniforms(uniforms).setUniforms({
jointType: Number(jointRounded),
capType: Number(capRounded),
billboard,
widthUnits: UNIT[widthUnits],
widthScale,
miterLimit,
widthMinPixels,
widthMaxPixels
}).draw();
}
_getModel(gl) {
const SEGMENT_INDICES = [0, 1, 2, 1, 4, 2, 1, 3, 4, 3, 5, 4];
const SEGMENT_POSITIONS = [0, 0, 0, -1, 0, 1, 1, -1, 1, 1, 1, 0];
return new Model(gl, { ...this.getShaders(),
id: this.props.id,
geometry: new Geometry({
drawMode: 4,
attributes: {
indices: new Uint16Array(SEGMENT_INDICES),
positions: {
value: new Float32Array(SEGMENT_POSITIONS),
size: 2
}
}
}),
isInstanced: true
});
}
calculatePositions(attribute) {
const {
pathTesselator
} = this.state;
attribute.startIndices = pathTesselator.vertexStarts;
attribute.value = pathTesselator.get('positions');
}
calculateSegmentTypes(attribute) {
const {
pathTesselator
} = this.state;
attribute.startIndices = pathTesselator.vertexStarts;
attribute.value = pathTesselator.get('segmentTypes');
}
}
_defineProperty(PathLayer, "defaultProps", defaultProps);
_defineProperty(PathLayer, "layerName", 'PathLayer');
//# sourceMappingURL=path-layer.js.map