@deck.gl-community/layers 9.0.0-alpha.1 → 9.0.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.
Files changed (39) hide show
  1. package/dist/index.cjs +605 -0
  2. package/dist/index.cjs.map +7 -0
  3. package/dist/index.d.ts +6 -0
  4. package/dist/index.js +6 -5
  5. package/dist/path-marker-layer/arrow-2d-geometry.d.ts +4 -0
  6. package/dist/path-marker-layer/arrow-2d-geometry.js +58 -0
  7. package/dist/path-marker-layer/create-path-markers.d.ts +18 -0
  8. package/dist/path-marker-layer/create-path-markers.js +78 -0
  9. package/dist/path-marker-layer/path-marker-layer.d.ts +40 -0
  10. package/dist/path-marker-layer/path-marker-layer.js +124 -0
  11. package/dist/path-marker-layer/polyline.d.ts +18 -0
  12. package/dist/path-marker-layer/polyline.js +40 -0
  13. package/dist/path-outline-layer/outline.d.ts +8 -0
  14. package/dist/path-outline-layer/outline.js +100 -0
  15. package/dist/path-outline-layer/path-outline-layer.d.ts +34 -0
  16. package/dist/path-outline-layer/path-outline-layer.js +116 -0
  17. package/dist/tile-source-layer/tile-source-layer.d.ts +43 -0
  18. package/dist/tile-source-layer/tile-source-layer.js +109 -0
  19. package/package.json +27 -13
  20. package/src/index.ts +7 -4
  21. package/src/path-marker-layer/arrow-2d-geometry.ts +65 -0
  22. package/src/path-marker-layer/create-path-markers.ts +122 -0
  23. package/src/path-marker-layer/path-marker-layer.ts +183 -0
  24. package/src/path-marker-layer/polyline.ts +44 -0
  25. package/src/path-outline-layer/outline.ts +107 -0
  26. package/src/path-outline-layer/path-outline-layer.ts +159 -0
  27. package/src/{tile-source-layer.ts → tile-source-layer/tile-source-layer.ts} +30 -22
  28. package/dist/data-driven-tile-3d-layer/data-driven-tile-3d-layer.js +0 -193
  29. package/dist/data-driven-tile-3d-layer/data-driven-tile-3d-layer.js.map +0 -1
  30. package/dist/data-driven-tile-3d-layer/utils/colorize-tile.js +0 -31
  31. package/dist/data-driven-tile-3d-layer/utils/colorize-tile.js.map +0 -1
  32. package/dist/data-driven-tile-3d-layer/utils/filter-tile.js +0 -146
  33. package/dist/data-driven-tile-3d-layer/utils/filter-tile.js.map +0 -1
  34. package/dist/index.js.map +0 -1
  35. package/dist/tile-source-layer.js +0 -112
  36. package/dist/tile-source-layer.js.map +0 -1
  37. package/src/data-driven-tile-3d-layer/data-driven-tile-3d-layer.ts +0 -261
  38. package/src/data-driven-tile-3d-layer/utils/colorize-tile.ts +0 -53
  39. package/src/data-driven-tile-3d-layer/utils/filter-tile.ts +0 -179
@@ -0,0 +1,6 @@
1
+ export type { TileSourceLayerProps } from './tile-source-layer/tile-source-layer';
2
+ export { TileSourceLayer } from './tile-source-layer/tile-source-layer';
3
+ export type { PathOutlineLayerProps } from './path-outline-layer/path-outline-layer';
4
+ export { PathOutlineLayer } from './path-outline-layer/path-outline-layer';
5
+ export type { PathMarkerLayerProps } from './path-marker-layer/path-marker-layer';
6
+ export { PathMarkerLayer } from './path-marker-layer/path-marker-layer';
package/dist/index.js CHANGED
@@ -1,5 +1,6 @@
1
- export { TileSourceLayer } from "./tile-source-layer.js";
2
- export { DataDrivenTile3DLayer } from "./data-driven-tile-3d-layer/data-driven-tile-3d-layer.js";
3
- export { colorizeTile } from "./data-driven-tile-3d-layer/utils/colorize-tile.js";
4
- export { filterTile } from "./data-driven-tile-3d-layer/utils/filter-tile.js";
5
- //# sourceMappingURL=index.js.map
1
+ // deck.gl-community
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+ export { TileSourceLayer } from './tile-source-layer/tile-source-layer';
5
+ export { PathOutlineLayer } from './path-outline-layer/path-outline-layer';
6
+ export { PathMarkerLayer } from './path-marker-layer/path-marker-layer';
@@ -0,0 +1,4 @@
1
+ import { Geometry } from '@luma.gl/engine';
2
+ export declare class Arrow2DGeometry extends Geometry {
3
+ constructor(opts?: {});
4
+ }
@@ -0,0 +1,58 @@
1
+ // deck.gl-community
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+ import { Geometry } from '@luma.gl/engine';
5
+ export class Arrow2DGeometry extends Geometry {
6
+ constructor(opts = {}) {
7
+ super(Object.assign({}, opts, {
8
+ attributes: getArrowAttributes(opts),
9
+ topology: 'triangle-list'
10
+ }));
11
+ }
12
+ }
13
+ function getArrowAttributes({ length = 1, headSize = 0.2, tailWidth = 0.05, tailStart = 0.05 }) {
14
+ const texCoords = [
15
+ // HEAD
16
+ 0.5,
17
+ 1.0,
18
+ 0,
19
+ 0.5 - headSize / 2,
20
+ 1.0 - headSize,
21
+ 0,
22
+ 0.5 + headSize / 2,
23
+ 1.0 - headSize,
24
+ 0,
25
+ 0.5 - tailWidth / 2,
26
+ tailStart,
27
+ 0,
28
+ 0.5 + tailWidth / 2,
29
+ 1.0 - headSize,
30
+ 0,
31
+ 0.5 + tailWidth / 2,
32
+ tailStart,
33
+ 0,
34
+ 0.5 - tailWidth / 2,
35
+ tailStart,
36
+ 0,
37
+ 0.5 - tailWidth / 2,
38
+ 1.0 - headSize,
39
+ 0,
40
+ 0.5 + tailWidth / 2,
41
+ 1.0 - headSize,
42
+ 0
43
+ ];
44
+ const normals = [0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1];
45
+ // Center and scale
46
+ const positions = new Array(texCoords.length);
47
+ for (let i = 0; i < texCoords.length / 3; i++) {
48
+ const i3 = i * 3;
49
+ positions[i3 + 0] = (texCoords[i3 + 0] - 0.5) * length;
50
+ positions[i3 + 1] = (texCoords[i3 + 1] - 0.5) * length;
51
+ positions[i3 + 2] = 0;
52
+ }
53
+ return {
54
+ positions: { size: 3, value: new Float32Array(positions) },
55
+ normals: { size: 3, value: new Float32Array(normals) },
56
+ texCoords: { size: 2, value: new Float32Array(texCoords) }
57
+ };
58
+ }
@@ -0,0 +1,18 @@
1
+ /** GeoJSON style position coordinate vector */
2
+ export type Position = [number, number] | [number, number, number];
3
+ /** [red, green, blue, alpha] in premultiplied alpha format */
4
+ export type Color = [number, number, number, number];
5
+ export interface PathMarker {
6
+ position: Position;
7
+ angle: number;
8
+ color: Color;
9
+ object: unknown;
10
+ }
11
+ export declare function createPathMarkers({ data, getPath, getDirection, getColor, getMarkerPercentages, projectFlat }: {
12
+ data: any;
13
+ getPath?: (x: any, context: any) => any;
14
+ getDirection?: (x: any) => any;
15
+ getColor?: (x: any) => number[];
16
+ getMarkerPercentages?: (x: any, info: any) => number[];
17
+ projectFlat: any;
18
+ }): PathMarker[];
@@ -0,0 +1,78 @@
1
+ // deck.gl-community
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+ import { Vector2 } from '@math.gl/core';
5
+ function getLineLength(vPoints) {
6
+ // calculate total length
7
+ let lineLength = 0;
8
+ for (let i = 0; i < vPoints.length - 1; i++) {
9
+ lineLength += vPoints[i].distance(vPoints[i + 1]);
10
+ }
11
+ return lineLength;
12
+ }
13
+ const DEFAULT_COLOR = [0, 0, 0, 255];
14
+ const DEFAULT_DIRECTION = { forward: true, backward: false };
15
+ export function createPathMarkers({ data, getPath = (x, context) => x.path, getDirection = (x) => x.direction, getColor = (x) => DEFAULT_COLOR, getMarkerPercentages = (x, info) => [0.5], projectFlat }) {
16
+ const markers = [];
17
+ for (const object of data) {
18
+ const path = getPath(object, null);
19
+ const direction = getDirection(object) || DEFAULT_DIRECTION;
20
+ const color = getColor(object);
21
+ const vPoints = path.map((p) => new Vector2(p));
22
+ const vPointsReverse = vPoints.slice(0).reverse();
23
+ // calculate total length
24
+ const lineLength = getLineLength(vPoints);
25
+ // Ask for where to put markers
26
+ const percentages = getMarkerPercentages(object, { lineLength });
27
+ // Create the markers
28
+ for (const percentage of percentages) {
29
+ if (direction.forward) {
30
+ const marker = createMarkerAlongPath({
31
+ path: vPoints,
32
+ percentage,
33
+ lineLength,
34
+ color,
35
+ object,
36
+ projectFlat
37
+ });
38
+ markers.push(marker);
39
+ }
40
+ if (direction.backward) {
41
+ const marker = createMarkerAlongPath({
42
+ path: vPointsReverse,
43
+ percentage,
44
+ lineLength,
45
+ color,
46
+ object,
47
+ projectFlat
48
+ });
49
+ markers.push(marker);
50
+ }
51
+ }
52
+ }
53
+ return markers;
54
+ }
55
+ function createMarkerAlongPath({ path, percentage, lineLength, color, object, projectFlat }) {
56
+ const distanceAlong = lineLength * percentage;
57
+ let currentDistance = 0;
58
+ let previousDistance = 0;
59
+ let i = 0;
60
+ for (i = 0; i < path.length - 1; i++) {
61
+ currentDistance += path[i].distance(path[i + 1]);
62
+ if (currentDistance > distanceAlong) {
63
+ break;
64
+ }
65
+ previousDistance = currentDistance;
66
+ }
67
+ // If reached the end of the loop without exiting early,
68
+ // undo the final increment to avoid a null-pointer exception
69
+ if (i === path.length - 1) {
70
+ i -= 1;
71
+ }
72
+ const vDirection = path[i + 1].clone().subtract(path[i]).normalize();
73
+ const along = distanceAlong - previousDistance;
74
+ const vCenter = vDirection.clone().multiply(new Vector2(along, along)).add(path[i]);
75
+ const vDirection2 = new Vector2(projectFlat(path[i + 1])).subtract(projectFlat(path[i]));
76
+ const angle = (vDirection2.verticalAngle() * 180) / Math.PI;
77
+ return { position: [vCenter.x, vCenter.y, 0], angle, color, object };
78
+ }
@@ -0,0 +1,40 @@
1
+ import { CompositeLayer, DefaultProps } from '@deck.gl/core';
2
+ import { PathOutlineLayerProps } from '../path-outline-layer/path-outline-layer';
3
+ import { Arrow2DGeometry } from './arrow-2d-geometry';
4
+ import { Vector3 } from '@math.gl/core';
5
+ export type PathMarkerLayerProps<DataT> = PathOutlineLayerProps<DataT> & {
6
+ getDirection?: (x: any) => any;
7
+ getMarkerColor?: (x: any) => number[];
8
+ getMarkerPercentages?: (x: any, info: any) => number[];
9
+ highlightPoint?: any;
10
+ highlightIndex?: number;
11
+ MarkerLayer?: any;
12
+ markerLayerProps?: any;
13
+ sizeScale?: number;
14
+ fp64?: boolean;
15
+ nebulaLayer?: any;
16
+ };
17
+ export declare class PathMarkerLayer<DataT = any, ExtraPropsT = Record<string, unknown>> extends CompositeLayer<ExtraPropsT & Required<PathMarkerLayerProps<DataT>>> {
18
+ static layerName: string;
19
+ static defaultProps: DefaultProps<PathMarkerLayerProps<any>>;
20
+ state: {
21
+ closestPoint: Vector3 | null;
22
+ closestPoints?: {
23
+ position: Vector3;
24
+ }[];
25
+ markers: any[];
26
+ mesh: Arrow2DGeometry;
27
+ };
28
+ initializeState(): void;
29
+ projectFlat(xyz: any, viewport: any, coordinateSystem: any, coordinateOrigin: any): any;
30
+ updateState({ props, oldProps, changeFlags }: {
31
+ props: any;
32
+ oldProps: any;
33
+ changeFlags: any;
34
+ }): void;
35
+ _recalculateClosestPoint(): void;
36
+ getPickingInfo({ info }: {
37
+ info: any;
38
+ }): any;
39
+ renderLayers(): any[];
40
+ }
@@ -0,0 +1,124 @@
1
+ // deck.gl-community
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+ import { CompositeLayer, COORDINATE_SYSTEM } from '@deck.gl/core';
5
+ import { ScatterplotLayer } from '@deck.gl/layers';
6
+ import { SimpleMeshLayer } from '@deck.gl/mesh-layers';
7
+ import { PathOutlineLayer } from '../path-outline-layer/path-outline-layer';
8
+ import { Arrow2DGeometry } from './arrow-2d-geometry';
9
+ import { createPathMarkers } from './create-path-markers';
10
+ import { getClosestPointOnPolyline } from './polyline';
11
+ const DISTANCE_FOR_MULTI_ARROWS = 0.1;
12
+ const ARROW_HEAD_SIZE = 0.2;
13
+ const ARROW_TAIL_WIDTH = 0.05;
14
+ // const ARROW_CENTER_ADJUST = -0.8;
15
+ const DEFAULT_MARKER_LAYER = SimpleMeshLayer;
16
+ const DEFAULT_MARKER_LAYER_PROPS = {
17
+ mesh: new Arrow2DGeometry({ headSize: ARROW_HEAD_SIZE, tailWidth: ARROW_TAIL_WIDTH })
18
+ };
19
+ const defaultProps = Object.assign({}, PathOutlineLayer.defaultProps, {
20
+ MarkerLayer: DEFAULT_MARKER_LAYER,
21
+ markerLayerProps: DEFAULT_MARKER_LAYER_PROPS,
22
+ sizeScale: 100,
23
+ fp64: false,
24
+ highlightIndex: -1,
25
+ highlightPoint: null,
26
+ getPath: (x) => x.path,
27
+ getColor: (x) => x.color,
28
+ getMarkerColor: (x) => [0, 0, 0, 255],
29
+ getDirection: (x) => x.direction,
30
+ getMarkerPercentages: (object, { lineLength }) => lineLength > DISTANCE_FOR_MULTI_ARROWS ? [0.25, 0.5, 0.75] : [0.5]
31
+ });
32
+ export class PathMarkerLayer extends CompositeLayer {
33
+ static layerName = 'PathMarkerLayer';
34
+ static defaultProps = defaultProps;
35
+ state = undefined;
36
+ initializeState() {
37
+ this.state = {
38
+ markers: [],
39
+ mesh: new Arrow2DGeometry({ headSize: ARROW_HEAD_SIZE, tailWidth: ARROW_TAIL_WIDTH }),
40
+ closestPoint: null,
41
+ closestPoints: []
42
+ };
43
+ }
44
+ projectFlat(xyz, viewport, coordinateSystem, coordinateOrigin) {
45
+ if (coordinateSystem === COORDINATE_SYSTEM.METER_OFFSETS) {
46
+ const [dx, dy] = viewport.metersToLngLatDelta(xyz);
47
+ const [x, y] = coordinateOrigin;
48
+ return viewport.projectFlat([x + dx, dy + y]);
49
+ }
50
+ else if (coordinateSystem === COORDINATE_SYSTEM.LNGLAT_OFFSETS) {
51
+ const [dx, dy] = xyz;
52
+ const [x, y] = coordinateOrigin;
53
+ return viewport.projectFlat([x + dx, dy + y]);
54
+ }
55
+ return viewport.projectFlat(xyz);
56
+ }
57
+ updateState({ props, oldProps, changeFlags }) {
58
+ if (changeFlags.dataChanged || changeFlags.updateTriggersChanged) {
59
+ const { data, getPath, getDirection, getMarkerColor, getMarkerPercentages, coordinateSystem, coordinateOrigin } = this.props;
60
+ const { viewport } = this.context;
61
+ const projectFlat = (o) => this.projectFlat(o, viewport, coordinateSystem, coordinateOrigin);
62
+ this.state.markers = createPathMarkers({
63
+ data,
64
+ getPath,
65
+ getDirection,
66
+ getColor: getMarkerColor,
67
+ getMarkerPercentages,
68
+ projectFlat
69
+ });
70
+ this._recalculateClosestPoint();
71
+ }
72
+ if (changeFlags.propsChanged) {
73
+ if (props.point !== oldProps.point) {
74
+ this._recalculateClosestPoint();
75
+ }
76
+ }
77
+ }
78
+ _recalculateClosestPoint() {
79
+ const { highlightPoint, highlightIndex } = this.props;
80
+ if (highlightPoint && highlightIndex >= 0) {
81
+ const object = this.props.data[highlightIndex];
82
+ const points = this.props.getPath(object, null);
83
+ const { point } = getClosestPointOnPolyline({ points, p: highlightPoint });
84
+ this.state.closestPoints = [{ position: point }];
85
+ }
86
+ else {
87
+ this.state.closestPoints = [];
88
+ }
89
+ }
90
+ getPickingInfo({ info }) {
91
+ return Object.assign(info, {
92
+ // override object with picked feature
93
+ object: (info.object && info.object.path) || info.object
94
+ });
95
+ }
96
+ renderLayers() {
97
+ return [
98
+ new PathOutlineLayer(this.props, this.getSubLayerProps({
99
+ id: 'paths',
100
+ // Note: data has to be passed explicitly like this to avoid being empty
101
+ data: this.props.data
102
+ })),
103
+ new this.props.MarkerLayer(this.getSubLayerProps(Object.assign({}, this.props.markerLayerProps, {
104
+ id: 'markers',
105
+ data: this.state.markers,
106
+ getOrientation: (x) => [0, -x.angle, 0],
107
+ getColor: (x) => x.color,
108
+ sizeScale: this.props.sizeScale,
109
+ fp64: this.props.fp64,
110
+ pickable: false,
111
+ parameters: {
112
+ blend: false,
113
+ depthTest: false
114
+ }
115
+ }))),
116
+ this.state.closestPoints &&
117
+ new ScatterplotLayer({
118
+ id: `${this.props.id}-highlight`,
119
+ data: this.state.closestPoints,
120
+ fp64: this.props.fp64
121
+ })
122
+ ];
123
+ }
124
+ }
@@ -0,0 +1,18 @@
1
+ import { Vector3 } from '@math.gl/core';
2
+ export declare function getClosestPointOnLine({ p, p1, p2, clampToLine }: {
3
+ p: any;
4
+ p1: any;
5
+ p2: any;
6
+ clampToLine?: boolean;
7
+ }): Vector3;
8
+ export declare function getClosestPointOnPolyline({ p, points }: {
9
+ p: any;
10
+ points: any;
11
+ }): {
12
+ point: Vector3;
13
+ index: number;
14
+ p1: any;
15
+ p2: any;
16
+ distanceSquared: number;
17
+ distance: number;
18
+ };
@@ -0,0 +1,40 @@
1
+ // deck.gl-community
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+ import { Vector3, clamp } from '@math.gl/core';
5
+ // Return the closest point on a line segment
6
+ export function getClosestPointOnLine({ p, p1, p2, clampToLine = true }) {
7
+ const lineVector = new Vector3(p2).subtract(p1);
8
+ const pointVector = new Vector3(p).subtract(p1);
9
+ let dotProduct = lineVector.dot(pointVector);
10
+ if (clampToLine) {
11
+ dotProduct = clamp(dotProduct, 0, 1);
12
+ }
13
+ return lineVector.lerp(p1, p2, dotProduct);
14
+ }
15
+ // Return the closest point on a line segment
16
+ export function getClosestPointOnPolyline({ p, points }) {
17
+ p = new Vector3(p);
18
+ let pClosest = null;
19
+ let distanceSquared = Infinity;
20
+ let index = -1;
21
+ for (let i = 0; i < points.length - 1; ++i) {
22
+ const p1 = points[i];
23
+ const p2 = points[i + 1];
24
+ const pClosestOnLine = getClosestPointOnLine({ p, p1, p2 });
25
+ const distanceToLineSquared = p.distanceSquared(pClosestOnLine);
26
+ if (distanceToLineSquared < distanceSquared) {
27
+ distanceSquared = distanceToLineSquared;
28
+ pClosest = pClosestOnLine;
29
+ index = i;
30
+ }
31
+ }
32
+ return {
33
+ point: pClosest,
34
+ index,
35
+ p1: points[index],
36
+ p2: points[index + 1],
37
+ distanceSquared,
38
+ distance: Math.sqrt(distanceSquared)
39
+ };
40
+ }
@@ -0,0 +1,8 @@
1
+ declare function getUniforms({ outlineEnabled, outlineRenderShadowmap, outlineShadowmap }?: Record<string, any>): Record<string, any>;
2
+ export declare const outline: {
3
+ readonly name: "outline";
4
+ readonly vs: "#version 300 es\nin float instanceZLevel;\nout float outline_vzLevel;\nout vec4 outline_vPosition;\n\n// Set the z level for the outline shadowmap rendering\nvoid outline_setZLevel(float zLevel) {\n outline_vzLevel = zLevel;\n}\n\n// Store an adjusted position for texture2DProj\nvoid outline_setUV(vec4 position) {\n // mat4(\n // 0.5, 0.0, 0.0, 0.0,\n // 0.0, 0.5, 0.0, 0.0,\n // 0.0, 0.0, 0.5, 0.0,\n // 0.5, 0.5, 0.5, 1.0\n // ) * position;\n outline_vPosition = vec4(position.xyz * 0.5 + position.w * 0.5, position.w);\n}\n";
5
+ readonly fs: "uniform bool outline_uEnabled;\nuniform bool outline_uRenderOutlines;\nuniform sampler2D outline_uShadowmap;\n\nin float outline_vzLevel;\n// in vec2 outline_vUV;\nin vec4 outline_vPosition;\n\nout vec4 fragColor;\n\nconst float OUTLINE_Z_LEVEL_ERROR = 0.01;\n\n// Return a darker color in shadowmap\nvec4 outline_filterShadowColor(vec4 color) {\n return vec4(outline_vzLevel / 255., outline_vzLevel / 255., outline_vzLevel / 255., 1.);\n}\n\n// Return a darker color if in shadowmap\nvec4 outline_filterDarkenColor(vec4 color) {\n if (outline_uEnabled) {\n float maxZLevel;\n if (outline_vPosition.q > 0.0) {\n maxZLevel = texture2DProj(outline_uShadowmap, outline_vPosition).r * 255.;\n } else {\n discard;\n }\n if (maxZLevel < outline_vzLevel + OUTLINE_Z_LEVEL_ERROR) {\n vec4(color.rgb * 0.5, color.a);\n } else {\n discard;\n }\n }\n return color;\n}\n\n// if enabled and rendering outlines - Render depth to shadowmap\n// if enabled and rendering colors - Return a darker color if in shadowmap\n// if disabled, just return color\nvec4 outline_filterColor(vec4 color) {\n if (outline_uEnabled) {\n return outline_uRenderOutlines ?\n outline_filterShadowColor(color) :\n outline_filterDarkenColor(color);\n }\n return color;\n}\n";
6
+ readonly getUniforms: typeof getUniforms;
7
+ };
8
+ export {};
@@ -0,0 +1,100 @@
1
+ // deck.gl-community
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+ /* eslint-disable camelcase */
5
+ const INITIAL_STATE = {
6
+ outlineEnabled: false,
7
+ outlineRenderShadowmap: false,
8
+ outlineShadowmap: null
9
+ };
10
+ function getUniforms({ outlineEnabled, outlineRenderShadowmap, outlineShadowmap } = INITIAL_STATE) {
11
+ const uniforms = {};
12
+ if (outlineEnabled !== undefined) {
13
+ // ? 1.0 : 0.0;
14
+ uniforms.outline_uEnabled = outlineEnabled;
15
+ }
16
+ if (outlineRenderShadowmap !== undefined) {
17
+ // ? 1.0 : 0.0;
18
+ uniforms.outline_uRenderOutlines = outlineRenderShadowmap;
19
+ }
20
+ if (outlineShadowmap !== undefined) {
21
+ uniforms.outline_uShadowmap = outlineShadowmap;
22
+ }
23
+ return uniforms;
24
+ }
25
+ const vs = `\
26
+ #version 300 es
27
+ in float instanceZLevel;
28
+ out float outline_vzLevel;
29
+ out vec4 outline_vPosition;
30
+
31
+ // Set the z level for the outline shadowmap rendering
32
+ void outline_setZLevel(float zLevel) {
33
+ outline_vzLevel = zLevel;
34
+ }
35
+
36
+ // Store an adjusted position for texture2DProj
37
+ void outline_setUV(vec4 position) {
38
+ // mat4(
39
+ // 0.5, 0.0, 0.0, 0.0,
40
+ // 0.0, 0.5, 0.0, 0.0,
41
+ // 0.0, 0.0, 0.5, 0.0,
42
+ // 0.5, 0.5, 0.5, 1.0
43
+ // ) * position;
44
+ outline_vPosition = vec4(position.xyz * 0.5 + position.w * 0.5, position.w);
45
+ }
46
+ `;
47
+ const fs = `\
48
+ uniform bool outline_uEnabled;
49
+ uniform bool outline_uRenderOutlines;
50
+ uniform sampler2D outline_uShadowmap;
51
+
52
+ in float outline_vzLevel;
53
+ // in vec2 outline_vUV;
54
+ in vec4 outline_vPosition;
55
+
56
+ out vec4 fragColor;
57
+
58
+ const float OUTLINE_Z_LEVEL_ERROR = 0.01;
59
+
60
+ // Return a darker color in shadowmap
61
+ vec4 outline_filterShadowColor(vec4 color) {
62
+ return vec4(outline_vzLevel / 255., outline_vzLevel / 255., outline_vzLevel / 255., 1.);
63
+ }
64
+
65
+ // Return a darker color if in shadowmap
66
+ vec4 outline_filterDarkenColor(vec4 color) {
67
+ if (outline_uEnabled) {
68
+ float maxZLevel;
69
+ if (outline_vPosition.q > 0.0) {
70
+ maxZLevel = texture2DProj(outline_uShadowmap, outline_vPosition).r * 255.;
71
+ } else {
72
+ discard;
73
+ }
74
+ if (maxZLevel < outline_vzLevel + OUTLINE_Z_LEVEL_ERROR) {
75
+ vec4(color.rgb * 0.5, color.a);
76
+ } else {
77
+ discard;
78
+ }
79
+ }
80
+ return color;
81
+ }
82
+
83
+ // if enabled and rendering outlines - Render depth to shadowmap
84
+ // if enabled and rendering colors - Return a darker color if in shadowmap
85
+ // if disabled, just return color
86
+ vec4 outline_filterColor(vec4 color) {
87
+ if (outline_uEnabled) {
88
+ return outline_uRenderOutlines ?
89
+ outline_filterShadowColor(color) :
90
+ outline_filterDarkenColor(color);
91
+ }
92
+ return color;
93
+ }
94
+ `;
95
+ export const outline = {
96
+ name: 'outline',
97
+ vs,
98
+ fs,
99
+ getUniforms
100
+ };
@@ -0,0 +1,34 @@
1
+ import { PathLayer, PathLayerProps } from '@deck.gl/layers';
2
+ import type { DefaultProps, LayerContext } from '@deck.gl/core';
3
+ import { Framebuffer, Texture } from '@luma.gl/core';
4
+ /**
5
+ * Unit literal to shader unit number conversion.
6
+ */
7
+ export declare const UNIT: {
8
+ common: number;
9
+ meters: number;
10
+ pixels: number;
11
+ };
12
+ export type PathOutlineLayerProps<DataT> = PathLayerProps<DataT> & {
13
+ dashJustified?: boolean;
14
+ getDashArray?: [number, number] | ((d: DataT) => [number, number] | null);
15
+ getZLevel?: (d: DataT, index: number) => number;
16
+ };
17
+ export declare class PathOutlineLayer<DataT = any, ExtraPropsT = Record<string, unknown>> extends PathLayer<DataT, ExtraPropsT & Required<PathOutlineLayerProps<DataT>>> {
18
+ static layerName: string;
19
+ static defaultProps: DefaultProps<PathOutlineLayerProps<any>>;
20
+ state: {
21
+ model?: any;
22
+ pathTesselator: any;
23
+ outlineFramebuffer: Framebuffer;
24
+ dummyTexture: Texture;
25
+ };
26
+ getShaders(): any;
27
+ initializeState(context: LayerContext): void;
28
+ draw({ moduleParameters, parameters, uniforms, context }: {
29
+ moduleParameters?: {};
30
+ parameters: any;
31
+ uniforms: any;
32
+ context: any;
33
+ }): void;
34
+ }
@@ -0,0 +1,116 @@
1
+ // deck.gl-community
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+ import { PathLayer } from '@deck.gl/layers';
5
+ import { GL } from '@luma.gl/constants';
6
+ import { outline } from './outline';
7
+ /**
8
+ * Unit literal to shader unit number conversion.
9
+ */
10
+ export const UNIT = {
11
+ common: 0,
12
+ meters: 1,
13
+ pixels: 2
14
+ };
15
+ // TODO - this should be built into assembleShaders
16
+ function injectShaderCode({ source, code = '' }) {
17
+ const INJECT_CODE = /}[^{}]*$/;
18
+ return source.replace(INJECT_CODE, code.concat('\n}\n'));
19
+ }
20
+ const VS_CODE = `\
21
+ outline_setUV(gl_Position);
22
+ outline_setZLevel(instanceZLevel);
23
+ `;
24
+ const FS_CODE = `\
25
+ fragColor = outline_filterColor(fragColor);
26
+ `;
27
+ const defaultProps = {
28
+ getZLevel: () => 0
29
+ };
30
+ export class PathOutlineLayer extends PathLayer {
31
+ static layerName = 'PathOutlineLayer';
32
+ static defaultProps = defaultProps;
33
+ state = undefined;
34
+ // Override getShaders to inject the outline module
35
+ getShaders() {
36
+ const shaders = super.getShaders();
37
+ return Object.assign({}, shaders, {
38
+ modules: shaders.modules.concat([outline]),
39
+ vs: injectShaderCode({ source: shaders.vs, code: VS_CODE }),
40
+ fs: injectShaderCode({ source: shaders.fs, code: FS_CODE })
41
+ });
42
+ }
43
+ // @ts-expect-error PathLayer is missing LayerContext arg
44
+ initializeState(context) {
45
+ super.initializeState();
46
+ // Create an outline "shadow" map
47
+ // TODO - we should create a single outlineMap for all layers
48
+ this.setState({
49
+ outlineFramebuffer: context.device.createFramebuffer({}),
50
+ dummyTexture: context.device.createTexture({})
51
+ });
52
+ // Create an attribute manager
53
+ // @ts-expect-error check whether this.getAttributeManager works here
54
+ this.state.attributeManager.addInstanced({
55
+ instanceZLevel: {
56
+ size: 1,
57
+ type: GL.UNSIGNED_BYTE,
58
+ accessor: 'getZLevel'
59
+ }
60
+ });
61
+ }
62
+ // Override draw to add render module
63
+ draw({ moduleParameters = {}, parameters, uniforms, context }) {
64
+ // Need to calculate same uniforms as base layer
65
+ const { jointRounded, capRounded, billboard, miterLimit, widthUnits, widthScale, widthMinPixels, widthMaxPixels } = this.props;
66
+ uniforms = Object.assign({}, uniforms, {
67
+ jointType: Number(jointRounded),
68
+ capType: Number(capRounded),
69
+ billboard,
70
+ widthUnits: UNIT[widthUnits],
71
+ widthScale,
72
+ miterLimit,
73
+ widthMinPixels,
74
+ widthMaxPixels
75
+ });
76
+ // Render the outline shadowmap (based on segment z orders)
77
+ const { outlineFramebuffer, dummyTexture } = this.state;
78
+ // TODO(v9): resize, see 'sf' example.
79
+ // outlineFramebuffer.resize();
80
+ // TODO(v9) clear FBO
81
+ // outlineFramebuffer.clear({ color: true, depth: true, stencil: true });
82
+ this.state.model.updateModuleSettings({
83
+ outlineEnabled: true,
84
+ outlineRenderShadowmap: true,
85
+ outlineShadowmap: dummyTexture
86
+ });
87
+ this.state.model.draw({
88
+ uniforms: Object.assign({}, uniforms, {
89
+ jointType: 0,
90
+ widthScale: this.props.widthScale * 1.3
91
+ }),
92
+ parameters: {
93
+ depthTest: false,
94
+ // Biggest value needs to go into buffer
95
+ blendEquation: GL.MAX
96
+ },
97
+ framebuffer: outlineFramebuffer
98
+ });
99
+ // Now use the outline shadowmap to render the lines (with outlines)
100
+ this.state.model.updateModuleSettings({
101
+ outlineEnabled: true,
102
+ outlineRenderShadowmap: false,
103
+ outlineShadowmap: outlineFramebuffer
104
+ });
105
+ this.state.model.draw({
106
+ uniforms: Object.assign({}, uniforms, {
107
+ jointType: Number(jointRounded),
108
+ capType: Number(capRounded),
109
+ widthScale: this.props.widthScale
110
+ }),
111
+ parameters: {
112
+ depthTest: false
113
+ }
114
+ });
115
+ }
116
+ }