Example #1
0
        private void getAlphaMode(BabylonPBRBaseSimpleMaterial babylonMaterial, out GLTFMaterial.AlphaMode alphaMode, out float?alphaCutoff)
        {
            alphaMode   = GLTFMaterial.AlphaMode.OPAQUE;
            alphaCutoff = 0.5f;
            switch (babylonMaterial.transparencyMode)
            {
            case (int)BabylonPBRMetallicRoughnessMaterial.TransparencyMode.OPAQUE:
                alphaMode = GLTFMaterial.AlphaMode.OPAQUE;
                break;

            case (int)BabylonPBRMetallicRoughnessMaterial.TransparencyMode.ALPHABLEND:
                alphaMode = GLTFMaterial.AlphaMode.BLEND;
                break;

            case (int)BabylonPBRMetallicRoughnessMaterial.TransparencyMode.ALPHATEST:
                alphaCutoff = babylonMaterial.alphaCutOff;
                alphaMode   = GLTFMaterial.AlphaMode.MASK;
                break;

            case (int)BabylonPBRMetallicRoughnessMaterial.TransparencyMode.ALPHATESTANDBLEND:
                logger.RaiseWarning("GLTFExporter.Material | Alpha test and blend mode is not supported in glTF. Alpha blend is used instead.", 3);
                alphaMode = GLTFMaterial.AlphaMode.BLEND;
                break;

            default:
                logger.RaiseWarning("GLTFExporter.Material | Unsupported transparency mode: " + babylonMaterial.transparencyMode, 3);
                break;
            }
        }
        /// <summary>
        /// Export dedicated to SpecGloss Material
        /// </summary>
        /// <param name="materialNode">the material node interface</param>
        /// <param name="babylonScene">the scene to export the material</param>
        private void ExportPbrSpecGlossMaterial(IIGameMaterial materialNode, BabylonScene babylonScene)
        {
            // build material decorator
            PbrSpecGlossDecorator maxDecorator = new PbrSpecGlossDecorator(materialNode);
            // get the custom babylon attribute decorator
            BabylonCustomAttributeDecorator babylonDecorator = maxDecorator.BabylonCustomAttributes;

            // the target material
            var babylonMaterial = new BabylonPBRSpecularGlossinessMaterial(maxDecorator.Id)
            {
                maxGameMaterial     = materialNode,
                name                = maxDecorator.Name,
                backFaceCulling     = babylonDecorator.BackFaceCulling,
                doubleSided         = !babylonDecorator.BackFaceCulling,
                separateCullingPass = babylonDecorator.SeparateCullingPass,
                isUnlit             = babylonDecorator.IsUnlit,
                baseColor           = maxDecorator.BaseColor.ToArray(),
            };

            // --- Global ---
            if (babylonMaterial.isUnlit)
            {
                // Ignore values
                babylonMaterial.specularColor = BabylonPBRBaseSimpleMaterial.BlackColor();
                babylonMaterial.glossiness    = 0;
            }
            else
            {
                babylonMaterial.glossiness    = maxDecorator.Glossiness;
                babylonMaterial.specularColor = maxDecorator.SpecularColor.ToArray();
                babylonMaterial.emissive      = maxDecorator.EmitColor.ToArray();
            }

            // --- Textures ---
            float[] multiplyColor = null;
            if (exportParameters.exportTextures)
            {
                ITexmap diffuseTexMap = maxDecorator.BaseColorMap;
                ITexmap alphaTexMap   = maxDecorator.OpacityMap;
                bool    isOpacity     = true;
                babylonMaterial.diffuseTexture = ExportBaseColorAlphaTexture(diffuseTexMap, alphaTexMap, babylonMaterial.baseColor, babylonMaterial.alpha, babylonScene, out multiplyColor, isOpacity);
                if (multiplyColor != null)
                {
                    babylonMaterial.baseColor = multiplyColor;
                }

                if (!babylonMaterial.isUnlit)
                {
                    // Metallic, roughness, ambient occlusion
                    ITexmap specularTexMap         = maxDecorator.SpecularMap;
                    ITexmap glossinessTexMap       = maxDecorator.GlossinessMap;
                    ITexmap ambientOcclusionTexMap = maxDecorator.AmbientOcclusionMap;

                    if (specularTexMap != null || glossinessTexMap != null)
                    {
                        // Merge Specular and Glossiness
                        RaiseVerbose("Merge Specular and Glossiness", 2);
                        BabylonTexture specularGlossinessTexture = ExportSpecularGlossinessTexture(babylonMaterial.specularColor, specularTexMap, babylonMaterial.glossiness, glossinessTexMap, babylonScene);
                        babylonMaterial.specularGlossinessTexture = specularGlossinessTexture;
                    }

                    if (ambientOcclusionTexMap != null)
                    {
                        // Simply export occlusion texture
                        RaiseVerbose("Export occlusion texture", 2);
                        babylonMaterial.occlusionTexture = ExportTexture(ambientOcclusionTexMap, babylonScene);
                    }

                    var     normalMapAmount = maxDecorator.BumpMapAmount;
                    ITexmap normalTexMap    = maxDecorator.NormalMap;
                    babylonMaterial.normalTexture = ExportTexture(normalTexMap, babylonScene, normalMapAmount);

                    ITexmap emitTexMap = maxDecorator.EmitColormMap;
                    babylonMaterial.emissiveTexture = ExportTexture(emitTexMap, babylonScene);

                    if (babylonMaterial.specularGlossinessTexture != null && !babylonDecorator.UseMaxFactor)
                    {
                        babylonMaterial.glossiness    = glossinessTexMap != null ? 1.0f : 0.0f;
                        babylonMaterial.specularColor = specularTexMap != null?BabylonPBRBaseSimpleMaterial.WhiteColor() : BabylonPBRBaseSimpleMaterial.BlackColor();
                    }
                }
            }


            // --- Finalize ---
            if (babylonMaterial.alpha != 1.0f || (babylonMaterial.diffuseTexture != null && babylonMaterial.diffuseTexture.hasAlpha))
            {
                babylonMaterial.transparencyMode = (int)BabylonMaterial.TransparencyMode.ALPHABLEND;
            }

            if (babylonMaterial.transparencyMode == (int)BabylonMaterial.TransparencyMode.ALPHATEST)
            {
                // Set the alphaCutOff value explicitely to avoid different interpretations on different engines
                // Use the glTF default value rather than the babylon one
                babylonMaterial.alphaCutOff = 0.5f;
            }

            if (babylonMaterial.emissiveTexture != null)
            {
                babylonMaterial.emissive = new[] { 1.0f, 1.0f, 1.0f };
            }


            // List all babylon material attributes
            // Those attributes are currently stored into the native material
            // They should not be exported as extra attributes
            var doNotExport = BabylonCustomAttributeDecorator.ListPrivatePropertyNames().ToList();

            // Export the custom attributes of this material
            babylonMaterial.metadata = ExportExtraAttributes(materialNode, babylonScene, doNotExport);

            if (exportParameters.pbrFull)
            {
                var fullPBR = new BabylonPBRMaterial(babylonMaterial)
                {
                    directIntensity      = babylonDecorator.DirectIntensity,
                    emissiveIntensity    = babylonDecorator.EmissiveIntensity,
                    environmentIntensity = babylonDecorator.EnvironementIntensity,
                    specularIntensity    = babylonDecorator.SpecularIntensity,
                    maxGameMaterial      = babylonMaterial.maxGameMaterial
                };
                babylonScene.MaterialsList.Add(fullPBR);
            }
            else
            {
                // Add the material to the scene
                babylonScene.MaterialsList.Add(babylonMaterial);
            }
        }