Multiple bounce refractions using glsl refract()

Hi all, does anyone have any examples of using glsl refract() via a material plugin? Just trying to figure out the right workflow and steps for this as the docs are a little limiting when it comes to material plugins.

Im trying to essentially port this over to bjs

https://codesandbox.io/p/sandbox/zqrreo

I don’t think a material plugin is the right solution, because it’s used to inject code into existing standard/PBR materials, whereas the file you’ve linked implements a complete material. So a ShaderMaterial would probably be more appropriate.

Note, however, that it won’t be an easy task to port the code, because there’s a lot more to it than the code we see in MeshRefractionMaterial.tsx: there are a number of imports you’ll need to port, including “three-mesh-bvh”, which has both a js and a glsl part.

Is the refractionTexture not a good fit ?

They are doing ray tracing to better simulate refractions inside the diamond (hence the “bvh” package).

1 Like

@sebvan and @Evgeni_Popov , we’re trying to do multiple bounces for the refraction for accurate jewelry/gemstone rendering. We can do two bounces using just refraction texture but what if we want to do more (ideally 3 or 4). Feels like the approach above is the way to go unless there is a way to hack refractionTexture to do 3 bounces?

I guess I’ll start here and see how far I get.
https://doc.babylonjs.com/contribute/toBabylon/matForMatLibrary

Yes, I think that ray tracing is the best solution if you want to compute multiple bounces, for accurate results. Wrapping everything in a new material would be the best indeed, the hard part will be the port of the bvh library.

ya, that the bvh lib is key. Was kinda hoping you guys had something internally or in the community that did what bvh does. found this but its non-gpu based and the dev stopped working on it.

sure would be nice to have a bvh lib in BJS, thats a big ask but would open up a lot of possibilities for rendering.

I will try to tinker with bvh a little in the mean time but you’ll get a lot of loves if someone internally could look into this.