/*
 * Adapted from Dice So Nice! 6.4.5 by Simone Ricciardi, JDW and contributors.
 * Upstream commit b365efc7433eb2db2ccbd91da2029ac6c8b78ed5.
 * Modified for the standalone Beranth’ahl lab on 2026-11-06.
 * GNU AGPL version 3.0; see LICENSE.md and UPSTREAM.md. No warranty.
 */
// Verbatim upstream normal blending, preserved as a source reference.
	static normalBlendingShaderFragment(shader) {
		shader.fragmentShader = /* glsl */`
			uniform sampler2D detailNormalMap;
			uniform float detailNormalScale;
			uniform vec2 normalMapUvOffset;
			uniform vec2 normalMapUvRepeat;
			${shader.fragmentShader}
		`.replace(
			/* glsl */`#include <normal_fragment_maps>`,
			/* glsl */`#ifdef USE_NORMALMAP_OBJECTSPACE

				normal = texture2D( normalMap, vNormalMapUv ).xyz * 2.0 - 1.0;

				#ifdef FLIP_SIDED
					normal = - normal;
				#endif

				#ifdef DOUBLE_SIDED
					normal = normal * faceDirection;
				#endif

				normal = normalize( normalMatrix * normal );

			#elif defined( USE_NORMALMAP_TANGENTSPACE )

				vec3 mapN = texture2D( normalMap, vNormalMapUv ).xyz * 2.0 - 1.0;

				#if defined( USE_PACKED_NORMALMAP )
					mapN = vec3( mapN.xy, sqrt( saturate( 1.0 - dot( mapN.xy, mapN.xy ) ) ) );
				#endif

				mapN.xy *= normalScale;

				vec2 detailUv = (vNormalMapUv - normalMapUvOffset) / normalMapUvRepeat;
				vec3 detailN = texture2D( detailNormalMap, detailUv ).xyz * 2.0 - 1.0;
				detailN.xy *= detailNormalScale;
				mapN = normalize( vec3( mapN.xy + detailN.xy, mapN.z ) );

				normal = normalize( tbn * mapN );

			#elif defined( USE_BUMPMAP )

				normal = perturbNormalArb( - vViewPosition, normal, dHdxy_fwd(), faceDirection );

			#endif`
		);
	}
