Example #1
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);
                }
            }
        }
Example #2
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);
        }
Example #3
0
        private static void ToAssimp(Module.Export.Assimp.Context context, Scene scene, Module.Export.Assimp.Mesh mesh_indexes, aiMesh assimp_mesh)
        {
            Mesh mesh = scene.meshes[mesh_indexes.mesh];

            assimp_mesh.mMaterialIndex = (uint)mesh_indexes.material;

            using (aiString assimp_mesh_name = new aiString(mesh.name))
            {
                assimp_mesh.mName = assimp_mesh_name.Unmanaged();
            }

            if (mesh.vertices.Length > 0)
            {
                using (aiVector3DArray vertices = assimp_mesh.Vertices)
                {
                    Assimp.Convert.UnityToAssimp.Array(Assimp.Convert.UnityToAssimp.Vector3, mesh.vertices, vertices);
                }
            }
            if (mesh.normals.Length > 0)
            {
                using (aiVector3DArray normals = assimp_mesh.Normals)
                {
                    Assimp.Convert.UnityToAssimp.Array(Assimp.Convert.UnityToAssimp.Vector3, mesh.normals, normals);
                }
            }
            if (mesh.tangents.Length > 0)
            {
                using (aiVector3DArray tangents = assimp_mesh.Tangents)
                {
                    Assimp.Convert.UnityToAssimp.Array(Assimp.Convert.UnityToAssimp.Tangent, mesh.tangents, tangents);
                }
            }
            if (mesh_indexes.submesh < mesh.submeshes.Length)
            {
                // Support for submeshes: this mesh represent only one submesh of the original mesh
                SubMesh sub_mesh = mesh.submeshes[mesh_indexes.submesh];

                if (sub_mesh != null && sub_mesh.triangles != null && sub_mesh.triangles.Length > 0)
                {
                    using (aiFaceArray faces = assimp_mesh.Faces)
                    {
                        Assimp.Convert.UnityToAssimp.Face(sub_mesh.triangles, sub_mesh.topology, faces);
                    }
                }
            }
            if (mesh.uv1.Length > 0 || mesh.uv2.Length > 0)
            {
                using (aiVector3DMultiArray texture_coords = assimp_mesh.TextureCoords)
                {
                    if (mesh.uv1.Length > 0)
                    {
                        using (aiVector3DArray texture_coords0 = texture_coords.Get(0))
                        {
                            Assimp.Convert.UnityToAssimp.Array(Assimp.Convert.UnityToAssimp.UV, mesh.uv1, texture_coords0);
                        }
                    }

                    if (mesh.uv2.Length > 0)
                    {
                        using (aiVector3DArray texture_coords1 = texture_coords.Get(1))
                        {
                            Assimp.Convert.UnityToAssimp.Array(Assimp.Convert.UnityToAssimp.UV, mesh.uv2, texture_coords1);
                        }
                    }
                }
            }
            if (mesh.colors.Length > 0)
            {
                using (aiColor4DMultiArray colors = assimp_mesh.Colors)
                {
                    using (aiColor4DArray colors0 = colors.Get(0))
                    {
                        Assimp.Convert.UnityToAssimp.Array(Assimp.Convert.UnityToAssimp.Color, mesh.colors, colors0);
                    }
                }
            }

            context.progress.Update(ASSIMP_PROGRESS_FACTOR);
        }