Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 23 additions & 9 deletions src/renderers/webgl/WebGLShadowMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -302,17 +302,31 @@ function WebGLShadowMap( renderer, objects, capabilities ) {

}

result.alphaMap = material.alphaMap;
result.alphaTest = material.alphaTest;
result.map = material.map;
if ( customMaterial === undefined ) {
Copy link
Collaborator

@Mugen87 Mugen87 Mar 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you introduce this new if statement?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously, when a user provided customDepthMaterial, the original code overwrote properties like alphaMap or alphaTest of the customMaterial without this if statement

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mind testing this version of getDepthMaterial()? Only two lines need to be changed and the codesandbox works:

function getDepthMaterial( object, material, light, type ) {

	let result = null;

	const customMaterial = ( light.isPointLight === true ) ? object.customDistanceMaterial : object.customDepthMaterial;

	if ( customMaterial !== undefined ) {

		result = customMaterial;

	} else {

		result = ( light.isPointLight === true ) ? _distanceMaterial : _depthMaterial;

		if ( ( renderer.localClippingEnabled && material.clipShadows === true && Array.isArray( material.clippingPlanes ) && material.clippingPlanes.length !== 0 ) ||
			( material.displacementMap && material.displacementScale !== 0 ) ||
			( material.alphaMap && material.alphaTest > 0 ) ||
			( material.map && material.alphaTest > 0 ) ||
			( material.alphaToCoverage === true ) ) {

			// in this case we need a unique material instance reflecting the
			// appropriate state

			const keyA = result.uuid, keyB = material.uuid;

			let materialsForVariant = _materialCache[ keyA ];

			if ( materialsForVariant === undefined ) {

				materialsForVariant = {};
				_materialCache[ keyA ] = materialsForVariant;

			}

			let cachedMaterial = materialsForVariant[ keyB ];

			if ( cachedMaterial === undefined ) {

				cachedMaterial = result.clone();
				materialsForVariant[ keyB ] = cachedMaterial;
				material.addEventListener( 'dispose', onMaterialDispose );

			}

			result = cachedMaterial;

		}

	}

	result.visible = material.visible;
	result.wireframe = material.wireframe;

	if ( type === VSMShadowMap ) {

		result.side = ( material.shadowSide !== null ) ? material.shadowSide : material.side;

	} else {

		result.side = ( material.shadowSide !== null ) ? material.shadowSide : shadowSide[ material.side ];

	}

	result.alphaMap = material.alphaMap;
	result.alphaTest = ( material.alphaToCoverage === true ) ? 0.5 : material.alphaTest;
	result.map = material.map;

	result.clipShadows = material.clipShadows;
	result.clippingPlanes = material.clippingPlanes;
	result.clipIntersection = material.clipIntersection;

	result.displacementMap = material.displacementMap;
	result.displacementScale = material.displacementScale;
	result.displacementBias = material.displacementBias;

	result.wireframeLinewidth = material.wireframeLinewidth;
	result.linewidth = material.linewidth;

	if ( light.isPointLight === true && result.isMeshDistanceMaterial === true ) {

		const materialProperties = renderer.properties.get( result );
		materialProperties.light = light;

	}

	return result;

}

Copy link
Collaborator

@Mugen87 Mugen87 Mar 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To further explain the issue: alphaToCoverage requires a MSAA context which is not available when rendering shadow maps. The idea is to "emulate" the transparency by setting alphaTest to a fixed value. The resulting shadows should be a good approximation.


result.clipShadows = material.clipShadows;
result.clippingPlanes = material.clippingPlanes;
result.clipIntersection = material.clipIntersection;
result.alphaMap = material.alphaMap;
result.map = material.map;

result.displacementMap = material.displacementMap;
result.displacementScale = material.displacementScale;
result.displacementBias = material.displacementBias;
// Check if the original material uses alphaToCoverage
if ( material.alphaToCoverage ) {

result.alphaTest = 0.5;

} else {

result.alphaTest = material.alphaTest;

}

result.clipShadows = material.clipShadows;
result.clippingPlanes = material.clippingPlanes;
result.clipIntersection = material.clipIntersection;

result.displacementMap = material.displacementMap;
result.displacementScale = material.displacementScale;
result.displacementBias = material.displacementBias;

}

result.wireframeLinewidth = material.wireframeLinewidth;
result.linewidth = material.linewidth;
Expand Down
Loading