# Decal on a Flat view

**URL:** <https://forum.babylonjs.com/t/decal-on-a-flat-view/34871>\
**Category:** Questions\
**Created:** [October 17, 2022, 2:47pm UTC](https://forum.babylonjs.com/t/decal-on-a-flat-view/34871 "2022-10-17T14:47:25Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![PichouPichou](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/pichoupichou/32/316_2.png) [@PichouPichou](https://forum.babylonjs.com/u/PichouPichou)\
**Post date:** [October 17, 2022, 2:47pm UTC](https://forum.babylonjs.com/t/decal-on-a-flat-view/34871/1 "2022-10-17T14:47:25Z")

</div>

Hello everyone,

I need to display a decal on a plane view of my model’s texture.  
As you can see in this playground: [https://playground.babylonjs.com/#XWBES1#387](https://playground.babylonjs.com/#XWBES1#387)  
There are the model and the texture associated with the albedo above it on a plane. When I click on the model, a decal is added to it and what I need is to display the decal on my plane in the correct position based on where I clicked.  
I tried several things but none worked so far.

Thanks for your help  
PS: @Deltakosh, It seems I have failed 🤗 😄

---

<div class="post-metadata">

**Author:** ![Blake](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/blake/32/21509_2.png) [@Blake](https://forum.babylonjs.com/u/Blake)\
**Post date:** [October 17, 2022, 3:26pm UTC](https://forum.babylonjs.com/t/decal-on-a-flat-view/34871/2 "2022-10-17T15:26:46Z")

</div>

Heya, so I used getTextureCoordinates to get the UVs and multiplied them by the plane size to get the position on the plane. And just used (0, 0, -1) for the normal since that’s what direction the plane is facing. And also had to disable backFaceCulling to see the decals from behind when rotating. 🙂

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

EDIT: Hmm I noticed it looks odd from behind because the texture is mirrored on back of the plane. But you could e.g. reverse the backUVs and then clone the decal with position.x multiplied by -1. Then it looks more normal when clicking on the back of the model IMO. 🙂

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

---

<div class="post-metadata">

**Author:** ![PichouPichou](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/pichoupichou/32/316_2.png) [@PichouPichou](https://forum.babylonjs.com/u/PichouPichou)\
**Post date:** [October 17, 2022, 5:35pm UTC](https://forum.babylonjs.com/t/decal-on-a-flat-view/34871/3 "2022-10-17T17:35:37Z")

</div>

Thanks a lot @Blake ! This is exactly what I was looking for.

I made further tests using a custom texture with a rotation and it works great: [https://playground.babylonjs.com/#XWBES1#390](https://playground.babylonjs.com/#XWBES1#390)

The only thing which does not match yet between the Shirt and the Plane is the scale of the decal. If you put it on the back of the Shirt where it is written “Plasma purpose”, you can clearly see that it is way bigger on the plane. I could use a fixed ratio to adjust it but I want that stuff to work whatever the model and the UV maps. Do you know a way to do that?

---

<div class="post-metadata">

**Author:** ![Blake](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/blake/32/21509_2.png) [@Blake](https://forum.babylonjs.com/u/Blake)\
**Post date:** [October 17, 2022, 6:10pm UTC](https://forum.babylonjs.com/t/decal-on-a-flat-view/34871/4 "2022-10-17T18:10:54Z")

</div>

Your welcome. 🙂 Hmm but I can’t think of any way to calculate that size ratio programmatically… That’s a tricky one. 🤔 Ooh it looks like the front decal needs to be rotated 180 degrees too. 👇

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

---

<div class="post-metadata">

**Author:** ![PichouPichou](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/pichoupichou/32/316_2.png) [@PichouPichou](https://forum.babylonjs.com/u/PichouPichou)\
**Post date:** [October 18, 2022, 10:05am UTC](https://forum.babylonjs.com/t/decal-on-a-flat-view/34871/5 "2022-10-18T10:05:47Z")

</div>

With this model, you can just divide the decal size by 2 and it seems to be perfect:

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

Then I tried with another model which contains several meshes and it stops working:

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

Maybe the function getTextureCoordinates stops being accurate with several Meshes?

---

<div class="post-metadata">

**Author:** ![Blake](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/blake/32/21509_2.png) [@Blake](https://forum.babylonjs.com/u/Blake)\
**Post date:** [October 18, 2022, 10:50am UTC](https://forum.babylonjs.com/t/decal-on-a-flat-view/34871/6 "2022-10-18T10:50:05Z")

</div>

Hmm it looks like the issue with the UVs is that they need to have wrapping applied (these ones weren’t between 0 and 1). I did the uv wrapping/sanitizing on line 88. EDIT: actually that looks like it’s working pretty well but v is inverted for this one I guess… 🙂

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

---

<div class="post-metadata">

**Author:** ![PichouPichou](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/pichoupichou/32/316_2.png) [@PichouPichou](https://forum.babylonjs.com/u/PichouPichou)\
**Post date:** [October 18, 2022, 12:11pm UTC](https://forum.babylonjs.com/t/decal-on-a-flat-view/34871/7 "2022-10-18T12:11:52Z")

</div>

Cool, I understand that the UV Map on the model I used was not the best. One thing to be very careful about for this use case indeed!

Now I said rotation works but in this example, you can see that the rotation of the decal is always the same on the plane when it should also depend on how it is applied to the 3D Mesh.

My instinct is that it is the normal of the 3D Decal that might allow us to determine the correct rotation on the plane but I wouldn’t know what formula to use.

---

<div class="post-metadata">

**Author:** ![Blake](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/blake/32/21509_2.png) [@Blake](https://forum.babylonjs.com/u/Blake)\
**Post date:** [October 18, 2022, 2:41pm UTC](https://forum.babylonjs.com/t/decal-on-a-flat-view/34871/8 "2022-10-18T14:41:18Z")

</div>

The rotation of the decals looks okay to me. Maybe you could use a screenshot to show/highlight what you mean? 🤔

But if you’re talking about the v inversion thou, that could be fixed by inverting v when creating the plane (lines 31 and 32) and inverting the v returned by getTextureCoordinates (line 94). Not sure how to tell which models that would need to be done for thou, I guess it depends on how the UVs were mapped for that model… ☹

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

---

<div class="post-metadata">

**Author:** ![PichouPichou](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/pichoupichou/32/316_2.png) [@PichouPichou](https://forum.babylonjs.com/u/PichouPichou)\
**Post date:** [October 18, 2022, 3:42pm UTC](https://forum.babylonjs.com/t/decal-on-a-flat-view/34871/9 "2022-10-18T15:42:42Z")

</div>

Yep this is better after your v inversion. Actually, it is even more correct with these frontUVs/backUVs parameters as it shows the texture as it really is so this is awesome thanks!

Here is a screenshot to highlight the rotation issue:

 ![Screenshot 2022-10-18 at 17.20.33](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/e/f/ef50427838c35824da7ac2618f9f5c8cae95107c.png)

If we follow the stitching both in 3D and 2D (red lines), we can see that the text is not applied the same way. I would say that on a flat surface it works great but as soon as you go to edges or curvy parts of the Tshirt, it becomes less reliable.  
Is there a way to correct that rotation with the normal vector from pick maybe?

---

<div class="post-metadata">

**Author:** ![Blake](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/blake/32/21509_2.png) [@Blake](https://forum.babylonjs.com/u/Blake)\
**Post date:** [October 18, 2022, 5:07pm UTC](https://forum.babylonjs.com/t/decal-on-a-flat-view/34871/10 "2022-10-18T17:07:35Z")

</div>

Heya, so I’m still struggling to understand how it should look and why it should look that way… Maybe it will make sense to people more familiar with modeling thou, I’m really more of a code monkey than an artist/modeler LOL so this part is stretching my brain! 😓 😉 😄

---

<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:** [November 10, 2022, 10:45am UTC](https://forum.babylonjs.com/t/decal-on-a-flat-view/34871/11 "2022-11-10T10:45:53Z")

</div>

Hello @PichouPichou just checking in if you have any more questions

---

<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:** [November 10, 2022, 9:26pm UTC](https://forum.babylonjs.com/t/decal-on-a-flat-view/34871/12 "2022-11-10T21:26:07Z")

</div>

Maybe this can help you:

> **[Another take at decals](https://babylonjs.medium.com/another-take-at-decals-ef8ec19221a1)**
>
> Before talking about the real subject of this post, let me introduce myself in a few words because it is the custom when a new team member…

It describes a technic to project decals into a texture and then use this texture as an additional diffuse texture when rendering the mesh.

---

<div class="post-metadata">

**Author:** ![PichouPichou](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/pichoupichou/32/316_2.png) [@PichouPichou](https://forum.babylonjs.com/u/PichouPichou)\
**Post date:** [November 16, 2022, 9:31am UTC](https://forum.babylonjs.com/t/decal-on-a-flat-view/34871/13 "2022-11-16T09:31:22Z")

</div>

Thanks for your additional pieces of information.  
@Evgeni_Popov for my need the text must not be distorted on the Texture view so it doesn’t work but that was an interesting read and I learned a bit more about Projection Matrix 👍

The best idea I have so far is to do several picks along the text decal on the 3D view to then calculate a better approximation of the rotation needed on the texture view.

The project has been put on hold on my side, that’s why I didn’t come back on this thread 😉
