Calculate Indices for SubMesh from Polygon with holes

Hello together,

I could need some help to calculate the starting Index and indexCount for a Submesh, which should be a “Side” from a Polygon Mesh.

Here is the background story, if it’s too long, you may skip it reading. It’s just for understanding what happened in my mind and how i got to the conclusion using SubMeshes:

In my Project the main goal is to somehow configure a building. Currently I’m busy with making the Walls look pretty. I used to use the PolygonMeshBuilder and switched to the MeshBuilder.ExtrudePolygon (more convenient and new). To scale my Material according to the length of my Wall, I calculated the needed uScale/vScale for the diffuseTexture. Everything was fine. Well, except when I got to the problem, when I had to use another Texture for the backside of the wall. At first it sounded pretty easy, because you can use the FaceUV (Parameter in ExtrudePolygon to chanve u/v, cropping the area you need). This is where the pain started. You can’t scale the Texture and have the Scale changed and expect to get the “cropped” image repeated on your mesh. That’s not how it works (obviously after reading the documentation again :slight_smile: ). Now I’m trying to part the Polygon into Submeshes and use different Materials.
Good idea and it worked. But only if the Polygon was a rectangle and the the holes too.

My current Situation:
I have a Polygon which has 5 Corner points and n-amount of holes (all rectangles) and it is extruded.
I need to calculate somehow the indices for the “front-side” and “back-side” of it.
There should be a smart way to do it, but I can’t get into it.
Sorry if it’s rude to ask directly for maybe something simple like that, but I realy need it asap done :sweat_smile:

I already did it successfully with the other walls, which are always sole rectangles. But this shape with more than 4 is killing me right now.

Thank you in advance and excuse my bad english.

To explain the image:
this is the 5 Corner Wall with 4 Holes (presented by solid walls, dev purposes)
Wireframe is turned on, so sorry for confusion :smiley:

Here are the working walls, I mentioned before. This is how it should look like in the end (except it is not a rectangle)



Hi Ice-T,

Welcome to Babylon! The technique I usually go with for having outside walls textured one way while the inside is textured differently is to use different meshes, so I’m not super familiar with the approach you’ve outlined here. (Very nice explanation, by the way, quite easy to read!) Without being able to see your code, I can only guess that you’re using getVerticesData() to look at the vertices directly. If your mesh has normals (and if it doesn’t, you could probably add some with createNormals()), you could use getVerticesData() to traverse those and label certain vertex indices as “front” or “back” depending on which way the normal faces. Other options I can think of include a custom shader that “splats” the correct texture depending on the normal direction, but I doubt that would be a preferable approach unless you’re already a big fan of custom shaders.

If you can put your code in a Playground, we might be able to look closer and help more directly; but in the mean time, hope this helps you with your project, and best of luck!


Dear Syntheticmagus,

thank you for the warm welcome and answering to my question. Sorry for the late response, as it was too late in my timezone :sleeping: I just tried it now and it works!

Your guesses hit the mark! Thank you very much for pointing me in the right direction.
Actually you gave me the whole solution to my problem!
To identify the Positions according to the normals (which gives you all you need) is simple yet ingenious! I’m very grateful for the help and appreciate the time you guys take to help newbies like me :upside_down_face:

For everybody who stumbles upon this, this is what I did:

  1. Loop all normals and save the array-index which corresponds to your desired direction (wall-side front/back in my case e.g. [0: (0,1,0) → 3:(0,1,0)–>…etc.])
  2. Loop all indices and check against the saved positions from early.
    Save your indices which corresponds to the positions.
  3. Now you have your start-end index for the submesh of your desired “side”

Again big thanks @syntheticmagus!

Screenshot: Works like a charm! :slight_smile: (btw, i used colors to identify missing triangles, so no confusion :laughing: )

1 Like