abey79 / vsketch

Generative plotter art environment for Python
https://vsketch.readthedocs.io/en/latest/
Other
492 stars 50 forks source link

Support 3D projection onto canvas #396

Open very-meanly opened 1 year ago

very-meanly commented 1 year ago

I often initially create designs in Processing, and then rewrite the code in Python using vsketch if I want to plot it with fills. One of the convenient aspects of Processing is that it supports a 3D renderer that can do both orthographic and perspective projections. When I try to adapt the digital design for a plot in vsketch (fills are not supported in Processing's SVG renderer), it is difficult to achieve the same appearance since vsketch does not support 3 dimensions.

While it is trivial to do orthographic projection from 3D points in vsketch, perspective projection is a bit trickier unless you're already familiar with how to do this. Having a method that accepts x/y/z coordinates and a field of view and then returns the point's projection onto the 2D surface formed by the canvas would open up interesting possibilities for plots without having to support full 3D rendering across vsketch's entire suite of methods.

very-meanly commented 1 year ago

I have a working implementation of this in code but it is really dirty because I customized it to my needs. :) I should be able to write a more generalized version of this using vsketch's conventions though. Here's an example of the plot in both orthographic and perspective projections:

Orthographic

Screen Shot 2023-08-13 at 11 00 41 PM

Perspective

Screen Shot 2023-08-13 at 10 59 52 PM
abey79 commented 1 year ago

Cool! 🤩

A good was to go about this would be to discuss the APIs before spending too much time fine-tuning the implementation. That can be done here or in a draft PR.

very-meanly commented 1 year ago

@abey79 Yep, I definitely will do a draft PR and then can adjust from there based on any feedback! One thought - this is something that could feasibly be implemented in vpype. Is there any precedent for doing an implementation in both projects? Like, have you ever implemented something in vpype and then added wrappers in vsketch as a convenience method so that the end user doesn’t have to call the vpype method directly? I gotta learn to stop commenting on issues before I’ve had coffee. Realized that wouldn’t work since vpype wouldn’t have access to z-index data.

abey79 commented 1 year ago

Well, vsketch is based on and uses vpype, so yes, anything that's in vpype can be used in vsketch. This can happen in two ways:

BTW, if you haven't yet, you might take a look at https://github.com/abey79/vpype-perspective and https://github.com/abey79/lines.

There is an alternative avenue that you might consider that would arguably be more practical. You could work on a standalone python package, publish it, and use it in vsketch sketches. If your package produces a bunch of numpy arrays of complexes, then it's a matter of calling vsk.polygon() on them. Your package could certainly depend on vpype if some of the stuff there is useful to you (e.g. cropping, etc.). Also, this wouldn't preclude some form of tighter integration with vsketch down the line if/when things are more mature.