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

Fix prepass renderer when rendering alpha blended materials

Open Popov72 opened this issue 3 years ago • 0 comments

Currently, when the prepass renderer (PPR) renders alpha blended materials, it must set the blend states accordingly because the first texture of the MRT is the color texture: the colors must be correctly blended in this texture.

However, the other textures of the MRT (normal, position, depth, ...) also inherits the alpha blended settings! It is in general wrong, as there's no meaning to alpha blend the normals/positions/...

When the alpha mode of the material is ALPHA_COMBINE it is working as expected because the blend equation is SRC ALPHA * SRC + (1 - SRC ALPHA) * DEST with SRC ALPHA sets either as 0 or 1 by the fragment shader, depending on the alpha value. So, the final data recorded in the normal/position/... textures will be either the data computed in the fragment shader or the data already store in the texture will be preserved, which is ok.

However, if the blend equation is different (so if the material is not using ALPHA_COMBINE) the normal/position/... textures won't be correct.

For eg, in this PG: https://playground.babylonjs.com/#CHNWS7#30

When alphaMode=ALPHA_COMBINE for the big sphere: image (first 1/3 of picture=colors, second 1/3=normals, third 1/3=positions)

If alphaMode=ALPHA_MULTIPLY: image

To fix the problem, we must set different blend states for texture 0 and textures 1/2/3/... of the PPR MRT => we should implement this extension

Popov72 avatar Sep 21 '22 12:09 Popov72