public void Load(string materialFileName) { using (var stream = _materialStreamProvider.Open(materialFileName)) { if (stream != null) { _loader.Load(stream); } } }
/// <summary> /// Deprecated, cannot solve tangent properly. /// </summary> static void importObj() { var openFileDialog2 = new OpenFileDialog(); string res = ""; if (openFileDialog2.ShowDialog() == DialogResult.No) { return; } res = openFileDialog2.FileName; var objLoaderFactory = new ObjLoaderFactory(); MaterialStreamProvider msp = new MaterialStreamProvider(); var openFileDialog3 = new OpenFileDialog(); openFileDialog3.Title = "Choose MTL file:"; if (openFileDialog3.ShowDialog() == DialogResult.No) { return; } msp.Open(openFileDialog3.FileName); var objLoader = objLoaderFactory.Create(msp); FileStream fileStream = new FileStream(res, FileMode.Open); LoadResult result = objLoader.Load(fileStream); // ObjLoader.Loader.Data.Elements.Face f = result.Groups[0].Faces[0]; // ObjLoader.Loader.Data.Elements.FaceVertex[] fv =getVertices(f); // string groups = new JavaScriptSerializer().Serialize(fv); //string vertices = new JavaScriptSerializer().Serialize(result.Vertices); //MessageBox.Show(groups,"Group info"); // MessageBox.Show(vertices, "V info"); fileStream.Close(); //Step 1 add a new buffer layout for my program: int layoutCount = targetFlver.BufferLayouts.Count; FLVER.BufferLayout newBL = new FLVER.BufferLayout(); newBL.Add(new FLVER.BufferLayout.Member(0, 0, FLVER.BufferLayout.MemberType.Float3, FLVER.BufferLayout.MemberSemantic.Position, 0)); newBL.Add(new FLVER.BufferLayout.Member(0, 12, FLVER.BufferLayout.MemberType.Byte4B, FLVER.BufferLayout.MemberSemantic.Normal, 0)); newBL.Add(new FLVER.BufferLayout.Member(0, 16, FLVER.BufferLayout.MemberType.Byte4B, FLVER.BufferLayout.MemberSemantic.Tangent, 0)); newBL.Add(new FLVER.BufferLayout.Member(0, 20, FLVER.BufferLayout.MemberType.Byte4B, FLVER.BufferLayout.MemberSemantic.Tangent, 1)); newBL.Add(new FLVER.BufferLayout.Member(0, 24, FLVER.BufferLayout.MemberType.Byte4B, FLVER.BufferLayout.MemberSemantic.BoneIndices, 0)); newBL.Add(new FLVER.BufferLayout.Member(0, 28, FLVER.BufferLayout.MemberType.Byte4C, FLVER.BufferLayout.MemberSemantic.BoneWeights, 0)); newBL.Add(new FLVER.BufferLayout.Member(0, 32, FLVER.BufferLayout.MemberType.Byte4C, FLVER.BufferLayout.MemberSemantic.VertexColor, 1)); newBL.Add(new FLVER.BufferLayout.Member(0, 36, FLVER.BufferLayout.MemberType.UVPair, FLVER.BufferLayout.MemberSemantic.UV, 0)); targetFlver.BufferLayouts.Add(newBL); int materialCount = targetFlver.Materials.Count; FLVER.Mesh mn = new FLVER.Mesh(); mn.MaterialIndex = 0; mn.BoneIndices = new List <int>(); mn.BoneIndices.Add(0); mn.BoneIndices.Add(1); mn.BoundingBoxMax = new Vector3(1, 1, 1); mn.BoundingBoxMin = new Vector3(-1, -1, -1); mn.BoundingBoxUnk = new Vector3(); mn.Unk1 = 0; mn.DefaultBoneIndex = 0; mn.Dynamic = false; mn.VertexBuffers = new List <FLVER.VertexBuffer>(); mn.VertexBuffers.Add(new FLVER.VertexBuffer(0, layoutCount, -1)); mn.Vertices = new List <FLVER.Vertex>(); // mn.Vertices.Add(generateVertex(new Vector3(1,0,0),new Vector3(0,0,0),new Vector3(0,0,0),new Vector3(0,1,0),new Vector3(1,0,0))); //mn.Vertices.Add(generateVertex(new Vector3(0, 1, 0), new Vector3(0, 0, 0), new Vector3(0, 0, 0), new Vector3(0, 1, 0), new Vector3(1, 0, 0))); //mn.Vertices.Add(generateVertex(new Vector3(0, 0, 1), new Vector3(0, 0, 0), new Vector3(0, 0, 0), new Vector3(0, 1, 0), new Vector3(1, 0, 0))); if (result.Groups.Count == 0) { MessageBox.Show("You imported nothing!"); return; } MessageBox.Show("Vertice number:" + result.Vertices.Count + "Texture V number:" + result.Textures.Count + "Normal number:" + result.Normals.Count + "Face groups:" + result.Groups[0].Faces.Count); VertexNormalList[] vnlist = new VertexNormalList[result.Vertices.Count + 1]; for (int i = 0; i < vnlist.Length; i++) { vnlist[i] = new VertexNormalList(); } List <uint> faceIndexs = new List <uint>(); uint[] textureIndexs = new uint[result.Vertices.Count + 1]; foreach (var gr in result.Groups) { foreach (var faces in gr.Faces) { var vList = getVertices(faces); /*for (int i3 = 0; i3 < vList.Length - 2; i3++) * { * faceIndexs.Add((uint)(vList[i3].VertexIndex)-1); * faceIndexs.Add((uint)(vList[i3+1].VertexIndex)-1); * faceIndexs.Add((uint)(vList[i3+2].VertexIndex)-1); * }*/ if (vList.Length == 4) { faceIndexs.Add((uint)(vList[0].VertexIndex) - 1); faceIndexs.Add((uint)(vList[2].VertexIndex) - 1); faceIndexs.Add((uint)(vList[1].VertexIndex) - 1); //record normal to help calculate vertex normals int helperI = 0; vnlist[(uint)(vList[helperI].VertexIndex) - 1].add(new Vector3D(result.Normals[vList[helperI].NormalIndex - 1].X, result.Normals[vList[helperI].NormalIndex - 1].Y, result.Normals[vList[helperI].NormalIndex - 1].Z)); textureIndexs[(vList[helperI].VertexIndex) - 1] = ((uint)vList[helperI].TextureIndex - 1); helperI = 2; vnlist[(uint)(vList[helperI].VertexIndex) - 1].add(new Vector3D(result.Normals[vList[helperI].NormalIndex - 1].X, result.Normals[vList[helperI].NormalIndex - 1].Y, result.Normals[vList[helperI].NormalIndex - 1].Z)); textureIndexs[(vList[helperI].VertexIndex) - 1] = ((uint)vList[helperI].TextureIndex - 1); helperI = 1; vnlist[(uint)(vList[helperI].VertexIndex) - 1].add(new Vector3D(result.Normals[vList[helperI].NormalIndex - 1].X, result.Normals[vList[helperI].NormalIndex - 1].Y, result.Normals[vList[helperI].NormalIndex - 1].Z)); textureIndexs[(vList[helperI].VertexIndex) - 1] = ((uint)vList[helperI].TextureIndex - 1); faceIndexs.Add((uint)(vList[2].VertexIndex) - 1); faceIndexs.Add((uint)(vList[0].VertexIndex) - 1); faceIndexs.Add((uint)(vList[3].VertexIndex) - 1); helperI = 2; vnlist[(uint)(vList[helperI].VertexIndex) - 1].add(new Vector3D(result.Normals[vList[helperI].NormalIndex - 1].X, result.Normals[vList[helperI].NormalIndex - 1].Y, result.Normals[vList[helperI].NormalIndex - 1].Z)); textureIndexs[(vList[helperI].VertexIndex) - 1] = ((uint)vList[helperI].TextureIndex - 1); helperI = 0; vnlist[(uint)(vList[helperI].VertexIndex) - 1].add(new Vector3D(result.Normals[vList[helperI].NormalIndex - 1].X, result.Normals[vList[helperI].NormalIndex].Y, result.Normals[vList[helperI].NormalIndex].Z)); textureIndexs[(vList[helperI].VertexIndex) - 1] = ((uint)vList[helperI].TextureIndex - 1); helperI = 3; vnlist[(uint)(vList[helperI].VertexIndex) - 1].add(new Vector3D(result.Normals[vList[helperI].NormalIndex].X, result.Normals[vList[helperI].NormalIndex].Y, result.Normals[vList[helperI].NormalIndex].Z)); textureIndexs[(vList[helperI].VertexIndex) - 1] = ((uint)vList[helperI].TextureIndex - 1); } else if (vList.Length == 3) { faceIndexs.Add((uint)(vList[0].VertexIndex) - 1); faceIndexs.Add((uint)(vList[2].VertexIndex) - 1); faceIndexs.Add((uint)(vList[1].VertexIndex) - 1); int helperI = 2; vnlist[(uint)(vList[helperI].VertexIndex) - 1].add(new Vector3D(result.Normals[vList[helperI].NormalIndex - 1].X, result.Normals[vList[helperI].NormalIndex - 1].Y, result.Normals[vList[helperI].NormalIndex - 1].Z)); textureIndexs[(vList[helperI].VertexIndex) - 1] = ((uint)vList[helperI].TextureIndex - 1); helperI = 0; vnlist[(uint)(vList[helperI].VertexIndex) - 1].add(new Vector3D(result.Normals[vList[helperI].NormalIndex - 1].X, result.Normals[vList[helperI].NormalIndex - 1].Y, result.Normals[vList[helperI].NormalIndex - 1].Z)); textureIndexs[(vList[helperI].VertexIndex) - 1] = ((uint)vList[helperI].TextureIndex - 1); helperI = 1; vnlist[(uint)(vList[helperI].VertexIndex) - 1].add(new Vector3D(result.Normals[vList[helperI].NormalIndex - 1].X, result.Normals[vList[helperI].NormalIndex - 1].Y, result.Normals[vList[helperI].NormalIndex - 1].Z)); textureIndexs[(vList[helperI].VertexIndex) - 1] = ((uint)vList[helperI].TextureIndex - 1); } } } //mn.FaceSets[0].Vertices = new uint [3]{0,1,2 }; mn.FaceSets = new List <FLVER.FaceSet>(); //FLVER.Vertex myv = new FLVER.Vertex(); //myv.Colors = new List<FLVER.Vertex.Color>(); mn.FaceSets.Add(generateBasicFaceSet()); mn.FaceSets[0].Vertices = faceIndexs.ToArray(); //Set all the vertices. for (int iv = 0; iv < result.Vertices.Count; iv++) { var v = result.Vertices[iv]; Vector3 uv1 = new Vector3(); Vector3 uv2 = new Vector3(); Vector3 normal = new Vector3(0, 1, 0); Vector3 tangent = new Vector3(1, 0, 0); if (result.Textures != null) { if (iv < result.Textures.Count) { var vm = result.Textures[(int)textureIndexs[iv]]; uv1 = new Vector3(vm.X, vm.Y, 0); uv2 = new Vector3(vm.X, vm.Y, 0); } } normal = vnlist[iv].calculateAvgNormal().toNumV3(); tangent = RotatePoint(normal, 0, (float)Math.PI / 2, 0); mn.Vertices.Add(generateVertex(new Vector3(v.X, v.Y, v.Z), uv1, uv2, normal, tangent)); } FLVER.Material matnew = new JavaScriptSerializer().Deserialize <FLVER.Material>(new JavaScriptSerializer().Serialize(targetFlver.Materials[0])); matnew.Name = res.Substring(res.LastIndexOf('\\') + 1); targetFlver.Materials.Add(matnew); mn.MaterialIndex = materialCount; targetFlver.Meshes.Add(mn); MessageBox.Show("Added a custom mesh! PLease click modify to save it!"); updateVertices(); //mn.Vertices.Add(); }