@deck.gl/arcgis 9.0.0-beta.7 → 9.0.0-beta.9

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/src/commons.ts CHANGED
@@ -1,48 +1,82 @@
1
1
  /* eslint-disable no-invalid-this */
2
2
 
3
- import type {Device} from '@luma.gl/core';
4
- import {Model} from '@luma.gl/engine';
5
3
  import {GL} from '@luma.gl/constants';
6
-
4
+ import {Model, Geometry} from '@luma.gl/engine';
7
5
  import {Deck} from '@deck.gl/core';
6
+ import type {Device, Texture, Framebuffer} from '@luma.gl/core';
7
+ import type {WebGLDevice} from '@luma.gl/webgl';
8
+
9
+ interface Renderer {
10
+ redraw: () => void;
11
+ }
8
12
 
9
- export function initializeResources(device: Device) {
10
- // What is `this` referring to this function???
11
- this.buffer = device.createBuffer(new Int8Array([-1, -1, 1, -1, -1, 1, 1, 1]));
13
+ export type RenderResources = {
14
+ deck: Deck;
15
+ texture: Texture;
16
+ model: Model;
17
+ fbo: Framebuffer;
18
+ };
19
+
20
+ export function initializeResources(this: Renderer, device: Device): RenderResources {
21
+ const texture = device.createTexture({
22
+ format: 'rgba8unorm',
23
+ width: 1,
24
+ height: 1,
25
+ sampler: {
26
+ minFilter: 'linear',
27
+ magFilter: 'linear',
28
+ addressModeU: 'clamp-to-edge',
29
+ addressModeV: 'clamp-to-edge'
30
+ }
31
+ });
12
32
 
13
- this.model = new Model(device, {
33
+ const model = new Model(device, {
14
34
  vs: `\
15
35
  #version 300 es
16
- in vec2 a_pos;
36
+ in vec2 pos;
17
37
  out vec2 v_texcoord;
18
38
  void main(void) {
19
- gl_Position = vec4(a_pos, 0.0, 1.0);
20
- v_texcoord = (a_pos + 1.0) / 2.0;
39
+ gl_Position = vec4(pos, 0.0, 1.0);
40
+ v_texcoord = (pos + 1.0) / 2.0;
21
41
  }
22
42
  `,
23
43
  fs: `\
24
44
  #version 300 es
25
45
  precision mediump float;
26
- uniform sampler2D u_texture;
46
+ uniform sampler2D deckglTexture;
27
47
  in vec2 v_texcoord;
28
48
  out vec4 fragColor;
49
+
29
50
  void main(void) {
30
- vec4 rgba = texture(u_texture, v_texcoord);
31
- rgba.rgb *= rgba.a;
32
- fragColor = rgba;
51
+ vec4 imageColor = texture(deckglTexture, v_texcoord);
52
+ imageColor.rgb *= imageColor.a;
53
+ fragColor = imageColor;
33
54
  }
34
55
  `,
35
- attributes: {
36
- // eslint-disable-next-line camelcase
37
- a_pos: this.buffer
56
+ bindings: {
57
+ deckglTexture: texture
58
+ },
59
+ parameters: {
60
+ depthWriteEnabled: true,
61
+ depthCompare: 'less-equal'
38
62
  },
39
- vertexCount: 4,
40
- drawMode: GL.TRIANGLE_STRIP
63
+ geometry: new Geometry({
64
+ topology: 'triangle-strip',
65
+ attributes: {
66
+ pos: {size: 2, value: new Int8Array([-1, -1, 1, -1, -1, 1, -1, 1, 1, 1, 1, -1])}
67
+ }
68
+ }),
69
+ vertexCount: 6
41
70
  });
42
71
 
43
- this.deckFbo = device.createFramebuffer({width: 1, height: 1});
72
+ const fbo = device.createFramebuffer({
73
+ id: 'deckfbo',
74
+ width: 1,
75
+ height: 1,
76
+ colorAttachments: [texture]
77
+ });
44
78
 
45
- this.deckInstance = new Deck({
79
+ const deckInstance = new Deck({
46
80
  // The view state will be set dynamically to track the MapView current extent.
47
81
  viewState: {},
48
82
 
@@ -50,7 +84,7 @@ void main(void) {
50
84
  controller: false,
51
85
 
52
86
  // We use the same WebGL context as the ArcGIS API for JavaScript.
53
- gl: device.gl,
87
+ device,
54
88
 
55
89
  // We need depth testing in general; we don't know what layers might be added to the deck.
56
90
  parameters: {
@@ -58,7 +92,7 @@ void main(void) {
58
92
  },
59
93
 
60
94
  // This deck renders into an auxiliary framebuffer.
61
- _framebuffer: this.deckFbo,
95
+ _framebuffer: fbo,
62
96
 
63
97
  // To disable canvas resizing, since the FBO is owned by the ArcGIS API for JavaScript.
64
98
  width: null,
@@ -66,49 +100,68 @@ void main(void) {
66
100
 
67
101
  _customRender: redrawReason => {
68
102
  if (redrawReason === 'arcgis') {
69
- this.deckInstance._drawLayers(redrawReason);
103
+ deckInstance._drawLayers(redrawReason);
70
104
  } else {
71
105
  this.redraw();
72
106
  }
73
107
  }
74
108
  });
109
+
110
+ return {deck: deckInstance, texture, fbo, model};
75
111
  }
76
112
 
77
- export function render({gl, width, height, viewState}) {
78
- const screenFbo = gl.getParameter(gl.FRAMEBUFFER_BINDING);
113
+ export function render(
114
+ resources: RenderResources,
115
+ viewport: {
116
+ width: number;
117
+ height: number;
118
+ longitude: number;
119
+ latitude: number;
120
+ zoom: number;
121
+ altitude?: number;
122
+ pitch: number;
123
+ bearing: number;
124
+ }
125
+ ) {
126
+ const {model, deck, fbo} = resources;
127
+ const device = model.device;
128
+ const screenFbo = (device as WebGLDevice).getParametersWebGL(GL.FRAMEBUFFER_BINDING);
129
+ const {width, height, ...viewState} = viewport;
79
130
 
80
131
  /* global window */
81
132
  const dpr = window.devicePixelRatio;
82
- width = Math.round(width * dpr);
83
- height = Math.round(height * dpr);
133
+ const pixelWidth = Math.round(width * dpr);
134
+ const pixelHeight = Math.round(height * dpr);
84
135
 
85
- this.deckFbo.resize({width, height});
136
+ fbo.resize({width: pixelWidth, height: pixelHeight});
86
137
 
87
- this.deckInstance.setProps({viewState});
138
+ deck.setProps({viewState});
88
139
  // redraw deck immediately into deckFbo
89
- this.deckInstance.redraw('arcgis');
140
+ deck.redraw('arcgis');
90
141
 
91
142
  // We overlay the texture on top of the map using the full-screen quad.
92
- const device: Device = this.deckInstance.deck.device;
143
+
144
+ const textureToScreenPass = device.beginRenderPass({
145
+ framebuffer: screenFbo,
146
+ parameters: {viewport: [0, 0, pixelWidth, pixelHeight]},
147
+ clearColor: [0, 0, 0, 0],
148
+ clearDepth: 1
149
+ });
150
+
93
151
  device.withParametersWebGL(
94
152
  {
95
153
  blend: true,
96
- blendFunc: [gl.ONE, gl.ONE_MINUS_SRC_ALPHA],
97
- framebuffer: screenFbo,
98
- viewport: [0, 0, width, height]
154
+ blendFunc: [GL.ONE, GL.ONE_MINUS_SRC_ALPHA]
99
155
  },
100
156
  () => {
101
- // eslint-disable-next-line camelcase
102
- this.model.setUniforms({u_texture: this.deckFbo}).draw(this.context.renderPass);
157
+ model.draw(textureToScreenPass);
103
158
  }
104
159
  );
105
160
  }
106
161
 
107
- export function finalizeResources() {
108
- this.deckInstance?.finalize();
109
- this.deckInstance = null;
110
-
111
- this.model?.delete();
112
- this.buffer?.delete();
113
- this.deckFbo?.delete();
162
+ export function finalizeResources(resources: RenderResources) {
163
+ resources.deck.finalize();
164
+ resources.model.destroy();
165
+ resources.fbo.destroy();
166
+ resources.texture.destroy();
114
167
  }
@@ -1,12 +1,10 @@
1
+ import {WebGLDevice} from '@luma.gl/webgl';
1
2
  import {initializeResources, render, finalizeResources} from './commons';
2
3
 
3
4
  export default function createDeckLayerView2D(BaseLayerViewGL2D) {
4
5
  return BaseLayerViewGL2D.createSubclass({
5
6
  properties: {
6
- deckgl: {},
7
- deckFbo: {},
8
- model: {},
9
- buffer: {}
7
+ resources: null
10
8
  },
11
9
 
12
10
  // Attach is called as soon as the layer view is ready to start rendering.
@@ -14,13 +12,15 @@ export default function createDeckLayerView2D(BaseLayerViewGL2D) {
14
12
  // We use a full-screen quad and shaders to composite the frame rendered
15
13
  // with deck.gl on top of the MapView. Composition uses the MapView context.
16
14
  const gl = this.context;
17
- initializeResources.call(this, gl);
15
+ const device = WebGLDevice.attach(gl);
16
+
17
+ this.resources = initializeResources.call(this, device);
18
18
 
19
19
  // Update deck props
20
- this.layer.deck.on('change', props => this.deckInstance.setProps(props));
20
+ this.layer.deck.on('change', props => this.resources.deck.setProps(props));
21
21
 
22
22
  // We need to start drawing the deck.gl layer immediately.
23
- this.deckInstance.setProps(this.layer.deck.toJSON());
23
+ this.resources.deck.setProps(this.layer.deck.toJSON());
24
24
  },
25
25
 
26
26
  redraw() {
@@ -29,7 +29,8 @@ export default function createDeckLayerView2D(BaseLayerViewGL2D) {
29
29
 
30
30
  // Called when the layer must be destroyed.
31
31
  detach() {
32
- finalizeResources.call(this);
32
+ finalizeResources(this.resources);
33
+ this.resources = null;
33
34
  },
34
35
 
35
36
  // Called every time that the layer view must be rendered.
@@ -38,17 +39,14 @@ export default function createDeckLayerView2D(BaseLayerViewGL2D) {
38
39
  // The view state must be kept in-sync with the MapView of the ArcGIS API.
39
40
  const state = renderParameters.state;
40
41
 
41
- render.call(this, {
42
- gl: renderParameters.context,
42
+ render(this.resources, {
43
43
  width,
44
44
  height,
45
- viewState: {
46
- latitude: this.view.center.latitude,
47
- longitude: this.view.center.longitude,
48
- zoom: this.view.featuresTilingScheme.scaleToLevel(state.scale),
49
- bearing: -state.rotation,
50
- pitch: 0
51
- }
45
+ latitude: this.view.center.latitude,
46
+ longitude: this.view.center.longitude,
47
+ zoom: this.view.featuresTilingScheme.scaleToLevel(state.scale),
48
+ bearing: -state.rotation,
49
+ pitch: 0
52
50
  });
53
51
  }
54
52
  });
@@ -0,0 +1,65 @@
1
+ /* eslint-disable no-invalid-this */
2
+ import {WebGLDevice} from '@luma.gl/webgl';
3
+ import {initializeResources, render, finalizeResources, RenderResources} from './commons';
4
+
5
+ import SceneView from '@arcgis/core/views/SceneView';
6
+ import type {DeckProps} from '@deck.gl/core';
7
+
8
+ /** Convert ArcGIS FOV to WebMercatorViewport altitude */
9
+ function arcgisFOVToDeckAltitude(fov: number, aspectRatio: number): number {
10
+ // ArcGIS fov is corner-to-corner
11
+ const D = Math.sqrt(1 + aspectRatio ** 2);
12
+ const halfFOV = (fov / 2 / 180) * Math.PI;
13
+ return D / 2 / Math.tan(halfFOV);
14
+ }
15
+
16
+ export default function createDeckRenderer(DeckProps, externalRenderers) {
17
+ class DeckRenderer {
18
+ view: SceneView;
19
+ deck: any;
20
+ resources: RenderResources | null = null;
21
+
22
+ constructor(view: SceneView, props: DeckProps) {
23
+ this.view = view;
24
+ this.deck = new DeckProps(props);
25
+ }
26
+
27
+ setup(context) {
28
+ const gl = context.gl;
29
+ const device = WebGLDevice.attach(gl);
30
+ const resources = initializeResources.call(this, device);
31
+
32
+ this.deck.on('change', props => resources.deck.setProps(props));
33
+
34
+ resources.deck.setProps(this.deck.toJSON());
35
+ this.resources = resources;
36
+ }
37
+
38
+ dispose() {
39
+ if (this.resources) {
40
+ finalizeResources(this.resources);
41
+ }
42
+ }
43
+
44
+ redraw() {
45
+ externalRenderers.requestRender(this.view);
46
+ }
47
+
48
+ render() {
49
+ const [width, height] = this.view.size;
50
+
51
+ render(this.resources!, {
52
+ width,
53
+ height,
54
+ latitude: this.view.center.latitude,
55
+ longitude: this.view.center.longitude,
56
+ altitude: arcgisFOVToDeckAltitude(this.view.camera.fov, width / height),
57
+ zoom: this.view.zoom,
58
+ bearing: this.view.camera.heading,
59
+ pitch: this.view.camera.tilt
60
+ });
61
+ }
62
+ }
63
+
64
+ return DeckRenderer;
65
+ }
@@ -2,11 +2,20 @@ import createDeckProps from './deck-props';
2
2
  import createDeckLayer from './deck-layer';
3
3
  import createDeckLayerView2D from './deck-layer-view-2d';
4
4
  import createDeckRenderer from './deck-renderer';
5
- import {loadModules as esriLoaderLoadModules} from 'esri-loader';
5
+ import {loadModules as esriLoaderLoadModules, ILoadScriptOptions} from 'esri-loader';
6
6
 
7
- let arcGIS = null;
7
+ type LoadedModules = {
8
+ DeckLayer: any;
9
+ DeckRenderer: any;
10
+ modules?: unknown[];
11
+ };
8
12
 
9
- export async function loadArcGISModules(modules, loadScriptOptions) {
13
+ let arcGIS: LoadedModules;
14
+
15
+ export async function loadArcGISModules(
16
+ modules: string[],
17
+ loadScriptOptions: ILoadScriptOptions
18
+ ): Promise<LoadedModules> {
10
19
  const namespace = Array.isArray(modules) ? null : modules;
11
20
  await loadArcGISModule(namespace, loadScriptOptions);
12
21
 
@@ -18,7 +27,10 @@ export async function loadArcGISModules(modules, loadScriptOptions) {
18
27
  return arcGIS;
19
28
  }
20
29
 
21
- function loadArcGISModule(esri, loadScriptOptions) {
30
+ async function loadArcGISModule(
31
+ esri,
32
+ loadScriptOptions: ILoadScriptOptions
33
+ ): Promise<LoadedModules> {
22
34
  if (arcGIS) {
23
35
  // Already loaded
24
36
  return arcGIS;
@@ -34,7 +46,7 @@ function loadArcGISModule(esri, loadScriptOptions) {
34
46
  return initialize(Layer, Accessor, BaseLayerViewGL2D, externalRenderers);
35
47
  }
36
48
 
37
- return esriLoaderLoadModules(
49
+ const [Layer, Accessor, BaseLayerViewGL2D, externalRenderers] = await esriLoaderLoadModules(
38
50
  [
39
51
  'esri/layers/Layer',
40
52
  'esri/core/Accessor',
@@ -42,12 +54,11 @@ function loadArcGISModule(esri, loadScriptOptions) {
42
54
  'esri/views/3d/externalRenderers'
43
55
  ],
44
56
  loadScriptOptions
45
- ).then(([Layer, Accessor, BaseLayerViewGL2D, externalRenderers]) => {
46
- return initialize(Layer, Accessor, BaseLayerViewGL2D, externalRenderers);
47
- });
57
+ );
58
+ return initialize(Layer, Accessor, BaseLayerViewGL2D, externalRenderers);
48
59
  }
49
60
 
50
- function initialize(Layer, Accessor, BaseLayerViewGL2D, externalRenderers) {
61
+ function initialize(Layer, Accessor, BaseLayerViewGL2D, externalRenderers): LoadedModules {
51
62
  const DeckProps = createDeckProps(Accessor);
52
63
  const DeckLayerView2D = createDeckLayerView2D(BaseLayerViewGL2D);
53
64
  const DeckLayer = createDeckLayer(DeckProps, Layer, DeckLayerView2D);
@@ -1,56 +0,0 @@
1
- /* eslint-disable no-invalid-this */
2
-
3
- import {initializeResources, render, finalizeResources} from './commons';
4
-
5
- // ArcGIS fov is corner-to-corner
6
- function arcgisFOVToDeckAltitude(fov, aspectRatio) {
7
- const D = Math.sqrt(1 + aspectRatio ** 2);
8
- const halfFOV = (fov / 2 / 180) * Math.PI;
9
- return D / 2 / Math.tan(halfFOV);
10
- }
11
-
12
- export default function createDeckRenderer(DeckProps, externalRenderers) {
13
- class DeckRenderer {
14
- constructor(view, props) {
15
- this.view = view;
16
- this.deck = new DeckProps(props);
17
- }
18
-
19
- setup(context) {
20
- const gl = context.gl;
21
- initializeResources.call(this, gl);
22
-
23
- this.deck.on('change', props => this.deckInstance.setProps(props));
24
-
25
- this.deckInstance.setProps(this.deck.toJSON());
26
- }
27
-
28
- dispose() {
29
- finalizeResources.call(this);
30
- }
31
-
32
- redraw() {
33
- externalRenderers.requestRender(this.view);
34
- }
35
-
36
- render(context) {
37
- const [width, height] = this.view.size;
38
-
39
- render.call(this, {
40
- gl: context.gl,
41
- width,
42
- height,
43
- viewState: {
44
- latitude: this.view.center.latitude,
45
- longitude: this.view.center.longitude,
46
- altitude: arcgisFOVToDeckAltitude(this.view.camera.fov, width / height),
47
- zoom: this.view.zoom,
48
- bearing: this.view.camera.heading,
49
- pitch: this.view.camera.tilt
50
- }
51
- });
52
- }
53
- }
54
-
55
- return DeckRenderer;
56
- }
File without changes
File without changes
File without changes