public void Mtl_LoadMtl_TwoMaterials() { // Arrange var mtlFile = new[] { "newmtl Material", "Ns 96.078431", "Ka 1.000000 1.000000 1.000000", "Kd 0.630388 0.620861 0.640000", "Ks 0.500000 0.500000 0.500000", "Ke 0.000000 0.000000 0.000000", "Tf 0.000000 0.000000 0.000000", "Ni 1.000000", "d 1.000000", "illum 2", "", "newmtl Material.001", "Ns 96.078431", "Ka 1.000000 1.000000 1.000000", "Kd 0.640000 0.026578 0.014364", "Ks 0.500000 0.500000 0.500000", "Ke 0.000000 0.000000 0.000000", "Ni 1.000000", "d 1.000000", "illum 2" }; // Act mtl.LoadMtl(mtlFile); // Assert Assert.AreEqual(2, mtl.MaterialList.Count); ObjParser.Types.Material first = mtl.MaterialList[0]; Assert.AreEqual("Material", first.Name); Assert.AreEqual(96.078431f, first.SpecularExponent); Assert.AreEqual(1.0f, first.AmbientReflectivity.r); Assert.AreEqual(1.0f, first.AmbientReflectivity.g); Assert.AreEqual(1.0f, first.AmbientReflectivity.b); Assert.AreEqual(0.630388f, first.DiffuseReflectivity.r); Assert.AreEqual(0.620861f, first.DiffuseReflectivity.g); Assert.AreEqual(0.640000f, first.DiffuseReflectivity.b); Assert.AreEqual(0.5f, first.SpecularReflectivity.r); Assert.AreEqual(0.5f, first.SpecularReflectivity.g); Assert.AreEqual(0.5f, first.SpecularReflectivity.b); Assert.AreEqual(0.0f, first.EmissiveCoefficient.r); Assert.AreEqual(0.0f, first.EmissiveCoefficient.g); Assert.AreEqual(0.0f, first.EmissiveCoefficient.b); Assert.AreEqual(0.0f, first.TransmissionFilter.r); Assert.AreEqual(0.0f, first.TransmissionFilter.g); Assert.AreEqual(0.0f, first.TransmissionFilter.b); Assert.AreEqual(1.0f, first.OpticalDensity); Assert.AreEqual(1.0f, first.Dissolve); Assert.AreEqual(2, first.IlluminationModel); ObjParser.Types.Material second = mtl.MaterialList[1]; Assert.AreEqual("Material.001", second.Name); Assert.AreEqual(96.078431f, second.SpecularExponent); }
ObjParser.Types.Material FindMaterial(string matName) { ObjParser.Types.Material retMat = null; foreach (ObjParser.Types.Material mat in mtl.MaterialList) { if (mat.Name == matName) { retMat = mat; } } return(retMat); }
private List <ObjParser.Types.Material> ObjMaterialsOfListMaterials(List <Material> materials) { List <ObjParser.Types.Material> objMaterials = new List <ObjParser.Types.Material>(); foreach (Material material in materials) { ObjParser.Types.Material objMaterial = new ObjParser.Types.Material(); objMaterial.Name = EscapeMaterialName(material.name); objMaterial.DiffuseReflectivity = ObjColorOfColor(material.color); objMaterial.SpecularExponent = 96.078431f; objMaterial.SpecularReflectivity = ObjColorOfColor(new Color(0.5f, 0.5f, 0.5f)); objMaterial.EmissiveCoefficient = ObjColorOfColor(new Color(0, 0, 0)); objMaterial.IlluminationModel = 2; objMaterials.Add(objMaterial); } return(objMaterials); }
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); }
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); }