public void ConvertToCSX(string path, string dest, int splitcount = 0, bool flipnorms = false) { var obj = new ObjReader(); obj.Parse(path); Console.WriteLine("Loaded Obj"); var facecount = 0; foreach (var grp in obj.Groups) { facecount += grp.Faces.Count; } if (splitcount == 0) { var sw = new StreamWriter(File.OpenWrite(dest)); sw.WriteLine(@"<?xml version=""1.0"" encoding=""utf-88"" standalone=""yes""?> <!--Torque Constructor Scene document http://www.garagegames.com--> <ConstructorScene version = ""4"" creator = ""Torque Constructor"" date = ""2017/10/30 14:40:36""> <Sunlight azimuth = ""180"" elevation = ""35"" color = ""255 255 255"" ambient = ""64 64 64""/> <LightingOptions lightingSystem = """" ineditor_defaultLightmapSize = ""256"" ineditor_maxLightmapSize = ""256"" ineditor_lightingPerformanceHint = ""0"" ineditor_shadowPerformanceHint = ""1"" ineditor_TAPCompatibility = ""0"" ineditor_useSunlight = ""0"" export_defaultLightmapSize = ""256"" export_maxLightmapSize = ""256"" export_lightingPerformanceHint = ""0"" export_shadowPerformanceHint = ""1"" export_TAPCompatibility = ""0"" export_useSunlight = ""0""/> <GameTypes> <GameType name = ""Constructor""/> <GameType name = ""Torque""/> </GameTypes> <DetailLevels current = ""0""> <DetailLevel minPixelSize = ""0"" actionCenter = ""0 0 0""> <InteriorMap brushScale = ""32"" lightScale = ""8"" ambientColor = ""0 0 0"" ambientColorEmerg = ""0 0 0""> <Entities nextEntityID = ""1"" > <Entity id = ""0"" classname = ""worldspawn"" gametype = ""Torque"" isPointEntity = ""0""> <Properties detail_number = ""0"" min_pixels = ""250"" geometry_scale = ""32"" light_geometry_scale = ""8"" ambient_color = ""0 0 0"" emergency_ambient_color = ""0 0 0"" mapversion = ""220""/> </Entity> </Entities> <Brushes nextBrushID =""" + (facecount) + "\">"); var id = 0; Console.WriteLine("Writing " + facecount + " brushes,this may take a while"); for (var i = 0; i < obj.Groups.Count; i++) { var group = obj.Groups[i]; for (int k = 0; k < group.Faces.Count; k++) { Face face = group.Faces[k]; List <Vector3> vertices = new List <Vector3>(); var p1 = group.Points[face.v1]; var p2 = group.Points[face.v2]; var p3 = group.Points[face.v3]; var n = ((p2 - p1) * (p2 - p3)).Normalize(); if (float.IsNaN(n.X) || float.IsNaN(n.Y) || float.IsNaN(n.Z)) { continue; // Don't export 'linear' faces } sw.Write(" <Brush id=\"" + id + "\" owner=\"0\" type=\"0\" pos=\"0 0 -0.05\" transform=\"1 0 0 0 0 1 0 0 0 0 1 -0.05 0 0 0 1\" group=\"-1\" locked=\"0\" nextFaceID=\"" + "5" + "\" nextVertexID=\"" + "5" + "\">" + Environment.NewLine + " <Vertices>" + Environment.NewLine); n = n.Scalar(-1 / 20f); var p4 = (p1 + p2 + p3).Scalar(1 / 3f) + n; var plane = new Plane(p1, p2, p3); if (flipnorms) { plane = plane.Inverted(); p4 = p4 - (n.Scalar(2)); } vertices.Add(p1); vertices.Add(p2); vertices.Add(p3); vertices.Add(p4); //foreach (var index in group.Indices) //{ // vertices.Add(obj.Points[index - 1]); //} foreach (var vertex in vertices) { sw.WriteLine(" <Vertex pos=\"" + vertex.ToString() + "\" />"); } sw.WriteLine(" </Vertices>"); TexGenPlanes texgen; texgen = SolveTexGen(p1, p2, p3, group.UV[face.uv1], group.UV[face.uv2], group.UV[face.uv3]); texgen.d1 = 0; texgen.d2 = 0; var texgenstr = texgen.x1 + " " + texgen.y1 + " " + texgen.z1 + " " + texgen.d1 + " " + texgen.x2 + " " + texgen.y2 + " " + texgen.z2 + " " + texgen.d2 + " 0 1 1"; if (!flipnorms) { sw.WriteLine(" <Face id=\"" + 1 + "\" plane=\"" + plane.x + " " + plane.y + " " + plane.z + " " + plane.D + "\" album=\"" + "" + "\" material=\"" + obj.MtlLib[face.material] + "\" texgens=\"" + texgenstr + "\" texRot=\"" + "0" + "\" texScale=\"" + "1" + " " + "1" + "\" texDiv=\"" + "32 32" + "\">"); sw.WriteLine(" <Indices indices=\"0 1 2\"/>"); sw.WriteLine(" </Face>"); sw.WriteLine(" <Face id=\"" + 2 + "\" plane=\"" + new Plane(p4, p2, p1).ToString() + "\" album=\"" + "" + "\" material=\"" + obj.MtlLib[face.material] + "\" texgens=\"" + texgenstr + "\" texRot=\"" + "0" + "\" texScale=\"" + "1" + " " + "1" + "\" texDiv=\"" + "32 32" + "\">"); sw.WriteLine(" <Indices indices=\"3 1 0\"/>"); sw.WriteLine(" </Face>"); sw.WriteLine(" <Face id=\"" + 3 + "\" plane=\"" + new Plane(p1, p3, p4).ToString() + "\" album=\"" + "" + "\" material=\"" + obj.MtlLib[face.material] + "\" texgens=\"" + texgenstr + "\" texRot=\"" + "0" + "\" texScale=\"" + "1" + " " + "1" + "\" texDiv=\"" + "32 32" + "\">"); sw.WriteLine(" <Indices indices=\"0 2 3\"/>"); sw.WriteLine(" </Face>"); sw.WriteLine(" <Face id=\"" + 4 + "\" plane=\"" + new Plane(p2, p3, p4).ToString() + "\" album=\"" + "" + "\" material=\"" + obj.MtlLib[face.material] + "\" texgens=\"" + texgenstr + "\" texRot=\"" + "0" + "\" texScale=\"" + "1" + " " + "1" + "\" texDiv=\"" + "32 32" + "\">"); sw.WriteLine(" <Indices indices=\"1 2 3\"/>"); sw.WriteLine(" </Face>"); } else { sw.WriteLine(" <Face id=\"" + 1 + "\" plane=\"" + plane.x + " " + plane.y + " " + plane.z + " " + plane.D + "\" album=\"" + "" + "\" material=\"" + obj.MtlLib[face.material] + "\" texgens=\"" + texgenstr + "\" texRot=\"" + "0" + "\" texScale=\"" + "1" + " " + "1" + "\" texDiv=\"" + "32 32" + "\">"); sw.WriteLine(" <Indices indices=\"2 1 0\"/>"); sw.WriteLine(" </Face>"); sw.WriteLine(" <Face id=\"" + 2 + "\" plane=\"" + new Plane(p4, p2, p1).Inverted().ToString() + "\" album=\"" + "" + "\" material=\"" + obj.MtlLib[face.material] + "\" texgens=\"" + texgenstr + "\" texRot=\"" + "0" + "\" texScale=\"" + "1" + " " + "1" + "\" texDiv=\"" + "32 32" + "\">"); sw.WriteLine(" <Indices indices=\"0 1 3\"/>"); sw.WriteLine(" </Face>"); sw.WriteLine(" <Face id=\"" + 3 + "\" plane=\"" + new Plane(p1, p3, p4).Inverted().ToString() + "\" album=\"" + "" + "\" material=\"" + obj.MtlLib[face.material] + "\" texgens=\"" + texgenstr + "\" texRot=\"" + "0" + "\" texScale=\"" + "1" + " " + "1" + "\" texDiv=\"" + "32 32" + "\">"); sw.WriteLine(" <Indices indices=\"3 2 0\"/>"); sw.WriteLine(" </Face>"); sw.WriteLine(" <Face id=\"" + 4 + "\" plane=\"" + new Plane(p2, p3, p4).Inverted().ToString() + "\" album=\"" + "" + "\" material=\"" + obj.MtlLib[face.material] + "\" texgens=\"" + texgenstr + "\" texRot=\"" + "0" + "\" texScale=\"" + "1" + " " + "1" + "\" texDiv=\"" + "32 32" + "\">"); sw.WriteLine(" <Indices indices=\"3 2 1\"/>"); sw.WriteLine(" </Face>"); } sw.Write(" </Brush>"); Console.WriteLine("Wrote Brush " + k); id++; } } Console.WriteLine("Finishing up"); sw.Write(@" </Brushes> </InteriorMap> </DetailLevel> </DetailLevels> </ConstructorScene>"); sw.Flush(); sw.Close(); } else { var splitobjsraw = obj.Groups.Select(a => SplitObjects(a, splitcount)).ToList(); List <Obj> splitobjs = new List <Obj>(); splitobjsraw.ForEach(a => splitobjs.AddRange(a)); for (var i = 0; i < splitobjs.Count; i++) { var fname = dest.Insert(dest.IndexOf(".csx"), "_" + i); var sw = new StreamWriter(File.OpenWrite(fname)); var innerfacecount = 0; foreach (var grp in splitobjs) { innerfacecount += grp.Faces.Count; } sw.WriteLine(@"<?xml version=""1.0"" encoding=""utf-88"" standalone=""yes""?> <!--Torque Constructor Scene document http://www.garagegames.com--> <ConstructorScene version = ""4"" creator = ""Torque Constructor"" date = ""2017/10/30 14:40:36""> <Sunlight azimuth = ""180"" elevation = ""35"" color = ""255 255 255"" ambient = ""64 64 64""/> <LightingOptions lightingSystem = """" ineditor_defaultLightmapSize = ""256"" ineditor_maxLightmapSize = ""256"" ineditor_lightingPerformanceHint = ""0"" ineditor_shadowPerformanceHint = ""1"" ineditor_TAPCompatibility = ""0"" ineditor_useSunlight = ""0"" export_defaultLightmapSize = ""256"" export_maxLightmapSize = ""256"" export_lightingPerformanceHint = ""0"" export_shadowPerformanceHint = ""1"" export_TAPCompatibility = ""0"" export_useSunlight = ""0""/> <GameTypes> <GameType name = ""Constructor""/> <GameType name = ""Torque""/> </GameTypes> <DetailLevels current = ""0""> <DetailLevel minPixelSize = ""0"" actionCenter = ""0 0 0""> <InteriorMap brushScale = ""32"" lightScale = ""8"" ambientColor = ""0 0 0"" ambientColorEmerg = ""0 0 0""> <Entities nextEntityID = ""1"" > <Entity id = ""0"" classname = ""worldspawn"" gametype = ""Torque"" isPointEntity = ""0""> <Properties detail_number = ""0"" min_pixels = ""250"" geometry_scale = ""32"" light_geometry_scale = ""8"" ambient_color = ""0 0 0"" emergency_ambient_color = ""0 0 0"" mapversion = ""220""/> </Entity> </Entities> <Brushes nextBrushID =""" + (innerfacecount) + "\">"); var id = 0; Console.WriteLine("Writing " + facecount + " brushes,this may take a while"); var group = splitobjs[i]; for (int k = 0; k < group.Faces.Count; k++) { Face face = group.Faces[k]; sw.Write(" <Brush id=\"" + id + "\" owner=\"0\" type=\"0\" pos=\"0 0 -0.05\" transform=\"1 0 0 0 0 1 0 0 0 0 1 -0.05 0 0 0 1\" group=\"-1\" locked=\"0\" nextFaceID=\"" + "5" + "\" nextVertexID=\"" + "5" + "\">" + Environment.NewLine + " <Vertices>" + Environment.NewLine); List <Vector3> vertices = new List <Vector3>(); var p1 = group.Points[face.v1]; var p2 = group.Points[face.v2]; var p3 = group.Points[face.v3]; var n = ((p2 - p1) * (p2 - p3)).Normalize(); n = n.Scalar(-1 / 20f); var p4 = (p1 + p2 + p3).Scalar(1 / 3f) + n; var plane = new Plane(p1, p2, p3); if (flipnorms) { plane = plane.Inverted(); p4 = p4 - (n.Scalar(2)); } vertices.Add(p1); vertices.Add(p2); vertices.Add(p3); vertices.Add(p4); //foreach (var index in group.Indices) //{ // vertices.Add(obj.Points[index - 1]); //} foreach (var vertex in vertices) { sw.WriteLine(" <Vertex pos=\"" + vertex.ToString() + "\" />"); } sw.WriteLine(" </Vertices>"); TexGenPlanes texgen; texgen = SolveTexGen(p1, p2, p3, group.UV[face.uv1], group.UV[face.uv2], group.UV[face.uv3]); texgen.d1 = 0; texgen.d2 = 0; var texgenstr = texgen.x1 + " " + texgen.y1 + " " + texgen.z1 + " " + texgen.d1 + " " + texgen.x2 + " " + texgen.y2 + " " + texgen.z2 + " " + texgen.d2 + " 0 1 1"; if (!flipnorms) { sw.WriteLine(" <Face id=\"" + 1 + "\" plane=\"" + plane.x + " " + plane.y + " " + plane.z + " " + plane.D + "\" album=\"" + "" + "\" material=\"" + obj.MtlLib[face.material] + "\" texgens=\"" + texgenstr + "\" texRot=\"" + "0" + "\" texScale=\"" + "1" + " " + "1" + "\" texDiv=\"" + "32 32" + "\">"); sw.WriteLine(" <Indices indices=\"0 1 2\"/>"); sw.WriteLine(" </Face>"); sw.WriteLine(" <Face id=\"" + 2 + "\" plane=\"" + new Plane(p4, p2, p1).ToString() + "\" album=\"" + "" + "\" material=\"" + obj.MtlLib[face.material] + "\" texgens=\"" + texgenstr + "\" texRot=\"" + "0" + "\" texScale=\"" + "1" + " " + "1" + "\" texDiv=\"" + "32 32" + "\">"); sw.WriteLine(" <Indices indices=\"3 1 0\"/>"); sw.WriteLine(" </Face>"); sw.WriteLine(" <Face id=\"" + 3 + "\" plane=\"" + new Plane(p1, p3, p4).ToString() + "\" album=\"" + "" + "\" material=\"" + obj.MtlLib[face.material] + "\" texgens=\"" + texgenstr + "\" texRot=\"" + "0" + "\" texScale=\"" + "1" + " " + "1" + "\" texDiv=\"" + "32 32" + "\">"); sw.WriteLine(" <Indices indices=\"0 2 3\"/>"); sw.WriteLine(" </Face>"); sw.WriteLine(" <Face id=\"" + 4 + "\" plane=\"" + new Plane(p2, p3, p4).ToString() + "\" album=\"" + "" + "\" material=\"" + obj.MtlLib[face.material] + "\" texgens=\"" + texgenstr + "\" texRot=\"" + "0" + "\" texScale=\"" + "1" + " " + "1" + "\" texDiv=\"" + "32 32" + "\">"); sw.WriteLine(" <Indices indices=\"1 2 3\"/>"); sw.WriteLine(" </Face>"); } else { sw.WriteLine(" <Face id=\"" + 1 + "\" plane=\"" + plane.x + " " + plane.y + " " + plane.z + " " + plane.D + "\" album=\"" + "" + "\" material=\"" + obj.MtlLib[face.material] + "\" texgens=\"" + texgenstr + "\" texRot=\"" + "0" + "\" texScale=\"" + "1" + " " + "1" + "\" texDiv=\"" + "32 32" + "\">"); sw.WriteLine(" <Indices indices=\"2 1 0\"/>"); sw.WriteLine(" </Face>"); sw.WriteLine(" <Face id=\"" + 2 + "\" plane=\"" + new Plane(p4, p2, p1).Inverted().ToString() + "\" album=\"" + "" + "\" material=\"" + obj.MtlLib[face.material] + "\" texgens=\"" + texgenstr + "\" texRot=\"" + "0" + "\" texScale=\"" + "1" + " " + "1" + "\" texDiv=\"" + "32 32" + "\">"); sw.WriteLine(" <Indices indices=\"0 1 3\"/>"); sw.WriteLine(" </Face>"); sw.WriteLine(" <Face id=\"" + 3 + "\" plane=\"" + new Plane(p1, p3, p4).Inverted().ToString() + "\" album=\"" + "" + "\" material=\"" + obj.MtlLib[face.material] + "\" texgens=\"" + texgenstr + "\" texRot=\"" + "0" + "\" texScale=\"" + "1" + " " + "1" + "\" texDiv=\"" + "32 32" + "\">"); sw.WriteLine(" <Indices indices=\"3 2 0\"/>"); sw.WriteLine(" </Face>"); sw.WriteLine(" <Face id=\"" + 4 + "\" plane=\"" + new Plane(p2, p3, p4).Inverted().ToString() + "\" album=\"" + "" + "\" material=\"" + obj.MtlLib[face.material] + "\" texgens=\"" + texgenstr + "\" texRot=\"" + "0" + "\" texScale=\"" + "1" + " " + "1" + "\" texDiv=\"" + "32 32" + "\">"); sw.WriteLine(" <Indices indices=\"3 2 1\"/>"); sw.WriteLine(" </Face>"); } sw.Write(" </Brush>"); Console.WriteLine("Wrote Brush " + k); id++; } Console.WriteLine("Finishing up"); sw.Write(@" </Brushes> </InteriorMap> </DetailLevel> </DetailLevels> </ConstructorScene>"); sw.Flush(); sw.Close(); } } }
public void ConvertToMAP(string path, string dest) { var obj = new ObjReader(); obj.Parse(path); Console.WriteLine("Loaded Obj"); var sw = new StreamWriter(File.OpenWrite(dest)); var facecount = 0; foreach (var grp in obj.Groups) { facecount += grp.Faces.Count; } sw.WriteLine(@"{ ""classname"" ""worldspawn"" ""detail_number"" ""0"" ""min_pixels"" ""250"" ""geometry_scale"" ""32"" ""light_geometry_scale"" ""8"" ""ambient_color"" ""0 0 0"" ""emergency_ambient_color"" ""0 0 0"" ""mapversion"" ""220"""); var id = 0; Console.WriteLine("Writing " + facecount + " brushes,this may take a while"); for (var i = 0; i < obj.Groups.Count; i++) { var group = obj.Groups[i]; for (int k = 0; k < group.Faces.Count; k++) { Face face = group.Faces[k]; List <Vector3> vertices = new List <Vector3>(); vertices.Add(group.Points[face.v1]); vertices.Add(group.Points[face.v2]); vertices.Add(group.Points[face.v3]); //foreach (var index in group.Indices) //{ // vertices.Add(obj.Points[index - 1]); //} var p1 = group.Points[face.v1]; var p2 = group.Points[face.v2]; var p3 = group.Points[face.v3]; var n = ((p2 - p1) * (p2 - p3)).Normalize(); n = n.Scalar(-1 / 20f); var p4 = (p1 + p2 + p3).Scalar(1 / 3f) + n; var plane = new Plane(p1, p2, p3); TexGenPlanes texgen; texgen = SolveTexGen(p1, p2, p3, group.UV[face.uv1], group.UV[face.uv2], group.UV[face.uv3]); texgen.d1 = 0; texgen.d2 = 0; var texgenstr = " [ " + texgen.x1 + " " + texgen.y1 + " " + texgen.z1 + " " + texgen.d1 + " ] [" + texgen.x2 + " " + texgen.y2 + " " + texgen.z2 + " " + texgen.d2 + " ] 0 1 1"; sw.WriteLine(" {"); sw.WriteLine(" ( " + p1.ToString() + " ) ( " + p2.ToString() + " ) ( " + p3.ToString() + " ) " + obj.MtlLib[face.material] + texgenstr); sw.WriteLine(" ( " + p4.ToString() + " ) ( " + p2.ToString() + " ) ( " + p1.ToString() + " ) " + obj.MtlLib[face.material] + texgenstr); sw.WriteLine(" ( " + p1.ToString() + " ) ( " + p3.ToString() + " ) ( " + p4.ToString() + " ) " + obj.MtlLib[face.material] + texgenstr); sw.WriteLine(" ( " + p2.ToString() + " ) ( " + p3.ToString() + " ) ( " + p4.ToString() + " ) " + obj.MtlLib[face.material] + texgenstr); sw.WriteLine(" }"); Console.WriteLine("Wrote Brush " + k); id++; } } Console.WriteLine("Finishing up"); sw.Write(@"}"); sw.Flush(); sw.Close(); }