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

Small error in handling of `KHR_materials_volume` attenuation color

Open flashk opened this issue 2 years ago • 0 comments

Hello,

I noticed an issue with models that use KHR_materials_volume with certain attenuation color values. The function applyVolumeAttenuation in punctual.glsl has the following line:

vec3 attenuationCoefficient = -log(attenuationColor) / attenuationDistance;

The result of log(0) is undefined, so any color channels with a value of 0 will result in an invalid attenuation coefficient. For example, I modified DragonAttenuation.gltf to have an attenuationColor of [1.0, 0.0, 0.0] and increased attenuationDistance to 10.0. This should result in the dragon having a very faint red color due to the large attenuation distance, but it shows up as a bright red in the viewer, as if there were a very small attenuation distance.

This can be fixed by clamping the attenuation color to some minimum non-zero value before passing it into the log function:

vec3 attenuationCoefficient = -log(max(attenuationColor, vec3(1.0e-5))) / attenuationDistance;

flashk avatar Aug 29 '23 06:08 flashk