I’m trying to port a WebGLRenderer shader patch from onBeforeCompile to TSL nodes for WebGPURenderer, but no luck yet.
Here’s the old code, working:
// threeMat is a MeshPhysicalMaterial
threeMat.onBeforeCompile = (params, renderer) => {
params.fragmentShader = params.fragmentShader.replace(
/*glsl*/ `vec4 diffuseColor = vec4( diffuse, opacity );`,
/*glsl*/ `vec4 diffuseColor = vec4( diffuse, opacity );
float edgePos = vMapUv.x;
float pixelWidth = fwidth(edgePos);
float fade = pixelWidth * 1.5;
float alphaFactor = smoothstep(-fade, 0.0, edgePos);
diffuseColor.a *= alphaFactor;
if (diffuseColor.a <= 0.0) discard;
`,
)
}
Here’s the new code, not working:
const antiAliasClippingNode = Fn(() => {
const mapUv = uv()
const edgePos = mapUv.x
const pixelWidth = fwidth(edgePos)
const fade = pixelWidth.mul(1.5)
const alphaFactor = smoothstep(fade.negate(), 0.0, edgePos)
If(diffuseColor.a.mul(alphaFactor).lessThanEqual(0.0), () => Discard())
return alphaFactor
})
// threeMat is now a MeshPhysicalNodeMaterial
threeMat.opacityNode = antiAliasClippingNode()
Is there anything obviously wrong?
Before screenshot (expected):
After screenshot (not working):
The reason I’m clipping is so that I can show multiple materials on the same mesh. I translate the texture, and clip/antialias/discard the pixels for the material that should be on top.
In this case the black material should be on top, and when clipped, it should show the teal wood material.
I feel like I missing something small, but not obvious. ![]()

