three.js icon indicating copy to clipboard operation
three.js copied to clipboard

MeshPhysicalMaterial should use the thin surface model when thickness is zero

Open WestLangley opened this issue 3 years ago • 7 comments

The thin-surface model and the volume model should be separate models in the MeshPhysicalMaterial shader.

Currently, only the volume model is implemented, and if thickness is 0, all components of attenuation color must be non-zero, otherwise the material renders black. A properly-implemented thin-surface model would prevent that.

Maybe define USE_VOLUME if ( .thickness > 0 ). Then something like:

#ifdef USE_TRANSMISSION

	#ifdef USE_TRANSMISSIONMAP

		transmissionFactor *= texture2D( transmissionMap, vUv ).r;

	#endif

	#ifdef USE_THICKNESSMAP

		thicknessFactor *= texture2D( thicknessMap, vUv ).g;

	#endif

	#ifdef USE_VOLUME

		vec4 transmission = getIBLVolumeRefraction( ... );

	#else

		vec4 transmission = getIBLThinSurfaceRefraction( ... );

	#endif

	totalDiffuse = mix( totalDiffuse, transmission.rgb, transmissionFactor );

#endif

three.js version: r.137

WestLangley avatar Feb 09 '22 17:02 WestLangley

Anyone interested in having a go at this?

WestLangley avatar Feb 09 '22 17:02 WestLangley

I can give it a go. Where do I find getIBLThinSurfaceRefraction?

mrdoob avatar Feb 09 '22 23:02 mrdoob

I think it can be inferred by comparing the following two sections:

https://github.com/KhronosGroup/glTF/tree/main/extensions/2.0/Khronos/KHR_materials_transmission#implementation-notes

and

https://github.com/KhronosGroup/glTF/tree/main/extensions/2.0/Khronos/KHR_materials_volume#implementation.

I haven't tried, however. It's not easy to comprehend -- until, of course, you do. :-)

WestLangley avatar Feb 10 '22 00:02 WestLangley

It'll take me a bit too long to comprehend...

@elalish do you know if any other engine/viewer has already written a getIBLThinSurfaceRefraction?

mrdoob avatar Feb 10 '22 19:02 mrdoob

Babylon is usually pretty quick with supporting glTF extensions; I'd check there. Thin is pretty easy because it means no refraction (your view vector passes straight through to the IBL). The only thing you have to account for is getting the right mip from the roughness, but that should be basically the same as for volume. I believe attenuation color is defined as part of the volume spec, so for thin transmission it's not a thing. Maybe just give it a default (1, 1, 1) value to turn it off?

elalish avatar Feb 10 '22 19:02 elalish

/ping @donmccurdy ...just in case you are not aware of this...

WestLangley avatar Sep 11 '22 00:09 WestLangley

Thanks @WestLangley! I haven't run into this problem yet in working with MeshPhysicalMaterial, but I agree with your suggestions here. 👍

donmccurdy avatar Sep 16 '22 13:09 donmccurdy