#Keyword _ALPHAPREMULTIPLY_ON

1 messages · Page 1 of 1 (latest)

sour bloom
#

_ALPHAPREMULTIPLY_ON
Does anyone know how to make this keyword work in shader graph ?

In the image, it always returns blue whether or not the blend mode is set to Premultiply.

I tried other keywords like _ALPHATEST_ON or _ALPHAMODULATE_ON and they work fine. Why not this one ? Did I miss something ?

mortal carbon
sour bloom
#

Thank you for the response,
the property _BlendModePreserveSpecular adds a checkbox called Preserve Specular lighting when the blend mode is put on Alpha or Additive, it works well but it has nothing to do with Premultiply.

mortal carbon
#

Hmm kinda. Shader Graph's "Premultiply" mode has been a bit of a mess. Afaik it sets the Blend operation correctly but the keyword is a separate thing that Unity defines.

In the past the _ALPHAPREMULTIPLY_ON was linked directly to the Blend Mode, hence the naming. But the purpose of the keyword was to "move the multiply with alpha into the shader". For unlit graphs it would do :

#ifdef _ALPHAPREMULTIPLY_ON
    surfaceDescription.BaseColor *= surfaceDescription.Alpha;
#endif

And for PBR/Lit I think that would only be on the diffuse side of shading calculations, leaving specular alone.

Doing that made it basically equivalent to the Alpha blend mode. There's many threads mentioning that confusing behaviour/bug. e.g.
https://realtimevfx.com/t/im-having-a-hard-time-understanding-how-premultiplied-blend-mode-is-suppose-to-work-in-unity/17552/
https://discussions.unity.com/t/pre-mult-bug-issue-unresolves/734353

#

In 2021.2 the above snippet was removed which fixed that for unlit graphs. But afaik it stayed the same for lit (Premultiply would still diffuse *= alpha)
As of 2022.1 that behaviour was separated. The keyword is the same but despite the name _ALPHAPREMULTIPLY_ON, it is tied to _BlendModePreserveSpecular and not related to the blend mode anymore. Similar to what I linked earlier, this shows when the keyword is enabled :

bool preserveSpecular = (material.HasProperty(Property.BlendModePreserveSpecular) && material.GetFloat(Property.BlendModePreserveSpecular) > 0) && blendMode != BlendMode.Multiply && blendMode != BlendMode.Premultiply;
if (preserveSpecular){
     srcBlendRGB = UnityEngine.Rendering.BlendMode.One;
     material.EnableKeyword(ShaderKeywordStrings._ALPHAPREMULTIPLY_ON);
}

(Could probably confirm by using your graph with Alpha blend mode and toggling the Preserve Specular Lighting option. It should switch between blue/red)

Not sure why they didn't rename the keyword... perhaps for backwards compatibility reasons. But that seems to be how it is (based on reading various branches of the Graphics repo, mainly BaseShaderGUI.cs). If you actually need to know the blend mode, you're probably better off making your own keyword & setting it from a script.