I’m trying to multiply a custom opacity with the built-in opacity.
With WebGLRenderer, it could be done with shader patching like this:
threeMat.onBeforeCompile = (params, renderer) => {
params.fragmentShader = params.fragmentShader.replace(
/*glsl*/ `vec4 diffuseColor = vec4( diffuse, opacity );`,
/*glsl*/ `vec4 diffuseColor = vec4( diffuse, opacity );
float alphaFactor = /*...custom multiplier...*/ 0.1234; // using a constant for sake of example
diffuseColor.a *= alphaFactor;
`,
)
}
With WebGPURenderer, I’ve no luck achieving the same yet, although I bet I’m missing something simple. Here’s the TSL code, but it has no effect on opacity:
material.opacityNode = Fn(() => {
const alphaFactor = /*...custom multiplier...*/ Const(0.1234); // using a constant for sake of example
return diffuseColor.a.mul(alphaFactor) // this is not working
})()
Here’s a live JSFiddle example using TSL. In the example, the end result is not multiplied:
const material = new THREE.MeshPhongNodeMaterial( {
map,
transparent: true,
opacity: 0.000001,
} );
material.opacityNode = Fn(() => {
const alphaFactor = Const(0.5)
return diffuseColor.a.mul(alphaFactor) // not working, opacity shows up as 0.5, not 0.5 * 0.000001
})()
I’m expecting the same result as in this fiddle where I explicitly set the opacity value to the expected result of 0.000001 * 0.5:
const material = new THREE.MeshPhongNodeMaterial( {
map,
transparent: true,
opacity: 0.000001 * 0.5, // this works (but it is not dynamic TSL)
} );
// material.opacityNode = Fn(() => {
// const alphaFactor = Const(0.5)
// return diffuseColor.a.mul(alphaFactor) // not working, opacity shows up as 0.5, not 0.5 * 0.000001
// })()