public void Import(SyncTexture syncTexture, Texture2D texture)
            {
                var data = syncTexture.Source;

                texture.LoadImage(data);

                if (syncTexture.ConvertToNormalMap)
                {
                    ConvertToNormalMap(texture, k_NormalMapIntensity, false);
                }
            }
            public Texture2D CreateNew(SyncTexture syncTexture)
            {
                var linear  = syncTexture.ConvertToNormalMap || QualitySettings.activeColorSpace == ColorSpace.Linear;
                var texture = new Texture2D(1, 1, TextureFormat.RGBA32, Texture.GenerateAllMips, linear)
                {
                    name       = syncTexture.Name,
                    anisoLevel = 4,
                    wrapMode   = TextureWrapMode.Repeat
                };

                return(texture);
            }
Example #3
0
        public SyncMaterial BuildMaterial(Obj curObj, PublisherTransaction transaction)
        {
            SyncMaterial material = null;

            ObjParser.Types.Material mat = FindMaterial(curObj.UseMtl);
            if (mat != null)
            {
                material = new SyncMaterial(new SyncId("MaterialId_" + mat.Name), mat.Name);
                Random random = new Random();
                //material.AlbedoColor = new SyncColor((float)random.NextDouble(), (float)random.NextDouble(), (float)random.NextDouble());
                material.AlbedoColor = SyncColor.From256(200, 200, 200); // test

                Console.WriteLine("\t\tColor: "
                                  + material.AlbedoColor.R.ToString() + ", "
                                  + material.AlbedoColor.G.ToString() + ", "
                                  + material.AlbedoColor.B.ToString() + ", ");

                // テクスチャが見つかった場合
                if (mat.map_Kd.Length > 0)
                {
                    //material.AlbedoFade = 1.0f; // 0.5f; //2021.5.26
                    SyncTexture texture = BuildTexture(mat);
                    if (texture != null)
                    {
                        transaction.Send(texture);
                        Console.WriteLine("\tTexture Id: " + texture.Id);
                        Console.WriteLine("\tTexture Name: " + texture.Name);

                        SyncMap albedoMap = new SyncMap(texture.Id, new Vector2(), new Vector2(1, 1));
                        material.AlbedoMap = albedoMap;
                    }
                }
            }
            else
            {
                Console.WriteLine("Warning: No Material.");
                material             = new SyncMaterial(new SyncId("MaterialId_" + "DEFAULT"), "DEFAULT");
                material.AlbedoColor = SyncColor.From256(200, 0, 0);
            }
            return(material);
        }
Example #4
0
        public SyncTexture BuildTexture(ObjParser.Types.Material mat)
        {
            // ストリップパスとか相対パスの場合を考慮する
            string filePath = System.IO.Path.GetDirectoryName(mat.map_Kd);

            if (filePath.Length == 0)
            {
                filePath = System.IO.Path.GetDirectoryName(fileName) + "\\" + mat.map_Kd;
            }

            string      name    = System.IO.Path.GetFileNameWithoutExtension(filePath);
            string      ext     = System.IO.Path.GetExtension(filePath);
            SyncTexture texture = new SyncTexture(new SyncId("TextureId_" + name), name);

            Console.WriteLine("\tTexture file: " + filePath);
            try
            {
                //System.Drawing.Image img = System.Drawing.Image.FromFile(filePath);
#if false
                // 最初の案→なんかおかしい
                byte[] ary;
                using (MemoryStream ms = new MemoryStream())
                {
                    if (ext.ToLower() == ".jpg" || ext.ToLower() == ".jpeg")
                    {
                        img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                    }
                    else if (ext.ToLower() == ".png")
                    {
                        img.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                    }
                    else
                    {
                        Console.WriteLine("\t" + mat.map_Kd + " is unknown format");
                        return(null);
                    }
                    ary = ms.ToArray();
                }
                texture.Source = ary.ToArray();
#else
                // 更に改善:解像度を2のN乗にする
                bool   requireResize = false;
                Bitmap bmp           = new Bitmap(filePath);
                int    width         = bmp.Width;
                int    height        = bmp.Height;
                if (!IsPow2(width))
                {
                    requireResize = true;
                    int beki = 1;
                    while (true)
                    {
                        if (width < beki)
                        {
                            width = beki / 2; // 1段階小さくする
                            break;
                        }
                        beki *= 2;
                    }
                }
                if (!IsPow2(bmp.Height))
                {
                    requireResize = true;
                    int beki = 1;
                    while (true)
                    {
                        if (height < beki)
                        {
                            height = beki / 2; // 1段階小さくする
                            break;
                        }
                        beki *= 2;
                    }
                }
                Bitmap bmpResize = new Bitmap(bmp, width, height);

                // ファイル名変更
                string resizeFileName = filePath;
                if (requireResize)
                {
                    resizeFileName.ToLower();
                    resizeFileName = resizeFileName.Replace(".jpg", "_resize.jpg"); // ファイル名差し替え
                    bmpResize.Save(resizeFileName);                                 // リサイズして画像出力
                }
                bmpResize.Dispose();
                bmp.Dispose();

                // 2021.5.26の案 // UnityEditorで上手くいくのを確認した方法 //
                System.IO.FileStream   fileStream = new System.IO.FileStream(resizeFileName, System.IO.FileMode.Open, System.IO.FileAccess.Read);
                System.IO.BinaryReader bin        = new System.IO.BinaryReader(fileStream);
                byte[] ary = bin.ReadBytes((int)bin.BaseStream.Length);
                bin.Close();
                texture.Source = ary;

                /*
                 * // こっちだとどうかな // [?]という画像に置き換わる現象が出た
                 * List<byte> ary2 = new List<byte>();
                 *
                 * Bitmap bmp = new Bitmap(filePath);
                 * for (int x = 0; x < bmp.Width; x++)
                 * {
                 *  for (int y = 0; y < bmp.Height; y++)
                 *  {
                 *      Color color = bmp.GetPixel(x, y);
                 *      ary2.Add(color.R);
                 *      ary2.Add(color.G);
                 *      ary2.Add(color.B);
                 *      ary2.Add(color.A);
                 *  }
                 * }
                 * texture.Source = ary2.ToArray();
                 */
#endif
            }
            catch (System.IO.FileNotFoundException e)
            {
                Console.WriteLine("FileNotFoundException: " + e.Message);
            }
            return(texture);
        }