本文主要是介绍C#创建obj三维模型文件,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
介绍
使用开源库创建obj三维模型文件。
开源库地址:https://github.com/JeremyAnsel/JeremyAnsel.Media.WavefrontObj
相关API地址:https://jeremyansel.github.io/JeremyAnsel.Media.WavefrontObj/api/JeremyAnsel.Media.WavefrontObj.ObjFile.html
obj格式解读:https://blog.csdn.net/shebao3333/article/details/132094257
示例
效果图(灰色为外,红色为内):
测试代码:
//面片1var face1Pts = new float[][]{new float[]{0,0,0},new float[]{0,1,0},new float[]{1,1,0},new float[]{1,0,0},};//面片2var face2Pts = new float[][]{new float[]{0,0,0},new float[]{1,0,0},new float[]{1,0,0.5f},new float[]{0,0,0.5f},};//面片3var face3Pts = new float[][]{new float[]{0,1,0},new float[]{0,1,0.5f},new float[]{1,1,0.5f},new float[]{1,1,0},};var obj = new ObjFile();//用组表示对象//第一个组有两个面var group1 = new ObjGroup("group1");obj.Groups.Add(group1);//添加点foreach (var pt in face1Pts){obj.Vertices.Add(new ObjVertex(pt[0], pt[1], pt[2]));}//添加面var face = new ObjFace();//根据右手定则确定面片的正面(外面)face.Vertices.Add(new ObjTriplet(1, 0, 0));face.Vertices.Add(new ObjTriplet(2, 0, 0));face.Vertices.Add(new ObjTriplet(3, 0, 0));face.Vertices.Add(new ObjTriplet(4, 0, 0));face.ObjectName = "group1-face1";face.MapName = "group1-face1";face.MaterialName = "Wall";//面片添加到组里,也需要添加到obj的Faces集合中group1.Faces.Add(face);obj.Faces.Add(face);//第一个组的第二个面foreach (var pt in face2Pts){obj.Vertices.Add(new ObjVertex(pt[0], pt[1], pt[2]));}var face2 = new ObjFace();face2.Vertices.Add(new ObjTriplet(5, 0, 0));face2.Vertices.Add(new ObjTriplet(6, 0, 0));face2.Vertices.Add(new ObjTriplet(7, 0, 0));face2.Vertices.Add(new ObjTriplet(8, 0, 0));face2.ObjectName = "group1-face2";face2.MapName = "group1-face2";face2.MaterialName = "Column";group1.Faces.Add(face2);obj.Faces.Add(face2);//创建第二个对象,第二个对象有一个面var group2 = new ObjGroup("group2");obj.Groups.Add(group2);foreach (var pt in face3Pts){obj.Vertices.Add(new ObjVertex(pt[0], pt[1], pt[2]));}var face3 = new ObjFace();face3.Vertices.Add(new ObjTriplet(9, 0, 0));face3.Vertices.Add(new ObjTriplet(10, 0, 0));face3.Vertices.Add(new ObjTriplet(11, 0, 0));face3.Vertices.Add(new ObjTriplet(12, 0, 0));face3.ObjectName = "group2-face1";face3.MapName = "group2-face1";face3.MaterialName = "Window";group2.Faces.Add(face3);obj.Faces.Add(face3);//其他参数//face.LevelOfDetail = 2;//face.MapName = "c";//face.MaterialName = "d";//face.SmoothingGroupNumber = 10;//face.IsBevelInterpolationEnabled = true;//face.IsColorInterpolationEnabled = true;//face.IsDissolveInterpolationEnabled = true;obj.WriteTo("./Tmp.obj");
生成的obj文件内容:
v 0.000000 0.000000 0.000000
v 0.000000 1.000000 0.000000
v 1.000000 1.000000 0.000000
v 1.000000 0.000000 0.000000
v 0.000000 0.000000 0.000000
v 1.000000 0.000000 0.000000
v 1.000000 0.000000 0.500000
v 0.000000 0.000000 0.500000
v 0.000000 1.000000 0.000000
v 0.000000 1.000000 0.500000
v 1.000000 1.000000 0.500000
v 1.000000 1.000000 0.000000
g group1
o group1-face1
usemap group1-face1
usemtl Wall
f 1 2 3 4
o group1-face2
usemap group1-face2
usemtl Column
f 5 6 7 8
g group2
o group2-face1
usemap group2-face1
usemtl Window
f 9 10 11 12
补充:
- 根据右手定则确定面片的正面(外面)
- ObjectName、MapName、MaterialName的赋值可以注释跳过。
这篇关于C#创建obj三维模型文件的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!