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); }
//写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); }