@luma.gl/engine 9.3.6 → 9.4.0-alpha.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 (181) hide show
  1. package/dist/animation/animator.d.ts +42 -0
  2. package/dist/animation/animator.d.ts.map +1 -0
  3. package/dist/animation/animator.js +53 -0
  4. package/dist/animation/animator.js.map +1 -0
  5. package/dist/animation-loop/animation-loop.d.ts +14 -2
  6. package/dist/animation-loop/animation-loop.d.ts.map +1 -1
  7. package/dist/animation-loop/animation-loop.js +40 -22
  8. package/dist/animation-loop/animation-loop.js.map +1 -1
  9. package/dist/animation-loop/animation-props.d.ts +2 -0
  10. package/dist/animation-loop/animation-props.d.ts.map +1 -1
  11. package/dist/animation-loop/make-animation-loop.d.ts +1 -0
  12. package/dist/animation-loop/make-animation-loop.d.ts.map +1 -1
  13. package/dist/animation-loop/make-animation-loop.js +3 -0
  14. package/dist/animation-loop/make-animation-loop.js.map +1 -1
  15. package/dist/application-utils/load-file.d.ts +2 -0
  16. package/dist/application-utils/load-file.d.ts.map +1 -1
  17. package/dist/application-utils/load-file.js +31 -5
  18. package/dist/application-utils/load-file.js.map +1 -1
  19. package/dist/compute/buffer-transform.d.ts +5 -1
  20. package/dist/compute/buffer-transform.d.ts.map +1 -1
  21. package/dist/compute/buffer-transform.js +7 -3
  22. package/dist/compute/buffer-transform.js.map +1 -1
  23. package/dist/compute/computation.d.ts +20 -8
  24. package/dist/compute/computation.d.ts.map +1 -1
  25. package/dist/compute/computation.js +34 -13
  26. package/dist/compute/computation.js.map +1 -1
  27. package/dist/compute/swap.d.ts.map +1 -1
  28. package/dist/compute/swap.js +17 -0
  29. package/dist/compute/swap.js.map +1 -1
  30. package/dist/dist.dev.js +4100 -1979
  31. package/dist/dist.min.js +288 -42
  32. package/dist/dynamic-buffer/dynamic-buffer.d.ts +171 -0
  33. package/dist/dynamic-buffer/dynamic-buffer.d.ts.map +1 -0
  34. package/dist/dynamic-buffer/dynamic-buffer.js +316 -0
  35. package/dist/dynamic-buffer/dynamic-buffer.js.map +1 -0
  36. package/dist/dynamic-texture/dynamic-texture.d.ts +14 -1
  37. package/dist/dynamic-texture/dynamic-texture.d.ts.map +1 -1
  38. package/dist/dynamic-texture/dynamic-texture.js +31 -2
  39. package/dist/dynamic-texture/dynamic-texture.js.map +1 -1
  40. package/dist/dynamic-texture/texture-binding-source.d.ts +55 -0
  41. package/dist/dynamic-texture/texture-binding-source.d.ts.map +1 -0
  42. package/dist/dynamic-texture/texture-binding-source.js +55 -0
  43. package/dist/dynamic-texture/texture-binding-source.js.map +1 -0
  44. package/dist/dynamic-texture/texture-data.d.ts +5 -3
  45. package/dist/dynamic-texture/texture-data.d.ts.map +1 -1
  46. package/dist/dynamic-texture/texture-data.js +5 -0
  47. package/dist/dynamic-texture/texture-data.js.map +1 -1
  48. package/dist/dynamic-texture/video-texture.d.ts +101 -0
  49. package/dist/dynamic-texture/video-texture.d.ts.map +1 -0
  50. package/dist/dynamic-texture/video-texture.js +272 -0
  51. package/dist/dynamic-texture/video-texture.js.map +1 -0
  52. package/dist/geometries/cube-geometry.d.ts +1 -0
  53. package/dist/geometries/cube-geometry.d.ts.map +1 -1
  54. package/dist/geometries/cube-geometry.js +22 -2
  55. package/dist/geometries/cube-geometry.js.map +1 -1
  56. package/dist/geometries/plane-geometry.d.ts +10 -0
  57. package/dist/geometries/plane-geometry.d.ts.map +1 -1
  58. package/dist/geometries/plane-geometry.js.map +1 -1
  59. package/dist/geometry/geometry-utils.d.ts +36 -1
  60. package/dist/geometry/geometry-utils.d.ts.map +1 -1
  61. package/dist/geometry/geometry-utils.js +137 -2
  62. package/dist/geometry/geometry-utils.js.map +1 -1
  63. package/dist/geometry/geometry.d.ts +52 -33
  64. package/dist/geometry/geometry.d.ts.map +1 -1
  65. package/dist/geometry/geometry.js +70 -19
  66. package/dist/geometry/geometry.js.map +1 -1
  67. package/dist/geometry/gpu-geometry.d.ts +37 -4
  68. package/dist/geometry/gpu-geometry.d.ts.map +1 -1
  69. package/dist/geometry/gpu-geometry.js +44 -36
  70. package/dist/geometry/gpu-geometry.js.map +1 -1
  71. package/dist/index.cjs +3990 -1961
  72. package/dist/index.cjs.map +4 -4
  73. package/dist/index.d.ts +20 -9
  74. package/dist/index.d.ts.map +1 -1
  75. package/dist/index.js +8 -5
  76. package/dist/index.js.map +1 -1
  77. package/dist/material/material-factory.d.ts +2 -1
  78. package/dist/material/material-factory.d.ts.map +1 -1
  79. package/dist/material/material-factory.js.map +1 -1
  80. package/dist/material/material.d.ts +29 -10
  81. package/dist/material/material.d.ts.map +1 -1
  82. package/dist/material/material.js +84 -21
  83. package/dist/material/material.js.map +1 -1
  84. package/dist/model/model.d.ts +81 -23
  85. package/dist/model/model.d.ts.map +1 -1
  86. package/dist/model/model.js +312 -78
  87. package/dist/model/model.js.map +1 -1
  88. package/dist/model/split-uniforms-and-bindings.d.ts +5 -2
  89. package/dist/model/split-uniforms-and-bindings.d.ts.map +1 -1
  90. package/dist/model/split-uniforms-and-bindings.js.map +1 -1
  91. package/dist/models/billboard-texture-model.d.ts +5 -2
  92. package/dist/models/billboard-texture-model.d.ts.map +1 -1
  93. package/dist/models/billboard-texture-model.js +28 -10
  94. package/dist/models/billboard-texture-model.js.map +1 -1
  95. package/dist/modules/picking/index-color-picking.d.ts +24 -0
  96. package/dist/modules/picking/index-color-picking.d.ts.map +1 -0
  97. package/dist/modules/picking/index-color-picking.js +205 -0
  98. package/dist/modules/picking/index-color-picking.js.map +1 -0
  99. package/dist/modules/picking/index-picking.d.ts +3 -3
  100. package/dist/modules/picking/index-picking.d.ts.map +1 -1
  101. package/dist/modules/picking/index-picking.js +23 -6
  102. package/dist/modules/picking/index-picking.js.map +1 -1
  103. package/dist/modules/picking/picking-manager.d.ts +21 -0
  104. package/dist/modules/picking/picking-manager.d.ts.map +1 -1
  105. package/dist/modules/picking/picking-manager.js +100 -0
  106. package/dist/modules/picking/picking-manager.js.map +1 -1
  107. package/dist/modules/picking/picking-uniforms.d.ts +4 -4
  108. package/dist/modules/picking/picking-uniforms.d.ts.map +1 -1
  109. package/dist/modules/picking/picking-uniforms.js +3 -0
  110. package/dist/modules/picking/picking-uniforms.js.map +1 -1
  111. package/dist/modules/picking/picking.d.ts +5 -1
  112. package/dist/modules/picking/picking.d.ts.map +1 -1
  113. package/dist/modules/picking/picking.js +6 -0
  114. package/dist/modules/picking/picking.js.map +1 -1
  115. package/dist/passes/get-fragment-shader.js +4 -4
  116. package/dist/passes/shader-pass-renderer.d.ts +92 -25
  117. package/dist/passes/shader-pass-renderer.d.ts.map +1 -1
  118. package/dist/passes/shader-pass-renderer.js +466 -72
  119. package/dist/passes/shader-pass-renderer.js.map +1 -1
  120. package/dist/passes/texture-transform-module.d.ts +10 -0
  121. package/dist/passes/texture-transform-module.d.ts.map +1 -0
  122. package/dist/passes/texture-transform-module.js +50 -0
  123. package/dist/passes/texture-transform-module.js.map +1 -0
  124. package/dist/shader-inputs.d.ts +33 -16
  125. package/dist/shader-inputs.d.ts.map +1 -1
  126. package/dist/shader-inputs.js +42 -12
  127. package/dist/shader-inputs.js.map +1 -1
  128. package/dist/utils/buffer-layout-helper.d.ts +0 -1
  129. package/dist/utils/buffer-layout-helper.d.ts.map +1 -1
  130. package/dist/utils/buffer-layout-helper.js +2 -11
  131. package/dist/utils/buffer-layout-helper.js.map +1 -1
  132. package/dist/utils/buffer-layout-order.d.ts.map +1 -1
  133. package/dist/utils/buffer-layout-order.js +4 -15
  134. package/dist/utils/buffer-layout-order.js.map +1 -1
  135. package/dist/utils/shader-module-utils.d.ts +2 -0
  136. package/dist/utils/shader-module-utils.d.ts.map +1 -1
  137. package/dist/utils/shader-module-utils.js +64 -3
  138. package/dist/utils/shader-module-utils.js.map +1 -1
  139. package/package.json +2 -2
  140. package/src/animation/animator.ts +79 -0
  141. package/src/animation-loop/animation-loop.ts +56 -22
  142. package/src/animation-loop/animation-props.ts +2 -0
  143. package/src/animation-loop/make-animation-loop.ts +4 -0
  144. package/src/application-utils/load-file.ts +38 -5
  145. package/src/compute/buffer-transform.ts +14 -5
  146. package/src/compute/computation.ts +53 -21
  147. package/src/compute/swap.ts +19 -0
  148. package/src/dynamic-buffer/dynamic-buffer.ts +453 -0
  149. package/src/dynamic-texture/dynamic-texture.ts +36 -3
  150. package/src/dynamic-texture/texture-binding-source.ts +94 -0
  151. package/src/dynamic-texture/texture-data.ts +12 -4
  152. package/src/dynamic-texture/video-texture.ts +334 -0
  153. package/src/geometries/cube-geometry.ts +24 -2
  154. package/src/geometries/plane-geometry.ts +10 -0
  155. package/src/geometry/geometry-utils.ts +209 -4
  156. package/src/geometry/geometry.ts +113 -36
  157. package/src/geometry/gpu-geometry.ts +61 -41
  158. package/src/index.ts +50 -14
  159. package/src/material/material-factory.ts +5 -1
  160. package/src/material/material.ts +123 -27
  161. package/src/model/model.ts +441 -102
  162. package/src/model/split-uniforms-and-bindings.ts +7 -3
  163. package/src/models/billboard-texture-model.ts +33 -12
  164. package/src/modules/picking/index-color-picking.ts +212 -0
  165. package/src/modules/picking/index-picking.ts +23 -6
  166. package/src/modules/picking/picking-manager.ts +141 -0
  167. package/src/modules/picking/picking-uniforms.ts +7 -4
  168. package/src/modules/picking/picking.ts +8 -0
  169. package/src/passes/get-fragment-shader.ts +4 -4
  170. package/src/passes/shader-pass-renderer.ts +691 -89
  171. package/src/passes/texture-transform-module.ts +52 -0
  172. package/src/shader-inputs.ts +65 -17
  173. package/src/utils/buffer-layout-helper.ts +2 -14
  174. package/src/utils/buffer-layout-order.ts +14 -24
  175. package/src/utils/shader-module-utils.ts +109 -3
  176. package/dist/geometry/gpu-table.d.ts +0 -1
  177. package/dist/geometry/gpu-table.d.ts.map +0 -1
  178. package/dist/geometry/gpu-table.js +0 -46
  179. package/dist/geometry/gpu-table.js.map +0 -1
  180. package/src/dynamic-texture/texture-data.ts.disabled +0 -296
  181. package/src/geometry/gpu-table.ts +0 -45
@@ -5,18 +5,22 @@
5
5
  import type {Binding, CompositeShaderType, UniformValue} from '@luma.gl/core';
6
6
  import type {ShaderModuleUniformValue} from '@luma.gl/shadertools';
7
7
  import {isNumericArray} from '@math.gl/types';
8
+ import type {TextureBindingSource} from '../dynamic-texture/texture-binding-source';
9
+
10
+ /** Shader input value routed through the binding map. */
11
+ type ShaderInputBinding = Binding | TextureBindingSource;
8
12
 
9
13
  export function isUniformValue(value: unknown): value is UniformValue {
10
14
  return isNumericArray(value) || typeof value === 'number' || typeof value === 'boolean';
11
15
  }
12
16
 
13
17
  type UniformsAndBindings = {
14
- bindings: Record<string, Binding>;
18
+ bindings: Record<string, ShaderInputBinding>;
15
19
  uniforms: Record<string, ShaderModuleUniformValue>;
16
20
  };
17
21
 
18
22
  export function splitUniformsAndBindings(
19
- uniforms: Record<string, Binding | ShaderModuleUniformValue>,
23
+ uniforms: Record<string, ShaderInputBinding | ShaderModuleUniformValue>,
20
24
  uniformTypes: Readonly<Record<string, CompositeShaderType>> = {}
21
25
  ): UniformsAndBindings {
22
26
  const result: UniformsAndBindings = {bindings: {}, uniforms: {}};
@@ -25,7 +29,7 @@ export function splitUniformsAndBindings(
25
29
  if (Object.prototype.hasOwnProperty.call(uniformTypes, name) || isUniformValue(uniform)) {
26
30
  result.uniforms[name] = uniform as ShaderModuleUniformValue;
27
31
  } else {
28
- result.bindings[name] = uniform as Binding;
32
+ result.bindings[name] = uniform as ShaderInputBinding;
29
33
  }
30
34
  });
31
35
 
@@ -2,7 +2,7 @@
2
2
  // SPDX-License-Identifier: MIT
3
3
  // Copyright (c) vis.gl contributors
4
4
 
5
- import {Device, Texture} from '@luma.gl/core';
5
+ import {Device, type CommandEncoder, Texture} from '@luma.gl/core';
6
6
  import type {ShaderModule} from '@luma.gl/shadertools';
7
7
  import {DynamicTexture} from '../dynamic-texture/dynamic-texture';
8
8
  import {ClipSpace, ClipSpaceProps} from './clip-space';
@@ -10,28 +10,33 @@ import {ClipSpace, ClipSpaceProps} from './clip-space';
10
10
  const backgroundModule = {
11
11
  name: 'background',
12
12
  uniformTypes: {
13
- scale: 'vec2<f32>'
13
+ scale: 'vec2<f32>',
14
+ flipY: 'i32'
14
15
  }
15
- } as const satisfies ShaderModule<{}, {scale: [number, number]}>;
16
+ } as const satisfies ShaderModule<{}, {scale: [number, number]; flipY: number}>;
16
17
 
17
18
  const BACKGROUND_FS_WGSL = /* wgsl */ `\
18
19
  @group(0) @binding(auto) var backgroundTexture: texture_2d<f32>;
19
20
  @group(0) @binding(auto) var backgroundTextureSampler: sampler;
20
21
  struct backgroundUniforms {
21
22
  scale: vec2<f32>,
23
+ flipY: i32,
22
24
  };
23
25
  @group(0) @binding(auto) var<uniform> background: backgroundUniforms;
24
26
 
25
27
  fn billboardTexture_getTextureUV(uv: vec2<f32>) -> vec2<f32> {
26
- let scale: vec2<f32> = background.scale;
27
- var position: vec2<f32> = (uv - vec2<f32>(0.5, 0.5)) / scale + vec2<f32>(0.5, 0.5);
28
- return position;
28
+ let scale: vec2<f32> = background.scale;
29
+ var position: vec2<f32> = (uv - vec2<f32>(0.5, 0.5)) / scale + vec2<f32>(0.5, 0.5);
30
+ if (background.flipY != 0) {
31
+ position.y = 1.0 - position.y;
32
+ }
33
+ return position;
29
34
  }
30
35
 
31
36
  @fragment
32
37
  fn fragmentMain(inputs: FragmentInputs) -> @location(0) vec4<f32> {
33
- let position: vec2<f32> = billboardTexture_getTextureUV(inputs.uv);
34
- return textureSample(backgroundTexture, backgroundTextureSampler, position);
38
+ let position: vec2<f32> = billboardTexture_getTextureUV(inputs.uv);
39
+ return textureSample(backgroundTexture, backgroundTextureSampler, position);
35
40
  }
36
41
  `;
37
42
 
@@ -43,6 +48,7 @@ uniform sampler2D backgroundTexture;
43
48
 
44
49
  layout(std140) uniform backgroundUniforms {
45
50
  vec2 scale;
51
+ int flipY;
46
52
  } background;
47
53
 
48
54
  in vec2 coordinate;
@@ -50,6 +56,9 @@ out vec4 fragColor;
50
56
 
51
57
  vec2 billboardTexture_getTextureUV(vec2 coord) {
52
58
  vec2 position = (coord - 0.5) / background.scale + 0.5;
59
+ if (background.flipY != 0) {
60
+ position.y = 1.0 - position.y;
61
+ }
53
62
  return position;
54
63
  }
55
64
 
@@ -69,6 +78,8 @@ export type BackgroundTextureModelProps = ClipSpaceProps & {
69
78
  backgroundTexture: Texture | DynamicTexture;
70
79
  /** If true, the texture is rendered into transparent areas of the screen only, i.e blended in where background alpha is small */
71
80
  blend?: boolean;
81
+ /** If true, flip the sampled texture vertically before drawing. */
82
+ flipY?: boolean;
72
83
  };
73
84
 
74
85
  /**
@@ -76,6 +87,7 @@ export type BackgroundTextureModelProps = ClipSpaceProps & {
76
87
  */
77
88
  export class BackgroundTextureModel extends ClipSpace {
78
89
  backgroundTexture: Texture = null!;
90
+ flipY = false;
79
91
 
80
92
  constructor(device: Device, props: BackgroundTextureModelProps) {
81
93
  super(device, {
@@ -110,6 +122,13 @@ export class BackgroundTextureModel extends ClipSpace {
110
122
  /** Update the background texture */
111
123
  setProps(props: Partial<BackgroundTextureModelProps>): void {
112
124
  const {backgroundTexture} = props;
125
+ if (props.flipY !== undefined) {
126
+ this.flipY = props.flipY;
127
+ if (this.backgroundTexture) {
128
+ this.updateScale(this.backgroundTexture);
129
+ }
130
+ }
131
+
113
132
  if (backgroundTexture) {
114
133
  this.setBindings({backgroundTexture});
115
134
 
@@ -129,15 +148,15 @@ export class BackgroundTextureModel extends ClipSpace {
129
148
  }
130
149
  }
131
150
 
132
- override predraw(): void {
151
+ override predraw(commandEncoder: CommandEncoder): void {
133
152
  // this.updateScale(this.backgroundTexture);
134
- super.predraw();
153
+ super.predraw(commandEncoder);
135
154
  }
136
155
 
137
156
  updateScale(texture: Texture): void {
138
157
  if (!texture) {
139
158
  // Initial scale to avoid rendering issues before texture is loaded
140
- this.shaderInputs.setProps({background: {scale: [1, 1]}});
159
+ this.shaderInputs.setProps({background: {scale: [1, 1], flipY: 0}});
141
160
  return;
142
161
  }
143
162
  const [screenWidth, screenHeight] = this.device.getCanvasContext().getDrawingBufferSize();
@@ -156,6 +175,8 @@ export class BackgroundTextureModel extends ClipSpace {
156
175
  scaleX = textureAspect / screenAspect;
157
176
  }
158
177
 
159
- this.shaderInputs.setProps({background: {scale: [scaleX, scaleY]}});
178
+ this.shaderInputs.setProps({
179
+ background: {scale: [scaleX, scaleY], flipY: this.flipY ? 1 : 0}
180
+ });
160
181
  }
161
182
  }
@@ -0,0 +1,212 @@
1
+ // luma.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+
5
+ import type {ShaderModule} from '@luma.gl/shadertools';
6
+
7
+ import type {PickingBindings, PickingProps, PickingUniforms} from './picking-uniforms';
8
+ import {pickingUniforms, GLSL_UNIFORMS, WGSL_UNIFORMS, INVALID_INDEX} from './picking-uniforms';
9
+
10
+ const source = /* wgsl */ `\
11
+ ${WGSL_UNIFORMS}
12
+
13
+ const COLOR_PICKING_INVALID_INDEX = ${INVALID_INDEX};
14
+ const COLOR_PICKING_MAX_OBJECT_INDEX = 16777214;
15
+ const COLOR_PICKING_MAX_BATCH_INDEX = 254;
16
+
17
+ fn picking_getInstanceObjectIndex(instanceIndex: u32) -> i32 {
18
+ return i32(instanceIndex);
19
+ }
20
+
21
+ fn picking_getVertexObjectIndex(vertexIndex: u32) -> i32 {
22
+ return i32(vertexIndex);
23
+ }
24
+
25
+ fn picking_getAttributeObjectIndex(objectIndex: i32) -> i32 {
26
+ return objectIndex;
27
+ }
28
+
29
+ fn picking_setObjectIndex(objectIndex: i32) -> i32 {
30
+ return picking_getAttributeObjectIndex(objectIndex);
31
+ }
32
+
33
+ fn picking_isObjectHighlighted(objectIndex: i32) -> bool {
34
+ return
35
+ picking.isHighlightActive != 0 &&
36
+ picking.highlightedBatchIndex == picking.batchIndex &&
37
+ picking.highlightedObjectIndex == objectIndex;
38
+ }
39
+
40
+ fn picking_filterHighlightColor(color: vec4<f32>, objectIndex: i32) -> vec4<f32> {
41
+ if (picking.isActive != 0 || !picking_isObjectHighlighted(objectIndex)) {
42
+ return color;
43
+ }
44
+
45
+ let highLightAlpha = picking.highlightColor.a;
46
+ let blendedAlpha = highLightAlpha + color.a * (1.0 - highLightAlpha);
47
+ if (blendedAlpha == 0.0) {
48
+ return vec4<f32>(color.rgb, 0.0);
49
+ }
50
+
51
+ let highLightRatio = highLightAlpha / blendedAlpha;
52
+ let blendedRGB = mix(color.rgb, picking.highlightColor.rgb, highLightRatio);
53
+ return vec4<f32>(blendedRGB, blendedAlpha);
54
+ }
55
+
56
+ fn picking_canEncodePickInfo(objectIndex: i32) -> bool {
57
+ return
58
+ objectIndex != COLOR_PICKING_INVALID_INDEX &&
59
+ objectIndex >= 0 &&
60
+ objectIndex <= COLOR_PICKING_MAX_OBJECT_INDEX &&
61
+ picking.batchIndex >= 0 &&
62
+ picking.batchIndex <= COLOR_PICKING_MAX_BATCH_INDEX;
63
+ }
64
+
65
+ fn picking_getPickingColor(objectIndex: i32) -> vec4<f32> {
66
+ if (!picking_canEncodePickInfo(objectIndex)) {
67
+ return vec4<f32>(0.0, 0.0, 0.0, 0.0);
68
+ }
69
+
70
+ let encodedObjectIndex = objectIndex + 1;
71
+ let red = encodedObjectIndex % 256;
72
+ let green = (encodedObjectIndex / 256) % 256;
73
+ let blue = (encodedObjectIndex / 65536) % 256;
74
+ let alpha = picking.batchIndex + 1;
75
+
76
+ return vec4<f32>(
77
+ f32(red) / 255.0,
78
+ f32(green) / 255.0,
79
+ f32(blue) / 255.0,
80
+ f32(alpha) / 255.0
81
+ );
82
+ }
83
+
84
+ fn picking_filterPickingColor(color: vec4<f32>, objectIndex: i32) -> vec4<f32> {
85
+ if (picking.isActive != 0) {
86
+ if (!picking_canEncodePickInfo(objectIndex)) {
87
+ discard;
88
+ }
89
+ return picking_getPickingColor(objectIndex);
90
+ }
91
+
92
+ return color;
93
+ }
94
+
95
+ fn picking_filterColor(color: vec4<f32>, objectIndex: i32) -> vec4<f32> {
96
+ var outColor = color;
97
+ outColor = picking_filterHighlightColor(outColor, objectIndex);
98
+ outColor = picking_filterPickingColor(outColor, objectIndex);
99
+ return outColor;
100
+ }
101
+ `;
102
+
103
+ const vs = /* glsl */ `\
104
+ ${GLSL_UNIFORMS}
105
+
106
+ const int INDEX_PICKING_MODE_INSTANCE = 0;
107
+ const int INDEX_PICKING_MODE_ATTRIBUTE = 1;
108
+ const int INDEX_PICKING_MODE_VERTEX = 2;
109
+
110
+ flat out int picking_objectIndex;
111
+
112
+ void picking_setObjectIndex(int objectIndex) {
113
+ switch (picking.indexMode) {
114
+ case INDEX_PICKING_MODE_INSTANCE:
115
+ picking_objectIndex = gl_InstanceID;
116
+ break;
117
+ case INDEX_PICKING_MODE_ATTRIBUTE:
118
+ picking_objectIndex = objectIndex;
119
+ break;
120
+ case INDEX_PICKING_MODE_VERTEX:
121
+ picking_objectIndex = gl_VertexID;
122
+ break;
123
+ }
124
+ }
125
+ `;
126
+
127
+ const fs = /* glsl */ `\
128
+ ${GLSL_UNIFORMS}
129
+
130
+ const int COLOR_PICKING_INVALID_INDEX = ${INVALID_INDEX};
131
+ const int COLOR_PICKING_MAX_OBJECT_INDEX = 16777214;
132
+ const int COLOR_PICKING_MAX_BATCH_INDEX = 254;
133
+
134
+ flat in int picking_objectIndex;
135
+
136
+ bool picking_isFragmentHighlighted() {
137
+ return
138
+ bool(picking.isHighlightActive) &&
139
+ picking.highlightedBatchIndex == picking.batchIndex &&
140
+ picking.highlightedObjectIndex == picking_objectIndex
141
+ ;
142
+ }
143
+
144
+ vec4 picking_filterHighlightColor(vec4 color) {
145
+ if (bool(picking.isActive)) {
146
+ return color;
147
+ }
148
+
149
+ if (!picking_isFragmentHighlighted()) {
150
+ return color;
151
+ }
152
+
153
+ float highLightAlpha = picking.highlightColor.a;
154
+ float blendedAlpha = highLightAlpha + color.a * (1.0 - highLightAlpha);
155
+ float highLightRatio = highLightAlpha / blendedAlpha;
156
+
157
+ vec3 blendedRGB = mix(color.rgb, picking.highlightColor.rgb, highLightRatio);
158
+ return vec4(blendedRGB, blendedAlpha);
159
+ }
160
+
161
+ bool picking_canEncodePickInfo(int objectIndex) {
162
+ return
163
+ objectIndex != COLOR_PICKING_INVALID_INDEX &&
164
+ objectIndex >= 0 &&
165
+ objectIndex <= COLOR_PICKING_MAX_OBJECT_INDEX &&
166
+ picking.batchIndex >= 0 &&
167
+ picking.batchIndex <= COLOR_PICKING_MAX_BATCH_INDEX;
168
+ }
169
+
170
+ vec4 picking_getPickingColor() {
171
+ if (!picking_canEncodePickInfo(picking_objectIndex)) {
172
+ return vec4(0.0);
173
+ }
174
+
175
+ int encodedObjectIndex = picking_objectIndex + 1;
176
+ int red = encodedObjectIndex % 256;
177
+ int green = (encodedObjectIndex / 256) % 256;
178
+ int blue = (encodedObjectIndex / 65536) % 256;
179
+ int alpha = picking.batchIndex + 1;
180
+
181
+ return vec4(float(red), float(green), float(blue), float(alpha)) / 255.0;
182
+ }
183
+
184
+ vec4 picking_filterPickingColor(vec4 color) {
185
+ if (bool(picking.isActive)) {
186
+ if (!picking_canEncodePickInfo(picking_objectIndex)) {
187
+ discard;
188
+ }
189
+ return picking_getPickingColor();
190
+ }
191
+
192
+ return color;
193
+ }
194
+
195
+ vec4 picking_filterColor(vec4 color) {
196
+ vec4 outColor = color;
197
+ outColor = picking_filterHighlightColor(outColor);
198
+ outColor = picking_filterPickingColor(outColor);
199
+ return outColor;
200
+ }
201
+ `;
202
+
203
+ /**
204
+ * Provides object-index based color picking and highlighting.
205
+ */
206
+ export const picking = {
207
+ ...pickingUniforms,
208
+ name: 'picking',
209
+ source,
210
+ vs,
211
+ fs
212
+ } as const satisfies ShaderModule<PickingProps, PickingUniforms, PickingBindings>;
@@ -13,16 +13,29 @@ const source = /* wgsl */ `\
13
13
  ${WGSL_UNIFORMS}
14
14
 
15
15
  const INDEX_PICKING_MODE_INSTANCE = 0;
16
- const INDEX_PICKING_MODE_CUSTOM = 1;
16
+ const INDEX_PICKING_MODE_ATTRIBUTE = 1;
17
+ const INDEX_PICKING_MODE_VERTEX = 2;
17
18
  const INDEX_PICKING_INVALID_INDEX = ${INVALID_INDEX}; // 2^32 - 1
18
19
 
19
20
  /**
20
21
  * WGSL shaders need to carry the returned object index through their own stage outputs.
21
22
  */
22
- fn picking_setObjectIndex(objectIndex: i32) -> i32 {
23
+ fn picking_getInstanceObjectIndex(instanceIndex: u32) -> i32 {
24
+ return i32(instanceIndex);
25
+ }
26
+
27
+ fn picking_getVertexObjectIndex(vertexIndex: u32) -> i32 {
28
+ return i32(vertexIndex);
29
+ }
30
+
31
+ fn picking_getAttributeObjectIndex(objectIndex: i32) -> i32 {
23
32
  return objectIndex;
24
33
  }
25
34
 
35
+ fn picking_setObjectIndex(objectIndex: i32) -> i32 {
36
+ return picking_getAttributeObjectIndex(objectIndex);
37
+ }
38
+
26
39
  fn picking_isObjectHighlighted(objectIndex: i32) -> bool {
27
40
  return
28
41
  picking.isHighlightActive != 0 &&
@@ -63,7 +76,8 @@ const vs = /* glsl */ `\
63
76
  ${GLSL_UNIFORMS}
64
77
 
65
78
  const int INDEX_PICKING_MODE_INSTANCE = 0;
66
- const int INDEX_PICKING_MODE_CUSTOM = 1;
79
+ const int INDEX_PICKING_MODE_ATTRIBUTE = 1;
80
+ const int INDEX_PICKING_MODE_VERTEX = 2;
67
81
 
68
82
  const int INDEX_PICKING_INVALID_INDEX = ${INVALID_INDEX}; // 2^32 - 1
69
83
 
@@ -71,16 +85,19 @@ flat out int picking_objectIndex;
71
85
 
72
86
  /**
73
87
  * Vertex shaders should call this function to set the object index.
74
- * If using instance or vertex mode, argument will be ignored, 0 can be supplied.
88
+ * If using instance or vertex mode, the argument is ignored and 0 can be supplied.
75
89
  */
76
90
  void picking_setObjectIndex(int objectIndex) {
77
91
  switch (picking.indexMode) {
78
92
  case INDEX_PICKING_MODE_INSTANCE:
79
93
  picking_objectIndex = gl_InstanceID;
80
94
  break;
81
- case INDEX_PICKING_MODE_CUSTOM:
95
+ case INDEX_PICKING_MODE_ATTRIBUTE:
82
96
  picking_objectIndex = objectIndex;
83
97
  break;
98
+ case INDEX_PICKING_MODE_VERTEX:
99
+ picking_objectIndex = gl_VertexID;
100
+ break;
84
101
  }
85
102
  }
86
103
  `;
@@ -132,7 +149,7 @@ vec4 picking_filterHighlightColor(vec4 color) {
132
149
  ivec4 picking_getPickingColor() {
133
150
  // Assumes that colorAttachment0 is rg32int
134
151
  // TODO? - we could render indices into a second color attachment and not mess with fragColor
135
- return ivec4(picking_objectIndex, picking.batchIndex, 0u, 0u);
152
+ return ivec4(picking_objectIndex, picking.batchIndex, 0, 0);
136
153
  }
137
154
 
138
155
  vec4 picking_filterPickingColor(vec4 color) {
@@ -10,6 +10,8 @@ const INDEX_PICKING_ATTACHMENT_INDEX = 1;
10
10
  const INDEX_PICKING_CLEAR_COLOR = new Int32Array([INVALID_INDEX, INVALID_INDEX, 0, 0]);
11
11
  const COLOR_PICKING_MAX_OBJECT_INDEX = 16777214;
12
12
  const COLOR_PICKING_MAX_BATCH_INDEX = 254;
13
+ const TOOLTIP_OFFSET_PIXELS = 14;
14
+ const TOOLTIP_MARGIN_PIXELS = 8;
13
15
 
14
16
  /** Information about picked object */
15
17
  export type PickInfo = {
@@ -17,8 +19,15 @@ export type PickInfo = {
17
19
  objectIndex: number | null;
18
20
  };
19
21
 
22
+ /** Text shown by `PickingManager` beside the latest picked cursor position. */
23
+ export type PickingTooltip = string | null;
24
+
20
25
  export type PickingMode = 'auto' | 'index' | 'color';
21
26
  export type ResolvedPickingMode = Exclude<PickingMode, 'auto'>;
27
+ export type PickingShouldPickOptions = {
28
+ /** Re-render picking even when the cursor has not moved. */
29
+ force?: boolean;
30
+ };
22
31
  /** @deprecated Use `PickingMode`. */
23
32
  export type PickingBackend = PickingMode;
24
33
  /** @deprecated Use `ResolvedPickingMode`. */
@@ -29,6 +38,11 @@ export type PickingManagerProps = {
29
38
  shaderInputs?: ShaderInputs<{picking: typeof pickingUniforms.props}>;
30
39
  /** Callback */
31
40
  onObjectPicked?: (info: PickInfo) => void;
41
+ /**
42
+ * Returns tooltip text for the latest pick result.
43
+ * Returning `null` hides the tooltip.
44
+ */
45
+ getTooltip?: (info: PickInfo) => PickingTooltip;
32
46
  /** Select a picking mode. Defaults to `color`. Use `auto` to prefer `index` when supported. */
33
47
  mode?: PickingMode;
34
48
  /** @deprecated Use `mode`. */
@@ -96,10 +110,15 @@ export class PickingManager {
96
110
  pickInfo: PickInfo = {batchIndex: null, objectIndex: null};
97
111
  /** Framebuffer used for picking */
98
112
  framebuffer: Framebuffer | null = null;
113
+ /** Last cursor position that triggered a picking render/readback. */
114
+ protected lastMousePosition: [number, number] | null = null;
115
+ /** Tooltip element created lazily when `getTooltip` returns visible content. */
116
+ protected tooltipElement: HTMLDivElement | null = null;
99
117
 
100
118
  static defaultProps: Required<PickingManagerProps> = {
101
119
  shaderInputs: undefined!,
102
120
  onObjectPicked: () => {},
121
+ getTooltip: () => null,
103
122
  mode: 'color',
104
123
  backend: 'color'
105
124
  };
@@ -119,6 +138,8 @@ export class PickingManager {
119
138
 
120
139
  destroy() {
121
140
  this.framebuffer?.destroy();
141
+ this.tooltipElement?.remove();
142
+ this.tooltipElement = null;
122
143
  }
123
144
 
124
145
  // TODO - Ask for a cached framebuffer? a Framebuffer factory?
@@ -132,7 +153,35 @@ export class PickingManager {
132
153
 
133
154
  /** Clear highlighted / picked object */
134
155
  clearPickState() {
156
+ this.lastMousePosition = null;
135
157
  this.setPickingProps({highlightedBatchIndex: null, highlightedObjectIndex: null});
158
+ this.hideTooltip();
159
+ }
160
+
161
+ /** Return whether callers need to render a fresh picking pass for this cursor position. */
162
+ shouldPick(
163
+ mousePosition: [number, number] | null | undefined,
164
+ options: PickingShouldPickOptions = {}
165
+ ): mousePosition is [number, number] {
166
+ if (!mousePosition) {
167
+ if (this.lastMousePosition) {
168
+ this.clearPickState();
169
+ }
170
+ return false;
171
+ }
172
+
173
+ const [mouseX, mouseY] = mousePosition;
174
+ if (
175
+ !options.force &&
176
+ this.lastMousePosition &&
177
+ this.lastMousePosition[0] === mouseX &&
178
+ this.lastMousePosition[1] === mouseY
179
+ ) {
180
+ return false;
181
+ }
182
+
183
+ this.lastMousePosition = [mouseX, mouseY];
184
+ return true;
136
185
  }
137
186
 
138
187
  /** Prepare for rendering picking colors */
@@ -168,6 +217,7 @@ export class PickingManager {
168
217
  this.props.onObjectPicked(pickInfo);
169
218
  }
170
219
 
220
+ this.updateTooltip(pickInfo, mousePosition);
171
221
  this.setPickingProps({
172
222
  isActive: false,
173
223
  highlightedBatchIndex: pickInfo.batchIndex,
@@ -334,6 +384,97 @@ export class PickingManager {
334
384
  pickInfo.batchIndex !== this.pickInfo.batchIndex
335
385
  );
336
386
  }
387
+
388
+ protected updateTooltip(pickInfo: PickInfo, mousePosition: [number, number]): void {
389
+ const tooltipText = this.props.getTooltip(pickInfo);
390
+ if (!tooltipText) {
391
+ this.hideTooltip();
392
+ return;
393
+ }
394
+
395
+ const tooltipElement = this.getOrCreateTooltipElement();
396
+ if (!tooltipElement) {
397
+ return;
398
+ }
399
+
400
+ tooltipElement.textContent = tooltipText;
401
+ tooltipElement.style.display = 'block';
402
+ this.positionTooltip(tooltipElement, mousePosition);
403
+ }
404
+
405
+ protected hideTooltip(): void {
406
+ if (this.tooltipElement) {
407
+ this.tooltipElement.style.display = 'none';
408
+ }
409
+ }
410
+
411
+ protected getOrCreateTooltipElement(): HTMLDivElement | null {
412
+ if (this.tooltipElement) {
413
+ return this.tooltipElement;
414
+ }
415
+ if (
416
+ typeof document === 'undefined' ||
417
+ typeof window === 'undefined' ||
418
+ typeof HTMLCanvasElement === 'undefined' ||
419
+ typeof this.device.getDefaultCanvasContext !== 'function'
420
+ ) {
421
+ return null;
422
+ }
423
+
424
+ const canvas = this.device.getDefaultCanvasContext().canvas;
425
+ if (!(canvas instanceof HTMLCanvasElement) || !canvas.parentElement) {
426
+ return null;
427
+ }
428
+
429
+ const tooltipElement = document.createElement('div');
430
+ tooltipElement.style.position = 'absolute';
431
+ tooltipElement.style.zIndex = '12';
432
+ tooltipElement.style.display = 'none';
433
+ tooltipElement.style.maxWidth = '220px';
434
+ tooltipElement.style.padding = '8px 10px';
435
+ tooltipElement.style.borderRadius = '6px';
436
+ tooltipElement.style.background = 'rgba(15, 23, 42, 0.92)';
437
+ tooltipElement.style.color = '#f8fafc';
438
+ tooltipElement.style.font = '600 13px/1.35 system-ui, sans-serif';
439
+ tooltipElement.style.pointerEvents = 'none';
440
+ tooltipElement.style.whiteSpace = 'nowrap';
441
+ tooltipElement.style.boxShadow = '0 10px 24px rgba(15, 23, 42, 0.22)';
442
+
443
+ const tooltipParent = canvas.parentElement;
444
+ if (window.getComputedStyle(tooltipParent).position === 'static') {
445
+ tooltipParent.style.position = 'relative';
446
+ }
447
+ tooltipParent.appendChild(tooltipElement);
448
+ this.tooltipElement = tooltipElement;
449
+ return tooltipElement;
450
+ }
451
+
452
+ protected positionTooltip(tooltipElement: HTMLDivElement, mousePosition: [number, number]): void {
453
+ if (
454
+ typeof HTMLCanvasElement === 'undefined' ||
455
+ typeof this.device.getDefaultCanvasContext !== 'function'
456
+ ) {
457
+ return;
458
+ }
459
+
460
+ const canvas = this.device.getDefaultCanvasContext().canvas;
461
+ if (!(canvas instanceof HTMLCanvasElement)) {
462
+ return;
463
+ }
464
+
465
+ const maxLeft = Math.max(
466
+ TOOLTIP_MARGIN_PIXELS,
467
+ canvas.clientWidth - tooltipElement.offsetWidth - TOOLTIP_MARGIN_PIXELS
468
+ );
469
+ const maxTop = Math.max(
470
+ TOOLTIP_MARGIN_PIXELS,
471
+ canvas.clientHeight - tooltipElement.offsetHeight - TOOLTIP_MARGIN_PIXELS
472
+ );
473
+ const left = Math.min(mousePosition[0] + TOOLTIP_OFFSET_PIXELS, maxLeft);
474
+ const top = Math.min(mousePosition[1] + TOOLTIP_OFFSET_PIXELS, maxTop);
475
+ tooltipElement.style.left = `${Math.max(TOOLTIP_MARGIN_PIXELS, left)}px`;
476
+ tooltipElement.style.top = `${Math.max(TOOLTIP_MARGIN_PIXELS, top)}px`;
477
+ }
337
478
  }
338
479
 
339
480
  export {COLOR_PICKING_MAX_BATCH_INDEX, COLOR_PICKING_MAX_OBJECT_INDEX};
@@ -9,7 +9,7 @@ import type {ShaderModule} from '@luma.gl/shadertools';
9
9
  const DEFAULT_HIGHLIGHT_COLOR: NumberArray4 = [0, 1, 1, 1];
10
10
 
11
11
  export const INVALID_INDEX = -1;
12
- export type PickingPayloadMode = 'instance' | 'attribute';
12
+ export type PickingPayloadMode = 'instance' | 'attribute' | 'vertex';
13
13
 
14
14
  /**
15
15
  * Props for the picking module, which depending on mode renders picking colors or highlighted item.
@@ -20,7 +20,7 @@ export type PickingPayloadMode = 'instance' | 'attribute';
20
20
  export type PickingProps = {
21
21
  /** Are we picking? I.e. rendering picking colors? */
22
22
  isActive?: boolean;
23
- /** Whether the payload is sourced from the builtin instance index or a custom integer attribute */
23
+ /** Whether the payload is sourced from a builtin index or a custom integer attribute */
24
24
  indexMode?: PickingPayloadMode;
25
25
  /** Identifier of the batch currently being rendered */
26
26
  batchIndex?: number;
@@ -44,8 +44,8 @@ export type PickingUniforms = {
44
44
  * When false, renders normal colors, with the exception of selected object which is rendered with highlight
45
45
  */
46
46
  isActive: boolean;
47
- /** Whether the current payload comes from instance_index or a custom integer attribute */
48
- indexMode: 0 | 1;
47
+ /** Whether the current payload comes from a builtin index or a custom integer attribute */
48
+ indexMode: 0 | 1 | 2;
49
49
  /** Identifier of the batch currently being rendered */
50
50
  batchIndex: number;
51
51
 
@@ -120,6 +120,9 @@ function getUniforms(props: PickingProps = {}, prevUniforms?: PickingUniforms):
120
120
  case 'attribute':
121
121
  uniforms.indexMode = 1;
122
122
  break;
123
+ case 'vertex':
124
+ uniforms.indexMode = 2;
125
+ break;
123
126
  case undefined:
124
127
  // no change
125
128
  break;