Пример #1
0
        /// <summary>
        ///     マテリアルをUnity用に変換する
        /// </summary>
        /// <returns>Unity用マテリアル</returns>
        /// <param name='material_index'>PMX用マテリアルインデックス</param>
        /// <param name='is_transparent'>透過か</param>
        private Material ConvertMaterial(uint material_index, bool is_transparent)
        {
            var material = format_.material_list.material[material_index];

            //先にテクスチャ情報を検索
            Texture2D main_texture = null;

            if (material.usually_texture_index == uint.MaxValue)
            {
                main_texture = Texture2D.whiteTexture;
            }
            else
            {
                var texture_file_name = format_.texture_list.texture_file[material.usually_texture_index];

                var path = format_.meta_header.folder + "/" + texture_file_name;
                if (File.Exists(path))
                {
                    if (material.usually_texture_index < format_.texture_list.texture_file.Length)
                    {
                        if (Path.GetExtension(texture_file_name).ToUpper() == ".DDS")
                        {
                            main_texture           = Texture2D.whiteTexture;
                            main_texture           = DDSImage.LoadDDS(path);
                            main_texture.wrapModeV = TextureWrapMode.MirrorOnce;
                        }
                        else if (Path.GetExtension(texture_file_name).ToUpper() == ".BMP")
                        {
                            main_texture = BMPLoader.LoadBMP(path).ToTexture2D();
                        }
                        else if (Path.GetExtension(texture_file_name).ToUpper() == ".TGA")
                        {
                            try
                            {
                                var bitmap = TargaImage.LoadTargaImage(path);
                                main_texture = new Texture2D(bitmap.Width, bitmap.Height);

                                var stream = new MemoryStream();
                                bitmap.Save(stream, ImageFormat.Png);
                                main_texture.LoadImage(stream.ToArray());
                                stream.Close();
                                main_texture.Apply();
                            }
                            catch (Exception e)
                            {
                                Debug.Log("TGA Exception");
                                Debug.Log(texture_file_name);
                                Debug.LogError(e.Message);
                                main_texture = Texture2D.whiteTexture;
                            }

                            //main_texture = CreateTexture(path);
                        }
                        else
                        {
                            main_texture = new Texture2D(1, 1, TextureFormat.RGBA32, false);
                            main_texture.LoadImage(File.ReadAllBytes(path)); // Fill the texture field
                        }

                        //main_texture.alphaIsTransparency = true;
                    }
                }
                else
                {
                    main_texture = Texture2D.whiteTexture;
                }
            }

            var result = new Material(Resources.Load <Material>("Transparent"))
            {
                renderQueue = (int)(3000 + material_index)
            };

            result.SetTexture("_BaseColorMap", main_texture);
            result.SetVector("_BaseColor", material.diffuse_color);
            result.SetFloat("_TransparentSortPriority", material_index);
            result.name = format_.material_list.material[material_index].name;

            try
            {
                foreach (var m in GameObject.Find("Preview Builder Main").GetComponent <PreviewBuilder.PreviewBuilder>()
                         .MaterialExtraConfigurationList.Materials)
                {
                    foreach (var item in m.MaterialName)
                    {
                        if (result.name.ToUpper().Contains(item.ToUpper()))
                        {
                            result.SetFloat("_AlphaCutoffPrepass", float.Parse(m.Throttle));
                            Debug.Log($"{result.name} {m.Throttle}");
                            goto Found;
                        }
                        else
                        {
                            var defaultValue = (from t in GameObject.Find("Preview Builder Main")
                                                .GetComponent <PreviewBuilder.PreviewBuilder>().MaterialExtraConfigurationList.Materials
                                                where t.Name == "Default"
                                                select t).FirstOrDefault();
                            result.SetFloat("_AlphaCutoffPrepass", float.Parse(defaultValue.Throttle));
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Debug.LogError(e.Message);
            }

Found:
            return(result);
        }
Пример #2
0
        /// <summary>
        ///     マテリアルをUnity用に変換する
        /// </summary>
        /// <returns>Unity用マテリアル</returns>
        /// <param name='material_index'>PMX用マテリアルインデックス</param>
        /// <param name='is_transparent'>透過か</param>
        private Material ConvertMaterial(uint material_index, bool is_transparent)
        {
            var material = format_.material_list.material[material_index];

            //先にテクスチャ情報を検索
            Texture2D main_texture = null;

            if (material.usually_texture_index == uint.MaxValue)
            {
                main_texture = Texture2D.whiteTexture;
            }
            else
            {
                var texture_file_name = format_.texture_list.texture_file[material.usually_texture_index];

                var path = format_.meta_header.folder + "/" + texture_file_name;
                if (File.Exists(path))
                {
                    if (material.usually_texture_index < format_.texture_list.texture_file.Length)
                    {
                        if (texture_file_name.EndsWith(".dds", StringComparison.OrdinalIgnoreCase))
                        {
                            try
                            {
                                main_texture           = DDSImage.LoadDDS(path);
                                main_texture.wrapModeV = TextureWrapMode.MirrorOnce;
                            }
                            catch
                            {
                                main_texture = Texture2D.whiteTexture;
                            }
                        }
                        else if (texture_file_name.EndsWith(".bmp", StringComparison.OrdinalIgnoreCase))
                        {
                            main_texture = BMPLoader.LoadBMP(path).ToTexture2D();
                        }
                        else if (texture_file_name.EndsWith(".tga", StringComparison.OrdinalIgnoreCase))
                        {
                            try
                            {
                                var bitmap = TargaImage.LoadTargaImage(path);
                                main_texture = new Texture2D(bitmap.Width, bitmap.Height);

                                var stream = new MemoryStream();
                                bitmap.Save(stream, ImageFormat.Png);
                                main_texture.LoadImage(stream.ToArray());
                                stream.Close();
                                main_texture.Apply();
                            }
                            catch (Exception e)
                            {
                                Debug.Log("TGA Exception");
                                Debug.Log(texture_file_name);
                                Debug.LogError(e.Message);
                                main_texture = Texture2D.whiteTexture;
                            }

                            //main_texture = CreateTexture(path);
                        }
                        else
                        {
                            main_texture = new Texture2D(1, 1, TextureFormat.RGBA32, false);
                            main_texture.LoadImage(File.ReadAllBytes(path)); // Fill the texture field
                        }

                        //main_texture.alphaIsTransparency = true;
                    }
                }
                else
                {
                    main_texture = Texture2D.whiteTexture;
                }
            }

#if Unlit
            var result = new Material(Resources.Load <Material>("MMDUnlit"))
            {
                renderQueue = (int)(3000 + material_index)
            };
            result.SetTexture("_UnlitColorMap", main_texture);
            result.SetVector("_UnlitColor", material.diffuse_color);
            result.SetFloat("_TransparentSortPriority", material_index);
            result.name = format_.material_list.material[material_index].name;
            return(result);
#else
            var result = new Material(Resources.Load <Material>("MMDLit"))
            {
                renderQueue = (int)(3000 + material_index)
            };
            result.SetTexture("_BaseColorMap", main_texture);
            result.SetVector("_BaseColor", material.diffuse_color);
            result.SetFloat("_Smoothness", 0f);
            result.SetFloat("_TransparentSortPriority", material_index);
            result.name = format_.material_list.material[material_index].name;
            return(result);
#endif
        }