Exemplo n.º 1
0
        protected bool  CreateTextureParameter(FBXImporter.Material _Material, string _PropertyName, Scene.Materials.MaterialParameters _MaterialParameters, string _ParameterName, string _OpacityPropertyName)
        {
            FBXImporter.ObjectProperty Property = _Material.FindProperty(_PropertyName);
            if (Property == null)
            {
                return(false);                  // No such property...
            }
            if (Property.Textures.Length == 0)
            {
                return(false);                  // That property has no texture...
            }
            // Check for opacity
            string OpacityTextureRelativeFileName = null;

            if (_OpacityPropertyName != null)
            {
                FBXImporter.ObjectProperty OpacityProperty = _Material.FindProperty(_OpacityPropertyName);
                if (OpacityProperty != null && OpacityProperty.Textures.Length != 0)
                {
                    OpacityTextureRelativeFileName = OpacityProperty.Textures[0].RelativeFileName;
                }
            }

            // Create the parameter with that texture
            Scene.Materials.Texture2D Tex = m_Scene.CreateTexture(Property.Textures[0].RelativeFileName, OpacityTextureRelativeFileName, Property.Textures[0].UseMipMap);
            _MaterialParameters.CreateParameter(_ParameterName, Scene.Materials.MaterialParameters.PARAMETER_TYPE.TEXTURE2D).AsTexture2D.Value = Tex;

            return(true);
        }
Exemplo n.º 2
0
 public Primitive( LoaderTempMesh _OwnerMesh, SceneLoader _Owner, string _Name, FBXImporter.Material _Material )
     : base(_Owner, _Name)
 {
     m_OwnerMesh = _OwnerMesh;
     m_Material = _Material;
     // 				if ( m_Material == null )
     // 					throw new Exception( "Invalid material for primitive \"" + _Name + "\"!" );
 }
Exemplo n.º 3
0
 /// <summary>
 /// Creates a texture parameter from a material property that contains a texture (e.g. DiffuseColor, SpecularColor, etc.)
 /// </summary>
 /// <param name="_Material"></param>
 /// <param name="_PropertyName"></param>
 /// <param name="_MaterialParameters"></param>
 /// <param name="_ParameterName"></param>
 /// <returns>True if a texture is available</returns>
 protected bool  CreateTextureParameter(FBXImporter.Material _Material, string _PropertyName, Scene.Materials.MaterialParameters _MaterialParameters, string _ParameterName)
 {
     return(CreateTextureParameter(_Material, _PropertyName, _MaterialParameters, _ParameterName, null));
 }
Exemplo n.º 4
0
        /// <summary>
        /// Attempts to map FBX materials to render techniques
        /// </summary>
        /// <param name="_Materials">The list of materials to process</param>
        protected void  ProcessMaterials(FBXImporter.Material[] _Materials)
        {
            m_Material2Parameters.Clear();

            foreach (FBXImporter.Material Mat in _Materials)
            {
                Scene.Materials.MaterialParameters MatParams = null;

// DEBUG
// if ( Mat.Name == "sp_00_svod" )
//  MatParams = null;
// DEBUG

                if (m_MaterialsDatabase != null)
                {                       // Handle the special case of scenes that were exported as OBJ format and whos material informations lie in the diffuse color "texture"
                    FBXImporter.ObjectProperty DiffuseColorProperty = Mat.FindProperty("DiffuseColor");
                    if (DiffuseColorProperty != null && DiffuseColorProperty.Textures.Length > 0)
                    {
                        string PseudoTextureName = DiffuseColorProperty.Textures[0].AbsoluteFileName;                           // Actually a material name!
                        MaterialsDatabase.Material MatOverride = m_MaterialsDatabase.FindByName(PseudoTextureName);
                        if (MatOverride != null)
                        {                               // Replace this material's textures by the actual material's textures (does it make sense? ^^)
                            if (MatOverride.TextureDiffuse != null)
                            {
                                FBXImporter.ObjectProperty TextureProperty = Mat.FindProperty("DiffuseColor");
                                if (TextureProperty != null)
                                {
                                    TextureProperty.Textures = new FBXImporter.Texture[] { new FBXImporter.Texture(Mat.ParentScene, MatOverride.TextureDiffuse, MatOverride.TextureDiffuse, MatOverride.TextureDiffuse) };
                                }
                            }
                            if (MatOverride.TextureNormal != null)
                            {
                                FBXImporter.ObjectProperty TextureProperty = Mat.FindProperty("NormalMap");
                                if (TextureProperty != null)
                                {
                                    TextureProperty.Textures = new FBXImporter.Texture[] { new FBXImporter.Texture(Mat.ParentScene, MatOverride.TextureNormal, MatOverride.TextureNormal, MatOverride.TextureNormal) };
                                }
                            }
                            if (MatOverride.TextureSpecular != null)
                            {
                                FBXImporter.ObjectProperty TextureProperty = Mat.FindProperty("SpecularColor");
                                if (TextureProperty != null)
                                {
                                    TextureProperty.Textures = new FBXImporter.Texture[] { new FBXImporter.Texture(Mat.ParentScene, MatOverride.TextureSpecular, MatOverride.TextureSpecular, MatOverride.TextureSpecular) };
                                }
                            }
                        }
                    }
                }

                // Specialize the material
                if (Mat is FBXImporter.MaterialHardwareShader)
                {
                    FBXImporter.MaterialHardwareShader HardwareMaterial = Mat as FBXImporter.MaterialHardwareShader;

                    MatParams = m_Scene.CreateMaterialParameters(Mat.Name, HardwareMaterial.RelativeURL);

                    foreach (FBXImporter.MaterialHardwareShader.TableEntry Entry in HardwareMaterial.ShaderEntries)
                    {
                        switch (Entry.TypeName)
                        {
                        case "Boolean":
                            MatParams.CreateParameter(Entry.Name, Scene.Materials.MaterialParameters.PARAMETER_TYPE.BOOL).AsBool.Value = (bool)Entry.Value;
                            break;

                        case "Integer":
                            MatParams.CreateParameter(Entry.Name, Scene.Materials.MaterialParameters.PARAMETER_TYPE.INT).AsInt.Value = (int)Entry.Value;
                            break;

                        case "Float":
                            MatParams.CreateParameter(Entry.Name, Scene.Materials.MaterialParameters.PARAMETER_TYPE.FLOAT).AsFloat.Value = (float)Entry.Value;
                            break;

                        case "Float2":
                            MatParams.CreateParameter(Entry.Name, Scene.Materials.MaterialParameters.PARAMETER_TYPE.FLOAT2).AsFloat2.Value = Entry.Value as Vector2D;
                            break;

                        case "Float3":
                            MatParams.CreateParameter(Entry.Name, Scene.Materials.MaterialParameters.PARAMETER_TYPE.FLOAT3).AsFloat3.Value = Entry.Value as Vector;
                            break;

                        case "Float4":
                            MatParams.CreateParameter(Entry.Name, Scene.Materials.MaterialParameters.PARAMETER_TYPE.FLOAT4).AsFloat4.Value = Entry.Value as Vector4D;
                            break;

                        case "Matrix":
                            MatParams.CreateParameter(Entry.Name, Scene.Materials.MaterialParameters.PARAMETER_TYPE.MATRIX4).AsMatrix4.Value = Entry.Value as Matrix4x4;
                            break;

                        case "Texture":
                            CreateTextureParameter(Mat, Entry.Name, MatParams, Entry.Name);
                            break;
                        }
                    }

                    m_Material2Parameters[Mat] = MatParams;
                    continue;
                }

                FBXImporter.Material SpecificMaterial = null;

                if (Mat is FBXImporter.MaterialPhong)
                {
                    SpecificMaterial = Mat as FBXImporter.MaterialPhong;
                    MatParams        = m_Scene.CreateMaterialParameters(Mat.Name, "Phong");
                }
                else if (Mat is FBXImporter.MaterialLambert)
                {
                    SpecificMaterial = Mat as FBXImporter.MaterialLambert;
                    MatParams        = m_Scene.CreateMaterialParameters(Mat.Name, "Lambert");
                }
                else
                {
                    continue;                           // Unrecognized hence unsupported material type...
                }
//              // Lambert parameters
//              MatParams.CreateParameter( "AmbientColor", Scene.Materials.MaterialParameters.PARAMETER_TYPE.FLOAT3 ).AsFloat3.Value = SpecificMaterial.AmbientColor;
//              CreateTextureParameter( Mat, "AmbientColor", MatParams, "AmbientTexture" );
//              MatParams.CreateParameter( "AmbientFactor", Scene.Materials.MaterialParameters.PARAMETER_TYPE.FLOAT ).AsFloat.Value = SpecificMaterial.AmbientFactor;
//
//              MatParams.CreateParameter( "DiffuseColor", Scene.Materials.MaterialParameters.PARAMETER_TYPE.FLOAT3 ).AsFloat3.Value = SpecificMaterial.DiffuseColor;
//              MatParams.CreateParameter( "DiffuseFactor", Scene.Materials.MaterialParameters.PARAMETER_TYPE.FLOAT ).AsFloat.Value = SpecificMaterial.DiffuseFactor;
//              bool	bHasDiffuseTexture = CreateTextureParameter( Mat, "DiffuseColor", MatParams, "DiffuseTexture", "TransparentColor" );
//              MatParams.CreateParameter( "HasDiffuseTexture", Scene.Materials.MaterialParameters.PARAMETER_TYPE.BOOL ).AsBool.Value = bHasDiffuseTexture;
//
//              MatParams.CreateParameter( "EmissiveColor", Scene.Materials.MaterialParameters.PARAMETER_TYPE.FLOAT3 ).AsFloat3.Value = SpecificMaterial.EmissiveColor;
//              MatParams.CreateParameter( "EmissiveFactor", Scene.Materials.MaterialParameters.PARAMETER_TYPE.FLOAT ).AsFloat.Value = SpecificMaterial.EmissiveFactor;
//              CreateTextureParameter( Mat, "EmissiveColor", MatParams, "EmissiveTexture" );
//
//              FBXImporter.ObjectProperty	OpacityProp = SpecificMaterial.FindProperty( "Opacity" );
//              float	fOpacity = OpacityProp != null ? (float) OpacityProp.Value : 1.0f;
//              MatParams.CreateParameter( "Opacity", Scene.Materials.MaterialParameters.PARAMETER_TYPE.FLOAT ).AsFloat.Value = fOpacity;
// //				MatParams.IsOpaque = fOpacity >= 1.0f;
//
//              bool	bHasNormalTexture = CreateTextureParameter( Mat, "Bump", MatParams, "NormalTexture" );
//              MatParams.CreateParameter( "HasNormalTexture", Scene.Materials.MaterialParameters.PARAMETER_TYPE.BOOL ).AsBool.Value = bHasNormalTexture;


                // Lambert parameters
                MatParams.CreateParameter("AmbientColor", Scene.Materials.MaterialParameters.PARAMETER_TYPE.FLOAT3).AsFloat3.Value = SpecificMaterial.FindProperty("AmbientColor").AsVector3;
                CreateTextureParameter(Mat, "AmbientColor", MatParams, "AmbientTexture");
                MatParams.CreateParameter("AmbientFactor", Scene.Materials.MaterialParameters.PARAMETER_TYPE.FLOAT).AsFloat.Value = SpecificMaterial.FindProperty("AmbientFactor").AsFloat;

                MatParams.CreateParameter("DiffuseColor", Scene.Materials.MaterialParameters.PARAMETER_TYPE.FLOAT3).AsFloat3.Value = SpecificMaterial.FindProperty("DiffuseColor").AsVector3;
                MatParams.CreateParameter("DiffuseFactor", Scene.Materials.MaterialParameters.PARAMETER_TYPE.FLOAT).AsFloat.Value  = SpecificMaterial.FindProperty("DiffuseFactor").AsFloat;
                bool bHasDiffuseTexture = CreateTextureParameter(Mat, "DiffuseColor", MatParams, "DiffuseTexture", "TransparentColor");
                MatParams.CreateParameter("HasDiffuseTexture", Scene.Materials.MaterialParameters.PARAMETER_TYPE.BOOL).AsBool.Value = bHasDiffuseTexture;

                MatParams.CreateParameter("EmissiveColor", Scene.Materials.MaterialParameters.PARAMETER_TYPE.FLOAT3).AsFloat3.Value = SpecificMaterial.FindProperty("EmissiveColor").AsVector3;
                MatParams.CreateParameter("EmissiveFactor", Scene.Materials.MaterialParameters.PARAMETER_TYPE.FLOAT).AsFloat.Value  = SpecificMaterial.FindProperty("EmissiveFactor").AsFloat;
                bool bHasEmissiveTexture = CreateTextureParameter(Mat, "EmissiveColor", MatParams, "EmissiveTexture");

                float fOpacity = SpecificMaterial.FindProperty("Opacity").AsFloat;
                MatParams.CreateParameter("Opacity", Scene.Materials.MaterialParameters.PARAMETER_TYPE.FLOAT).AsFloat.Value = fOpacity;
//					MatParams.IsOpaque = fOpacity >= 1.0f;

                bool bHasNormalTexture = false;
                if (Mat.FindProperty("Bump").Textures.Length > 0)
                {
                    bHasNormalTexture = CreateTextureParameter(Mat, "Bump", MatParams, "NormalTexture");
                }
                else if (Mat.FindProperty("NormalMap").Textures.Length > 0)
                {
                    bHasNormalTexture = CreateTextureParameter(Mat, "NormalMap", MatParams, "NormalTexture");
                }
                MatParams.CreateParameter("HasNormalTexture", Scene.Materials.MaterialParameters.PARAMETER_TYPE.BOOL).AsBool.Value = bHasNormalTexture;


                // Phong parameters
                try
                {
                    MatParams.CreateParameter("ReflectionColor", Scene.Materials.MaterialParameters.PARAMETER_TYPE.FLOAT3).AsFloat3.Value = SpecificMaterial.FindProperty("ReflectionColor").AsVector3;
                    CreateTextureParameter(Mat, "ReflectionColor", MatParams, "ReflectionTexture");
                    MatParams.CreateParameter("ReflectionFactor", Scene.Materials.MaterialParameters.PARAMETER_TYPE.FLOAT).AsFloat.Value = SpecificMaterial.FindProperty("ReflectionFactor").AsFloat;

                    MatParams.CreateParameter("Shininess", Scene.Materials.MaterialParameters.PARAMETER_TYPE.FLOAT).AsFloat.Value       = SpecificMaterial.FindProperty("Shininess").AsFloat;
                    MatParams.CreateParameter("SpecularColor", Scene.Materials.MaterialParameters.PARAMETER_TYPE.FLOAT3).AsFloat3.Value = SpecificMaterial.FindProperty("SpecularColor").AsVector3;
                    MatParams.CreateParameter("SpecularFactor", Scene.Materials.MaterialParameters.PARAMETER_TYPE.FLOAT).AsFloat.Value  = SpecificMaterial.FindProperty("SpecularFactor").AsFloat;
                    bool bHasSpecularTexture = CreateTextureParameter(Mat, "SpecularColor", MatParams, "SpecularTexture");
                    MatParams.CreateParameter("HasSpecularTexture", Scene.Materials.MaterialParameters.PARAMETER_TYPE.BOOL).AsBool.Value = bHasSpecularTexture;
                }
                catch (Exception)
                {
                }

                // Register the material
                m_Material2Parameters[Mat] = MatParams;
            }
        }