I was wondering how three js users would implement a functionality like the viewBox of SVG in a 2d three js scenario.
viewBox specified the SVG is cut, hiding shapes (or part of them) that locate outside the rectangle defined by the two
I have a software where I need to implement
viewBox in order to cut some
ShapeBufferGeometry but I don’t want to hide other 2d objects, for example the grid helper.
I was thinking to instantiate 4 big rectangular shapes and use them as “holes” for the geometries, but I’m not sure this will work: in my early prototypes I have strange results; plus, this technique seems to corrupts uvs coordinates, breaking a big piece of code I’ve written some months ago.
So… any advice? There’s something already done? Someone had to do this same thing in the past?
Have you considered to use a CSG library after generating your shape geometry? There are actually various projects available, see: Looking for Updated plug-in for CSG
I’m not sure this helps but a subtract operation could a possible solution for your issue.
I would look into the Material.clippingPlanes capability to cull fragments outside the box you want to render. Depending on the details of your use case you could use stencil buffer rendering / testing, as well.
Thanks to everyone.
I’ll leave this message here in case someone wants to achieve the same result.
I’ve decided to try with
Material.clippingPlanes since it seemed easier, and it worked. It’s necessary to create 4 planes - one for every limit - and the trick is done.
My code looks more or less like this, but don’t take it too seriously: I still need to work on that and maybe something is missing.
const viewbox = [
new Plane(new Vector3(0, 1, 0), -scheme.viewBox),
new Plane(new Vector3(0, -1, 0), scheme.viewBox + scheme.viewBox),
new Plane(new Vector3(1, 0, 0), -scheme.viewBox),
new Plane(new Vector3(-1, 0, 0), scheme.viewBox + scheme.viewBox)
I haven’t tried a CSG plugin, it seemed too far from my reach. Can’t say if this approach will work or not.