Compiling Node Materials with Instances on Safari

I seem to have run in an error, where node materials would not compile on safari browser. It seems to throw an error related to gl_instanceID undeclared identifier (sorry I do not own a mac product and could not copy paste it). It seems to be related to this issue: ios - Use of undeclared identifier 'gl_InstanceID' - Stack Overflow

Are instances normally supported on OpenGLES2.0 / Safari by BabylonJS? If so, then I guess the new node materials might miss a fallback function? I guess it is related to the Instances Node Block.

I hope the issue is reproducable through this playground: Babylon.js Playground

Kind regards!

@Evgeni_Popov, is awesome with it ?

I could test on a iPhone SE. It reports that it supports instanced arrays but does not support gl_InstanceID whereas it should. Or at least it should support gl_InstanceIDEXT but it does not support it neither…

So, I don’t know how we should handle that…

Note that enabling the WebGL2 experimental support make the PG work.

Safari FTW !!! maybe we could detect the wrong use on those devices and display a custom message ?

I havent tested if PBRMaterials work. But so far the issue only appears, if I use the Node Material. Would this mean that gl_InstanceIDEXT works for other materials, but not for Node Material?

Only the node material Instances block uses the gl_InstanceID variable, the standard/PBR materials don’t use it. This variable is only provided by the node material if the user needs it for some reason but the Instances block would work without exposing this value…

Could we only include it if supported, like if ios do not expose it ???

Yes, but how to detect that gl_InstanceID is supported? iOS does support it in some cases as when I enabled WebGL2 experimental support it did work… Maybe if iOS + WebGL1? Also, I could not test on MacOS as I don’t have a Mac, we would need to know if gl_InstanceID is supported on MacOS + WebGL1.

yup in capabilities we could add a special one and at the moment put like = badOs && webgl1 :wink:

Then we could update based on the issues we ll have later ?

Ok, here’s the PR:


Hi @Evgeni_Popov @sebavan

I just got his issue again in Safari on Mac and iPhone whit BabylonJS 4.2 and I see that his PR is not working anymore. I got the same issue. This fix is not working anymore or what is the reason for this?


The PR was bugged and has been fixed in march this year, so for 5.0 only.

What you can do is set engine.getCaps().canUseGLInstanceID = !engine.getCaps().canUseGLInstanceID; to disable usage of gl_InstanceID (the bug was that the meaning of canUseGLInstanceID was reversed).

1 Like

Hi @Evgeni_Popov

So there is a solution for Babylon 4.2 or not? Cause This solution is for Babylon 5 As I understand it?


No, 5.0 has the fixed PR in so there’s nothing to do with 5.0.

engine.getCaps().canUseGLInstanceID = !engine.getCaps().canUseGLInstanceID; has to be done for 4.2

Hi @Evgeni_Popov

This value does not exist in BabylonJS 4.2 when I try to set it up? And adding it as it is is not making the problem go away.

Or I’m missing something?


This returns undefined and in Chrome and in Safari.


Indeed, the first (bugged) fix was already for Babylon 5.0!

So, you will need 5.0 to have this fixed. For a hacky way to make it work in 4.2, you can overload the faulty function:


WOW thank you for that :slight_smile: