Jump to content

Alpha Blend Mode Help


MackeyK24
 Share

Recommended Posts

Yo @Deltakosh and @Sebavan ... I need your help yet again in trying to understand the correct Alpha Blend Mode I should be using for each of the blend mode that unity uses (Opaque - Cutout - Fade - Transparent)

I need help in translating what _SrcBlend and _DstBlend values should equal for babylon. Here is the shader code that hey use to enable the various ALPHABLEND, ALPHATEST, etc...

public static void SetupMaterialWithBlendMode(Material material, BlendMode blendMode)
{
switch (blendMode)
{
case BlendMode.Opaque:
material.SetOverrideTag("RenderType", "");
material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
material.SetInt("_ZWrite", 1);
material.DisableKeyword("_ALPHATEST_ON");
material.DisableKeyword("_ALPHABLEND_ON");
material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
material.renderQueue = -1;
break;
case BlendMode.Cutout:
material.SetOverrideTag("RenderType", "TransparentCutout");
material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
material.SetInt("_ZWrite", 1);
material.EnableKeyword("_ALPHATEST_ON");
material.DisableKeyword("_ALPHABLEND_ON");
material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
material.renderQueue = (int)UnityEngine.Rendering.RenderQueue.AlphaTest;
break;
case BlendMode.Fade:
material.SetOverrideTag("RenderType", "Transparent");
material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
material.SetInt("_ZWrite", 0);
material.DisableKeyword("_ALPHATEST_ON");
material.EnableKeyword("_ALPHABLEND_ON");
material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
material.renderQueue = (int)UnityEngine.Rendering.RenderQueue.Transparent;
break;
case BlendMode.Transparent:
material.SetOverrideTag("RenderType", "Transparent");
material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
material.SetInt("_ZWrite", 0);
material.DisableKeyword("_ALPHATEST_ON");
material.DisableKeyword("_ALPHABLEND_ON");
material.EnableKeyword("_ALPHAPREMULTIPLY_ON");
material.renderQueue = (int)UnityEngine.Rendering.RenderQueue.Transparent;
break;
}
}

 

Here is my code to setup transparency for babylon for the export:

 

private static void DumpTransparency(Texture2D texture, Material material, BabylonSystemMaterial babylonPbrMaterial)
{
// Standard Alpha Transparency Mode
babylonPbrMaterial.alpha = 1.0f;
if (babylonPbrMaterial.albedo != null && babylonPbrMaterial.albedo.Length >= 4) {
babylonPbrMaterial.alpha = babylonPbrMaterial.albedo[3];
}
if (texture != null && texture.alphaIsTransparency == true) {
babylonPbrMaterial.backFaceCulling = false;
babylonPbrMaterial.useAlphaFromAlbedoTexture = true;
if (babylonPbrMaterial.albedoTexture != null) {
babylonPbrMaterial.albedoTexture.hasAlpha = true;
}
} else {
babylonPbrMaterial.useAlphaFromAlbedoTexture = false;
if (babylonPbrMaterial.albedoTexture != null) {
babylonPbrMaterial.albedoTexture.hasAlpha = false;
}
}
babylonPbrMaterial.alphaCutoff = 0.4f;
babylonPbrMaterial.transparencyMode = (int)BabylonTransparencyMode.Opaque;
if (material.HasProperty("_Mode")) {
var blendMode = (BlendMode)material.GetFloat("_Mode");
if (blendMode == BlendMode.Transparent) {
babylonPbrMaterial.alphaMode = 7; // PreMultiply
babylonPbrMaterial.alphaCutoff = 0.4f;
babylonPbrMaterial.transparencyMode = null;
} else if (blendMode == BlendMode.Cutout) {
babylonPbrMaterial.alphaMode = 2; // Default combine - but what should it be ???
babylonPbrMaterial.alphaCutoff = material.HasProperty("_Cutoff") ? material.GetFloat("_Cutoff") : 0.4f;
babylonPbrMaterial.transparencyMode = (int)BabylonTransparencyMode.AlphaTest;
} else if (blendMode == BlendMode.Fade) {
babylonPbrMaterial.alphaMode = 2; // Default combine - but what should it be ???
babylonPbrMaterial.alphaCutoff = material.HasProperty("_FadeCutoff") ? material.GetFloat("_FadeCutoff") : 0.4f;
babylonPbrMaterial.transparencyMode = (int)BabylonTransparencyMode.AlphaBlend;
}
}
}
 

 

From looking at the _SrcBlend and _DstBlend from unity, the only one I can tell for sure is TRANSPARENT... IT SAYS PREMULTIPLY

But what should I use for CUTOUT and FADE

Should FADE be Babylon Alpha Blend Mode Subtractive ()

material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);

material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);

Can anyone PLEASE try to interpret the Unity Blend Info to what that would be in babylon :)

PRETTY PLEASE :):):) 

 

 

 

 

Link to comment
Share on other sites

This is a better DumpTransparency... I just need to know what to set material.alphaMode for the four different mode...

 

I have included the Unity _SrcBlend and _DstBlend and _ALPHAPREMULIPLY as REMARKS to use a guide to try and figure what is the best mode we should use for alpha mode based on the KIND OF properties they are using. Please take a look at the code and try tell me what I should put for material.alphaMode

 

private static void DumpTransparency(Texture2D texture, Material material, BabylonSystemMaterial babylonPbrMaterial)
{
// Standard Alpha Transparency Mode
bool alphaIsTransparency = false;
babylonPbrMaterial.alpha = 1.0f;
babylonPbrMaterial.alphaCutoff = 0.4f;
babylonPbrMaterial.transparencyMode = null;
if (babylonPbrMaterial.albedo != null && babylonPbrMaterial.albedo.Length >= 4) {
babylonPbrMaterial.alpha = babylonPbrMaterial.albedo[3];
}
if (texture != null && texture.alphaIsTransparency == true) {
alphaIsTransparency = true;
babylonPbrMaterial.backFaceCulling = false;
babylonPbrMaterial.useAlphaFromAlbedoTexture = true;
if (babylonPbrMaterial.albedoTexture != null) {
babylonPbrMaterial.albedoTexture.hasAlpha = true;
}
} else {
babylonPbrMaterial.useAlphaFromAlbedoTexture = false;
if (babylonPbrMaterial.albedoTexture != null) {
babylonPbrMaterial.albedoTexture.hasAlpha = false;
}
}
if (material.HasProperty("_Mode")) {
var blendMode = (BlendMode)material.GetFloat("_Mode");
if (blendMode == BlendMode.Opaque) {
//material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
//material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
//babylonPbrMaterial.alphaMode = 2;
 
babylonPbrMaterial.alphaCutoff = 0.4f;
babylonPbrMaterial.transparencyMode = (int)BabylonTransparencyMode.Opaque;
} else if (blendMode == BlendMode.Cutout) {
//material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
//material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
//babylonPbrMaterial.alphaMode = 2;
 
babylonPbrMaterial.alphaCutoff = material.HasProperty("_Cutoff") ? material.GetFloat("_Cutoff") : 0.4f;
babylonPbrMaterial.transparencyMode = (alphaIsTransparency) ? (int)BabylonTransparencyMode.AlphaTestAndBlend : (int)BabylonTransparencyMode.AlphaTest;
} else if (blendMode == BlendMode.Fade) {
//material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
//material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
//babylonPbrMaterial.alphaMode = 2;
 
babylonPbrMaterial.alphaCutoff = material.HasProperty("_FadeCutoff") ? material.GetFloat("_FadeCutoff") : 0.4f;
babylonPbrMaterial.transparencyMode = (alphaIsTransparency) ? (int)BabylonTransparencyMode.AlphaBlend : (int)BabylonTransparencyMode.Opaque;
} else if (blendMode == BlendMode.Transparent) {
//material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
//material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
//material.EnableKeyword("_ALPHAPREMULTIPLY_ON");
//babylonPbrMaterial.alphaMode = 7;
 
babylonPbrMaterial.alphaCutoff = 0.4f;
babylonPbrMaterial.transparencyMode = (alphaIsTransparency) ? (int)BabylonTransparencyMode.AlphaBlend : (int)BabylonTransparencyMode.Opaque;
}
}
}
 

 

 

 

Link to comment
Share on other sites

4 hours ago, Deltakosh said:

You can find here how we deal with glBlend which is the same as the value you have in unity:

https://github.com/BabylonJS/Babylon.js/blob/master/src/Engine/babylon.engine.ts#L3958

Pick the one that works for you

I am not trying to pick one... i sm trying to match the four options that unity uses for alpha mode... i just dont know means SrcBlend One and DstBlend Zero... would tbat be additive or subtractive or combine or what...

And should fade be combine

And should transparent be premultply

Link to comment
Share on other sites

This is what I finalized on Dumping Transparency ... this should be a pretty good match to what unity is doing for Alpha Blending. Please review and tell me what you think:

        private static void DumpTransparency(Texture2D texture, Material material, BabylonSystemMaterial babylonPbrMaterial)
        {
            // Standard Alpha Transparency Mode
            bool alphaIsTransparency = false;
            babylonPbrMaterial.alpha = 1.0f;
            babylonPbrMaterial.alphaMode = 2;
            babylonPbrMaterial.alphaCutoff = material.HasProperty("_Cutoff") ? material.GetFloat("_Cutoff") : 0.4f;
            if (babylonPbrMaterial.albedo != null && babylonPbrMaterial.albedo.Length >= 4) {
                babylonPbrMaterial.alpha = babylonPbrMaterial.albedo[3];
            }
            if (texture != null && texture.alphaIsTransparency == true) {
                alphaIsTransparency = true;
                babylonPbrMaterial.backFaceCulling = false;
                babylonPbrMaterial.useAlphaFromAlbedoTexture = true;
                if (babylonPbrMaterial.albedoTexture != null) {
                    babylonPbrMaterial.albedoTexture.hasAlpha = true;
                }
            } else {
                babylonPbrMaterial.useAlphaFromAlbedoTexture = false;
                if (babylonPbrMaterial.albedoTexture != null) {
                    babylonPbrMaterial.albedoTexture.hasAlpha = false;
                }
            }
            if (material.HasProperty("_Mode")) {
                var blendMode = (BlendMode)material.GetFloat("_Mode");
                if (blendMode == BlendMode.Opaque) {
                    babylonPbrMaterial.alphaMode = 2; // Note: Alpha Not Used For Blending
                    babylonPbrMaterial.transparencyMode = (int)BabylonTransparencyMode.Opaque;
                } else if (blendMode == BlendMode.Cutout) {
                    babylonPbrMaterial.alphaMode = 2; // Note: Alpha Not Used For Blending
                    babylonPbrMaterial.transparencyMode = (int)BabylonTransparencyMode.AlphaTest;
                } else if (blendMode == BlendMode.Fade) {
                    babylonPbrMaterial.alphaMode = 2; // Note: Alpha Combine Blending Mode
                    babylonPbrMaterial.transparencyMode = (alphaIsTransparency) ? (int)BabylonTransparencyMode.AlphaBlend : (int)BabylonTransparencyMode.Opaque;
                } else if (blendMode == BlendMode.Transparent) {
                    babylonPbrMaterial.alphaMode = 7; // Note: Alpha Pre Multiply Blending Mode
                    babylonPbrMaterial.transparencyMode = (alphaIsTransparency) ? (int)BabylonTransparencyMode.AlphaBlend : (int)BabylonTransparencyMode.Opaque;
                }
            }
        }

 

 

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...