# How to set materials per faces of a CSG generated mesh

**URL:** <https://forum.babylonjs.com/t/how-to-set-materials-per-faces-of-a-csg-generated-mesh/34030>\
**Category:** Questions\
**Tags:** material, csg\
**Created:** [September 19, 2022, 3:58am UTC](https://forum.babylonjs.com/t/how-to-set-materials-per-faces-of-a-csg-generated-mesh/34030 "2022-09-19T03:58:50Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![evidanary](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evidanary/32/26170_2.png) [@evidanary](https://forum.babylonjs.com/u/evidanary)\
**Post date:** [September 19, 2022, 3:58am UTC](https://forum.babylonjs.com/t/how-to-set-materials-per-faces-of-a-csg-generated-mesh/34030/1 "2022-09-19T03:58:50Z")

</div>

See playground link : [https://playground.babylonjs.com/#F32WKN#1](https://playground.babylonjs.com/#F32WKN#1)

I’m creating a cubby in a shelf such that I’d like to set different textures & lightmap for each of the faces inside the cubby. So there will be at least 4 texture+ lightmap pair for inside of the cubby and 1 texture + lightmap pair for rest of the faces.

In the documentation - [Map Materials to Individual Mesh Faces | Babylon.js Documentation](https://doc.babylonjs.com/divingDeeper/materials/using/texturePerBoxFace) it says

> This method is only available when creating a mesh using the _MeshBuilder_ method.

How do I get the top, bottom, left, right face such that I can set the material for them?

---

<div class="post-metadata">

**Author:** ![labris](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/labris/32/12576_2.png) [@labris](https://forum.babylonjs.com/u/labris)\
**Post date:** [September 19, 2022, 4:26am UTC](https://forum.babylonjs.com/t/how-to-set-materials-per-faces-of-a-csg-generated-mesh/34030/2 "2022-09-19T04:26:53Z")

</div>

In the same documentation it is written:

Face numbering using `MeshBuilder.CreateBox` is that

- side 0 faces the positive z direction
- side 1 faces the negative z direction
- side 2 faces the positive x direction
- side 3 faces the negative x direction
- side 4 faces the positive y direction
- side 5 faces the negative y direction

These are box sides at the moment of creation, hope this will help 🙂

---

<div class="post-metadata">

**Author:** ![evidanary](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evidanary/32/26170_2.png) [@evidanary](https://forum.babylonjs.com/u/evidanary)\
**Post date:** [September 19, 2022, 5:19am UTC](https://forum.babylonjs.com/t/how-to-set-materials-per-faces-of-a-csg-generated-mesh/34030/3 "2022-09-19T05:19:33Z")

</div>

Ahh…makes sense. I failed to notice that the CSG subtract function takes in to account the materials for each of the meshes.

For anyone of you who are interested - here’s the playground link which shows that if you subtract a green box from a red box, then the resulting material of the mesh is a combination of both.

> **[Babylon.js Playground](https://playground.babylonjs.com/#F32WKN%232)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

So the trick here is to apply the required textures on the box that will be used to subtract and it will show up in the resulting mesh.

---

<div class="post-metadata">

**Author:** ![evidanary](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evidanary/32/26170_2.png) [@evidanary](https://forum.babylonjs.com/u/evidanary)\
**Post date:** [September 19, 2022, 6:07am UTC](https://forum.babylonjs.com/t/how-to-set-materials-per-faces-of-a-csg-generated-mesh/34030/4 "2022-09-19T06:07:24Z")

</div>

@labris actually this did not work after adding a texture material to the object that will be used to subtract. I’ve recreated this in the playground [https://playground.babylonjs.com/#F32WKN#4](https://playground.babylonjs.com/#F32WKN#4)

As you can see the inner cube has the material applied with the facenumbers. However, after subtracting this cube from the outer mesh, the material in the new mesh is not retained.

This somehow works if the inner cube has faceColors (as seen above in [https://playground.babylonjs.com/#F32WKN#2](https://playground.babylonjs.com/#F32WKN#2)), but doesn’t work with faceUV options.

\*To clarify : I’m expecting to see the face number 2,3,4,5 to show in left, bottom, right, top area of the inside cavity.

---

<div class="post-metadata">

**Author:** ![carolhmj](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/carolhmj/32/25918_2.png) [@carolhmj](https://forum.babylonjs.com/u/carolhmj)\
**Post date:** [September 19, 2022, 12:52pm UTC](https://forum.babylonjs.com/t/how-to-set-materials-per-faces-of-a-csg-generated-mesh/34030/5 "2022-09-19T12:52:50Z")

</div>

It actually doesn’t consider _materials_, it considers _vertex colors_. So if you apply the colors as materials, they won’t be considered: [CSG subtract | Babylon.js Playground (babylonjs.com)](https://playground.babylonjs.com/#0UD9VE). Which means textures aren’t considered either, as these are applied through materials too: [CSG subtract | Babylon.js Playground (babylonjs.com)](https://playground.babylonjs.com/#0UD9VE)

---

<div class="post-metadata">

**Author:** ![evidanary](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evidanary/32/26170_2.png) [@evidanary](https://forum.babylonjs.com/u/evidanary)\
**Post date:** [September 19, 2022, 5:22pm UTC](https://forum.babylonjs.com/t/how-to-set-materials-per-faces-of-a-csg-generated-mesh/34030/6 "2022-09-19T17:22:01Z")

</div>

@carolhmj@labris does this mean there is no way to achieve my intended outcome?

I was hoping there was an API that would give me the inner cavity as 4 separate planes. I could then create a new plane of that same dimensions and position and overlay it on top of the 4 sides of the inner cavity (making the inner cavity texture transparent).

Is there:

1. API that would give me the inner cavity as 4 separate planes? Perhaps I can shoot a ray from the center and see the face where it intersects and somehow get a plane from it ?
2. Allow me to set only those planes transparent in the mesh (leaving all other facets intact)?

Perhaps @bghgary might know? Sorry to tag.

---

<div class="post-metadata">

**Author:** ![carolhmj](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/carolhmj/32/25918_2.png) [@carolhmj](https://forum.babylonjs.com/u/carolhmj)\
**Post date:** [September 19, 2022, 5:54pm UTC](https://forum.babylonjs.com/t/how-to-set-materials-per-faces-of-a-csg-generated-mesh/34030/7 "2022-09-19T17:54:40Z")

</div>

For 1, nope, the only thing you get is the complete CSG mesh but that has no idea of what objects were used to create it. For 2, you can set some parts on the mesh with different transparencies by using either an opacity texture, or multimaterials: [Morph setPositions, setNormals | Babylon.js Playground (babylonjs.com)](https://playground.babylonjs.com/#2Q4S2S#1152)

---

<div class="post-metadata">

**Author:** ![JohnK](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/johnk/32/26764_2.png) [@JohnK](https://forum.babylonjs.com/u/JohnK)\
**Post date:** [September 19, 2022, 6:03pm UTC](https://forum.babylonjs.com/t/how-to-set-materials-per-faces-of-a-csg-generated-mesh/34030/8 "2022-09-19T18:03:49Z")

</div>

Two thoughts on alternatives that may give you a way to go.

1. [UVs for ExtrudedPolygons?](https://forum.babylonjs.com/t/uvs-for-extrudedpolygons/32348)

2. Add the correct uvs in the correct places to this method [Making A Frame | Babylon.js Documentation](https://doc.babylonjs.com/toolsAndResources/utilities/Frame) using the code for a box faceUVs and faceColors as a basis

> <https://github.com/BabylonJS/Babylon.js/blob/master/packages/dev/core/src/Meshes/Builders/boxBuilder.ts>
