Exemple #1
0
        public bool SetTextureMappingAxis(aiTextureType type, uint index, aiVector3D INPUT)
        {
            bool ret = assimp_swigPINVOKE.aiMaterial_SetTextureMappingAxis(swigCPtr, (int)type, index, aiVector3D.getCPtr(INPUT));

            if (assimp_swigPINVOKE.SWIGPendingException.Pending)
            {
                throw assimp_swigPINVOKE.SWIGPendingException.Retrieve();
            }
            return(ret);
        }
Exemple #2
0
        public bool SetUVTransform(aiTextureType type, uint index, aiUVTransform INPUT)
        {
            bool ret = assimp_swigPINVOKE.aiMaterial_SetUVTransform(swigCPtr, (int)type, index, aiUVTransform.getCPtr(INPUT));

            if (assimp_swigPINVOKE.SWIGPendingException.Pending)
            {
                throw assimp_swigPINVOKE.SWIGPendingException.Retrieve();
            }
            return(ret);
        }
Exemple #3
0
        public bool GetTextureOperation(aiTextureType type, uint index, out aiTextureOp OUTPUT)
        {
            bool ret = assimp_swigPINVOKE.aiMaterial_GetTextureOperation(swigCPtr, (int)type, index, out OUTPUT);

            if (assimp_swigPINVOKE.SWIGPendingException.Pending)
            {
                throw assimp_swigPINVOKE.SWIGPendingException.Retrieve();
            }
            return(ret);
        }
Exemple #4
0
        public bool GetTexturePath(aiTextureType type, uint index, aiString OUTPUT)
        {
            bool ret = assimp_swigPINVOKE.aiMaterial_GetTexturePath(swigCPtr, (int)type, index, aiString.getCPtr(OUTPUT));

            if (assimp_swigPINVOKE.SWIGPendingException.Pending)
            {
                throw assimp_swigPINVOKE.SWIGPendingException.Retrieve();
            }
            return(ret);
        }
Exemple #5
0
        public bool SetTextureBlend(aiTextureType type, uint index, float INPUT)
        {
            bool ret = assimp_swigPINVOKE.aiMaterial_SetTextureBlend(swigCPtr, (int)type, index, INPUT);

            return(ret);
        }
Exemple #6
0
        public bool GetTextureBlend(aiTextureType type, uint index, out float OUTPUT)
        {
            bool ret = assimp_swigPINVOKE.aiMaterial_GetTextureBlend(swigCPtr, (int)type, index, out OUTPUT);

            return(ret);
        }
Exemple #7
0
        public bool SetTextureOperation(aiTextureType type, uint index, aiTextureOp INPUT)
        {
            bool ret = assimp_swigPINVOKE.aiMaterial_SetTextureOperation(swigCPtr, (int)type, index, INPUT);

            return(ret);
        }
Exemple #8
0
        private static void FromAssimpAlphaTexture(Module.Import.Assimp.Context context, Material material, aiMaterial material_data, aiScene scene, string unity_property, aiTextureType texture_type, Color default_color, Func <Color, float> op)
        {
            if (material_data.GetTextureCount(texture_type) > 0)
            {
                using (aiString texture_name = new aiString())
                {
                    if (material_data.GetTexturePath(texture_type, 0, texture_name))
                    {
                        Texture alpha    = new Texture(context, texture_name.ToString(), scene);
                        Texture base_tex = null;

                        Material.TextureParams param = material.GetTextureParams(unity_property);

                        if (param != null)
                        {
                            CLARTE.Backport.Tuple <Texture, uint> res = context.scene.GetAssimpTexture(param.index);

                            if (res != null)
                            {
                                base_tex = res.Item1;
                            }
                            else
                            {
                                Debug.LogErrorFormat("Invalid texture index. '{0}' was registered for material '{1}' as texture with index '{2}'. However no texture was found with this index.", unity_property, material.Name, param.index);
                            }
                        }
                        else
                        {
                            CLARTE.Backport.Tuple <Texture, uint> assimp_tex = context.scene.GetAssimpTexture(Guid.NewGuid().ToString(), () => new Texture("**E", alpha.width, alpha.height, default_color));

                            material.AddTextureParams(unity_property, new Material.TextureParams(assimp_tex.Item2));

                            base_tex = assimp_tex.Item1;
                        }

                        if (base_tex != null)
                        {
                            base_tex.AddToAlpha(alpha, op);

                            base_tex.filename = string.Format("**A{0}|{1}", base_tex.filename, texture_name.C_Str());
                        }
                    }
                }
            }
        }
Exemple #9
0
        public bool SetUVWSource(aiTextureType type, uint index, int INPUT)
        {
            bool ret = assimp_swigPINVOKE.aiMaterial_SetUVWSource(swigCPtr, (int)type, index, INPUT);

            return(ret);
        }
Exemple #10
0
        private static Material FromAssimp(Module.Import.Assimp.Context context, aiScene scene, aiMaterial material_data)
        {
            Material material = new Material();

            // Initialize dictionaries before hand because we do not know in advance wich ones we will need
            material.floats   = new Dictionary <string, float>();
            material.colors   = new Dictionary <string, Color>();
            material.textures = new Dictionary <string, TextureParams>();

            // Name
            using (aiString material_name = new aiString())
            {
                if (material_data.GetName(material_name))
                {
                    material.name = material_name.ToString();
                }
            }

            // shader
            material.shader = Constants.defaultAssimpShader;

            // Shininess
            float shininess;

            if (material_data.GetShininess(out shininess))
            {
                aiShadingMode shading;

                if (material_data.GetShadingModel(out shading))
                {
                    if (shading != aiShadingMode.aiShadingMode_Blinn && shading != aiShadingMode.aiShadingMode_Phong)
                    {
                        // Unsupported shading model
                        Debug.LogWarningFormat("The shading model for material {0} is not supported. The value for the shininess is likely to be incorrect.", material.name);
                    }
                }

                const int factor = 128;                 // unity shader factor
                shininess /= factor;
            }

            // Gloss
            float gloss;

            if (material_data.GetShininessStrength(out gloss))
            {
                shininess *= gloss;
            }

            // Reflectivity
            float reflectivity;

            if (material_data.GetReflectivity(out reflectivity))
            {
                material.floats.Add(Assimp.Convert.unityMetallicValueName, reflectivity);
            }

            material.floats.Add(Assimp.Convert.unityGlossinessValueName, Smoothness(shininess, reflectivity));

            // Colors
            foreach (KeyValuePair <string, Assimp.Convert.GetColor> pair in Assimp.Convert.GetColors(material_data))
            {
                using (aiColor4D color = new aiColor4D())
                {
                    if (pair.Value(color))
                    {
                        Color unity_color = Assimp.Convert.AssimpToUnity.Color(color);

                        bool set_color = true;

                        switch (pair.Key)
                        {
                        case Assimp.Convert.unityDiffuseColorName:
                            // Global opacity
                            float opacity;

                            if (material_data.GetOpacity(out opacity) && opacity < 1.0f)
                            {
                                unity_color.a = opacity;

                                material.floats.Add(Assimp.Convert.unityRenderModeName, (float)CLARTE.Shaders.Standard.Utility.BlendMode.TRANSPARENT);
                            }

                            break;

                        case Assimp.Convert.unitySpecularColorName:
                            // Specular color must be very close to black
                            unity_color = 0.1f * unity_color;

                            break;

                        case Assimp.Convert.unityEmissiveColorName:
                            if (!CLARTE.Shaders.Standard.Utility.ShouldEmissionBeEnabled(unity_color))
                            {
                                set_color = false;
                            }

                            break;
                        }

                        if (set_color)
                        {
                            material.colors.Add(pair.Key, unity_color);
                        }
                    }
                }
            }

            // Textures
            foreach (KeyValuePair <string, aiTextureType> pair in Assimp.Convert.textureTypes)
            {
                // Make a copy to avoid problem of loop variable captured by reference by lambda expression
                string        texture_key  = pair.Key;
                aiTextureType texture_type = pair.Value;

                context.threads.AddTask(() => Texture.FromAssimp(context, material, scene, material_data, texture_key, texture_type, reflectivity));
            }

            // We must dispose of the given parameter to free unused memory. However other tasks may still be using this material (i.e. textures), so we will let the garbage collector do it's job.
            //material_data.Dispose();

            context.progress.Update(ASSIMP_PROGRESS_FACTOR);

            return(material);
        }
Exemple #11
0
        private void ToAssimp(Module.Export.Assimp.Context context, Scene scene, aiMaterial assimp_material)
        {
            // Name
            if (!string.IsNullOrEmpty(name))
            {
                using (aiString assimp_material_name = new aiString(name))
                {
                    assimp_material.SetName(assimp_material_name.Unmanaged());
                }
            }

            // Set flag for transparent texture if the shader use transparency
            if (renderQueue == (int)UnityEngine.Rendering.RenderQueue.Transparent)
            {
                assimp_material.SetTextureFlags(aiTextureType.aiTextureType_DIFFUSE, 0, aiTextureFlags.aiTextureFlags_UseAlpha);
            }

            // Reflectivity
            float reflectivity;

            if (floats == null || !floats.TryGetValue("_Metallic", out reflectivity))
            {
                reflectivity = 0f;
            }

            assimp_material.SetReflectivity(reflectivity);

            // Shininess
            float smoothness;

            if (floats == null || !floats.TryGetValue("_Glossiness", out smoothness))
            {
                smoothness = 0f;
            }

            float shininess = 2.0f * smoothness - (reflectivity > 0.0f ? reflectivity : 0.0f);

            if (shininess > 0.0f)
            {
                const int factor = 128;                 // unity shader factor

                assimp_material.SetShadingModel(aiShadingMode.aiShadingMode_Phong);
                assimp_material.SetShininess(shininess * factor);
                assimp_material.SetShininessStrength(1.0f);
            }
            else
            {
                assimp_material.SetShadingModel(aiShadingMode.aiShadingMode_Gouraud);
            }

            // Colors
            if (colors != null)
            {
                foreach (KeyValuePair <string, Assimp.Convert.SetColor> pair in Assimp.Convert.SetColors(assimp_material))
                {
                    if (colors.ContainsKey(pair.Key))
                    {
                        Color unity_color = colors[pair.Key];

                        switch (pair.Key)
                        {
                        case Assimp.Convert.unityDiffuseColorName:
                            if (unity_color.a < 1.0f)
                            {
                                assimp_material.SetOpacity(unity_color.a);
                            }

                            break;

                        case Assimp.Convert.unitySpecularColorName:
                            // Revert specular color to original value
                            unity_color = 10.0f * unity_color;

                            break;

                        default:
                            break;
                        }

                        using (aiColor4D color = Assimp.Convert.UnityToAssimp.Color(unity_color))
                        {
                            pair.Value(color);
                        }
                    }
                }
            }

            // Textures
            if (textures != null)
            {
                Dictionary <Texture, aiTextureType> textures_types = new Dictionary <Texture, aiTextureType>();

                // Get supported textures
                foreach (KeyValuePair <string, aiTextureType> pair in Assimp.Convert.textureTypes)
                {
                    if (textures.ContainsKey(pair.Key))
                    {
                        Texture texture = scene.textures[textures[pair.Key].index];

                        if (texture != null)
                        {
                            textures_types.Add(texture, pair.Value);
                        }
                    }
                }

                // Export each supported textures
                foreach (KeyValuePair <Texture, aiTextureType> texture_pair in textures_types)
                {
                    // Make a copy to avoid problem of loop variable captured by reference by lambda expression
                    Texture       texture      = texture_pair.Key;
                    aiTextureType texture_type = texture_pair.Value;

                    context.threads.AddTask(() => texture.ToAssimp(context, texture_type, assimp_material));
                }
            }

            context.progress.Update(ASSIMP_PROGRESS_FACTOR);
        }
Exemple #12
0
        private void ToAssimp(Module.Export.Assimp.Context context, string texture_name, aiTextureType texture_type, aiMaterial material)
        {
            if (!string.IsNullOrEmpty(texture_name))
            {
                string final_texture_name = null;

                if (texture_name.Length >= 1 && texture_name[0] == '*')
                {
                    // Special textures
                    if (texture_name.Length >= 3 && texture_name[1] == '*')
                    {
                        switch (texture_name[2])
                        {
                        case 'N':
                            // New normal texture generated from height map
                            texture_type = aiTextureType.aiTextureType_HEIGHT;

                            final_texture_name = texture_name.Substring(3);

                            break;

                        case 'A':
                            // Secondary texture encoded in alpha channel
                            string[] textures = texture_name.Substring(3).Split('|');

                            if (textures.Length == 2)
                            {
                                ToAssimp(context, textures[0], texture_type, material);

                                switch (texture_type)
                                {
                                case aiTextureType.aiTextureType_DIFFUSE:
                                    texture_type = aiTextureType.aiTextureType_OPACITY;
                                    break;

                                case aiTextureType.aiTextureType_SPECULAR:
                                    texture_type = aiTextureType.aiTextureType_SHININESS;
                                    break;

                                default:
                                    break;
                                }

                                ToAssimp(context, textures[1], texture_type, material);
                            }
                            else
                            {
                                throw new FormatException("The texture + alpha should contain identifiers to only two original textures");
                            }

                            break;

                        case 'E':
                            // Empty texture

                            break;

                        default:
                            break;
                        }
                    }
                    else                     // Embeded texture
                    {
                        if (unityTexture != null)
                        {
                            using (aiTextureArray textures = context.scene.Textures)
                            {
                                uint index = textures.Size();

                                final_texture_name = "*" + index;

                                using (aiTexture texture = new aiTexture())
                                {
                                    texture.data          = unityTexture.EncodeToPNG();
                                    texture.achFormatHint = "png";

                                    textures.Set(index, texture.Unmanaged());
                                }
                            }
                        }
                    }
                }
                else
                {
                    final_texture_name = texture_name;
                }

                if (final_texture_name != null)
                {
                    using (aiString assimp_texture_name = new aiString(final_texture_name))
                    {
                        lock (material)
                        {
                            material.SetTexturePath(texture_type, 0, assimp_texture_name.Unmanaged());
                        }
                    }

                    context.progress.Update(ASSIMP_PROGRESS_FACTOR);
                }
            }
        }
Exemple #13
0
 public void ToAssimp(Module.Export.Assimp.Context context, aiTextureType texture_type, aiMaterial material)
 {
     ToAssimp(context, filename, texture_type, material);
 }
Exemple #14
0
    public uint GetTextureCount(aiTextureType type)
    {
        uint ret = AssimpPINVOKE.aiMaterial_GetTextureCount(swigCPtr, (int)type);

        return(ret);
    }
Exemple #15
0
        public bool SetMappingModeV(aiTextureType type, uint index, aiTextureMapMode INPUT)
        {
            bool ret = assimp_swigPINVOKE.aiMaterial_SetMappingModeV(swigCPtr, (int)type, index, INPUT);

            return(ret);
        }
Exemple #16
0
 public uint GetTextureCount(aiTextureType type) {
   uint ret = AssimpPINVOKE.aiMaterial_GetTextureCount(swigCPtr, (int)type);
   return ret;
 }
Exemple #17
0
        public static void FromAssimp(Module.Import.Assimp.Context context, Material material, aiScene scene, aiMaterial material_data, string texture_key, aiTextureType texture_type, float reflectivity)
        {
            if (texture_type != aiTextureType.aiTextureType_NONE && material_data.GetTextureCount(texture_type) > 0)
            {
                using (aiString texture_name = new aiString())
                {
                    if (material_data.GetTexturePath(texture_type, 0, texture_name))
                    {
                        string filename = texture_name.C_Str();

                        uint index = context.scene.GetAssimpTexture(filename, () => new Texture(context, filename, scene)).Item2;

                        material.AddTextureParams(texture_key, new Material.TextureParams(index));

                        context.progress.Update(ASSIMP_PROGRESS_FACTOR);
                    }
                }
            }

            Color default_color;

            // Add textures as alpha channel of existing textures, or compute normal map from heightmap if not defined.
            switch (texture_key)
            {
            case Assimp.Convert.unityMainTexName:
                default_color = Color.white;

                Color?diffuse = material.GetColor(Assimp.Convert.unityDiffuseColorName);

                if (diffuse.HasValue)
                {
                    default_color = diffuse.Value;
                }

                // Opacity as main texture alpha channel
                FromAssimpAlphaTexture(context, material, material_data, scene, Assimp.Convert.unityMainTexName, aiTextureType.aiTextureType_OPACITY, default_color, c => c.grayscale);
                break;

            case Assimp.Convert.unityMetallicGlossName:
                default_color = Color.black;

                float?metallic = material.GetFloat(Assimp.Convert.unityMetallicValueName);

                if (metallic.HasValue)
                {
                    default_color = new Color(metallic.Value, metallic.Value, metallic.Value, 1f);
                }

                // Shininess as alpha channel of metallic gloss map
                FromAssimpAlphaTexture(context, material, material_data, scene, Assimp.Convert.unityMetallicGlossName, aiTextureType.aiTextureType_SHININESS, default_color, c => Material.Smoothness(c.grayscale, reflectivity));
                break;

            case Assimp.Convert.unitySpecGlossName:
                default_color = Color.black;

                Color?specular = material.GetColor(Assimp.Convert.unitySpecularColorName);

                if (specular.HasValue)
                {
                    default_color = specular.Value;
                }

                // Shininess as alpha channel of specular gloss map
                FromAssimpAlphaTexture(context, material, material_data, scene, Assimp.Convert.unitySpecGlossName, aiTextureType.aiTextureType_SHININESS, default_color, c => Material.Smoothness(c.grayscale, reflectivity));
                break;

            case Assimp.Convert.unityBumpName:
                // Bump mapping from heightmap if not defined from normals
                FromAssimpNormalsFromHeightmap(context, material, material_data, scene);
                break;
            }
        }