# BJS 5.5.6 changed rendering order and broke CSS3DRenderer

**URL:** <https://forum.babylonjs.com/t/bjs-5-5-6-changed-rendering-order-and-broke-css3drenderer/37123>\
**Category:** Bugs\
**Tags:** rendering\
**Created:** [January 5, 2023, 9:40am UTC](https://forum.babylonjs.com/t/bjs-5-5-6-changed-rendering-order-and-broke-css3drenderer/37123 "2023-01-05T09:40:14Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![DavidR](https://avatars.discourse-cdn.com/v4/letter/d/3ab097/32.png) [@DavidR](https://forum.babylonjs.com/u/DavidR)\
**Post date:** [January 5, 2023, 9:40am UTC](https://forum.babylonjs.com/t/bjs-5-5-6-changed-rendering-order-and-broke-css3drenderer/37123/1 "2023-01-05T09:40:14Z")

</div>

Hello,  
we are using 3D embedded html element based on CSS3DRenderer. It worked great in BJS 5.5.5, but BJS 5.5.6 changed rendering order and broke it. I think 🤷‍♂️

This is the original [forum thread](https://forum.babylonjs.com/t/youtube-videos-on-a-mesh-port-of-css3drenderer-js/10600) with source project.

You can see the difference here [https://playground.babylonjs.com/#1DX9UE#523](https://playground.babylonjs.com/#1DX9UE#523)  
Simply switch BJS version between 4 and 5.40 and watch the sphere and box overlapping the youtube html element. Unfortunately I don’t know how to switch to specific BJS version, but it works with BJS 4.

Our implementation is in TypeScript and I tried to port it back to the playground [https://playground.babylonjs.com/#EAVSEW#7](https://playground.babylonjs.com/#EAVSEW#7) , but unfortunately my 3D objects are always behind the youtube html element. Guess I incorrectly setup html elements order.  
The important code line is 171:

```auto
container.style.zIndex = '0'

```

If I change it to `-1` the iframe is not visible at all. But that’s different issue I think.

I suspect that the cause of the overlapping change lies in this [bugfix PR](https://github.com/BabylonJS/Babylon.js/pull/12504/files) but cannot confirm it. Could you please take a look? I may not understand the rendering pipeline correctly, perhaps you could give me more information?

Thank you

---

<div class="post-metadata">

**Author:** ![DavidR](https://avatars.discourse-cdn.com/v4/letter/d/3ab097/32.png) [@DavidR](https://forum.babylonjs.com/u/DavidR)\
**Post date:** [January 5, 2023, 9:44am UTC](https://forum.babylonjs.com/t/bjs-5-5-6-changed-rendering-order-and-broke-css3drenderer/37123/2 "2023-01-05T09:44:34Z")

</div>

Here are screenshots with two BJS versions and z-index configurations:

BJS 5.5.5, CSSRenderer.zIndex -1  
Works great

 ![embedded html - babylon 5.5.5, CSSRenderer.zIndex -1](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/4/f/4f24fca9dd1bb74f0c5416acc4f805223dd26728.jpeg)

BJS 5.5.5, CSSRenderer.zIndex 0  
iframe is always in front

 ![embedded html - babylon 5.5.5, CSSRenderer.zIndex 0](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/a/6/a694896f0880391f5d5cf188386958ae1eea0263.jpeg)

BJS 5.5.6, CSSRenderer.zIndex -1  
iframe is not visible at all

 ![embedded html - babylon 5.5.6, CSSRenderer.zIndex -1](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/4/1/41fc8056fd36c2b4e34d976e18c82541e84daf55.jpeg)

BJS 5.5.6, CSSRenderer.zIndex 0  
iframe is always in front

 ![embedded html - babylon 5.5.6, CSSRenderer.zIndex 0](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/0/8/083c0e3a0663421ecd68b2b76c3f4df9edea829f.jpeg)

---

<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:** [January 5, 2023, 10:54am UTC](https://forum.babylonjs.com/t/bjs-5-5-6-changed-rendering-order-and-broke-css3drenderer/37123/3 "2023-01-05T10:54:26Z")

</div>

Babylon.js does not draw meshes in the `scene.meshes` order anymore, but sort them per material instead, for speed optimization reasons.

The PG relies on the plane being drawn first to work.

There are several way to fix it:

- set `renderingGroupId` to 1 for all objects but the plane: [https://playground.babylonjs.com/#1DX9UE#654](https://playground.babylonjs.com/#1DX9UE#654)
- create a material for each object, but make sure to create the material for the plane first. Because the meshes are sorted according to their material id, the plane will be the first to be drawn after sorting: [https://playground.babylonjs.com/#1DX9UE#655](https://playground.babylonjs.com/#1DX9UE#655). Note that in the PG, I updated line 136 to make sure to use my material and not the one created by the CSS renderer
- you can provide your own sorting method. See how it is done here: [Guys i got some thing wrong that make a hole - #3 by Evgeni\_Popov](https://forum.babylonjs.com/t/guys-i-got-some-thing-wrong-that-make-a-hole/36806/3)

---

<div class="post-metadata">

**Author:** ![DavidR](https://avatars.discourse-cdn.com/v4/letter/d/3ab097/32.png) [@DavidR](https://forum.babylonjs.com/u/DavidR)\
**Post date:** [January 5, 2023, 1:36pm UTC](https://forum.babylonjs.com/t/bjs-5-5-6-changed-rendering-order-and-broke-css3drenderer/37123/4 "2023-01-05T13:36:03Z")

</div>

Awesome, thank you! I’ll give it a try.

---

<div class="post-metadata">

**Author:** ![DavidR](https://avatars.discourse-cdn.com/v4/letter/d/3ab097/32.png) [@DavidR](https://forum.babylonjs.com/u/DavidR)\
**Post date:** [January 6, 2023, 9:32am UTC](https://forum.babylonjs.com/t/bjs-5-5-6-changed-rendering-order-and-broke-css3drenderer/37123/5 "2023-01-06T09:32:53Z")

</div>

I chose to alter the rendering group IDs. It works great but there are few more steps needed if you are using more complex scenes with skybox:

1. Use rendering group IDs like this:

```javascript
embeddedHtmlElement = 0 // Plane mesh
skybox = 1 // Box mesh
environment = 2 // 3D objects, avatars, etc.

```

1. When creating scene

```javascript
// Needs to be transparent for underlaying htms/css content (iframe) to be seen
scene.clearColor = new Color4(0, 0, 0, 0)
// Skybox (its rendering group) must NOT clear buffers,
// otherwise rendering groups with higher id (3D environment) won't be visible
scene.setRenderingAutoClearDepthStencil(RenderingGroup.skybox, false, false, false)

```
