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

Possibility to have 2D gridLines in 3D and limit its area #2311

Open
Breviel opened this issue Mar 18, 2022 · 8 comments · May be fixed by #2504
Open

Possibility to have 2D gridLines in 3D and limit its area #2311

Breviel opened this issue Mar 18, 2022 · 8 comments · May be fixed by #2504

Comments

@Breviel
Copy link

Breviel commented Mar 18, 2022

Hey,

I was wondering if there was a possibility to limit the size and dimensions of the gridlines drawn in 3D.

In pyqtgraph there was a possbility to limit the size
gridplt = gl.GLGridItem(size=pg.Vector(15, 15, 0))
2D_gridlines_pyqtgraph

in vispy I use following, but I don't seem to find if it is possible at all to limit the size. Also to limit it to be a 2D grid in 3D plot.
plotGrid = scene.visuals.GridLines(scale=(1, 1), color="white", parent=view.scene)
3D_gridlines_vispy

Thanks in advance!

@Breviel Breviel changed the title Possibility to have 2D gridLines in 3D and limit their dimensions Possibility to have 2D gridLines in 3D and limit its area Mar 18, 2022
@djhoese
Copy link
Member

djhoese commented Mar 18, 2022

As you've discovered, this feature/functionality does not exist. The GridLinesVisual was developed specifically for 2D and would have to be updated to work in 3D. This will likely require a contribution from someone outside the core vispy maintainer group as I don't think we have the time for this work right now.

...but the current version looks pretty cool 😜

@mars0001
Copy link
Contributor

Hi,
I am interested in this feature, too. Is there some documentation that guides through the module architecture?

@djhoese
Copy link
Member

djhoese commented Mar 31, 2022

@mars0001 I'm not sure what you'll need to fix this, but I'm hoping that starting from the main GridLinesVisual will be enough (at least as a start):

https://github.com/vispy/vispy/blob/main/vispy/visuals/gridlines.py

This visual is based on the ImageVisual (https://github.com/vispy/vispy/blob/main/vispy/visuals/image.py) which is just displaying a 2D image on the Canvas. The GridLinesVisual cheats by overriding some of the functions used by the ImageVisual to make it get an endless amount of "image data" (the lines of the grid). You can see in the little shader code that is in the gridlines module that it is only given a 2-element vector location:

vec4 grid_color(vec2 pos) {

So I guess if you had a modified version of this GLSL (OpenGL Shader Language) implementation to "know" that the grid should only stretch so far, then the logic in the GridLinesVisual's __init__ could use either the "infinite" version of the shader function or the size limited version of the shader.

This is all just a guess, but I'm hoping this is all that's needed.

@singal95
Copy link

singal95 commented Mar 1, 2023

Hey,

I was wondering if there was a possibility to limit the size and dimensions of the gridlines drawn in 3D.

In pyqtgraph there was a possbility to limit the size gridplt = gl.GLGridItem(size=pg.Vector(15, 15, 0)) 2D_gridlines_pyqtgraph

in vispy I use following, but I don't seem to find if it is possible at all to limit the size. Also to limit it to be a 2D grid in 3D plot. plotGrid = scene.visuals.GridLines(scale=(1, 1), color="white", parent=view.scene) 3D_gridlines_vispy

Thanks in advance!

I have this issue as well, and solved this in an ugly way, which may help you if you still need

before you call GridLines() in your application, first assign the global variable vispy.visuals.gridline._GRID_COLOR with new string which have additional lines comparing to its original at line 59

add

if (local_pos.x < 0 || local_pos.y > 200 || local_pos.y < -50 || local_pos.y > 50) {
    discard;
}

@djhoese
Copy link
Member

djhoese commented Mar 1, 2023

I don't think I suggested in my previous comments, but one thing that could be done for people wanting this is to use the LineVisual with a series of line segments (or a series of connected vertices where you're smart about switching directions).

Thanks @singal95 for the suggestion. Your solution makes me think that maybe something like the ClipperFilter could be used:

https://github.com/vispy/vispy/blob/main/vispy/visuals/filters/clipper.py

I'm not sure what the parameters would have to be, but if you then did my_grid_visual.attach(clipper_instance) it might "just work" without having to modify the visual shader code at all. Thoughts?

@singal95
Copy link

singal95 commented Mar 7, 2023

Thanks for the tips @djhoese, I tried the clipper you mentioned, but the result is not exactly what I expected.

@djhoese
Copy link
Member

djhoese commented Mar 7, 2023

but the result is not exactly what I expected.

What was the result?

@DanAurea
Copy link
Contributor

DanAurea commented May 28, 2023

Hi,

I think the biggest struggle is due to how GridLines are being implemented.
They are being implemented as ImageVisual with impostor method which is ok for 2D projection since you most probably want to cover the whole view with grid lines.

With 3D projection such as turntable camera it will create artefacts as observed in your first screenshot, workaround proposed @singal95 wouldn't solve the issue but instead discard vertices based on screen pos.

One way would be either to reimplement how GridLinesVisual are being done (there's plenty of shader/explanation on the web on how to) or I found another way based on current implementation.

If you set the method as "subdivide" instead of "impostor" inside the gridlines visual it would create only a "portion" of the grid you could then rework and add configurable size inside the visual to allows the shader to limit grid drawing to the world coordinates you are expecting to.

I provided here an example with simply a quick test code for substituting impostor method to subdivide:
Source code
image

Be aware that's it's not a final solution and not compatible with 2D without further refinement since the grid in 2D would also being affected.

@soraxas soraxas linked a pull request Jun 24, 2023 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants