# Dynamic Texture projection after a CSG subtraction

**URL:** <https://forum.babylonjs.com/t/dynamic-texture-projection-after-a-csg-subtraction/22852>\
**Category:** Questions\
**Created:** [August 6, 2021, 1:45am UTC](https://forum.babylonjs.com/t/dynamic-texture-projection-after-a-csg-subtraction/22852 "2021-08-06T01:45:13Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![sholand](https://avatars.discourse-cdn.com/v4/letter/s/c5a1d2/32.png) [@sholand](https://forum.babylonjs.com/u/sholand)\
**Post date:** [August 6, 2021, 1:45am UTC](https://forum.babylonjs.com/t/dynamic-texture-projection-after-a-csg-subtraction/22852/1 "2021-08-06T01:45:13Z")

</div>

Hi All, in 2017 JohnK helped me with a custom extrusion which I then perform CSG subtraction.

I used a multi material and it looks nice with the indented part in green.

 ![Screen Shot 2021-08-05 at 9.15.11 PM](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/1/2/12a66da869f0ad79a1b91246560b15fa19af15ef.png)

I am trying to have a dynamic texture which fits the base object but continues onto the green material.

So in my playground, I need to have the american flag cover the green part inside the rectangle.

I also would like the sides and bottom of the mesh to look like before without the dynamic texture.

I spent hours looking at projection mapping and uv stuff but I can’t figure it out.

Here is the playground that shows the dynamic texture with the american flag.

[https://playground.babylonjs.com/#BC4XLY](https://playground.babylonjs.com/#BC4XLY)

Thanks!

---

<div class="post-metadata">

**Author:** ![RaananW](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/raananw/32/28955_2.png) [@RaananW](https://forum.babylonjs.com/u/RaananW)\
**Post date:** [August 6, 2021, 7:06am UTC](https://forum.babylonjs.com/t/dynamic-texture-projection-after-a-csg-subtraction/22852/2 "2021-08-06T07:06:14Z")

</div>

If you want the flag to be pasted all over the green part as well you will either need to cut the texture correctly and set it there or use a single material with correct uv mapping.  
“correct uv mapping” is a rather interesting subject 🙂 First it is your decision how uvs are mapped around this kind of object. There are tools that can help you here, but if you want to do it programmatically you will need to know where each point is and assign the right uv coordinates for it.

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [August 6, 2021, 11:22am UTC](https://forum.babylonjs.com/t/dynamic-texture-projection-after-a-csg-subtraction/22852/3 "2021-08-06T11:22:49Z")

</div>

As your object is planar you can try to generate the texture coordinates from the x/z coordinates of the vertices:

[https://playground.babylonjs.com/#BC4XLY#1](https://playground.babylonjs.com/#BC4XLY#1)

However, the diffuse texture applies to the whole mesh, so even the back part of the mesh is textured with the flag… I think you would need to split the object into two parts if you want different texturing on the front/back.

---

<div class="post-metadata">

**Author:** ![sholand](https://avatars.discourse-cdn.com/v4/letter/s/c5a1d2/32.png) [@sholand](https://forum.babylonjs.com/u/sholand)\
**Post date:** [August 6, 2021, 3:40pm UTC](https://forum.babylonjs.com/t/dynamic-texture-projection-after-a-csg-subtraction/22852/4 "2021-08-06T15:40:32Z")

</div>

@Evgeni_Popov This is great! Thank you SO much for your help! I think this will work for me.

Question, I added another shape and it has a plain background. (shapes will be polygons as well as rectangles…)

What would I need to do to have it work for all indentations?

Here is the new playground

[https://playground.babylonjs.com/#BC4XLY#2](https://playground.babylonjs.com/#BC4XLY#2)

Thanks!

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [August 6, 2021, 4:00pm UTC](https://forum.babylonjs.com/t/dynamic-texture-projection-after-a-csg-subtraction/22852/5 "2021-08-06T16:00:32Z")

</div>

You should either add a new material to the multi material to account for the new submesh, or simply disable submeshes when calling `cCSG.toMesh` (4th parameter to false).

---

<div class="post-metadata">

**Author:** ![sholand](https://avatars.discourse-cdn.com/v4/letter/s/c5a1d2/32.png) [@sholand](https://forum.babylonjs.com/u/sholand)\
**Post date:** [August 6, 2021, 4:17pm UTC](https://forum.babylonjs.com/t/dynamic-texture-projection-after-a-csg-subtraction/22852/6 "2021-08-06T16:17:27Z")

</div>

Disabling submeshes worked!

When would be a good reason to use submeshes?

Thanks!

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [August 6, 2021, 4:28pm UTC](https://forum.babylonjs.com/t/dynamic-texture-projection-after-a-csg-subtraction/22852/7 "2021-08-06T16:28:29Z")

</div>

When you need different materials for different parts but still want a single mesh (for ease of use, as opposed to having one mesh per part), but in your case you wan’t the same material applied for the whole mesh.

---

<div class="post-metadata">

**Author:** ![sholand](https://avatars.discourse-cdn.com/v4/letter/s/c5a1d2/32.png) [@sholand](https://forum.babylonjs.com/u/sholand)\
**Post date:** [August 6, 2021, 4:44pm UTC](https://forum.babylonjs.com/t/dynamic-texture-projection-after-a-csg-subtraction/22852/8 "2021-08-06T16:44:19Z")

</div>

Thank you for the explanation.

I have one last question which I hope will be my last.

In my application I have switched the axis (it was so long ago so I don’t remember exactly) but my z goes up and down.

 ![Screen Shot 2021-08-06 at 12.33.52 PM](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/4/6/4697c121e0eb907534afa632b62f2efbeb5ce909.png)

It’s kind of hard to see but the texture is ending up on the side instead of the face.

Instead of x and z, would i change to y and z somehow?

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [August 6, 2021, 6:29pm UTC](https://forum.babylonjs.com/t/dynamic-texture-projection-after-a-csg-subtraction/22852/9 "2021-08-06T18:29:18Z")

</div>

Sorry, I don’t understand the picture nor the question(?)

---

<div class="post-metadata">

**Author:** ![sholand](https://avatars.discourse-cdn.com/v4/letter/s/c5a1d2/32.png) [@sholand](https://forum.babylonjs.com/u/sholand)\
**Post date:** [August 6, 2021, 6:45pm UTC](https://forum.babylonjs.com/t/dynamic-texture-projection-after-a-csg-subtraction/22852/10 "2021-08-06T18:45:30Z")

</div>

@Evgeni_Popov I was able to figure it out and it works really great!

I can’t thank you enough!!!

---

<div class="post-metadata">

**Author:** ![sholand](https://avatars.discourse-cdn.com/v4/letter/s/c5a1d2/32.png) [@sholand](https://forum.babylonjs.com/u/sholand)\
**Post date:** [August 11, 2021, 5:32pm UTC](https://forum.babylonjs.com/t/dynamic-texture-projection-after-a-csg-subtraction/22852/11 "2021-08-11T17:32:44Z")

</div>

Thanks to @Evgeni_Popov I have the texture mapping nicely.

My problem is that I really can’t have the texture on the sides of the interior indentations.

So just need black walls there and not the flag.

Any ideas?

 ![Screen Shot 2021-08-11 at 1.28.42 PM](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/c/5/c5224c29b02fcd3950bab723a85e0aee7079fbf9.jpeg)

[https://playground.babylonjs.com/#BC4XLY#3](https://playground.babylonjs.com/#BC4XLY#3)

---

<div class="post-metadata">

**Author:** ![bghgary](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/bghgary/32/48532_2.png) [@bghgary](https://forum.babylonjs.com/u/bghgary)\
**Post date:** [August 12, 2021, 4:00pm UTC](https://forum.babylonjs.com/t/dynamic-texture-projection-after-a-csg-subtraction/22852/12 "2021-08-12T16:00:38Z")

</div>

I’m thinking there are at least two ways to do this. (I haven’t tried these myself.) One would be to add geometry for the vertical walls and use a different material on them (or maybe change the normals to face a direction \>180 degrees of the light to make them black). Another would be use a custom shader with derivative (dfdy, dfdx) checks to black out the vertical parts.

---

<div class="post-metadata">

**Author:** ![sholand](https://avatars.discourse-cdn.com/v4/letter/s/c5a1d2/32.png) [@sholand](https://forum.babylonjs.com/u/sholand)\
**Post date:** [August 13, 2021, 6:47pm UTC](https://forum.babylonjs.com/t/dynamic-texture-projection-after-a-csg-subtraction/22852/13 "2021-08-13T18:47:55Z")

</div>

Hi @Evgeni_Popov, it turns out my idea for getting the sides and bottom to be solid did not work… You mentioned somehow splitting the object into two parts so I can get a different texture for sides and bottom? How would I go about doing that? Thanks!

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [August 13, 2021, 7:24pm UTC](https://forum.babylonjs.com/t/dynamic-texture-projection-after-a-csg-subtraction/22852/14 "2021-08-13T19:24:48Z")

</div>

I don’t really know how you can create separated objects, as you are using CSG… The easiest way would be to build it in Blender or a DCC tool like that.

You can also apply the idea from @bghgary regarding usage of the derivatives:  
[https://playground.babylonjs.com/#BC4XLY#4](https://playground.babylonjs.com/#BC4XLY#4)

You can change the color of the back line 177 and the color of the vertical parts lines 226 and 227. It is currently (0.2, 0.2, 0.2) = dark grey.

---

<div class="post-metadata">

**Author:** ![sholand](https://avatars.discourse-cdn.com/v4/letter/s/c5a1d2/32.png) [@sholand](https://forum.babylonjs.com/u/sholand)\
**Post date:** [August 13, 2021, 7:51pm UTC](https://forum.babylonjs.com/t/dynamic-texture-projection-after-a-csg-subtraction/22852/15 "2021-08-13T19:51:23Z")

</div>

@Evgeni_Popov This is perfect! exactly what I need.

I am getting this error : matPrinted.Vertex\_Definitions is not a function

Maybe I have an old version of Babylon?

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [August 13, 2021, 7:54pm UTC](https://forum.babylonjs.com/t/dynamic-texture-projection-after-a-csg-subtraction/22852/16 "2021-08-13T19:54:31Z")

</div>

I don’t know, I think `Vertex_Definitions` exists for a long time. We can’t test in the Playground for the time being, however, as using an older version of Babylon is broken at the time (should be fixed today).

---

<div class="post-metadata">

**Author:** ![sholand](https://avatars.discourse-cdn.com/v4/letter/s/c5a1d2/32.png) [@sholand](https://forum.babylonjs.com/u/sholand)\
**Post date:** [August 13, 2021, 10:12pm UTC](https://forum.babylonjs.com/t/dynamic-texture-projection-after-a-csg-subtraction/22852/17 "2021-08-13T22:12:33Z")

</div>

I upgraded the version and still get the same issue

mat1.Vertex\_Definitions(` varying vec3 vLocalPos; `);

It is thinking that this is a function.

I wonder why it works in the Playground…

\*\* UPDATED - I forgot to change the standard material to CustomMaterial. Works now \*\*

---

<div class="post-metadata">

**Author:** ![sholand](https://avatars.discourse-cdn.com/v4/letter/s/c5a1d2/32.png) [@sholand](https://forum.babylonjs.com/u/sholand)\
**Post date:** [August 14, 2021, 1:49am UTC](https://forum.babylonjs.com/t/dynamic-texture-projection-after-a-csg-subtraction/22852/18 "2021-08-14T01:49:06Z")

</div>

@Evgeni_Popov Almost working perfectly!

In my production code, I needed to switch the Z direction by rotating the mesh before my CSG.

Bottom line, using this change,  
matPrinted.Fragment\_Custom\_Diffuse(` vec3 d1 = dFdx(vLocalPos); vec3 d2 = dFdy(vLocalPos); if (abs(d1.z) > 1e-3 || abs(d2.z) > 1e-3) baseColor.rgb = vec3(0.2, 0.2, 0.2); `);  
I was able to modify your code a bit to get the Texture on sides (in what is now my Z direction to turn black. but the bottom still has the texture

 ![Screen Shot 2021-08-13 at 9.39.12 PM](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/3/6/362e930b49533c1f81b48db049e05e8558a14acc.jpeg)

I have tried everything I could think of but can’t figure out how to keep the top texture and turn the bottom black… ) here is snapshot of the bottom with some subtracted meshes

 ![Screen Shot 2021-08-13 at 9.39.20 PM](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/8/9/899509bf65193956534b0368bc7ab1c778c91c10.jpeg)

Thanks!!!

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [August 14, 2021, 8:21am UTC](https://forum.babylonjs.com/t/dynamic-texture-projection-after-a-csg-subtraction/22852/19 "2021-08-14T08:21:52Z")

</div>

In this formula:

```auto
if (abs(d1.y) > 1e-3 || abs(d2.y) > 1e-3 || vLocalPos.y < 0.5) baseColor.rgb = vec3(0.2, 0.2, 0.2);

```

it’s the `vLocalPos.y < 0.5` which is handling the color of the flat part under the part. You will need to adapt it if the top/bottom axis is not the Y axis for your mesh.

---

<div class="post-metadata">

**Author:** ![sholand](https://avatars.discourse-cdn.com/v4/letter/s/c5a1d2/32.png) [@sholand](https://forum.babylonjs.com/u/sholand)\
**Post date:** [August 14, 2021, 2:25pm UTC](https://forum.babylonjs.com/t/dynamic-texture-projection-after-a-csg-subtraction/22852/20 "2021-08-14T14:25:38Z")

</div>

@Evgeni_Popov I was able to use this if (vLocalPos.z \> 5.75) baseColor.rgb = vec3(0.2, 0.2, 0.2); and ir worked nicely.

Is there a way to make the vLocalPos.z \> 5.75 to work with a variable? like vLocalPos.z \> shapeDepth

Thanks!

[Next page](https://forum.babylonjs.com/t/dynamic-texture-projection-after-a-csg-subtraction/22852.md?page=2)
