public MyObject(Lib3dsMesh _node, Lib3dsFile _model) { this.node = _node; this.model = _model; if (_node.faces == null || _node.faces.Count == 0) { this.Flag = false; } else { this.Flag = true; this.Normalizes = new Lib3dsVertex[_node.faces.Count]; for (int i = 0; i < _node.faces.Count; i++) { Lib3dsFace face = _node.faces[i]; Lib3dsVertex Point1 = _node.vertices[face.index[0]]; Lib3dsVertex Point2 = _node.vertices[face.index[1]]; Lib3dsVertex Point3 = _node.vertices[face.index[2]]; this.Normalizes[i] = CreateNormalize(Point1, Point2, Point3); } } }
public static void Export(List <Group> groups, Lib3dsFile destination) { for (int i = 0; i < groups.Count; i++) { var group = groups[i]; var material = new Lib3dsMaterial(); material.name = group[ModelField.GroupName].ToString(); destination.materials.Add(material); group = group.TextureGroup[0]; material.ambient = (float[])group[ModelField.MaterialAmbient]; material.diffuse = (float[])group[ModelField.MaterialAmbient]; material.transparency = 1.0f - material.ambient[3]; var textureId = (int)group[ModelField.Texture]; if (textureId != -1) { material.texture1_map.name = $"..\\Texture\\{textureId}.png"; material.texture1_map.percent = 1.0f; material.texture1_map.scale[0] = (float)group[ModelField.TextureDivisionU]; material.texture1_map.scale[1] = (float)group[ModelField.TexturePositionV]; material.texture1_map.offset[0] = (float)group[ModelField.TexturePositionU]; material.texture1_map.offset[1] = (float)group[ModelField.TexturePositionV]; material.texture1_map.flags = Lib3dsTextureMapFlags.LIB3DS_TEXTURE_SUMMED_AREA; } } }
public static void ClearFirstMesh(Lib3dsFile f) { for (int i = 0; i < f.meshes[0].nvertices; i++) { f.meshes[0].vertices[i] = new Lib3dsVertex(); } }
//读取3DS文件,返回成功标识 public static bool readFile(String szFilename) { Lib3dsFile file = null; file = LIB3DS.lib3ds_file_open(szFilename); return(file != null); }
/// <summary> /// Handles loading of file, starts parser on separate thread /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void LoadButton_Click(object sender, RoutedEventArgs e) { OpenFileDialog file = new OpenFileDialog(); bool? result = file.ShowDialog(); if (result == true) { string path = file.FileName; if (Path.GetExtension(path).ToLower() == ".p3d") { ChangeStatus(new StatusUpdatedEventArguments("Loading p3d file", 10)); SelectedElement = null; P3DViewItems.Clear(); itemInfo.Children.Clear(); P3DFile = new P3D(this); P3DFile.StatusUpdated += new StatusUpdatedEventHandler(ChangeStatus); Task.Run(() => P3DFile.LoadP3D(path)); } else { if (Path.GetExtension(path).ToLower() == ".3ds") { ChangeStatus(new StatusUpdatedEventArguments("Loading 3ds file", 10)); P3DViewItems.Clear(); itemInfo.Children.Clear(); File3DS = LIB3DS.lib3ds_file_open(path); P3DFile = new P3D(this); Task.Run(() => P3DFile.Load3DS(File3DS)); return; } MessageBox.Show("Format not supported"); } } }
public void Export3DS(Lib3dsFile f, Matrix m) { if (STM != null) { Matrix t = MyMatrix * m; STM.Export3DS(f, t); } }
public void Export3DS(Lib3dsFile f) { if (STMC != null) { DebugOutput.PrintLn("Exported " + pcc.Exports[MyIndex].ObjectName + " #" + MyIndex, false); STMC.Export3DS(f, MyMatrix); } DebugOutput.PrintLn("IA done."); }
public int GetMaterial(string name, P3DMaterial type, Lib3dsFile file) { if (!Materials.ContainsKey(new Tuple <string, P3DMaterial>(name, type))) { Materials.Add(new Tuple <string, P3DMaterial>(name, type), GetMaterialForNameAndType(name, type, file)); counter++; } return(Materials[new Tuple <string, P3DMaterial>(name, type)]); }
public static void AddMeshTo3DS(Lib3dsFile res, PSKFile f, Matrix m) { Lib3dsMesh mesh = new Lib3dsMesh(); string name = "Box00" + res.meshes.Count; mesh.name = name; mesh.matrix = Matrix2FA(Matrix.Identity); mesh.vertices = new List <Lib3dsVertex>(); foreach (PSKFile.PSKPoint p in f.psk.points) { Vector3 v = p.ToVector3(); v = Vector3.TransformCoordinate(v, m); mesh.vertices.Add(new Lib3dsVertex(v.X, -v.Y, v.Z)); } mesh.texcos = new List <Lib3dsTexturecoordinate>(); for (int i = 0; i < f.psk.points.Count; i++) { foreach (PSKFile.PSKEdge e in f.psk.edges) { if (e.index == i) { mesh.texcos.Add(new Lib3dsTexturecoordinate(e.U, e.V)); } } } mesh.faces = new List <Lib3dsFace>(); foreach (PSKFile.PSKFace face in f.psk.faces) { Lib3dsFace ff = new Lib3dsFace(); ff.flags = 6; ff.index = new ushort[3]; ff.index[0] = (ushort)f.psk.edges[face.v0].index; ff.index[1] = (ushort)f.psk.edges[face.v2].index; ff.index[2] = (ushort)f.psk.edges[face.v1].index; mesh.faces.Add(ff); } mesh.nfaces = (ushort)mesh.faces.Count; mesh.nvertices = (ushort)mesh.vertices.Count; mesh.map_type = Lib3dsMapType.LIB3DS_MAP_NONE; mesh.object_flags = 0; mesh.color = 128; res.meshes.Add(mesh); Lib3dsNode node = new Lib3dsMeshInstanceNode(); node.matrixNode = Matrix2FA(Matrix.Identity); node.parent = null; node.parent_id = 0xffff; node.hasNodeID = true; node.type = Lib3dsNodeType.LIB3DS_NODE_MESH_INSTANCE; node.flags = res.nodes[0].flags; node.node_id = (ushort)(res.meshes.Count() - 1); node.name = name; res.nodes.Add(node); }
public static void ConvertPSKto3DS(PSKFile f, string path) { Lib3dsFile res = EmptyFile(); AddMeshTo3DS(res, f, Matrix.Identity); ClearFirstMesh(res); if (!LIB3DS.lib3ds_file_save(res, path)) { MessageBox.Show("Error!"); } }
// Function : open 3DS file. // Description : use locale::global to make sure we can read file from the path. // Input : 3DS file's path. // Output : nothing. public void OpenFile(string FileRoad) { OpenGL gl = new OpenGL(); MyModel = LIB3DS.lib3ds_file_open(FileRoad);//モデルデータ読み込み if (MyModel == null) { Console.WriteLine("Read 3DS file error!"); return; } }
protected override bool OnFinish(BinaryReader source, BinaryWriter destination) { Model = ModelExporter.Export(_groups, _vertices, _faces, _faceGroups); if (!LIB3DS.lib3ds_file_save(Model, destination.BaseStream)) { throw new Exception("Saving 3ds file failed"); } return(true); }
private TextureName GetTextureFrom3dsID(Lib3dsFile file, int id) { foreach (TextureName tex in GetTextureList()) { if (tex.Name == file.materials[id].texture1_map.name.ToLower()) { return(tex); } } return(GetTextureList()[0]); }
public void Export3DS(Lib3dsFile f) { for (int i = 0; i < STMC.Count; i++) { if (STMC[i] != null) { DebugOutput.PrintLn((i + 1) + "/" + STMC.Count + " Exported " + pcc.Exports[MyIndex].ObjectName + " #" + MyIndex, false); STMC[i].Export3DS(f, Matrices[i]); } } DebugOutput.PrintLn("STMCA done."); }
/// <summary> /// Create a new BuildingObjectLib3DS object. /// </summary> /// <param name="father">Father node</param> /// <param name="type">Type of this object</param> /// <param name="id">Id of this object</param> /// <param name="model">All models information</param> /// <param name="textures">All textures information</param> public BuildingObjectLib3DS(BuildingObjectLib3DS father, BuildingObjectType type, uint id, Lib3dsFile model, Hashtable textures) { this.Father = father; this.Type = type; this.Id = id; this.Model = model; this.Textures = textures; // Initialization this.Childs = new Hashtable(); this.Coordinate = new Vertex(); }
public void Exportscene3DS(string path) { Lib3dsFile f = Helper3DS.EmptyFile(); foreach (Levelfile l in Levels) { l.level.Export3DS(f); } Helper3DS.ClearFirstMesh(f); if (!LIB3DS.lib3ds_file_save(f, path)) { MessageBox.Show("Error while saving!"); } }
public void Export3DS(Lib3dsFile f) { foreach (StaticMeshCollectionActor stmca in STM_CA) { stmca.Export3DS(f); } foreach (StaticMeshActor stma in STM_A) { stma.Export3DS(f); } foreach (InterpActor ia in IA) { ia.Export3DS(f); } }
public bool Parse3DSMesh(Lib3dsFile file, Lib3dsMesh mesh) { foreach (Lib3dsVertex vert in mesh.vertices) { Vertices.Add(new P3DVertex(vert.x, vert.z, vert.y)); } foreach (Lib3dsFace face in mesh.faces) { if (face.index.Length > 3) { //MessageBox.Show("SHIT."); } TexturePolygon texPoly = new TexturePolygon(); texPoly.Texture = GetTextureFrom3dsID(file, face.material).Name; texPoly.Material = MaterialFrom3DS(file.materials[face.material], (face.smoothing_group != 0)); texPoly.SGFrom3DS = face.smoothing_group; texPoly.U1 = mesh.texcos[face.index[0]].s; texPoly.U2 = mesh.texcos[face.index[2]].s; texPoly.U3 = mesh.texcos[face.index[1]].s; texPoly.V1 = mesh.texcos[face.index[0]].t; texPoly.V2 = mesh.texcos[face.index[2]].t; texPoly.V3 = mesh.texcos[face.index[1]].t; texPoly.P1 = Convert.ToInt16(face.index[0]); texPoly.P2 = Convert.ToInt16(face.index[2]); texPoly.P3 = Convert.ToInt16(face.index[1]); Polygons.Add(texPoly); } Name = mesh.name; Application.Current.Dispatcher.BeginInvoke((Action)(() => ((P3DElementView)TreeItem.Header).content.Text = Name)); Size = 1; Flags = FlagFrom3DSName(mesh.name); NumVertices = Convert.ToInt16(Vertices.Count); NumPolys = Convert.ToInt16(Polygons.Count); LocalPos = new P3DVertex(mesh.matrix[3, 0], mesh.matrix[3, 1], mesh.matrix[3, 2]); Length = 0; Height = 0; Depth = 0; return(true); }
private int GetMaterialForNameAndType(string name, P3DMaterial type, Lib3dsFile file) { Lib3dsMaterial mat = LIB3DS.lib3ds_material_new(name); mat.texture1_map = new Lib3dsTextureMap(); mat.texture1_map.name = name; mat.diffuse[0] = 0.9f; mat.diffuse[1] = 0.9f; mat.diffuse[2] = 0.9f; // if (type == P3DMaterial.MAT_FLAT) // { // mat.diffuse[0] = 0.9f; // mat.diffuse[1] = 0.9f; // mat.diffuse[2] = 0.9f; // } // else if (type == P3DMaterial.MAT_GORAUD) // { // mat.diffuse[0] = 0.9f; // mat.diffuse[1] = 0f; // mat.diffuse[2] = 0f; // } // else if (type == P3DMaterial.MAT_GORAUD_METAL_ENV) // { // mat.diffuse[0] = 0f; // mat.diffuse[1] = 0f; // mat.diffuse[2] = 0.9f; // } // else // { // mat.diffuse[0] = 0f; // mat.diffuse[1] = 0.9f; // mat.diffuse[2] = 0f; // } LIB3DS.lib3ds_file_insert_material(file, mat, -1); return(counter); }
// public void LoadP3DAssimp(Scene scene) // { // Version = 0x02; // Size = new Extent(); // // TextureChunk = new TextureChunk(this); // foreach (Material mat in scene.Materials) // { // if (!TextureChunk.IsTextureInList(mat.TextureDiffuse.FilePath.ToLower())) // { // TextureChunk.TextureNames.Add(new TextureName(TextureChunk,mat.TextureDiffuse.FilePath.ToLower())); // } // } // TextureChunk.CalculateSizeFromTexturesList(); // TextureChunk.CalculateTexNum(); // // LightsChunk = new LightsChunk(this); // foreach (Assimp.Light light in scene.Lights) // { // string name = light.Name; // float x = light.Position.X; // float y = light.Position.Y; // float z = light.Position.Z; // float radius = 1; //ASSIMP doesn't save point light radius // int color = LightsChunk.ColorFromRGB(Convert.ToInt32(light.ColorDiffuse.R*255), Convert.ToInt32(light.ColorDiffuse.G * 255), Convert.ToInt32(light.ColorDiffuse.B * 255)); // bool corona = false; // bool lensFlare = false; // bool lightUpEnv = true; // LightsChunk.Lights.Add(new DataTypes.Light(LightsChunk, name, x, y, z, radius, color, corona, lensFlare, lightUpEnv)); // } // LightsChunk.CalculateSizeFromLightsList(); // LightsChunk.CalculateLightsNum(); // // MeshesChunk = new MeshesChunk(this); // MeshesChunk.MeshesNum = Convert.ToInt16(scene.MeshCount); // MeshesChunk.Size = 0; // //MessageBox.Show(scene.RootNode.Name); // foreach (Assimp.Mesh mesh in scene.Meshes) // { // //MessageBox.Show(); // } // } public void Load3DS(Lib3dsFile scene) { Version = 0x02; Size = new Extent(); TextureChunk = new TextureChunk(this); foreach (Lib3dsMaterial mat in scene.materials) { if (mat.texture1_map.name.ToLower().Equals(String.Empty)) { continue; } if (!TextureChunk.IsTextureInList(mat.texture1_map.name.ToLower())) { TextureChunk.TextureNames.Add(new TextureName(TextureChunk, mat.texture1_map.name.ToLower())); } } if (TextureChunk.TextureNames.Count == 0) { TextureChunk.TextureNames.Add(new TextureName(TextureChunk, "colwhite.tga")); } TextureChunk.CalculateSizeFromTexturesList(); TextureChunk.CalculateTexNum(); LightsChunk = new LightsChunk(this); foreach (Lib3dsLight light in scene.lights) { string name = light.name; float x = light.position[0]; float y = light.position[2]; float z = light.position[1]; float radius = light.inner_range; int color = LightsChunk.ColorFromRGB(Convert.ToInt32(light.color[0] * 255), Convert.ToInt32(light.color[1] * 255), Convert.ToInt32(light.color[2] * 255)); bool corona = false; bool lensFlare = false; bool lightUpEnv = true; LightsChunk.Lights.Add(new DataTypes.Light(LightsChunk, name, x, y, z, radius, color, corona, lensFlare, lightUpEnv)); } LightsChunk.CalculateSizeFromLightsList(); LightsChunk.CalculateLightsNum(); MeshesChunk = new MeshesChunk(this); MeshesChunk.MeshesNum = Convert.ToInt16(scene.meshes.Count); MeshesChunk.Size = 0; //MessageBox.Show(scene.RootNode.Name); foreach (Lib3dsMesh mesh in scene.meshes) { Mesh newMesh = new Mesh(MeshesChunk); newMesh.Parse3DSMesh(scene, mesh); newMesh.SortPolygonsAndGenerateTextureInfo(); newMesh.SeparateHardEdges(); newMesh.SeparateUVVertices(); newMesh.CalculateExtent(); MeshesChunk.Meshes.Add(newMesh); } //MeshesChunk.SeparateSubMeshesEdges(); MeshesChunk.ClearUnusedVertices(); MeshesChunk.CheckFlagsValidity(); MeshesChunk.CalculateMeshChunkSize(); P3DVertex origin = MeshesChunk.CalculateMeshesLocalPos(); MeshesChunk.MoveMeshesToOrigin(); LightsChunk.CalculateLightsPostionRelativeToOrigin(origin); Size.CalculateExtentFromMeshes(MeshesChunk.Meshes); UserDataChunk = new UserDataChunk(this); UserDataChunk.Size = 4; UserDataChunk.UserData = new byte[] { 0, 0, 0, 0 }; }
public static void ExportP3D(P3D p3d, string path) { Lib3dsFile file = LIB3DS.lib3ds_file_new(); //file.frames = 0; // Lib3dsMaterial mat = LIB3DS.lib3ds_material_new("c_white"); // //LIB3DS.lib3ds_file_insert_material(file, mat, -1); // mat.diffuse[0] = 1; // mat.diffuse[1] = 1; // mat.diffuse[2] = 1; foreach (Mesh P3Dmesh in p3d.MeshesChunk.Meshes) { Lib3dsMesh mesh = LIB3DS.lib3ds_mesh_new(P3Dmesh.Name); Lib3dsMeshInstanceNode inst; LIB3DS.lib3ds_file_insert_mesh(file, mesh, -1); LIB3DS.lib3ds_mesh_resize_vertices(mesh, (ushort)P3Dmesh.NumVertices, true, false); for (int i = 0; i < P3Dmesh.NumVertices; i++) { mesh.vertices[i].x = P3Dmesh.Vertices[i].x; mesh.vertices[i].z = P3Dmesh.Vertices[i].y; mesh.vertices[i].y = P3Dmesh.Vertices[i].z; mesh.texcos[i].s = findVerticeU(i, P3Dmesh); mesh.texcos[i].t = findVerticeV(i, P3Dmesh); } LIB3DS.lib3ds_mesh_resize_faces(mesh, (ushort)P3Dmesh.NumPolys); for (int i = 0; i < P3Dmesh.NumPolys; i++) { mesh.faces[i].index[2] = (ushort)P3Dmesh.Polygons[i].P1; mesh.faces[i].index[1] = (ushort)P3Dmesh.Polygons[i].P2; mesh.faces[i].index[0] = (ushort)P3Dmesh.Polygons[i].P3; mesh.faces[i].material = _3dsMaterials.Instance.GetMaterial(P3Dmesh.Polygons[i].Texture, P3Dmesh.Polygons[i].Material, file); mesh.faces[i].smoothing_group = (uint)1; } float[] pos = { P3Dmesh.LocalPos.x, P3Dmesh.LocalPos.z, P3Dmesh.LocalPos.y }; inst = LIB3DS.lib3ds_node_new_mesh_instance(mesh, String.Empty, pos, null, null); LIB3DS.lib3ds_file_append_node(file, inst, null); } foreach (Light p3dlight in p3d.LightsChunk.Lights) { Lib3dsLight light = LIB3DS.lib3ds_light_new(p3dlight.Name); LIB3DS.lib3ds_file_insert_light(file, light, -1); light.color[0] = Convert.ToSingle(p3dlight.GetColorRed()) / 255; light.color[1] = Convert.ToSingle(p3dlight.GetColorGreen()) / 255; light.color[2] = Convert.ToSingle(p3dlight.GetColorBlue()) / 255; float[] pos = { p3dlight.Position.x, p3dlight.Position.z, p3dlight.Position.y }; light.position = pos; light.inner_range = p3dlight.Radius; light.multiplier = 1.5f; Lib3dsOmnilightNode inst = LIB3DS.lib3ds_node_new_omnilight(light); LIB3DS.lib3ds_file_append_node(file, inst, null); } if (!LIB3DS.lib3ds_file_save(file, path)) { MessageBox.Show("ERROR: Saving 3ds file failed!"); } LIB3DS.lib3ds_file_free(file); }
//写3DS文件 protected virtual bool writeToFile() { if (mDbVertices == null || mDbTextureCoors == null || uFacesIndex == null) { return(false); } //新建LIB3DS文件对象 Lib3dsFile file = LIB3DS.lib3ds_file_new(); file.frames = 360; //新建网格节点 Lib3dsMesh mesh = LIB3DS.lib3ds_mesh_new("mesh"); Lib3dsMeshInstanceNode inst; LIB3DS.lib3ds_file_insert_mesh(file, mesh, -1); //一、将顶点写入网格 int nVertices = mDbVertices.GetLength(0); LIB3DS.lib3ds_mesh_resize_vertices(mesh, (ushort)nVertices, true, false); for (int i = 0; i < nVertices; i++) { Lib3dsVertex vertexTmp = new Lib3dsVertex(mDbVertices[i, 0], mDbVertices[i, 1], mDbVertices[i, 2]); LIB3DS.lib3ds_vector_copy(mesh.vertices[i], vertexTmp); //将纹理坐标写入网格 mesh.texcos[i] = new Lib3dsTexturecoordinate(mDbTextureCoors[i, 0], mDbTextureCoors[i, 1]); } //二、将纹理信息写入文件 Lib3dsMaterial mat = LIB3DS.lib3ds_material_new("material1"); LIB3DS.lib3ds_file_insert_material(file, mat, -1); //如果没有指定纹理,则默认为灰色材质 if (String.IsNullOrEmpty(mSzTextureFilename)) { mat.diffuse[0] = 0.5f; mat.diffuse[1] = 0.5f; mat.diffuse[2] = 0.5f; } else { mat.texture1_map.name = mSzTextureFilename; mat.texture1_map.percent = 1.0f; } //三、将三角化后的面的顶点索引号写入网格 int nFaces = uFacesIndex.GetLength(0); LIB3DS.lib3ds_mesh_resize_faces(mesh, (ushort)nFaces); for (int i = 0; i < nFaces; i++) { for (int j = 0; j < 3; j++) { mesh.faces[i].index[j] = uFacesIndex[i, j]; } //指定每个三角化后的面的材质 mesh.faces[i].material = 0; } inst = LIB3DS.lib3ds_node_new_mesh_instance(mesh, "01", null, null, null); LIB3DS.lib3ds_file_append_node(file, inst, null); if (!LIB3DS.lib3ds_file_save(file, mSzOutputFilename)) { LIB3DS.lib3ds_file_free(file); return(false); } LIB3DS.lib3ds_file_free(file); return(true); }