@deck.gl/experimental-layers
Version:
Experimental layers for deck.gl
177 lines (164 loc) • 5.2 kB
JavaScript
// 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';
const fp64LowPart = fp64.fp64LowPart;
import vs from './gpu-grid-cell-layer-vertex.glsl';
import fs from './gpu-grid-cell-layer-fragment.glsl';
const DEFAULT_MINCOLOR = [0, 0, 0, 255];
const DEFAULT_MAXCOLOR = [0, 255, 0, 255];
const AGGREGATION_DATA_UBO_INDEX = 0;
const 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: {}
};
export default class GPUGridCellLayer extends Layer {
getShaders() {
return {
vs,
fs,
modules: ['project32', 'lighting', 'picking', 'fp64']
};
}
initializeState() {
const attributeManager = this.getAttributeManager();
attributeManager.addInstanced({
instanceCounts: {
size: 4,
update: this.calculateInstanceCounts,
noAlloc: true
}
});
}
updateState(_ref) {
let props = _ref.props,
oldProps = _ref.oldProps,
changeFlags = _ref.changeFlags;
super.updateState({
props,
oldProps,
changeFlags
}); // Re-generate model if geometry changed
if (props.fp64 !== oldProps.fp64) {
const gl = this.context.gl;
if (this.state.model) {
this.state.model.delete();
}
const model = this._getModel(gl);
this._setupUniformBuffer(model);
this.setState({
model
});
this.state.attributeManager.invalidate('instanceCounts');
}
if (props.countsBuffer !== oldProps.countsBuffer) {
this.state.attributeManager.invalidate('instanceCounts');
}
}
_getModel(gl) {
return new Model(gl, Object.assign({}, this.getShaders(), {
id: this.props.id,
geometry: new CubeGeometry(),
isInstanced: true,
shaderCache: this.context.shaderCache
}));
}
draw(_ref2) {
let uniforms = _ref2.uniforms;
const _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;
const gridOriginLow = [fp64LowPart(gridOrigin[0]), fp64LowPart(gridOrigin[1])];
const 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,
extruded,
elevationScale,
coverage,
gridSize,
gridOrigin,
gridOriginLow,
gridOffset,
gridOffsetLow,
minColor,
maxColor
}));
maxCountBuffer.unbind({
target: GL.UNIFORM_BUFFER,
index: AGGREGATION_DATA_UBO_INDEX
});
}
calculateInstanceCounts(attribute) {
const countsBuffer = this.props.countsBuffer;
attribute.update({
buffer: countsBuffer
});
}
_setupUniformBuffer(model) {
const gl = this.context.gl;
const programHandle = model.program.handle;
const uniformBlockIndex = gl.getUniformBlockIndex(programHandle, 'AggregationData');
gl.uniformBlockBinding(programHandle, uniformBlockIndex, AGGREGATION_DATA_UBO_INDEX);
}
}
GPUGridCellLayer.layerName = 'GridCellLayer';
GPUGridCellLayer.defaultProps = defaultProps;
//# sourceMappingURL=gpu-grid-cell-layer.js.map