@deck.gl/extensions 8.9.0-alpha.4 → 8.9.0-beta.1

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 (201) hide show
  1. package/dist/dist.dev.js +3688 -14538
  2. package/dist/es5/brushing/brushing.js +0 -8
  3. package/dist/es5/brushing/brushing.js.map +1 -1
  4. package/dist/es5/clip/clip.js +5 -5
  5. package/dist/es5/clip/clip.js.map +1 -1
  6. package/dist/es5/collide/collide-effect.js +285 -0
  7. package/dist/es5/collide/collide-effect.js.map +1 -0
  8. package/dist/es5/collide/collide-pass.js +80 -0
  9. package/dist/es5/collide/collide-pass.js.map +1 -0
  10. package/dist/es5/collide/collide.js +116 -0
  11. package/dist/es5/collide/collide.js.map +1 -0
  12. package/dist/es5/collide/shader-module.js +39 -0
  13. package/dist/es5/collide/shader-module.js.map +1 -0
  14. package/dist/es5/common/projection-utils.js +164 -0
  15. package/dist/es5/common/projection-utils.js.map +1 -0
  16. package/dist/es5/data-filter/shader-module.js +1 -0
  17. package/dist/es5/data-filter/shader-module.js.map +1 -1
  18. package/dist/es5/fill-style/fill-style.js +16 -125
  19. package/dist/es5/fill-style/fill-style.js.map +1 -1
  20. package/dist/es5/fill-style/shaders.glsl.js +6 -8
  21. package/dist/es5/fill-style/shaders.glsl.js.map +1 -1
  22. package/dist/es5/fp64/project64.glsl.js +1 -1
  23. package/dist/es5/index.js +16 -0
  24. package/dist/es5/index.js.map +1 -1
  25. package/dist/es5/mask/mask-effect.js +280 -0
  26. package/dist/es5/mask/mask-effect.js.map +1 -0
  27. package/dist/es5/mask/mask-pass.js +106 -0
  28. package/dist/es5/mask/mask-pass.js.map +1 -0
  29. package/dist/es5/mask/mask.js +17 -4
  30. package/dist/es5/mask/mask.js.map +1 -1
  31. package/dist/es5/mask/shader-module.js +1 -1
  32. package/dist/es5/mask/shader-module.js.map +1 -1
  33. package/dist/es5/mask/utils.js +136 -0
  34. package/dist/es5/mask/utils.js.map +1 -0
  35. package/dist/es5/path-style/shaders.glsl.js +1 -1
  36. package/dist/es5/terrain/height-map-builder.js +116 -0
  37. package/dist/es5/terrain/height-map-builder.js.map +1 -0
  38. package/dist/es5/terrain/shader-module.js +84 -0
  39. package/dist/es5/terrain/shader-module.js.map +1 -0
  40. package/dist/es5/terrain/terrain-cover.js +259 -0
  41. package/dist/es5/terrain/terrain-cover.js.map +1 -0
  42. package/dist/es5/terrain/terrain-effect.js +337 -0
  43. package/dist/es5/terrain/terrain-effect.js.map +1 -0
  44. package/dist/es5/terrain/terrain-pass.js +122 -0
  45. package/dist/es5/terrain/terrain-pass.js.map +1 -0
  46. package/dist/es5/terrain/terrain-picking-pass.js +130 -0
  47. package/dist/es5/terrain/terrain-picking-pass.js.map +1 -0
  48. package/dist/es5/terrain/terrain.js +101 -0
  49. package/dist/es5/terrain/terrain.js.map +1 -0
  50. package/dist/es5/terrain/utils.js +32 -0
  51. package/dist/es5/terrain/utils.js.map +1 -0
  52. package/dist/es5/utils/debug.js +71 -0
  53. package/dist/es5/utils/debug.js.map +1 -0
  54. package/dist/esm/brushing/brushing.js +0 -7
  55. package/dist/esm/brushing/brushing.js.map +1 -1
  56. package/dist/esm/clip/clip.js +5 -4
  57. package/dist/esm/clip/clip.js.map +1 -1
  58. package/dist/esm/collide/collide-effect.js +250 -0
  59. package/dist/esm/collide/collide-effect.js.map +1 -0
  60. package/dist/esm/collide/collide-pass.js +30 -0
  61. package/dist/esm/collide/collide-pass.js.map +1 -0
  62. package/dist/esm/collide/collide.js +78 -0
  63. package/dist/esm/collide/collide.js.map +1 -0
  64. package/dist/esm/collide/shader-module.js +32 -0
  65. package/dist/esm/collide/shader-module.js.map +1 -0
  66. package/dist/esm/common/projection-utils.js +130 -0
  67. package/dist/esm/common/projection-utils.js.map +1 -0
  68. package/dist/esm/data-filter/shader-module.js +1 -0
  69. package/dist/esm/data-filter/shader-module.js.map +1 -1
  70. package/dist/esm/fill-style/fill-style.js +20 -72
  71. package/dist/esm/fill-style/fill-style.js.map +1 -1
  72. package/dist/esm/fill-style/shaders.glsl.js +5 -7
  73. package/dist/esm/fill-style/shaders.glsl.js.map +1 -1
  74. package/dist/esm/fp64/project64.glsl.js +1 -1
  75. package/dist/esm/index.js +2 -0
  76. package/dist/esm/index.js.map +1 -1
  77. package/dist/esm/mask/mask-effect.js +233 -0
  78. package/dist/esm/mask/mask-effect.js.map +1 -0
  79. package/dist/esm/mask/mask-pass.js +62 -0
  80. package/dist/esm/mask/mask-pass.js.map +1 -0
  81. package/dist/esm/mask/mask.js +14 -3
  82. package/dist/esm/mask/mask.js.map +1 -1
  83. package/dist/esm/mask/shader-module.js +1 -1
  84. package/dist/esm/mask/shader-module.js.map +1 -1
  85. package/dist/esm/mask/utils.js +109 -0
  86. package/dist/esm/mask/utils.js.map +1 -0
  87. package/dist/esm/path-style/shaders.glsl.js +1 -1
  88. package/dist/esm/terrain/height-map-builder.js +92 -0
  89. package/dist/esm/terrain/height-map-builder.js.map +1 -0
  90. package/dist/esm/terrain/shader-module.js +73 -0
  91. package/dist/esm/terrain/shader-module.js.map +1 -0
  92. package/dist/esm/terrain/terrain-cover.js +219 -0
  93. package/dist/esm/terrain/terrain-cover.js.map +1 -0
  94. package/dist/esm/terrain/terrain-effect.js +231 -0
  95. package/dist/esm/terrain/terrain-effect.js.map +1 -0
  96. package/dist/esm/terrain/terrain-pass.js +69 -0
  97. package/dist/esm/terrain/terrain-pass.js.map +1 -0
  98. package/dist/esm/terrain/terrain-picking-pass.js +75 -0
  99. package/dist/esm/terrain/terrain-picking-pass.js.map +1 -0
  100. package/dist/esm/terrain/terrain.js +62 -0
  101. package/dist/esm/terrain/terrain.js.map +1 -0
  102. package/dist/esm/terrain/utils.js +21 -0
  103. package/dist/esm/terrain/utils.js.map +1 -0
  104. package/dist/esm/utils/debug.js +61 -0
  105. package/dist/esm/utils/debug.js.map +1 -0
  106. package/dist.min.js +1359 -1
  107. package/package.json +6 -4
  108. package/src/brushing/brushing.ts +0 -11
  109. package/src/clip/clip.ts +6 -5
  110. package/src/collide/collide-effect.ts +257 -0
  111. package/src/collide/collide-pass.ts +35 -0
  112. package/src/collide/collide.ts +79 -0
  113. package/src/collide/shader-module.ts +113 -0
  114. package/src/common/projection-utils.ts +187 -0
  115. package/src/data-filter/shader-module.ts +6 -0
  116. package/src/fill-style/fill-style.ts +15 -53
  117. package/src/fill-style/shaders.glsl.ts +6 -7
  118. package/src/index.ts +4 -0
  119. package/src/mask/mask-effect.ts +255 -0
  120. package/src/mask/mask-pass.ts +67 -0
  121. package/src/mask/mask.ts +17 -7
  122. package/src/mask/shader-module.ts +7 -1
  123. package/src/mask/utils.ts +148 -0
  124. package/src/terrain/height-map-builder.ts +120 -0
  125. package/src/terrain/shader-module.ts +161 -0
  126. package/src/terrain/terrain-cover.ts +225 -0
  127. package/src/terrain/terrain-effect.ts +223 -0
  128. package/src/terrain/terrain-pass.ts +86 -0
  129. package/src/terrain/terrain-picking-pass.ts +87 -0
  130. package/src/terrain/terrain.ts +74 -0
  131. package/src/terrain/utils.ts +29 -0
  132. package/src/utils/debug.ts +59 -0
  133. package/typed/brushing/brushing.d.ts +0 -1
  134. package/typed/brushing/brushing.d.ts.map +1 -1
  135. package/typed/brushing/brushing.js +83 -0
  136. package/typed/brushing/shader-module.js +116 -0
  137. package/typed/clip/clip.d.ts +1 -1
  138. package/typed/clip/clip.d.ts.map +1 -1
  139. package/typed/clip/clip.js +114 -0
  140. package/typed/collide/collide-effect.d.ts +28 -0
  141. package/typed/collide/collide-effect.d.ts.map +1 -0
  142. package/typed/collide/collide-pass.d.ts +14 -0
  143. package/typed/collide/collide-pass.d.ts.map +1 -0
  144. package/typed/collide/collide.d.ts +41 -0
  145. package/typed/collide/collide.d.ts.map +1 -0
  146. package/typed/collide/shader-module.d.ts +4 -0
  147. package/typed/collide/shader-module.d.ts.map +1 -0
  148. package/typed/common/projection-utils.d.ts +27 -0
  149. package/typed/common/projection-utils.d.ts.map +1 -0
  150. package/typed/data-filter/aggregator.js +98 -0
  151. package/typed/data-filter/data-filter.js +159 -0
  152. package/typed/data-filter/shader-module.d.ts.map +1 -1
  153. package/typed/data-filter/shader-module.js +152 -0
  154. package/typed/fill-style/fill-style.d.ts +14 -5
  155. package/typed/fill-style/fill-style.d.ts.map +1 -1
  156. package/typed/fill-style/fill-style.js +152 -0
  157. package/typed/fill-style/shaders.glsl.d.ts.map +1 -1
  158. package/typed/fill-style/shaders.glsl.js +95 -0
  159. package/typed/fp64/fp64.js +34 -0
  160. package/typed/fp64/project64.glsl.js +100 -0
  161. package/typed/fp64/project64.js +50 -0
  162. package/typed/index.d.ts +4 -0
  163. package/typed/index.d.ts.map +1 -1
  164. package/typed/index.js +9 -0
  165. package/typed/mask/mask-effect.d.ts +42 -0
  166. package/typed/mask/mask-effect.d.ts.map +1 -0
  167. package/typed/mask/mask-pass.d.ts +19 -0
  168. package/typed/mask/mask-pass.d.ts.map +1 -0
  169. package/typed/mask/mask.d.ts +9 -3
  170. package/typed/mask/mask.d.ts.map +1 -1
  171. package/typed/mask/mask.js +48 -0
  172. package/typed/mask/shader-module.d.ts.map +1 -1
  173. package/typed/mask/shader-module.js +73 -0
  174. package/typed/mask/utils.d.ts +13 -0
  175. package/typed/mask/utils.d.ts.map +1 -0
  176. package/typed/path-style/path-style.js +107 -0
  177. package/typed/path-style/shaders.glsl.js +93 -0
  178. package/typed/terrain/height-map-builder.d.ts +38 -0
  179. package/typed/terrain/height-map-builder.d.ts.map +1 -0
  180. package/typed/terrain/height-map.d.ts +27 -0
  181. package/typed/terrain/height-map.d.ts.map +1 -0
  182. package/typed/terrain/shader-module-offset.d.ts +4 -0
  183. package/typed/terrain/shader-module-offset.d.ts.map +1 -0
  184. package/typed/terrain/shader-module-source.d.ts +4 -0
  185. package/typed/terrain/shader-module-source.d.ts.map +1 -0
  186. package/typed/terrain/shader-module.d.ts +31 -0
  187. package/typed/terrain/shader-module.d.ts.map +1 -0
  188. package/typed/terrain/terrain-cover.d.ts +46 -0
  189. package/typed/terrain/terrain-cover.d.ts.map +1 -0
  190. package/typed/terrain/terrain-effect.d.ts +29 -0
  191. package/typed/terrain/terrain-effect.d.ts.map +1 -0
  192. package/typed/terrain/terrain-pass.d.ts +11 -0
  193. package/typed/terrain/terrain-pass.d.ts.map +1 -0
  194. package/typed/terrain/terrain-picking-pass.d.ts +19 -0
  195. package/typed/terrain/terrain-picking-pass.d.ts.map +1 -0
  196. package/typed/terrain/terrain.d.ts +21 -0
  197. package/typed/terrain/terrain.d.ts.map +1 -0
  198. package/typed/terrain/utils.d.ts +5 -0
  199. package/typed/terrain/utils.d.ts.map +1 -0
  200. package/typed/utils/debug.d.ts +6 -0
  201. package/typed/utils/debug.d.ts.map +1 -0
package/src/mask/mask.ts CHANGED
@@ -1,10 +1,11 @@
1
- import {COORDINATE_SYSTEM, LayerExtension, log} from '@deck.gl/core';
1
+ import {COORDINATE_SYSTEM, Layer, LayerExtension, log} from '@deck.gl/core';
2
2
  import mask from './shader-module';
3
-
4
- import type {Layer} from '@deck.gl/core';
3
+ import MaskEffect from './mask-effect';
5
4
 
6
5
  const defaultProps = {
7
- maskId: ''
6
+ maskId: '',
7
+ maskByInstance: undefined,
8
+ maskInverted: false
8
9
  };
9
10
 
10
11
  export type MaskExtensionProps = {
@@ -18,6 +19,10 @@ export type MaskExtensionProps = {
18
19
  * If not specified, it is automatically deduced from the layer.
19
20
  */
20
21
  maskByInstance?: boolean;
22
+ /**
23
+ * Inverts the masking operation
24
+ */
25
+ maskInverted?: boolean;
21
26
  };
22
27
 
23
28
  /** Allows layers to show/hide objects by a geofence. */
@@ -25,11 +30,15 @@ export default class MaskExtension extends LayerExtension {
25
30
  static defaultProps = defaultProps;
26
31
  static extensionName = 'MaskExtension';
27
32
 
33
+ initializeState(this: Layer<MaskExtensionProps>) {
34
+ this.context.deck?._addDefaultEffect(new MaskEffect());
35
+ }
36
+
28
37
  getShaders(this: Layer<MaskExtensionProps>): any {
29
38
  // Infer by geometry if 'maskByInstance' prop isn't explictly set
30
39
  let maskByInstance = 'instancePositions' in this.getAttributeManager()!.attributes;
31
40
  // Users can override by setting the `maskByInstance` prop
32
- if ('maskByInstance' in this.props) {
41
+ if (this.props.maskByInstance !== undefined) {
33
42
  maskByInstance = Boolean(this.props.maskByInstance);
34
43
  }
35
44
  this.state.maskByInstance = maskByInstance;
@@ -40,9 +49,9 @@ export default class MaskExtension extends LayerExtension {
40
49
  }
41
50
 
42
51
  /* eslint-disable camelcase */
43
- draw(this: Layer<MaskExtensionProps>, {uniforms, context, moduleParameters}: any) {
52
+ draw(this: Layer<Required<MaskExtensionProps>>, {uniforms, context, moduleParameters}: any) {
44
53
  uniforms.mask_maskByInstance = this.state.maskByInstance;
45
- const {maskId = ''} = this.props;
54
+ const {maskId, maskInverted} = this.props;
46
55
  const {maskChannels} = moduleParameters;
47
56
  const {viewport} = context;
48
57
  if (maskChannels && maskChannels[maskId]) {
@@ -50,6 +59,7 @@ export default class MaskExtension extends LayerExtension {
50
59
  let {coordinateSystem: fromCoordinateSystem} = maskChannels[maskId];
51
60
  uniforms.mask_enabled = true;
52
61
  uniforms.mask_channel = index;
62
+ uniforms.mask_inverted = maskInverted;
53
63
 
54
64
  if (fromCoordinateSystem === COORDINATE_SYSTEM.DEFAULT) {
55
65
  fromCoordinateSystem = viewport.isGeospatial
@@ -14,6 +14,7 @@ const fs = `
14
14
  uniform sampler2D mask_texture;
15
15
  uniform int mask_channel;
16
16
  uniform bool mask_enabled;
17
+ uniform bool mask_inverted;
17
18
  bool mask_isInBounds(vec2 texCoords) {
18
19
  if (!mask_enabled) {
19
20
  return true;
@@ -29,7 +30,12 @@ bool mask_isInBounds(vec2 texCoords) {
29
30
  } else if (mask_channel == 3) {
30
31
  maskValue = maskColor.a;
31
32
  }
32
- return maskValue < 0.5;
33
+
34
+ if (mask_inverted) {
35
+ return maskValue >= 0.5;
36
+ } else {
37
+ return maskValue < 0.5;
38
+ }
33
39
  }
34
40
  `;
35
41
 
@@ -0,0 +1,148 @@
1
+ import {OrthographicView, WebMercatorViewport, log, Layer, Viewport} from '@deck.gl/core';
2
+ import {fitBounds} from '@math.gl/web-mercator';
3
+
4
+ export type MaskBounds = [number, number, number, number];
5
+
6
+ /*
7
+ * Compute the bounds of the mask in world space, such that it covers an
8
+ * area currently visible (extended by a buffer) or the area of the masking
9
+ * data, whichever is smaller
10
+ */
11
+ export function getMaskBounds({
12
+ layers,
13
+ viewport
14
+ }: {
15
+ layers: Layer[];
16
+ viewport: Viewport;
17
+ }): MaskBounds {
18
+ // Join the bounds of layer data
19
+ let bounds: MaskBounds | null = null;
20
+ for (const layer of layers) {
21
+ const subLayerBounds = layer.getBounds();
22
+ if (subLayerBounds) {
23
+ if (bounds) {
24
+ bounds[0] = Math.min(bounds[0], subLayerBounds[0][0]);
25
+ bounds[1] = Math.min(bounds[1], subLayerBounds[0][1]);
26
+ bounds[2] = Math.max(bounds[2], subLayerBounds[1][0]);
27
+ bounds[3] = Math.max(bounds[3], subLayerBounds[1][1]);
28
+ } else {
29
+ bounds = [
30
+ subLayerBounds[0][0],
31
+ subLayerBounds[0][1],
32
+ subLayerBounds[1][0],
33
+ subLayerBounds[1][1]
34
+ ];
35
+ }
36
+ }
37
+ }
38
+ const viewportBounds = viewport.getBounds();
39
+ if (!bounds) {
40
+ return viewportBounds;
41
+ }
42
+
43
+ // Expand viewport bounds by 2X. Heurestically chosen to avoid masking
44
+ // errors when mask is partially out of view
45
+ const paddedBounds = _doubleBounds(viewportBounds);
46
+
47
+ // When bounds of the mask are smaller than the viewport bounds simply use
48
+ // mask bounds, so as to maximize resolution & avoid mask rerenders
49
+ if (
50
+ bounds[2] - bounds[0] < paddedBounds[2] - paddedBounds[0] ||
51
+ bounds[3] - bounds[1] < paddedBounds[3] - paddedBounds[1]
52
+ ) {
53
+ return bounds;
54
+ }
55
+
56
+ // As viewport shrinks, to avoid pixelation along mask edges
57
+ // we need to reduce the bounds and only render the visible portion
58
+ // of the mask.
59
+ // We pad the viewport bounds to capture the section
60
+ // of the mask just outside the viewport to correctly maskByInstance.
61
+ // Intersect mask & padded viewport bounds
62
+ bounds[0] = Math.max(bounds[0], paddedBounds[0]);
63
+ bounds[1] = Math.max(bounds[1], paddedBounds[1]);
64
+ bounds[2] = Math.min(bounds[2], paddedBounds[2]);
65
+ bounds[3] = Math.min(bounds[3], paddedBounds[3]);
66
+ return bounds;
67
+ }
68
+
69
+ /*
70
+ * Compute viewport to render the mask into, covering the given bounds
71
+ */
72
+ export function getMaskViewport({
73
+ bounds,
74
+ viewport,
75
+ width,
76
+ height
77
+ }: {
78
+ bounds: MaskBounds;
79
+ viewport: Viewport;
80
+ width: number;
81
+ height: number;
82
+ }): Viewport | null {
83
+ if (bounds[2] <= bounds[0] || bounds[3] <= bounds[1]) {
84
+ return null;
85
+ }
86
+
87
+ if (viewport.resolution !== undefined) {
88
+ log.warn('MaskExtension is not supported in GlobeView')();
89
+ return null;
90
+ }
91
+
92
+ // Single pixel border to prevent mask bleeding at edge of texture
93
+ const padding = 1;
94
+ width -= padding * 2;
95
+ height -= padding * 2;
96
+
97
+ if (viewport.isGeospatial) {
98
+ const {longitude, latitude, zoom} = fitBounds({
99
+ width,
100
+ height,
101
+ bounds: [
102
+ [bounds[0], bounds[1]],
103
+ [bounds[2], bounds[3]]
104
+ ],
105
+ maxZoom: 20
106
+ });
107
+ return new WebMercatorViewport({
108
+ longitude,
109
+ latitude,
110
+ zoom,
111
+ x: padding,
112
+ y: padding,
113
+ width,
114
+ height
115
+ });
116
+ }
117
+
118
+ const center = [(bounds[0] + bounds[2]) / 2, (bounds[1] + bounds[3]) / 2, 0];
119
+ const scale = Math.min(
120
+ 1048576, // maxZoom of 20: Math.pow(2, 20) = 1048576
121
+ width / (bounds[2] - bounds[0]),
122
+ height / (bounds[3] - bounds[1])
123
+ );
124
+
125
+ return new OrthographicView({
126
+ x: padding,
127
+ y: padding
128
+ }).makeViewport({
129
+ width,
130
+ height,
131
+ viewState: {
132
+ target: center,
133
+ zoom: Math.log2(scale)
134
+ }
135
+ });
136
+ }
137
+
138
+ function _doubleBounds(bounds: MaskBounds): MaskBounds {
139
+ const size = {
140
+ x: bounds[2] - bounds[0],
141
+ y: bounds[3] - bounds[1]
142
+ };
143
+ const center = {
144
+ x: bounds[0] + 0.5 * size.x,
145
+ y: bounds[1] + 0.5 * size.y
146
+ };
147
+ return [center.x - size.x, center.y - size.y, center.x + size.x, center.y + size.y];
148
+ }
@@ -0,0 +1,120 @@
1
+ import {Framebuffer} from '@luma.gl/core';
2
+ import {joinLayerBounds, getRenderBounds, makeViewport, Bounds} from '../common/projection-utils';
3
+ import {createRenderTarget} from './utils';
4
+
5
+ import type {Viewport, Layer} from '@deck.gl/core';
6
+
7
+ const MAP_MAX_SIZE = 2048;
8
+
9
+ /**
10
+ * Manages the lifecycle of the height map (a framebuffer that encodes elevation).
11
+ * One instance of height map is is shared across all layers. It is updated when the viewport changes
12
+ * or when some terrain source layer's data changes.
13
+ * During the draw call of any terrainFittingMode:offset layers,
14
+ * the vertex shader reads from this framebuffer to retrieve its z offset.
15
+ */
16
+ export class HeightMapBuilder {
17
+ /** Viewport used to draw into the texture */
18
+ renderViewport: Viewport | null = null;
19
+ /** Bounds of the height map texture, in cartesian space */
20
+ bounds: Bounds | null = null;
21
+
22
+ protected fbo?: Framebuffer;
23
+ protected gl: WebGLRenderingContext;
24
+ /** Last rendered layers */
25
+ private layers: Layer[] = [];
26
+ /** Last layer.getBounds() */
27
+ private layersBounds: ([number[], number[]] | null)[] = [];
28
+ /** The union of layersBounds in cartesian space */
29
+ private layersBoundsCommon: Bounds | null = null;
30
+ private lastViewport: Viewport | null = null;
31
+
32
+ static isSupported(gl: WebGLRenderingContext): boolean {
33
+ return Framebuffer.isSupported(gl, {colorBufferFloat: true});
34
+ }
35
+
36
+ constructor(gl: WebGLRenderingContext) {
37
+ this.gl = gl;
38
+ }
39
+
40
+ /** Returns the height map framebuffer for read/write access.
41
+ * Returns null when the texture is invalid.
42
+ */
43
+ getRenderFramebuffer(): Framebuffer | null {
44
+ if (!this.renderViewport) {
45
+ return null;
46
+ }
47
+ if (!this.fbo) {
48
+ this.fbo = createRenderTarget(this.gl, {id: 'height-map', float: true});
49
+ }
50
+ return this.fbo;
51
+ }
52
+
53
+ /** Called every render cycle to check if the framebuffer needs update */
54
+ shouldUpdate({layers, viewport}: {layers: Layer[]; viewport: Viewport}): boolean {
55
+ const layersChanged =
56
+ layers.length !== this.layers.length ||
57
+ layers.some(
58
+ (layer, i) =>
59
+ // Layer instance is updated
60
+ // Layer props might have changed
61
+ // Undetermined props could have an effect on the output geometry of a terrain source,
62
+ // for example getElevation+updateTriggers, elevationScale, modelMatrix
63
+ layer !== this.layers[i] ||
64
+ // Some prop is in transition
65
+ layer.props.transitions ||
66
+ // Layer's geometry bounds have changed
67
+ layer.getBounds() !== this.layersBounds[i]
68
+ );
69
+
70
+ if (layersChanged) {
71
+ // Recalculate cached bounds
72
+ this.layers = layers;
73
+ this.layersBounds = layers.map(layer => layer.getBounds());
74
+ this.layersBoundsCommon = joinLayerBounds(layers, viewport);
75
+ }
76
+
77
+ const viewportChanged = !this.lastViewport || !viewport.equals(this.lastViewport);
78
+
79
+ if (!this.layersBoundsCommon) {
80
+ this.renderViewport = null;
81
+ } else if (layersChanged || viewportChanged) {
82
+ const bounds = getRenderBounds(this.layersBoundsCommon, viewport);
83
+ this.bounds = bounds;
84
+ this.lastViewport = viewport;
85
+
86
+ const scale = viewport.scale;
87
+ const pixelWidth = (bounds[2] - bounds[0]) * scale;
88
+ const pixelHeight = (bounds[3] - bounds[1]) * scale;
89
+
90
+ this.renderViewport =
91
+ pixelWidth > 0 || pixelHeight > 0
92
+ ? makeViewport({
93
+ // It's not important whether the geometry is visible in this viewport, because
94
+ // vertices will not use the standard project_to_clipspace in the DRAW_TO_HEIGHT_MAP shader
95
+ // However the viewport must have the same center and zoom as the screen viewport
96
+ // So that projection uniforms used for calculating z are the same
97
+ bounds: [
98
+ viewport.center[0] - 1,
99
+ viewport.center[1] - 1,
100
+ viewport.center[0] + 1,
101
+ viewport.center[1] + 1
102
+ ],
103
+ zoom: viewport.zoom,
104
+ width: Math.min(pixelWidth, MAP_MAX_SIZE),
105
+ height: Math.min(pixelHeight, MAP_MAX_SIZE),
106
+ viewport
107
+ })
108
+ : null;
109
+ return true;
110
+ }
111
+ return false;
112
+ }
113
+
114
+ delete() {
115
+ if (this.fbo) {
116
+ this.fbo.color.delete();
117
+ this.fbo.delete();
118
+ }
119
+ }
120
+ }
@@ -0,0 +1,161 @@
1
+ import {project} from '@deck.gl/core';
2
+ import type {_ShaderModule as ShaderModule} from '@deck.gl/core';
3
+
4
+ import type {Texture2D} from '@luma.gl/core';
5
+ import type {Bounds} from '../common/projection-utils';
6
+ import type {TerrainCover} from './terrain-cover';
7
+
8
+ /** Module parameters expected by the terrain shader module */
9
+ export type TerrainModuleSettings = {
10
+ pickingActive?: boolean;
11
+ heightMap: Texture2D | null;
12
+ heightMapBounds?: Bounds | null;
13
+ dummyHeightMap: Texture2D;
14
+ terrainCover?: TerrainCover | null;
15
+ drawToTerrainHeightMap?: boolean;
16
+ useTerrainHeightMap?: boolean;
17
+ terrainSkipRender?: boolean;
18
+ };
19
+
20
+ /** A model can have one of the following modes */
21
+ export const TERRAIN_MODE = {
22
+ NONE: 0,
23
+ /** A terrain layer rendering encoded ground elevation into the height map */
24
+ WRITE_HEIGHT_MAP: 1,
25
+ /** An offset layer reading encoded ground elevation from the height map */
26
+ USE_HEIGHT_MAP: 2,
27
+ /** A terrain layer rendering to screen, using the cover fbo overlaid with its own texture */
28
+ USE_COVER: 3,
29
+ /** A terrain layer rendering to screen, using the cover fbo as texture */
30
+ USE_COVER_ONLY: 4,
31
+ /** Draped layer is rendered into a texture, and never to screen */
32
+ SKIP: 5
33
+ };
34
+
35
+ const TERRAIN_MODE_CONSTANTS = Object.keys(TERRAIN_MODE)
36
+ .map(key => `const float TERRAIN_MODE_${key} = ${TERRAIN_MODE[key]}.0;`)
37
+ .join('\n');
38
+
39
+ export const terrainModule = {
40
+ name: 'terrain',
41
+ dependencies: [project],
42
+ inject: {
43
+ 'vs:#decl': `
44
+ uniform float terrain_mode;
45
+ uniform sampler2D terrain_map;
46
+ uniform vec4 terrain_bounds;
47
+ varying vec3 commonPos;
48
+ ${TERRAIN_MODE_CONSTANTS}
49
+ `,
50
+ 'vs:#main-start': `
51
+ if (terrain_mode == TERRAIN_MODE_SKIP) {
52
+ gl_Position = vec4(0.0);
53
+ return;
54
+ }
55
+ `,
56
+ 'vs:DECKGL_FILTER_GL_POSITION': `
57
+ commonPos = geometry.position.xyz;
58
+ if (terrain_mode == TERRAIN_MODE_WRITE_HEIGHT_MAP) {
59
+ vec2 texCoords = (commonPos.xy - terrain_bounds.xy) / terrain_bounds.zw;
60
+ position = vec4(texCoords * 2.0 - 1.0, 0.0, 1.0);
61
+ }
62
+ if (terrain_mode == TERRAIN_MODE_USE_HEIGHT_MAP) {
63
+ vec3 anchor = geometry.worldPosition;
64
+ anchor.z = 0.0;
65
+ vec3 anchorCommon = project_position(anchor);
66
+ vec2 texCoords = (anchorCommon.xy - terrain_bounds.xy) / terrain_bounds.zw;
67
+ if (texCoords.x >= 0.0 && texCoords.y >= 0.0 && texCoords.x <= 1.0 && texCoords.y <= 1.0) {
68
+ float terrainZ = texture2D(terrain_map, texCoords).r;
69
+ geometry.position.z += terrainZ;
70
+ position = project_common_position_to_clipspace(geometry.position);
71
+ }
72
+ }
73
+ `,
74
+ 'fs:#decl': `
75
+ uniform float terrain_mode;
76
+ uniform sampler2D terrain_map;
77
+ uniform vec4 terrain_bounds;
78
+ varying vec3 commonPos;
79
+ ${TERRAIN_MODE_CONSTANTS}
80
+ `,
81
+ 'fs:#main-start': `
82
+ if (terrain_mode == TERRAIN_MODE_WRITE_HEIGHT_MAP) {
83
+ gl_FragColor = vec4(commonPos.z, 0.0, 0.0, 1.0);
84
+ return;
85
+ }
86
+ `,
87
+ 'fs:DECKGL_FILTER_COLOR': `
88
+ if ((terrain_mode == TERRAIN_MODE_USE_COVER) || (terrain_mode == TERRAIN_MODE_USE_COVER_ONLY)) {
89
+ vec2 texCoords = (commonPos.xy - terrain_bounds.xy) / terrain_bounds.zw;
90
+ vec4 pixel = texture2D(terrain_map, texCoords);
91
+ if (terrain_mode == TERRAIN_MODE_USE_COVER_ONLY) {
92
+ color = pixel;
93
+ } else {
94
+ // pixel is premultiplied
95
+ color = pixel + color * (1.0 - pixel.a);
96
+ }
97
+ return;
98
+ }
99
+ `
100
+ },
101
+ getUniforms: (opts = {}, uniforms) => {
102
+ if ('dummyHeightMap' in opts) {
103
+ const {
104
+ drawToTerrainHeightMap,
105
+ heightMap,
106
+ heightMapBounds,
107
+ dummyHeightMap,
108
+ terrainCover,
109
+ useTerrainHeightMap,
110
+ terrainSkipRender
111
+ } = opts;
112
+ const {project_uCommonOrigin} = uniforms;
113
+
114
+ let mode: number = terrainSkipRender ? TERRAIN_MODE.SKIP : TERRAIN_MODE.NONE;
115
+ // height map if case USE_HEIGHT_MAP, terrain cover if USE_COVER, otherwise empty
116
+ let sampler: Texture2D = dummyHeightMap;
117
+ // height map bounds if case USE_HEIGHT_MAP, terrain cover bounds if USE_COVER, otherwise null
118
+ let bounds: number[] | null = null;
119
+ if (drawToTerrainHeightMap) {
120
+ mode = TERRAIN_MODE.WRITE_HEIGHT_MAP;
121
+ bounds = heightMapBounds!;
122
+ } else if (useTerrainHeightMap && heightMap) {
123
+ mode = TERRAIN_MODE.USE_HEIGHT_MAP;
124
+ sampler = heightMap;
125
+ bounds = heightMapBounds!;
126
+ } else if (terrainCover) {
127
+ // This is a terrain layer
128
+ const isPicking = opts.pickingActive;
129
+ sampler = isPicking
130
+ ? terrainCover.getPickingFramebuffer()
131
+ : terrainCover.getRenderFramebuffer();
132
+ if (isPicking) {
133
+ // Never render the layer itself in picking pass
134
+ mode = TERRAIN_MODE.SKIP;
135
+ }
136
+ if (sampler) {
137
+ mode = mode === TERRAIN_MODE.SKIP ? TERRAIN_MODE.USE_COVER_ONLY : TERRAIN_MODE.USE_COVER;
138
+ bounds = terrainCover.bounds;
139
+ } else {
140
+ sampler = dummyHeightMap;
141
+ }
142
+ }
143
+
144
+ /* eslint-disable camelcase */
145
+ return {
146
+ terrain_mode: mode,
147
+ terrain_map: sampler,
148
+ // Convert bounds to the common space, as [minX, minY, width, height]
149
+ terrain_bounds: bounds
150
+ ? [
151
+ bounds[0] - project_uCommonOrigin[0],
152
+ bounds[1] - project_uCommonOrigin[1],
153
+ bounds[2] - bounds[0],
154
+ bounds[3] - bounds[1]
155
+ ]
156
+ : [0, 0, 0, 0]
157
+ };
158
+ }
159
+ return null;
160
+ }
161
+ } as ShaderModule<TerrainModuleSettings>;