Beispiel #1
0
        private ModelData.Material Process(Material rawMaterial)
        {
            var material   = new ModelData.Material();
            var properties = material.Properties;

            // Setup all default properties for this material
            if (rawMaterial.HasBlendMode)
            {
                properties.SetProperty(MaterialKeysBase.BlendMode, (MaterialBlendMode)rawMaterial.BlendMode);
            }
            if (rawMaterial.HasBumpScaling)
            {
                properties.SetProperty(MaterialKeysBase.BumpScaling, rawMaterial.BumpScaling);
            }
            if (rawMaterial.HasColorAmbient)
            {
                properties.SetProperty(MaterialKeysBase.ColorAmbient, ConvertColor(rawMaterial.ColorAmbient));
            }
            if (rawMaterial.HasColorDiffuse)
            {
                properties.SetProperty(MaterialKeysBase.ColorDiffuse, ConvertColor(rawMaterial.ColorDiffuse));
            }
            if (rawMaterial.HasColorEmissive)
            {
                properties.SetProperty(MaterialKeysBase.ColorEmissive, (Color3)ConvertColor(rawMaterial.ColorEmissive));
            }
            if (rawMaterial.HasColorReflective)
            {
                properties.SetProperty(MaterialKeysBase.ColorReflective, ConvertColor(rawMaterial.ColorReflective));
            }
            if (rawMaterial.HasColorSpecular)
            {
                properties.SetProperty(MaterialKeysBase.ColorSpecular, (Color3)ConvertColor(rawMaterial.ColorSpecular));
            }
            if (rawMaterial.HasColorTransparent)
            {
                properties.SetProperty(MaterialKeysBase.ColorTransparent, ConvertColor(rawMaterial.ColorTransparent));
            }
            if (rawMaterial.HasName)
            {
                properties.SetProperty(MaterialKeysBase.Name, rawMaterial.Name);
            }
            if (rawMaterial.HasOpacity)
            {
                properties.SetProperty(MaterialKeysBase.Opacity, rawMaterial.Opacity);
            }
            if (rawMaterial.HasReflectivity)
            {
                properties.SetProperty(MaterialKeysBase.Reflectivity, rawMaterial.Reflectivity);
            }
            if (rawMaterial.HasShininess)
            {
                properties.SetProperty(MaterialKeysBase.Shininess, rawMaterial.Shininess);
            }
            if (rawMaterial.HasShininessStrength)
            {
                properties.SetProperty(MaterialKeysBase.ShininessStrength, rawMaterial.ShininessStrength);
            }
            if (rawMaterial.HasShadingMode)
            {
                properties.SetProperty(MaterialKeysBase.ShadingMode, (MaterialShadingMode)rawMaterial.ShadingMode);
            }
            if (rawMaterial.HasTwoSided)
            {
                properties.SetProperty(MaterialKeysBase.TwoSided, rawMaterial.IsTwoSided);
            }
            if (rawMaterial.HasWireFrame)
            {
                properties.SetProperty(MaterialKeysBase.Wireframe, rawMaterial.IsWireFrameEnabled);
            }

            // Iterate on other properties
            foreach (var rawProperty in rawMaterial.GetAllProperties())
            {
                var key = rawProperty.FullyQualifiedName;
                if (!properties.ContainsKey(key) && !AssimpMaterialDefaultNames.Contains(rawProperty.FullyQualifiedName))
                {
                    // Texture properties will be added after
                    if (!rawProperty.FullyQualifiedName.StartsWith("$tex"))
                    {
                        // Just use our own key for this material
                        if (key == "$mat.refracti,0,0")
                        {
                            key = "Refraction";
                        }

                        const string matNamePrefix = "$mat.";
                        if (key.StartsWith(matNamePrefix) && key.Length > matNamePrefix.Length)
                        {
                            var newName = key.Substring(matNamePrefix.Length);
                            key = new StringBuilder().Append(char.ToUpperInvariant(newName[0])).Append(newName.Substring(1)).ToString();
                        }

                        if (properties.ContainsKey(key))
                        {
                            continue;
                        }

                        switch (rawProperty.PropertyType)
                        {
                        case PropertyType.String:
                            properties.Add(key, rawProperty.AsString());
                            break;

                        case PropertyType.Float:
                            switch (rawProperty.ByteCount / 4)
                            {
                            case 1:
                                properties.Add(key, rawProperty.AsFloat());
                                break;

                            case 2:
                                properties.Add(key, new Vector2(rawProperty.AsFloatArray()));
                                break;

                            case 3:
                                properties.Add(key, new Vector3(rawProperty.AsFloatArray()));
                                break;

                            case 4:
                                properties.Add(key, new Vector4(rawProperty.AsFloatArray()));
                                break;

                            case 16:
                                properties.Add(key, new Matrix(rawProperty.AsFloatArray()));
                                break;
                            }
                            break;

                        case PropertyType.Integer:
                            switch (rawProperty.ByteCount / 4)
                            {
                            case 1:
                                properties.Add(key, rawProperty.AsInteger());
                                break;

                            default:
                                properties.Add(key, rawProperty.AsIntegerArray());
                                break;
                            }
                            break;

                        case PropertyType.Buffer:
                            properties.Add(key, rawProperty.RawData);
                            break;
                        }
                    }
                }
            }

            // Process textures
            foreach (TextureType textureType in Enum.GetValues(typeof(TextureType)))
            {
                if (textureType != TextureType.None)
                {
                    var textures = rawMaterial.GetTextures(textureType);
                    if (textures != null)
                    {
                        var materialTextures = new List <ModelData.MaterialTexture>();
                        material.Textures.Add(string.Format("{0}Texture", textureType), materialTextures);

                        foreach (var textureSlot in textures)
                        {
                            var textureFilePath    = textureSlot.FilePath.Replace(@"/", @"\");
                            var textureWinFilePath = textureFilePath;

                            // Remove any .\
                            while (textureFilePath.StartsWith(@".\"))
                            {
                                textureFilePath = textureFilePath.Substring(2);
                            }

                            var newTextureSlot = new ModelData.MaterialTexture()
                            {
                                FilePath    = textureFilePath,
                                BlendFactor = textureSlot.BlendFactor,
                                Operation   = (MaterialTextureOperator)textureSlot.Operation,
                                Index       = (int)textureSlot.TextureIndex,
                                UVIndex     = (int)textureSlot.UVIndex,
                                WrapMode    = ConvertWrapMode(textureSlot.WrapMode),
                                Flags       = (MaterialTextureFlags)textureSlot.Flags
                            };

                            var pathToFullTexture = Path.Combine(modelDirectory, textureWinFilePath);
                            if (!File.Exists(pathToFullTexture))
                            {
                                logger.Warning(string.Format("Texture [{0}] not found from path [{1}] for model [{2}]. Skip it", textureSlot.FilePath, pathToFullTexture, modelFilePath));
                            }

                            materialTextures.Add(newTextureSlot);
                        }
                    }
                }
            }

            return(material);
        }
Beispiel #2
0
        private ModelData.Material Process(Material rawMaterial)
        {
            var material = new ModelData.Material();
            var properties = material.Properties;

            // Setup all default properties for this material
            if (rawMaterial.HasBlendMode) properties.SetProperty(MaterialKeysBase.BlendMode, (MaterialBlendMode)rawMaterial.BlendMode);
            if (rawMaterial.HasBumpScaling) properties.SetProperty(MaterialKeysBase.BumpScaling, rawMaterial.BumpScaling);
            if (rawMaterial.HasColorAmbient) properties.SetProperty(MaterialKeysBase.ColorAmbient, ConvertColor(rawMaterial.ColorAmbient));
            if (rawMaterial.HasColorDiffuse) properties.SetProperty(MaterialKeysBase.ColorDiffuse, ConvertColor(rawMaterial.ColorDiffuse));
            if (rawMaterial.HasColorEmissive) properties.SetProperty(MaterialKeysBase.ColorEmissive, (Color3)ConvertColor(rawMaterial.ColorEmissive));
            if (rawMaterial.HasColorReflective) properties.SetProperty(MaterialKeysBase.ColorReflective, ConvertColor(rawMaterial.ColorReflective));
            if (rawMaterial.HasColorSpecular) properties.SetProperty(MaterialKeysBase.ColorSpecular, (Color3)ConvertColor(rawMaterial.ColorSpecular));
            if (rawMaterial.HasColorTransparent) properties.SetProperty(MaterialKeysBase.ColorTransparent, ConvertColor(rawMaterial.ColorTransparent));
            if (rawMaterial.HasName) properties.SetProperty(MaterialKeysBase.Name, rawMaterial.Name);
            if (rawMaterial.HasOpacity) properties.SetProperty(MaterialKeysBase.Opacity, rawMaterial.Opacity);
            if (rawMaterial.HasReflectivity) properties.SetProperty(MaterialKeysBase.Reflectivity, rawMaterial.Reflectivity);
            if (rawMaterial.HasShininess) properties.SetProperty(MaterialKeysBase.Shininess, rawMaterial.Shininess);
            if (rawMaterial.HasShininessStrength) properties.SetProperty(MaterialKeysBase.ShininessStrength, rawMaterial.ShininessStrength);
            if (rawMaterial.HasShadingMode) properties.SetProperty(MaterialKeysBase.ShadingMode, (MaterialShadingMode)rawMaterial.ShadingMode);
            if (rawMaterial.HasTwoSided) properties.SetProperty(MaterialKeysBase.TwoSided, rawMaterial.IsTwoSided);
            if (rawMaterial.HasWireFrame) properties.SetProperty(MaterialKeysBase.Wireframe, rawMaterial.IsWireFrameEnabled);

            // Iterate on other properties
            foreach (var rawProperty in rawMaterial.GetAllProperties())
            {
                var key = rawProperty.FullyQualifiedName;
                if (!properties.ContainsKey(key) && !AssimpMaterialDefaultNames.Contains(rawProperty.FullyQualifiedName))
                {
                    // Texture properties will be added after
                    if (!rawProperty.FullyQualifiedName.StartsWith("$tex"))
                    {
                        // Just use our own key for this material
                        if (key == "$mat.refracti,0,0") key = "Refraction";

                        const string matNamePrefix = "$mat.";
                        if (key.StartsWith(matNamePrefix) && key.Length > matNamePrefix.Length)
                        {
                            var newName = key.Substring(matNamePrefix.Length);
                            key = new StringBuilder().Append(char.ToUpperInvariant(newName[0])).Append(newName.Substring(1)).ToString();
                        }

                        if (properties.ContainsKey(key))
                        {
                            continue;
                        }

                        switch (rawProperty.PropertyType)
                        {
                            case PropertyType.String:
                                properties.Add(key, rawProperty.AsString());
                                break;
                            case PropertyType.Float:
                                switch (rawProperty.ByteCount / 4)
                                {
                                    case 1:
                                        properties.Add(key, rawProperty.AsFloat());
                                        break;
                                    case 2:
                                        properties.Add(key, new Vector2(rawProperty.AsFloatArray()));
                                        break;
                                    case 3:
                                        properties.Add(key, new Vector3(rawProperty.AsFloatArray()));
                                        break;
                                    case 4:
                                        properties.Add(key, new Vector4(rawProperty.AsFloatArray()));
                                        break;
                                    case 16:
                                        properties.Add(key, new Matrix(rawProperty.AsFloatArray()));
                                        break;
                                }
                                break;
                            case PropertyType.Integer:
                                switch (rawProperty.ByteCount / 4)
                                {
                                    case 1:
                                        properties.Add(key, rawProperty.AsInteger());
                                        break;
                                    default:
                                        properties.Add(key, rawProperty.AsIntegerArray());
                                        break;
                                }
                                break;
                            case PropertyType.Buffer:
                                properties.Add(key, rawProperty.RawData);
                                break;
                        }
                    }
                }
            }

            // Process textures
            foreach (TextureType textureType in Enum.GetValues(typeof(TextureType)))
            {
                if (textureType != TextureType.None)
                {
                    var textures = rawMaterial.GetTextures(textureType);
                    if (textures != null)
                    {
                        var materialTextures = new List<ModelData.MaterialTexture>();
                        material.Textures.Add(string.Format("{0}Texture", textureType), materialTextures);

                        foreach (var textureSlot in textures)
                        {
                            var textureFilePath = textureSlot.FilePath.Replace(@"/", @"\");
                            var textureWinFilePath = textureFilePath;

                            // Remove any .\
                            while (textureFilePath.StartsWith(@".\"))
                            {
                                textureFilePath = textureFilePath.Substring(2);
                            }

                            var newTextureSlot = new ModelData.MaterialTexture()
                                                     {
                                                         FilePath = textureFilePath,
                                                         BlendFactor = textureSlot.BlendFactor,
                                                         Operation = (MaterialTextureOperator)textureSlot.Operation,
                                                         Index = (int)textureSlot.TextureIndex,
                                                         UVIndex = (int)textureSlot.UVIndex,
                                                         WrapMode = ConvertWrapMode(textureSlot.WrapMode),
                                                         Flags = (MaterialTextureFlags)textureSlot.Flags
                                                     };

                            var pathToFullTexture = Path.Combine(modelDirectory, textureWinFilePath);
                            if (!File.Exists(pathToFullTexture))
                            {
                                logger.Warning(string.Format("Texture [{0}] not found from path [{1}] for model [{2}]. Skip it", textureSlot.FilePath, pathToFullTexture, modelFilePath));
                            }

                            materialTextures.Add(newTextureSlot);
                        }
                    }
                }
            }

            return material;
        }