Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add hit detection support to WebGL vector layer #15120

Merged
merged 8 commits into from Sep 14, 2023

Conversation

gberaudo
Copy link
Member

@gberaudo gberaudo commented Sep 12, 2023

Add hit detection to the WebGL vector layer renderer.
This is similar to the points layer:

  • when hit detection is enabled (the default), compute a color for each feature and add it to the vertex buffer;
  • render the frame twice: for the main framebuffer and for the hit detection buffer;
  • use the hit detection buffer to implement the forEachFeatureAtCoordinate method.

@gberaudo gberaudo force-pushed the webg_hit_detection branch 4 times, most recently from a221e3d to 6e3312b Compare September 12, 2023 14:42
@github-actions
Copy link

📦 Preview the website for this branch here: https://deploy-preview-15120--ol-site.netlify.app/.

@gberaudo gberaudo force-pushed the webg_hit_detection branch 2 times, most recently from a8762a4 to 73579c5 Compare September 13, 2023 12:32
@gberaudo gberaudo marked this pull request as ready for review September 13, 2023 12:35
@gberaudo gberaudo changed the title [WIP] Add hit detection support to WebGL vector layer Add hit detection support to WebGL vector layer Sep 13, 2023
@gberaudo gberaudo force-pushed the webg_hit_detection branch 8 times, most recently from 8c84260 to f7b964d Compare September 14, 2023 10:53
src/ol/render/webgl/MixedGeometryBatch.js Show resolved Hide resolved
src/ol/render/webgl/MixedGeometryBatch.js Outdated Show resolved Hide resolved
src/ol/render/webgl/MixedGeometryBatch.js Outdated Show resolved Hide resolved
src/ol/renderer/webgl/VectorLayer.js Outdated Show resolved Hide resolved
src/ol/renderer/webgl/VectorLayer.js Outdated Show resolved Hide resolved
Copy link
Contributor

@jahow jahow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also for firefox another workaround could be:

--- a/src/ol/render/webgl/VectorStyleRenderer.js
+++ b/src/ol/render/webgl/VectorStyleRenderer.js
@@ -504,6 +504,9 @@ class VectorStyleRenderer {
     frameState,
     preRenderCallback
   ) {
+    if (indicesBuffer.getArray().length === 0) {
+      return;
+    }
     this.helper_.useProgram(program, frameState);
     this.helper_.bindBuffer(verticesBuffer);
     this.helper_.bindBuffer(indicesBuffer);

Here we're simply skipping rendering if no primitives are found in the buffer. This way we don't add complexity to the ShaderBuilder API.

@gberaudo
Copy link
Member Author

Also for firefox another workaround could be:

--- a/src/ol/render/webgl/VectorStyleRenderer.js
+++ b/src/ol/render/webgl/VectorStyleRenderer.js
@@ -504,6 +504,9 @@ class VectorStyleRenderer {
     frameState,
     preRenderCallback
   ) {
+    if (indicesBuffer.getArray().length === 0) {
+      return;
+    }
     this.helper_.useProgram(program, frameState);
     this.helper_.bindBuffer(verticesBuffer);
     this.helper_.bindBuffer(indicesBuffer);

Here we're simply skipping rendering if no primitives are found in the buffer. This way we don't add complexity to the ShaderBuilder API.

Thanks, this looks like the proper thing to do.
I commited it and updated the tests (it was actually missing a few features in order to render correctly).

Copy link
Contributor

@jahow jahow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks perfect now, thanks a lot!!

@jahow jahow merged commit 70464b4 into openlayers:main Sep 14, 2023
8 checks passed
@gberaudo gberaudo deleted the webg_hit_detection branch September 14, 2023 17:41
@guldbran
Copy link

Excellent news, this is a crucial feature for our project @gberaudo 🙌🏻. Looking forward to 8.2 now @tschaub - keep up the good work!

@tschaub
Copy link
Member

tschaub commented Sep 15, 2023

@gberaudo - sorry to be the bearer of bad news, but I'm not seeing any feature info displayed on this example https://deploy-preview-15120--ol-site.netlify.app/en/latest/examples/webgl-vector-layer.html on Chrome 116 or Safari 16.4 on a Mac (macOS 13, M1 chip). Has anybody else seen it working on a Mac?

@ahocevar
Copy link
Member

Unfortunately I'm getting the same results as @tschaub .

@jahow
Copy link
Contributor

jahow commented Sep 18, 2023

Could reproduce as well on iOS systems only, no errors in the console. The hit detection render target looks completely empty. I suspect this line to make things behave inconsistently:

renderInstructions[currentIndex + shift++] = value[0] || value;

Here the value equals typically [0, 0, 0, 0.12345] for a hit color with a low id, so value[0] is falsy. This line then assigns the whole value to a slot in the Float32Array, which results in a NaN value. The fact that it works on certain systems is actually surprising. @gberaudo I hope that helps!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants