public static bool HasRuntimeAccess(IMtl mat)
 {
     if (mat.IsMultiMtl)
     {
         for (int i = 0; i < mat.NumSubMtls; i++)
         {
             IMtl childMat = mat.GetSubMtl(i);
             if (childMat != null)
             {
                 if (class_ID.Equals(childMat.ClassID))
                 {
                     int p = Tools.GetMaterialProperty(childMat, "uniqueInContainer");
                     if (Convert.ToBoolean(p))
                     {
                         return(true);
                     }
                 }
             }
         }
     }
     else if (class_ID.Equals(mat.ClassID))
     {
         int p = Tools.GetMaterialProperty(mat, "uniqueInContainer");
         if (Convert.ToBoolean(p))
         {
             return(true);
         }
     }
     return(false);
 }
Exemplo n.º 2
0
        MaterialInformation GetMaterialProperties(IMtl material, MessageMaterialRequest request)
        {
            if (material == null)
            {
                return null;
            }

            if (request.m_materialIndex < 0)
            {
                return GetMaterialProperties(material);
            }

            return GetMaterialProperties(material.GetSubMtl(request.m_materialIndex));
        }
        public static bool HasFlightSimMaterials(IMtl mat)
        {
            if (mat.IsMultiMtl)
            {
                for (int i = 0; i < mat.NumSubMtls; i++)
                {
                    IMtl childMat = mat.GetSubMtl(i);
                    if (childMat != null && class_ID.Equals(childMat.ClassID))
                    {
                        return(true);
                    }
                }
            }
            else if (mat != null && class_ID.Equals(mat.ClassID))
            {
                return(true);
            }


            return(false);
        }
        private void ExportMaterial(IMtl materialNode, BabylonScene babylonScene)
        {
            var name = materialNode.Name;
            var id = materialNode.GetGuid().ToString();

            RaiseMessage(name, 1);

            if (materialNode.NumSubMtls > 0)
            {
                var babylonMultimaterial = new BabylonMultiMaterial();
                babylonMultimaterial.name = name;
                babylonMultimaterial.id = id;

                var guids = new List<string>();

                for (var index = 0; index < materialNode.NumSubMtls; index++)
                {
                    var subMat = materialNode.GetSubMtl(index);

                    if (subMat != null)
                    {
                        guids.Add(subMat.GetGuid().ToString());

                        if (!referencedMaterials.Contains(subMat))
                        {
                            referencedMaterials.Add(subMat);
                            ExportMaterial(subMat, babylonScene);
                        }
                    }
                    else
                    {
                        guids.Add(Guid.Empty.ToString());
                    }
                }

                babylonMultimaterial.materials = guids.ToArray();

                babylonScene.MultiMaterialsList.Add(babylonMultimaterial);
                return;
            }


            var babylonMaterial = new BabylonMaterial();
            babylonMaterial.name = name;
            babylonMaterial.id = id;

            

            babylonMaterial.ambient = materialNode.GetAmbient(0, false).ToArray();
            babylonMaterial.diffuse = materialNode.GetDiffuse(0, false).ToArray();
            babylonMaterial.specular = materialNode.GetSpecular(0, false).Scale(materialNode.GetShinStr(0, false));
            babylonMaterial.specularPower = materialNode.GetShininess(0, false) * 256;

            babylonMaterial.emissive = materialNode.GetSelfIllumColorOn(0, false) ? materialNode.GetSelfIllumColor(0, false).ToArray() : materialNode.GetDiffuse(0, false).Scale(materialNode.GetSelfIllum(0, false));
            babylonMaterial.alpha = 1.0f - materialNode.GetXParency(0, false);

            var stdMat = materialNode.GetParamBlock(0).Owner as IStdMat2;

            if (stdMat != null)
            {
                babylonMaterial.backFaceCulling = !stdMat.TwoSided;
                babylonMaterial.wireframe = stdMat.Wire;

                // Textures
                babylonMaterial.ambientTexture = ExportTexture(stdMat, 0, babylonScene);                // Ambient
                babylonMaterial.diffuseTexture = ExportTexture(stdMat, 1, babylonScene);                // Diffuse
                babylonMaterial.specularTexture = ExportTexture(stdMat, 2, babylonScene);               // Specular
                babylonMaterial.emissiveTexture = ExportTexture(stdMat, 5, babylonScene);               // Emissive
                babylonMaterial.opacityTexture = ExportTexture(stdMat, 6, babylonScene, false, true);   // Opacity
                babylonMaterial.bumpTexture = ExportTexture(stdMat, 8, babylonScene);                   // Bump
                babylonMaterial.reflectionTexture = ExportTexture(stdMat, 9, babylonScene, true);       // Reflection

                // Constraints
                if (babylonMaterial.diffuseTexture != null)
                {
                    babylonMaterial.diffuse = new [] { 1.0f, 1.0f, 1.0f };
                }

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

                if (babylonMaterial.opacityTexture != null && babylonMaterial.diffuseTexture != null &&
                    babylonMaterial.diffuseTexture.name == babylonMaterial.opacityTexture.name &&
                    babylonMaterial.diffuseTexture.hasAlpha && !babylonMaterial.opacityTexture.getAlphaFromRGB)
                {
                    // This is a alpha testing purpose
                    babylonMaterial.opacityTexture = null;
                    babylonMaterial.diffuseTexture.hasAlpha = true;
                    RaiseWarning("Opacity texture was removed because alpha from diffuse texture can be use instead", 2);
                    RaiseWarning("If you do not want this behavior, just set Alpha Source = None on your diffuse texture", 2);
                }
            }

            babylonScene.MaterialsList.Add(babylonMaterial);
        }
        private void ExportMaterial(IMtl materialNode, BabylonScene babylonScene)
        {
            var name = materialNode.Name;
            var id   = materialNode.GetGuid().ToString();

            RaiseMessage(name, 1);

            if (materialNode.NumSubMtls > 0)
            {
                var babylonMultimaterial = new BabylonMultiMaterial {
                    name = name, id = id
                };

                var guids = new List <string>();

                for (var index = 0; index < materialNode.NumSubMtls; index++)
                {
                    var subMat = materialNode.GetSubMtl(index);

                    if (subMat != null)
                    {
                        guids.Add(subMat.GetGuid().ToString());

                        if (!referencedMaterials.Contains(subMat))
                        {
                            referencedMaterials.Add(subMat);
                            ExportMaterial(subMat, babylonScene);
                        }
                    }
                    else
                    {
                        guids.Add(Guid.Empty.ToString());
                    }
                }

                babylonMultimaterial.materials = guids.ToArray();

                babylonScene.MultiMaterialsList.Add(babylonMultimaterial);
                return;
            }


            var babylonMaterial = new BabylonMaterial
            {
                name          = name,
                id            = id,
                ambient       = materialNode.GetAmbient(0, false).ToArray(),
                diffuse       = materialNode.GetDiffuse(0, false).ToArray(),
                specular      = materialNode.GetSpecular(0, false).Scale(materialNode.GetShinStr(0, false)),
                specularPower = materialNode.GetShininess(0, false) * 256,
                emissive      =
                    materialNode.GetSelfIllumColorOn(0, false)
                        ? materialNode.GetSelfIllumColor(0, false).ToArray()
                        : materialNode.GetDiffuse(0, false).Scale(materialNode.GetSelfIllum(0, false)),
                alpha = 1.0f - materialNode.GetXParency(0, false)
            };


            var stdMat = materialNode.GetParamBlock(0).Owner as IStdMat2;

            if (stdMat != null)
            {
                babylonMaterial.backFaceCulling = !stdMat.TwoSided;
                babylonMaterial.wireframe       = stdMat.Wire;

                // Textures
                BabylonFresnelParameters fresnelParameters;

                babylonMaterial.ambientTexture = ExportTexture(stdMat, 0, out fresnelParameters, babylonScene);                // Ambient
                babylonMaterial.diffuseTexture = ExportTexture(stdMat, 1, out fresnelParameters, babylonScene);                // Diffuse
                if (fresnelParameters != null)
                {
                    babylonMaterial.diffuseFresnelParameters = fresnelParameters;
                }

                babylonMaterial.specularTexture = ExportTexture(stdMat, 2, out fresnelParameters, babylonScene);               // Specular
                babylonMaterial.emissiveTexture = ExportTexture(stdMat, 5, out fresnelParameters, babylonScene);               // Emissive
                if (fresnelParameters != null)
                {
                    babylonMaterial.emissiveFresnelParameters = fresnelParameters;
                    if (babylonMaterial.emissive[0] == 0 &&
                        babylonMaterial.emissive[1] == 0 &&
                        babylonMaterial.emissive[2] == 0)
                    {
                        babylonMaterial.emissive = new float[] { 1, 1, 1 };
                    }
                }

                babylonMaterial.opacityTexture = ExportTexture(stdMat, 6, out fresnelParameters, babylonScene, false, true);   // Opacity
                if (fresnelParameters != null)
                {
                    babylonMaterial.opacityFresnelParameters = fresnelParameters;
                }

                babylonMaterial.bumpTexture       = ExportTexture(stdMat, 8, out fresnelParameters, babylonScene);             // Bump
                babylonMaterial.reflectionTexture = ExportTexture(stdMat, 9, out fresnelParameters, babylonScene, true);       // Reflection
                if (fresnelParameters != null)
                {
                    if (babylonMaterial.reflectionTexture == null)
                    {
                        RaiseWarning("Fallout cannot be used with reflection channel without a texture", 2);
                    }
                    else
                    {
                        babylonMaterial.reflectionFresnelParameters = fresnelParameters;
                    }
                }

                // Constraints
                if (babylonMaterial.diffuseTexture != null)
                {
                    babylonMaterial.diffuse = new [] { 1.0f, 1.0f, 1.0f };
                }

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

                if (babylonMaterial.opacityTexture != null && babylonMaterial.diffuseTexture != null &&
                    babylonMaterial.diffuseTexture.name == babylonMaterial.opacityTexture.name &&
                    babylonMaterial.diffuseTexture.hasAlpha && !babylonMaterial.opacityTexture.getAlphaFromRGB)
                {
                    // This is a alpha testing purpose
                    babylonMaterial.opacityTexture          = null;
                    babylonMaterial.diffuseTexture.hasAlpha = true;
                    RaiseWarning("Opacity texture was removed because alpha from diffuse texture can be use instead", 2);
                    RaiseWarning("If you do not want this behavior, just set Alpha Source = None on your diffuse texture", 2);
                }
            }

            babylonScene.MaterialsList.Add(babylonMaterial);
        }