Enable KHR settings (transmission) in runtime

Hello everyone,

I’m trying to enable the extension KHR_materials_transmission for a material in runtime.
I have a model that has among other things KHR_materials_transmission enabled, but I can’t manage to enable this manually:
image

I have created this simple playground with 2 example models:

You can un/comment the model URL to compare. Is it possible at all to enable this manually or is it (currently) only possible by setting it in the model file itself? Where within babylon is it generally stored which extensions are enabled/applied to materials in the first place?

1 Like

You can’t manually do what the transmission extension does because it’s currently not possible to access the transmission helper class from a user code, see these posts for context:

There is a bounty for obtaining a texture of the opaque objects in the scene:

Once this texture has been obtained, simply use it as a refraction texture to simulate transmission.

Someone tried to make a PR out of it, but it didn’t work out:

I see, so from what I can gather it’s currently not possible to enable/add the extension after loading the model, mostly due to the current flow of the babylon.js engine? Like we can’t add the material to whatever list keeps track of where this extension logic should be applied?

And we can’t replicate the behavior either because we currently have no (good) way to get the texture the extension generates (a RenderTargetTexture of all opaque objects in the screen). I suppose we can’t replicate whatever the extension does internally because there’s no good “hook” or “observer” point to attach to where we could replicate and insert this?

No, the extension is parsed at loading time, and the transmission helper is created only if this extension exists.

Maybe you could “inject” this extension yourself during load, if we have a way to do it… Let’s ask @bghgary if it’s possible (be patient, he will be back next week).

You can’t replicate it because the TransmissionHelper class (which is used to implement the feature) is not available to user code. Actually, you can replicate it if you copy/paste the class in your own project :). It’s implemented here:

2 Likes

Yes, this is possible. If you use the onParsedObservable of the glTF loader, you can modify the glTF JSON before it loads. This is quite hacky though.

2 Likes

Thanks, with this I managed to create a working example:

https://playground.babylonjs.com/#592X04#7

I don’t image I can change any of the settings after I’ve enabled them?
e.g: changing the transmissionFactor of a material

You can change the parameters on the material. For eg, transmissionFactor is the intensity of the refraction effect:

Programmatically:

scene.getMaterialByName("Without KHR").subSurface.refractionIntensity = 0.5;