Shadow camera and layers

Hello. and happy new year. :fireworks:

So, I have a project that I need working on mobile and desktop.
You know what it means:
Mobile needs optimised content, and dektop has a big screen that will seem empty without lots of details.

So I’m tryint to use layers for that.
In 3DSmax I use the babylon.js to export gltf with some tags.
I parse them and depending on the tag I put them on different layers.
No tag, it should always render.
“low” tag, should render only in low details mode, “high” is for high details mode, bith respectively default mode for mobile and desktop.

It works fine, I am quite happy, but I have an issue with layers and shadows.

When it makes sense I want to use some of the low polygon models to cast shadows, even when in high detail mode.
I know how to do that when using writeColor to false on the material.
But 1/ There are some material I reuse on the scene, seems wastefull to have to clone them.
2/ That would require parsing every object everytime I change between high resolution mode.

Layers seem much more appropriate. But somehow it doesn’t work the way I tried:
A bit of code is beter to explain than a long post I guess, so here is a jsfiddle with comments on the code please have a look:
http://jsfiddle.net/3x0pn6wc/1/
Click on the preview to switch between lox details and high details mode.
And as you can see, although I set the directional light shadow camera layers to 1, where only the elements I want casting or receiving shadows are, the shadow map seems to be rendered depending on the layer set on the scene camera ( 2 or 3 ).

I tried to check the three.js code… but it’s a maze to me.
So can anybody please tell me if:
This is possible to do it this way?
What I am doing wrong?
Where to tweek the three.js code so that the shadow map is generated with the layers I actually ask it to use?

Thanks a bunch.

Out of curiosity, can I ask why you chose to use a bitwise operation in the line
if ( camera.layers.mask & 1 << 2 ) ?

To switch between layer 2 and 3 I have to know which layer I am actually rendering.
And unless I am wrong there are no function to test what layers are enabled or disabled.
The only parameter you can access is the mask and do a bitewise operation to know which is enabled.
Here 1 << 2 checks if layer 2 is enabled. 1 << 3 would check the third bite, therefore the layer 3.
Then using set will enable the layer and disable any other.

I guess I could have simply used an extra variable to store the state…