Skip to content

Add a flag to indicate if the most recently created canvas is 3D #5750

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

Open
1 of 17 tasks
SanzSeraph opened this issue Aug 2, 2022 · 8 comments · May be fixed by #5844
Open
1 of 17 tasks

Add a flag to indicate if the most recently created canvas is 3D #5750

SanzSeraph opened this issue Aug 2, 2022 · 8 comments · May be fixed by #5844

Comments

@SanzSeraph
Copy link

Increasing Access

This will increase access under the following rubric: "People with little or no prior experience in open source and creative coding"

Most appropriate sub-area of p5.js?

  • Accessibility
  • Color
  • Core/Environment/Rendering
  • Data
  • DOM
  • Events
  • Image
  • IO
  • Math
  • Typography
  • Utilities
  • WebGL
  • Build Process
  • Unit Testing
  • Localization Tools
  • Friendly Errors
  • Other (specify if possible)

Feature request details

While trying to figure out how to easily determine whether the current canvas is in 3D mode or 2D mode for a library I'm creating, I came across this SA article:

https://stackoverflow.com/questions/60931463/programmatically-detect-rendering-mode-in-p5js

It would appear that detecting the current canvas/p5.Graphics mode is less than straightforward. Many of the 3D methods throw an exception if you try to use them on a 2D canvas, so I think it would be useful to make it somewhat easier to determine the current mode.

I propose that we add a readonly property to p5 instances and p5.Graphics instances called .is3D, paralleling the existing .isP3D property of the p5.RendererGL class. Currently, you could also look at the _renderer property to see if it is an instance of p5.Renderer2D or p5.RendererGL, but that's no simpler than the solution proposed in the aforementioned SA article.

@limzykenneth
Copy link
Member

I think this would be good to have. For canvases, a conditional of canvas instanceof p5.Renderer2D/GL and for graphics graphic._renderer instanceof p5.Renderer2D/GL should be enough for a check for now.

@aferriss
Copy link
Contributor

aferriss commented Aug 5, 2022

Would it be better to call it isWebGL or something like that? Not all webGL canvases are drawing 3d shapes, and it might future proof the property against the default renderer if non-webGL 3d is ever added.

@SanzSeraph
Copy link
Author

SanzSeraph commented Aug 5, 2022

If a webGL canvas isn't being used to draw 3D shapes, would it not still let the developer use p5's 3D graphics methods? In my mind, the motivation for adding this flag is so that developers can determine if it's "safe" to use p5's 3D graphics methods. So as long as calling one of those methods will not result in an exception due to the assert3d call, whether that's because the canvas is using a webGL context or some other 3D renderer, then an is3D property would make sense, no? Unless there are other non-3D webGL methods in p5...

That being said, I think isWebGL would also be acceptable.

@aferriss
Copy link
Contributor

aferriss commented Aug 8, 2022

There's a couple non-3d methods in p5, shader() being one that I use often, but there's also loadShader(), createShader(), texture(), textureMode(), and textureWrap(). There might be more.

If I understand the problem correctly, what we're really looking for is a way to determine if a specific canvas's renderer is capable of drawing 3d shapes. The ability of the canvas to do so is defined by the type of renderer (canvas2d or webGL), so just knowing that a canvas is in webGL mode should be enough to tell you that. If later down the road, another renderer is added, or the ability to draw 3d shapes is added to existing renderers then is3d could become confusing since it might be possible to do 3d in a few different ways.

Not a hill I need to die on or anything just offering up a thought!

@SanzSeraph
Copy link
Author

What I mean by 3D methods is anything that requires a 3D rendering context, including the methods you mentioned. So as long as all of the p5 3D methods would be supported by any future 3D renderer (in the unlikely event that one is added) then the programmer shouldn't care whether it is webGL or some other context.

However, since webGL is pretty much the only game in town (as far as I know at least,) it probably makes sense to just call it .isWebGL. In the unlikely event that some other 3D renderer becomes available, then we could always add a more generic .is3D property at that time, if appropriate.

@limzykenneth
Copy link
Member

I would suggest going with .isWebGL as well, one to be consistent with how the canvas is created in the first place (createCanvas(400, 400, WEBGL), and two leave open the possibility of other renderers in the future.

I'm happy if either of you want to file a PR for this.

@spikyMalkallam
Copy link

Hi I've started working on this, could I get it assigned?

@davepagurek
Copy link
Contributor

Thanks @spikyMalkallam! I've assigned this to you.

@davepagurek davepagurek mentioned this issue Feb 14, 2023
2 tasks
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