Subtract text from a mesh

I am playing around with trying to subtract text from a mesh using csg2 and not having much luck with it. I am getting the error: Error while creating the CSG: Not manifold

I have done a lot of searching and try different things but nothing seems to work, any ideas?

Here is the playground link

https://playground.babylonjs.com/?BabylonToolkit#1VHZ3M#5

Your playground doesn’t work.

But I can guess the issue. 3D text is simply not manifold, not watertight, as it has hard edges (distinct front and back faces). You’d likely need to merge all duplicate points and possibly also separate each manifold letter mesh and subtract one at a time.

Even after doing this, I’d be very surprised if you got reliable and clean results from using 3D text (a complex mesh) in a CSG operation.

I realized the link was bad and updated it. Is there now way to subtract text from a mesh?

You’d need to also update the PG link in the original post, as I tried again and it’s still broken.

As I said, you’d first need to make the text mesh manifold/watertight by merging mesh vertices and possibly also separating each manifold letter mesh and subtract them one at a time. That’s technically possible I guess but honestly not worth the effort and the results I can guarantee will not be reliable, repeatable or clean.

Just my opinion. Maybe someone else will have better ideas …

1 Like

I updated the PG link again, I switch the text to MeshBuilder.CreateText and now I get the error

Error while creating the CSG: memory access out of bounds

Do you want a literal 3d subtraction, or do you want the visual effect of (inset) text on a mesh surface?

If the latter, consider a normalMap (aka bumpMap) generated from a DynamicTexture fed through HeightToNormalMap.

1 Like

I am wanting a literal 3d subtraction

I think this is the right track. Changing A to I (capital I has no holes) results in no CSG2 error. However, subtract doesn’t appear to do anything, and intersect results in “Unable to build mesh. Manifold has 0 vertex.”

This indicates to me that ‘I’ is not watertight. Not sure why it couldn’t be constructed as watertight. Just speculating.

I tried a similar thing - using an “l” (lower case l) which essentially is a box. What I found though was that even after mesh.forceSharedVertices() there were double the number of faces i.e. 24 instead of the expected 12, even though vertices was the correct 8. So I think the text is built as double-sided which could cause issues.

The “Not manifold” error means that the mesh or text is not closed. Make them closed: extrude the text, close any holes, merge vertices, and ensure the normals are correct.

This is what I am after but recessed instead of raised

For that effect, I think you’d be better off with a simple normal map, as @HiGreg suggested. Far less effort and far more performant:

2 Likes

I got this to work using a stl. I subtract the stl from the cube, then add a shorter cube to the original since the subtract seems to goes all the way through the cube.