@deck.gl/extensions 8.8.7 → 8.9.0-alpha.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/dist.dev.js +240 -240
- package/dist.min.js +1 -1
- package/package.json +2 -2
- package/typed/brushing/brushing.js +83 -0
- package/typed/brushing/shader-module.js +116 -0
- package/typed/clip/clip.js +114 -0
- package/typed/data-filter/aggregator.js +98 -0
- package/typed/data-filter/data-filter.js +159 -0
- package/typed/data-filter/shader-module.js +152 -0
- package/typed/fill-style/fill-style.js +152 -0
- package/typed/fill-style/shaders.glsl.js +95 -0
- package/typed/fp64/fp64.js +34 -0
- package/typed/fp64/project64.glsl.js +100 -0
- package/typed/fp64/project64.js +50 -0
- package/typed/index.js +9 -0
- package/typed/mask/mask.js +48 -0
- package/typed/mask/shader-module.js +73 -0
- package/typed/path-style/path-style.js +107 -0
- package/typed/path-style/shaders.glsl.js +93 -0
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
// Copyright (c) 2015 - 2017 Uber Technologies, Inc.
|
|
2
|
+
//
|
|
3
|
+
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
4
|
+
// of this software and associated documentation files (the "Software"), to deal
|
|
5
|
+
// in the Software without restriction, including without limitation the rights
|
|
6
|
+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
7
|
+
// copies of the Software, and to permit persons to whom the Software is
|
|
8
|
+
// furnished to do so, subject to the following conditions:
|
|
9
|
+
//
|
|
10
|
+
// The above copyright notice and this permission notice shall be included in
|
|
11
|
+
// all copies or substantial portions of the Software.
|
|
12
|
+
//
|
|
13
|
+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
14
|
+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
15
|
+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
16
|
+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
17
|
+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
18
|
+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
19
|
+
// THE SOFTWARE.
|
|
20
|
+
import { LayerExtension } from '@deck.gl/core';
|
|
21
|
+
const defaultProps = {
|
|
22
|
+
clipBounds: [0, 0, 1, 1]
|
|
23
|
+
};
|
|
24
|
+
const shaderFunction = `
|
|
25
|
+
uniform vec4 clip_bounds;
|
|
26
|
+
|
|
27
|
+
bool clip_isInBounds(vec2 position) {
|
|
28
|
+
return position.x >= clip_bounds[0] && position.y >= clip_bounds[1] && position.x < clip_bounds[2] && position.y < clip_bounds[3];
|
|
29
|
+
}
|
|
30
|
+
`;
|
|
31
|
+
/*
|
|
32
|
+
* The vertex-shader version clips geometries by their anchor position
|
|
33
|
+
* e.g. ScatterplotLayer - show if the center of a circle is within bounds
|
|
34
|
+
*/
|
|
35
|
+
const shaderModuleVs = {
|
|
36
|
+
name: 'clip-vs',
|
|
37
|
+
vs: shaderFunction
|
|
38
|
+
};
|
|
39
|
+
const injectionVs = {
|
|
40
|
+
'vs:#decl': `
|
|
41
|
+
varying float clip_isVisible;
|
|
42
|
+
`,
|
|
43
|
+
'vs:DECKGL_FILTER_GL_POSITION': `
|
|
44
|
+
clip_isVisible = float(clip_isInBounds(geometry.worldPosition.xy));
|
|
45
|
+
`,
|
|
46
|
+
'fs:#decl': `
|
|
47
|
+
varying float clip_isVisible;
|
|
48
|
+
`,
|
|
49
|
+
'fs:DECKGL_FILTER_COLOR': `
|
|
50
|
+
if (clip_isVisible < 0.5) discard;
|
|
51
|
+
`
|
|
52
|
+
};
|
|
53
|
+
/*
|
|
54
|
+
* The fragment-shader version clips pixels at the bounds
|
|
55
|
+
* e.g. PolygonLayer - show the part of the polygon that intersect with the bounds
|
|
56
|
+
*/
|
|
57
|
+
const shaderModuleFs = {
|
|
58
|
+
name: 'clip-fs',
|
|
59
|
+
fs: shaderFunction
|
|
60
|
+
};
|
|
61
|
+
const injectionFs = {
|
|
62
|
+
'vs:#decl': `
|
|
63
|
+
varying vec2 clip_commonPosition;
|
|
64
|
+
`,
|
|
65
|
+
'vs:DECKGL_FILTER_GL_POSITION': `
|
|
66
|
+
clip_commonPosition = geometry.position.xy;
|
|
67
|
+
`,
|
|
68
|
+
'fs:#decl': `
|
|
69
|
+
varying vec2 clip_commonPosition;
|
|
70
|
+
`,
|
|
71
|
+
'fs:DECKGL_FILTER_COLOR': `
|
|
72
|
+
if (!clip_isInBounds(clip_commonPosition)) discard;
|
|
73
|
+
`
|
|
74
|
+
};
|
|
75
|
+
export default class ClipExtension extends LayerExtension {
|
|
76
|
+
getShaders() {
|
|
77
|
+
// If `clipByInstance: true`, the entire object is shown/hidden based on its anchor position (done by vertex shader)
|
|
78
|
+
// Otherwise, the object is trimmed by the clip bounds (done by fragment shader)
|
|
79
|
+
// Default behavior: consider a layer instanced if it has attribute `instancePositions`
|
|
80
|
+
let clipByInstance = 'instancePositions' in this.getAttributeManager().attributes;
|
|
81
|
+
// Users can override by setting the `clipByInstance` prop
|
|
82
|
+
if ('clipByInstance' in this.props) {
|
|
83
|
+
clipByInstance = this.props.clipByInstance;
|
|
84
|
+
}
|
|
85
|
+
this.state.clipByInstance = clipByInstance;
|
|
86
|
+
return clipByInstance
|
|
87
|
+
? {
|
|
88
|
+
modules: [shaderModuleVs],
|
|
89
|
+
inject: injectionVs
|
|
90
|
+
}
|
|
91
|
+
: {
|
|
92
|
+
modules: [shaderModuleFs],
|
|
93
|
+
inject: injectionFs
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
draw({ uniforms }) {
|
|
97
|
+
const { clipBounds = defaultProps.clipBounds } = this.props;
|
|
98
|
+
if (this.state.clipByInstance) {
|
|
99
|
+
uniforms.clip_bounds = clipBounds;
|
|
100
|
+
}
|
|
101
|
+
else {
|
|
102
|
+
const corner0 = this.projectPosition([clipBounds[0], clipBounds[1], 0]);
|
|
103
|
+
const corner1 = this.projectPosition([clipBounds[2], clipBounds[3], 0]);
|
|
104
|
+
uniforms.clip_bounds = [
|
|
105
|
+
Math.min(corner0[0], corner1[0]),
|
|
106
|
+
Math.min(corner0[1], corner1[1]),
|
|
107
|
+
Math.max(corner0[0], corner1[0]),
|
|
108
|
+
Math.max(corner0[1], corner1[1])
|
|
109
|
+
];
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
ClipExtension.extensionName = 'ClipExtension';
|
|
114
|
+
ClipExtension.defaultProps = defaultProps;
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { Model, Texture2D, Framebuffer, isWebGL2 } from '@luma.gl/core';
|
|
2
|
+
import GL from '@luma.gl/constants';
|
|
3
|
+
const AGGREGATE_VS = `\
|
|
4
|
+
#define SHADER_NAME data-filter-vertex-shader
|
|
5
|
+
|
|
6
|
+
#ifdef FLOAT_TARGET
|
|
7
|
+
attribute float filterIndices;
|
|
8
|
+
attribute float filterPrevIndices;
|
|
9
|
+
#else
|
|
10
|
+
attribute vec2 filterIndices;
|
|
11
|
+
attribute vec2 filterPrevIndices;
|
|
12
|
+
#endif
|
|
13
|
+
|
|
14
|
+
varying vec4 vColor;
|
|
15
|
+
const float component = 1.0 / 255.0;
|
|
16
|
+
|
|
17
|
+
void main() {
|
|
18
|
+
#ifdef FLOAT_TARGET
|
|
19
|
+
dataFilter_value *= float(filterIndices != filterPrevIndices);
|
|
20
|
+
gl_Position = vec4(0.0, 0.0, 0.0, 1.0);
|
|
21
|
+
vColor = vec4(0.0, 0.0, 0.0, 1.0);
|
|
22
|
+
#else
|
|
23
|
+
// Float texture is not supported: pack result into 4 channels x 256 px x 64px
|
|
24
|
+
dataFilter_value *= float(filterIndices.x != filterPrevIndices.x);
|
|
25
|
+
float col = filterIndices.x;
|
|
26
|
+
float row = filterIndices.y * 4.0;
|
|
27
|
+
float channel = floor(row);
|
|
28
|
+
row = fract(row);
|
|
29
|
+
vColor = component * vec4(bvec4(channel == 0.0, channel == 1.0, channel == 2.0, channel == 3.0));
|
|
30
|
+
gl_Position = vec4(col * 2.0 - 1.0, row * 2.0 - 1.0, 0.0, 1.0);
|
|
31
|
+
#endif
|
|
32
|
+
gl_PointSize = 1.0;
|
|
33
|
+
}
|
|
34
|
+
`;
|
|
35
|
+
const AGGREGATE_FS = `\
|
|
36
|
+
#define SHADER_NAME data-filter-fragment-shader
|
|
37
|
+
precision highp float;
|
|
38
|
+
|
|
39
|
+
varying vec4 vColor;
|
|
40
|
+
|
|
41
|
+
void main() {
|
|
42
|
+
if (dataFilter_value < 0.5) {
|
|
43
|
+
discard;
|
|
44
|
+
}
|
|
45
|
+
gl_FragColor = vColor;
|
|
46
|
+
}
|
|
47
|
+
`;
|
|
48
|
+
export function supportsFloatTarget(gl) {
|
|
49
|
+
// https://developer.mozilla.org/en-US/docs/Web/API/WebGL_API/WebGL_best_practices#Support_for_float_textures_doesnt_mean_you_can_render_into_them!
|
|
50
|
+
return (gl.getExtension('EXT_float_blend') &&
|
|
51
|
+
// WebGL 2
|
|
52
|
+
(gl.getExtension('EXT_color_buffer_float') ||
|
|
53
|
+
// WebGL 1
|
|
54
|
+
gl.getExtension('WEBGL_color_buffer_float')));
|
|
55
|
+
}
|
|
56
|
+
// A 1x1 framebuffer object that encodes the total count of filtered items
|
|
57
|
+
export function getFramebuffer(gl, useFloatTarget) {
|
|
58
|
+
if (useFloatTarget) {
|
|
59
|
+
return new Framebuffer(gl, {
|
|
60
|
+
width: 1,
|
|
61
|
+
height: 1,
|
|
62
|
+
attachments: {
|
|
63
|
+
[GL.COLOR_ATTACHMENT0]: new Texture2D(gl, {
|
|
64
|
+
format: isWebGL2(gl) ? GL.RGBA32F : GL.RGBA,
|
|
65
|
+
type: GL.FLOAT,
|
|
66
|
+
mipmaps: false
|
|
67
|
+
})
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
return new Framebuffer(gl, {
|
|
72
|
+
width: 256,
|
|
73
|
+
height: 64,
|
|
74
|
+
depth: false
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
// Increments the counter based on dataFilter_value
|
|
78
|
+
export function getModel(gl, shaderOptions, useFloatTarget) {
|
|
79
|
+
shaderOptions.defines.NON_INSTANCED_MODEL = 1;
|
|
80
|
+
if (useFloatTarget) {
|
|
81
|
+
shaderOptions.defines.FLOAT_TARGET = 1;
|
|
82
|
+
}
|
|
83
|
+
return new Model(gl, {
|
|
84
|
+
id: 'data-filter-aggregation-model',
|
|
85
|
+
vertexCount: 1,
|
|
86
|
+
isInstanced: false,
|
|
87
|
+
drawMode: GL.POINTS,
|
|
88
|
+
vs: AGGREGATE_VS,
|
|
89
|
+
fs: AGGREGATE_FS,
|
|
90
|
+
...shaderOptions
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
export const parameters = {
|
|
94
|
+
blend: true,
|
|
95
|
+
blendFunc: [GL.ONE, GL.ONE, GL.ONE, GL.ONE],
|
|
96
|
+
blendEquation: [GL.FUNC_ADD, GL.FUNC_ADD],
|
|
97
|
+
depthTest: false
|
|
98
|
+
};
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
// Copyright (c) 2015 - 2017 Uber Technologies, Inc.
|
|
2
|
+
//
|
|
3
|
+
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
4
|
+
// of this software and associated documentation files (the "Software"), to deal
|
|
5
|
+
// in the Software without restriction, including without limitation the rights
|
|
6
|
+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
7
|
+
// copies of the Software, and to permit persons to whom the Software is
|
|
8
|
+
// furnished to do so, subject to the following conditions:
|
|
9
|
+
//
|
|
10
|
+
// The above copyright notice and this permission notice shall be included in
|
|
11
|
+
// all copies or substantial portions of the Software.
|
|
12
|
+
//
|
|
13
|
+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
14
|
+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
15
|
+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
16
|
+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
17
|
+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
18
|
+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
19
|
+
// THE SOFTWARE.
|
|
20
|
+
import { LayerExtension } from '@deck.gl/core';
|
|
21
|
+
import { shaderModule, shaderModule64 } from './shader-module';
|
|
22
|
+
import * as aggregator from './aggregator';
|
|
23
|
+
import { readPixelsToArray, clear } from '@luma.gl/core';
|
|
24
|
+
import GL from '@luma.gl/constants';
|
|
25
|
+
const defaultProps = {
|
|
26
|
+
getFilterValue: { type: 'accessor', value: 0 },
|
|
27
|
+
onFilteredItemsChange: { type: 'function', value: null, compare: false },
|
|
28
|
+
filterEnabled: true,
|
|
29
|
+
filterRange: [-1, 1],
|
|
30
|
+
filterSoftRange: null,
|
|
31
|
+
filterTransformSize: true,
|
|
32
|
+
filterTransformColor: true
|
|
33
|
+
};
|
|
34
|
+
const DATA_TYPE_FROM_SIZE = {
|
|
35
|
+
1: 'float',
|
|
36
|
+
2: 'vec2',
|
|
37
|
+
3: 'vec3',
|
|
38
|
+
4: 'vec4'
|
|
39
|
+
};
|
|
40
|
+
export default class DataFilterExtension extends LayerExtension {
|
|
41
|
+
constructor({ filterSize = 1, fp64 = false, countItems = false } = {}) {
|
|
42
|
+
if (!DATA_TYPE_FROM_SIZE[filterSize]) {
|
|
43
|
+
throw new Error('filterSize out of range');
|
|
44
|
+
}
|
|
45
|
+
super({ filterSize, fp64, countItems });
|
|
46
|
+
}
|
|
47
|
+
getShaders(extension) {
|
|
48
|
+
const { filterSize, fp64 } = extension.opts;
|
|
49
|
+
return {
|
|
50
|
+
modules: [fp64 ? shaderModule64 : shaderModule],
|
|
51
|
+
defines: {
|
|
52
|
+
DATAFILTER_TYPE: DATA_TYPE_FROM_SIZE[filterSize],
|
|
53
|
+
DATAFILTER_DOUBLE: Boolean(fp64)
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
initializeState(context, extension) {
|
|
58
|
+
const attributeManager = this.getAttributeManager();
|
|
59
|
+
if (attributeManager) {
|
|
60
|
+
attributeManager.add({
|
|
61
|
+
filterValues: {
|
|
62
|
+
size: extension.opts.filterSize,
|
|
63
|
+
type: extension.opts.fp64 ? GL.DOUBLE : GL.FLOAT,
|
|
64
|
+
accessor: 'getFilterValue',
|
|
65
|
+
shaderAttributes: {
|
|
66
|
+
filterValues: {
|
|
67
|
+
divisor: 0
|
|
68
|
+
},
|
|
69
|
+
instanceFilterValues: {
|
|
70
|
+
divisor: 1
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
const { gl } = this.context;
|
|
77
|
+
if (attributeManager && extension.opts.countItems) {
|
|
78
|
+
const useFloatTarget = aggregator.supportsFloatTarget(gl);
|
|
79
|
+
// This attribute is needed for variable-width data, e.g. Path, SolidPolygon, Text
|
|
80
|
+
// The vertex shader checks if a vertex has the same "index" as the previous vertex
|
|
81
|
+
// so that we only write one count cross multiple vertices of the same object
|
|
82
|
+
attributeManager.add({
|
|
83
|
+
filterIndices: {
|
|
84
|
+
size: useFloatTarget ? 1 : 2,
|
|
85
|
+
vertexOffset: 1,
|
|
86
|
+
type: GL.UNSIGNED_BYTE,
|
|
87
|
+
normalized: true,
|
|
88
|
+
accessor: (object, { index }) => {
|
|
89
|
+
const i = object && object.__source ? object.__source.index : index;
|
|
90
|
+
return useFloatTarget ? (i + 1) % 255 : [(i + 1) % 255, Math.floor(i / 255) % 255];
|
|
91
|
+
},
|
|
92
|
+
shaderAttributes: {
|
|
93
|
+
filterPrevIndices: {
|
|
94
|
+
vertexOffset: 0
|
|
95
|
+
},
|
|
96
|
+
filterIndices: {
|
|
97
|
+
vertexOffset: 1
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
});
|
|
102
|
+
const filterFBO = aggregator.getFramebuffer(gl, useFloatTarget);
|
|
103
|
+
const filterModel = aggregator.getModel(gl, extension.getShaders(extension), useFloatTarget);
|
|
104
|
+
this.setState({ filterFBO, filterModel });
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
updateState({ props, oldProps }) {
|
|
108
|
+
if (this.state.filterModel) {
|
|
109
|
+
const attributeManager = this.getAttributeManager();
|
|
110
|
+
const filterNeedsUpdate = attributeManager.attributes.filterValues.needsUpdate() ||
|
|
111
|
+
props.filterEnabled !== oldProps.filterEnabled ||
|
|
112
|
+
props.filterRange !== oldProps.filterRange ||
|
|
113
|
+
props.filterSoftRange !== oldProps.filterSoftRange;
|
|
114
|
+
if (filterNeedsUpdate) {
|
|
115
|
+
this.setState({ filterNeedsUpdate });
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
draw(params, extension) {
|
|
120
|
+
const { filterFBO, filterModel, filterNeedsUpdate } = this.state;
|
|
121
|
+
const { onFilteredItemsChange } = this.props;
|
|
122
|
+
if (filterNeedsUpdate && onFilteredItemsChange && filterModel) {
|
|
123
|
+
const { attributes: { filterValues, filterIndices } } = this.getAttributeManager();
|
|
124
|
+
filterModel.setVertexCount(this.getNumInstances());
|
|
125
|
+
const { gl } = this.context;
|
|
126
|
+
clear(gl, { framebuffer: filterFBO, color: [0, 0, 0, 0] });
|
|
127
|
+
filterModel
|
|
128
|
+
.updateModuleSettings(params.moduleParameters)
|
|
129
|
+
.setAttributes({
|
|
130
|
+
...filterValues.getShaderAttributes(),
|
|
131
|
+
...(filterIndices && filterIndices.getShaderAttributes())
|
|
132
|
+
})
|
|
133
|
+
.draw({
|
|
134
|
+
framebuffer: filterFBO,
|
|
135
|
+
parameters: {
|
|
136
|
+
...aggregator.parameters,
|
|
137
|
+
viewport: [0, 0, filterFBO.width, filterFBO.height]
|
|
138
|
+
}
|
|
139
|
+
});
|
|
140
|
+
const color = readPixelsToArray(filterFBO);
|
|
141
|
+
let count = 0;
|
|
142
|
+
for (let i = 0; i < color.length; i++) {
|
|
143
|
+
count += color[i];
|
|
144
|
+
}
|
|
145
|
+
onFilteredItemsChange({ id: this.id, count });
|
|
146
|
+
this.state.filterNeedsUpdate = false;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
finalizeState() {
|
|
150
|
+
const { filterFBO, filterModel } = this.state;
|
|
151
|
+
if (filterFBO) {
|
|
152
|
+
filterFBO.color.delete();
|
|
153
|
+
filterFBO.delete();
|
|
154
|
+
filterModel.delete();
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
DataFilterExtension.extensionName = 'DataFilterExtension';
|
|
159
|
+
DataFilterExtension.defaultProps = defaultProps;
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* data filter shader module
|
|
3
|
+
*/
|
|
4
|
+
const vs = `
|
|
5
|
+
uniform DATAFILTER_TYPE filter_min;
|
|
6
|
+
uniform DATAFILTER_TYPE filter_softMin;
|
|
7
|
+
uniform DATAFILTER_TYPE filter_softMax;
|
|
8
|
+
uniform DATAFILTER_TYPE filter_max;
|
|
9
|
+
uniform bool filter_useSoftMargin;
|
|
10
|
+
uniform bool filter_enabled;
|
|
11
|
+
uniform bool filter_transformSize;
|
|
12
|
+
|
|
13
|
+
#ifdef NON_INSTANCED_MODEL
|
|
14
|
+
#define DATAFILTER_ATTRIB filterValues
|
|
15
|
+
#define DATAFILTER_ATTRIB_64LOW filterValues64Low
|
|
16
|
+
#else
|
|
17
|
+
#define DATAFILTER_ATTRIB instanceFilterValues
|
|
18
|
+
#define DATAFILTER_ATTRIB_64LOW instanceFilterValues64Low
|
|
19
|
+
#endif
|
|
20
|
+
|
|
21
|
+
attribute DATAFILTER_TYPE DATAFILTER_ATTRIB;
|
|
22
|
+
#ifdef DATAFILTER_DOUBLE
|
|
23
|
+
attribute DATAFILTER_TYPE DATAFILTER_ATTRIB_64LOW;
|
|
24
|
+
|
|
25
|
+
uniform DATAFILTER_TYPE filter_min64High;
|
|
26
|
+
uniform DATAFILTER_TYPE filter_max64High;
|
|
27
|
+
#endif
|
|
28
|
+
|
|
29
|
+
varying float dataFilter_value;
|
|
30
|
+
|
|
31
|
+
float dataFilter_reduceValue(float value) {
|
|
32
|
+
return value;
|
|
33
|
+
}
|
|
34
|
+
float dataFilter_reduceValue(vec2 value) {
|
|
35
|
+
return min(value.x, value.y);
|
|
36
|
+
}
|
|
37
|
+
float dataFilter_reduceValue(vec3 value) {
|
|
38
|
+
return min(min(value.x, value.y), value.z);
|
|
39
|
+
}
|
|
40
|
+
float dataFilter_reduceValue(vec4 value) {
|
|
41
|
+
return min(min(value.x, value.y), min(value.z, value.w));
|
|
42
|
+
}
|
|
43
|
+
void dataFilter_setValue(DATAFILTER_TYPE valueFromMin, DATAFILTER_TYPE valueFromMax) {
|
|
44
|
+
if (filter_enabled) {
|
|
45
|
+
if (filter_useSoftMargin) {
|
|
46
|
+
dataFilter_value = dataFilter_reduceValue(
|
|
47
|
+
smoothstep(filter_min, filter_softMin, valueFromMin) *
|
|
48
|
+
(1.0 - smoothstep(filter_softMax, filter_max, valueFromMax))
|
|
49
|
+
);
|
|
50
|
+
} else {
|
|
51
|
+
dataFilter_value = dataFilter_reduceValue(
|
|
52
|
+
step(filter_min, valueFromMin) * step(valueFromMax, filter_max)
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
} else {
|
|
56
|
+
dataFilter_value = 1.0;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
`;
|
|
60
|
+
const fs = `
|
|
61
|
+
uniform bool filter_transformColor;
|
|
62
|
+
varying float dataFilter_value;
|
|
63
|
+
`;
|
|
64
|
+
const getUniforms = opts => {
|
|
65
|
+
if (!opts || !opts.extensions) {
|
|
66
|
+
return {};
|
|
67
|
+
}
|
|
68
|
+
const { filterRange = [-1, 1], filterEnabled = true, filterTransformSize = true, filterTransformColor = true } = opts;
|
|
69
|
+
const filterSoftRange = opts.filterSoftRange || filterRange;
|
|
70
|
+
const uniforms = Number.isFinite(filterRange[0])
|
|
71
|
+
? {
|
|
72
|
+
filter_min: filterRange[0],
|
|
73
|
+
filter_softMin: filterSoftRange[0],
|
|
74
|
+
filter_softMax: filterSoftRange[1],
|
|
75
|
+
filter_max: filterRange[1]
|
|
76
|
+
}
|
|
77
|
+
: {
|
|
78
|
+
filter_min: filterRange.map(r => r[0]),
|
|
79
|
+
filter_softMin: filterSoftRange.map(r => r[0]),
|
|
80
|
+
filter_softMax: filterSoftRange.map(r => r[1]),
|
|
81
|
+
filter_max: filterRange.map(r => r[1])
|
|
82
|
+
};
|
|
83
|
+
uniforms.filter_enabled = filterEnabled;
|
|
84
|
+
uniforms.filter_useSoftMargin = Boolean(opts.filterSoftRange);
|
|
85
|
+
uniforms.filter_transformSize = filterEnabled && filterTransformSize;
|
|
86
|
+
uniforms.filter_transformColor = filterEnabled && filterTransformColor;
|
|
87
|
+
return uniforms;
|
|
88
|
+
};
|
|
89
|
+
const getUniforms64 = opts => {
|
|
90
|
+
if (!opts || !opts.extensions) {
|
|
91
|
+
return {};
|
|
92
|
+
}
|
|
93
|
+
const uniforms = getUniforms(opts);
|
|
94
|
+
if (Number.isFinite(uniforms.filter_min)) {
|
|
95
|
+
const min64High = Math.fround(uniforms.filter_min);
|
|
96
|
+
uniforms.filter_min -= min64High;
|
|
97
|
+
uniforms.filter_softMin -= min64High;
|
|
98
|
+
uniforms.filter_min64High = min64High;
|
|
99
|
+
const max64High = Math.fround(uniforms.filter_max);
|
|
100
|
+
uniforms.filter_max -= max64High;
|
|
101
|
+
uniforms.filter_softMax -= max64High;
|
|
102
|
+
uniforms.filter_max64High = max64High;
|
|
103
|
+
}
|
|
104
|
+
else {
|
|
105
|
+
const min64High = uniforms.filter_min.map(Math.fround);
|
|
106
|
+
uniforms.filter_min = uniforms.filter_min.map((x, i) => x - min64High[i]);
|
|
107
|
+
uniforms.filter_softMin = uniforms.filter_softMin.map((x, i) => x - min64High[i]);
|
|
108
|
+
uniforms.filter_min64High = min64High;
|
|
109
|
+
const max64High = uniforms.filter_max.map(Math.fround);
|
|
110
|
+
uniforms.filter_max = uniforms.filter_max.map((x, i) => x - max64High[i]);
|
|
111
|
+
uniforms.filter_softMax = uniforms.filter_softMax.map((x, i) => x - max64High[i]);
|
|
112
|
+
uniforms.filter_max64High = max64High;
|
|
113
|
+
}
|
|
114
|
+
return uniforms;
|
|
115
|
+
};
|
|
116
|
+
const inject = {
|
|
117
|
+
'vs:#main-start': `
|
|
118
|
+
#ifdef DATAFILTER_DOUBLE
|
|
119
|
+
dataFilter_setValue(
|
|
120
|
+
DATAFILTER_ATTRIB - filter_min64High + DATAFILTER_ATTRIB_64LOW,
|
|
121
|
+
DATAFILTER_ATTRIB - filter_max64High + DATAFILTER_ATTRIB_64LOW
|
|
122
|
+
);
|
|
123
|
+
#else
|
|
124
|
+
dataFilter_setValue(DATAFILTER_ATTRIB, DATAFILTER_ATTRIB);
|
|
125
|
+
#endif
|
|
126
|
+
`,
|
|
127
|
+
'vs:DECKGL_FILTER_SIZE': `
|
|
128
|
+
if (filter_transformSize) {
|
|
129
|
+
size = size * dataFilter_value;
|
|
130
|
+
}
|
|
131
|
+
`,
|
|
132
|
+
'fs:DECKGL_FILTER_COLOR': `
|
|
133
|
+
if (dataFilter_value == 0.0) discard;
|
|
134
|
+
if (filter_transformColor) {
|
|
135
|
+
color.a *= dataFilter_value;
|
|
136
|
+
}
|
|
137
|
+
`
|
|
138
|
+
};
|
|
139
|
+
export const shaderModule = {
|
|
140
|
+
name: 'data-filter',
|
|
141
|
+
vs,
|
|
142
|
+
fs,
|
|
143
|
+
inject,
|
|
144
|
+
getUniforms
|
|
145
|
+
};
|
|
146
|
+
export const shaderModule64 = {
|
|
147
|
+
name: 'data-filter-fp64',
|
|
148
|
+
vs,
|
|
149
|
+
fs,
|
|
150
|
+
inject,
|
|
151
|
+
getUniforms: getUniforms64
|
|
152
|
+
};
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import { LayerExtension } from '@deck.gl/core';
|
|
2
|
+
import { Texture2D } from '@luma.gl/core';
|
|
3
|
+
import GL from '@luma.gl/constants';
|
|
4
|
+
import { patternShaders } from './shaders.glsl';
|
|
5
|
+
const defaultProps = {
|
|
6
|
+
fillPatternEnabled: true,
|
|
7
|
+
fillPatternAtlas: null,
|
|
8
|
+
fillPatternMapping: null,
|
|
9
|
+
fillPatternMask: true,
|
|
10
|
+
getFillPattern: { type: 'accessor', value: d => d.pattern },
|
|
11
|
+
getFillPatternScale: { type: 'accessor', value: 1 },
|
|
12
|
+
getFillPatternOffset: { type: 'accessor', value: [0, 0] }
|
|
13
|
+
};
|
|
14
|
+
const DEFAULT_TEXTURE_PARAMETERS = {
|
|
15
|
+
[GL.TEXTURE_MIN_FILTER]: GL.LINEAR,
|
|
16
|
+
// GL.LINEAR is the default value but explicitly set it here
|
|
17
|
+
[GL.TEXTURE_MAG_FILTER]: GL.LINEAR,
|
|
18
|
+
// for texture boundary artifact
|
|
19
|
+
[GL.TEXTURE_WRAP_S]: GL.CLAMP_TO_EDGE,
|
|
20
|
+
[GL.TEXTURE_WRAP_T]: GL.CLAMP_TO_EDGE
|
|
21
|
+
};
|
|
22
|
+
export default class FillStyleExtension extends LayerExtension {
|
|
23
|
+
constructor({ pattern = false } = {}) {
|
|
24
|
+
super({ pattern });
|
|
25
|
+
}
|
|
26
|
+
isEnabled(layer) {
|
|
27
|
+
return layer.getAttributeManager() && !layer.state.pathTesselator;
|
|
28
|
+
}
|
|
29
|
+
getShaders(extension) {
|
|
30
|
+
if (!extension.isEnabled(this)) {
|
|
31
|
+
return null;
|
|
32
|
+
}
|
|
33
|
+
return {
|
|
34
|
+
modules: [extension.opts.pattern && patternShaders].filter(Boolean)
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
initializeState(context, extension) {
|
|
38
|
+
if (!extension.isEnabled(this)) {
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
const attributeManager = this.getAttributeManager();
|
|
42
|
+
if (extension.opts.pattern) {
|
|
43
|
+
attributeManager.add({
|
|
44
|
+
fillPatternFrames: {
|
|
45
|
+
size: 4,
|
|
46
|
+
accessor: 'getFillPattern',
|
|
47
|
+
transform: extension.getPatternFrame.bind(this),
|
|
48
|
+
shaderAttributes: {
|
|
49
|
+
fillPatternFrames: {
|
|
50
|
+
divisor: 0
|
|
51
|
+
},
|
|
52
|
+
instanceFillPatternFrames: {
|
|
53
|
+
divisor: 1
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
},
|
|
57
|
+
fillPatternScales: {
|
|
58
|
+
size: 1,
|
|
59
|
+
accessor: 'getFillPatternScale',
|
|
60
|
+
defaultValue: 1,
|
|
61
|
+
shaderAttributes: {
|
|
62
|
+
fillPatternScales: {
|
|
63
|
+
divisor: 0
|
|
64
|
+
},
|
|
65
|
+
instanceFillPatternScales: {
|
|
66
|
+
divisor: 1
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
},
|
|
70
|
+
fillPatternOffsets: {
|
|
71
|
+
size: 2,
|
|
72
|
+
accessor: 'getFillPatternOffset',
|
|
73
|
+
shaderAttributes: {
|
|
74
|
+
fillPatternOffsets: {
|
|
75
|
+
divisor: 0
|
|
76
|
+
},
|
|
77
|
+
instanceFillPatternOffsets: {
|
|
78
|
+
divisor: 1
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
this.setState({
|
|
85
|
+
emptyTexture: new Texture2D(this.context.gl, {
|
|
86
|
+
data: new Uint8Array(4),
|
|
87
|
+
width: 1,
|
|
88
|
+
height: 1
|
|
89
|
+
})
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
updateState({ props, oldProps }, extension) {
|
|
93
|
+
if (!extension.isEnabled(this)) {
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
if (props.fillPatternAtlas && props.fillPatternAtlas !== oldProps.fillPatternAtlas) {
|
|
97
|
+
extension.loadPatternAtlas.call(this, props);
|
|
98
|
+
}
|
|
99
|
+
if (props.fillPatternMapping && props.fillPatternMapping !== oldProps.fillPatternMapping) {
|
|
100
|
+
extension.loadPatternMapping.call(this, props);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
draw(params, extension) {
|
|
104
|
+
if (!extension.isEnabled(this)) {
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
const { patternTexture } = this.state;
|
|
108
|
+
this.setModuleParameters({
|
|
109
|
+
fillPatternTexture: patternTexture || this.state.emptyTexture
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
finalizeState() {
|
|
113
|
+
const { patternTexture, emptyTexture } = this.state;
|
|
114
|
+
patternTexture?.delete();
|
|
115
|
+
emptyTexture?.delete();
|
|
116
|
+
}
|
|
117
|
+
async loadPatternAtlas({ fillPatternAtlas, fetch }) {
|
|
118
|
+
this.state.patternTexture?.delete();
|
|
119
|
+
this.setState({ patternTexture: null });
|
|
120
|
+
let image = fillPatternAtlas;
|
|
121
|
+
if (typeof image === 'string') {
|
|
122
|
+
image = await fetch(image, { propName: 'fillPatternAtlas', layer: this });
|
|
123
|
+
}
|
|
124
|
+
const patternTexture = image instanceof Texture2D
|
|
125
|
+
? image
|
|
126
|
+
: new Texture2D(this.context.gl, {
|
|
127
|
+
data: image,
|
|
128
|
+
parameters: DEFAULT_TEXTURE_PARAMETERS
|
|
129
|
+
});
|
|
130
|
+
this.setState({ patternTexture });
|
|
131
|
+
}
|
|
132
|
+
async loadPatternMapping({ fillPatternMapping, fetch }) {
|
|
133
|
+
this.setState({ patternMapping: null });
|
|
134
|
+
let patternMapping = fillPatternMapping;
|
|
135
|
+
if (typeof patternMapping === 'string') {
|
|
136
|
+
patternMapping = await fetch(patternMapping, {
|
|
137
|
+
propName: 'fillPatternMapping',
|
|
138
|
+
layer: this
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
this.setState({ patternMapping });
|
|
142
|
+
this.getAttributeManager().invalidate('getFillPattern');
|
|
143
|
+
this.setNeedsUpdate();
|
|
144
|
+
}
|
|
145
|
+
getPatternFrame(name) {
|
|
146
|
+
const { patternMapping } = this.state;
|
|
147
|
+
const def = patternMapping && patternMapping[name];
|
|
148
|
+
return def ? [def.x, def.y, def.width, def.height] : [0, 0, 0, 0];
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
FillStyleExtension.extensionName = 'FillStyleExtension';
|
|
152
|
+
FillStyleExtension.defaultProps = defaultProps;
|