glTF-Sample-Viewer icon indicating copy to clipboard operation
glTF-Sample-Viewer copied to clipboard

KHR_material_volume: refraction inconsistency?

Open echadwick-artist opened this issue 5 years ago • 11 comments

https://github.com/KhronosGroup/glTF-Sample-Environments/issues/7 My apologies if I originally posted this in the incorrect location.

2021-04-12_glTF-Sample-Viewer_Babylon-js-Sandbox

echadwick-artist avatar Apr 12 '21 20:04 echadwick-artist

We will have a look at it.

UX3D-nopper avatar Apr 13 '21 05:04 UX3D-nopper

The reason for visual differences compared to a ray caster might be the surface normal of the exiting ray. Our rasterization-based algorithm (and Babylon.js) assumes the same surface normal at the entry point and at the exit point of the volume. This means, as soon as the ray leaves the volume, the ray is parallel to the incident ray. As the beads change their normals abruptly, a ray caster delivers better results especially in this case.

The reason for visual differences compared to Babylon.js is the KHR_materials_volume.thicknessFactor. The model has a thicknessFactor of 0.02, however, Babylon.js seems to ignore this thicknessFactor and assumes a thicknessFactor of 1.

Attached is a rendering done in Sample Viewer of a version of the stained glass lamp where I have manually set the thicknessFactor to 1. With this, you get the same visual result compared to Babylon.js.

StainedGlassLamp_thickness_1

Screenshot (246) Screenshot (245)

UX3D-kanzler avatar Apr 13 '21 15:04 UX3D-kanzler

Ah, thank you.

I wonder why a proper thickness value appears incorrect, compared to reference photography? The bead shapes in the model aren't a perfect match, but should be close enough. (oh sorry, re-reading your reply, it's the exit ray!) 2021-04-13_reference

I've set the thickness a bit too generous, the horizontal thickness ranges between 0.013 and 0.007 meters. image

Same model, in the Enterprise PBR Sample Renderer pathtracer: 2021-04-12 17_01_10-Window

So does this mean I should be setting the thickness to 1 meter?

echadwick-artist avatar Apr 13 '21 16:04 echadwick-artist

So does this mean I should be setting the thickness to 1 meter?

In this case yes, however, it is more of a lucky chance that this looks similar to raytraced refraction.

I wonder why a proper thickness value appears incorrect, compared to reference photography?

This is one of the limitations of the rasterized version of the volume extension. The rasterizer cannot take the geometry of the transmissive object into account, it only uses the thickness value as specified in the material for all fragments of the transmissive object. This can look good enough, as with the mosquito in amber model, but will not be able to display effects involving complex optics such as e.g. lenses, diamond shapes, or drops.

I've set the thickness a bit too generous, the horizontal thickness ranges between 0.013 and 0.007 meters.

For many assets measuring the thickness can be a good starting point, for us it helped to tweak the parameter in a live preview to get the most pleasing results.

mobecher avatar Apr 14 '21 09:04 mobecher

As Eric stated in the call today, we can close it.

UX3D-nopper avatar Apr 14 '21 16:04 UX3D-nopper

Thanks, this resolves the issue for me.

echadwick-artist avatar Apr 14 '21 16:04 echadwick-artist

Apparently thicknessFactor should only be used to modulate attenuationColor, and not IOR? See this comment: https://github.com/BabylonJS/Babylon.js/issues/10193#issuecomment-820362391

echadwick-artist avatar Apr 15 '21 13:04 echadwick-artist

Responding to this comment https://github.com/BabylonJS/Babylon.js/issues/10193#issuecomment-820362391

I can see in the Khronos GLTF viewer that the IOR is modified by the thickness factor (or at least it's what it seems): image

From my understanding of the spec, IOR should not be modified by the volume extension...

glTF Sample Viewer also does not modify the IOR value for different thickness factors.

In accordance with the spec

We use the thickness map to estimate the distance that a light ray will travel until it exists the volume

we use the thickness value to determine the length of the ray in the volume. Therefore higher values can lead to a larger offset of the ray

mobecher avatar Apr 15 '21 14:04 mobecher

There's no thickness map in this asset, so the thickness is a constant all over the mesh. Also, it seems you change the direction of the refracted ray according to some calculation involving the thickness before querying the refraction texture (at least that's what the deformation inside each sphere suggests): that's something we don't do in Babylon, so that would explain the difference.

Popov72 avatar Apr 15 '21 19:04 Popov72

There's no thickness map in this asset, so the thickness is a constant all over the mesh.

We combine thicknessFactor and the value sampled from thicknessTexture (which defaults to 1.0 if no thicknessTexture is specified) by multiplication to a thickness value. This value is then used as the length of the refracted ray. Therefore if no thicknessTexture is specified, the thickness value will be constant for the material.

Also, it seems you change the direction of the refracted ray according

The direction of the refraced ray is not changed by the thickness value, the effect in the above mentioned screenshot is only the result of different ray lengths.

UX3D-kanzler avatar Apr 16 '21 11:04 UX3D-kanzler

Ok thanks for the explanation. I have also looked at the shader code to better understand. We do have a depth parameter in Babylon that is a multiplier for the ray, but it is a user defined value that existed before the thickness support and so which is not related to it. I guess we could add an option to also multiply by the thickness value when available...

Popov72 avatar Apr 16 '21 12:04 Popov72