admin 管理员组文章数量: 1184232
2024年3月9日发(作者:2019年钢材价格一览表)
}
// Dictionary mapping the DataType enumeration to the corresponding keyword. private static Dictionary
///
// Create the stream reader for the file var reader = new StreamReader(stream);
// Store the lines here var lines = new List
// Store the current line here var current = ;
// Read the file line by line and normalize them while ((current = ne()) != null) (NormalizeLine(current));
// Create empty mesh instance var obj = new WavefrontObject();
// Iterate over all lines foreach (string line in lines) { // Get line type and content DataType type = GetType(line); string content = GetContent(line, type);
// Line is a position if (type == on) (ParseVector3(content));
// Line is a texture coordinate if (type == rd)
if (type == rd) (ParseVector2(content));
// Line is a normal vector if (type == ) (ParseVector3(content));
// Line is a mesh sub group if (type == ) (new WavefrontFaceGroup() { Name = content });
// Line is a polygon if (type == ) { // Create the default group for all faces outside a group if ( == 0) (new WavefrontFaceGroup());
// Add the face to the last group added ().(ParseFace(content)); } }
return obj; }
// Trim beginning and end and collapse all whitespace in a string to single space. private string NormalizeLine(string line) { return e((), @"s+", " "); }
// Get the type of data stored in the specified line. private DataType GetType(string line) { // Iterate over the keywords foreach (var item in Keywords) { var type = ; var keyword = ;
// Line starts with current keyword if (r().StartsWith(r() + " ")) { // Return current type return type; } }
// No type return ; }
// Remove the keyword from the start of the line and return the result. // Returns an empty string if the specified type was . private string GetContent(string line, DataType type) { // If empty return empty string, // else remove the keyword from the start return type == : ing(Keywords[type].Length).Trim(); }
// Create an array of floats of arbitary length from a string representation, // where the floats are spearated by whitespace.
// where the floats are spearated by whitespace. private static float[] ParseFloatArray(string str, int count) { var floats = new float[count];
var segments = (' ');
for (int i = 0; i < count; i++) { if (i < ) { try { floats[i] = (segments[i], antCulture); } catch { floats[i] = 0; } } }
return floats; }
// Parse a 3D vector from a string definition in the form of: 2.0 3.0 1.0 private Vector2 ParseVector2(string str) { var components = ParseFloatArray(str, 3);
var vec = new Vector2(components[0], components[1]);
return components[2] == 0 vec : vec / components[2]; }
// Parse a 3D vector from a string definition in the form of: 1.0 2.0 3.0 1.0 private Vector3 ParseVector3(string str) { var components = ParseFloatArray(str, 4);
var vec = new Vector3(components[0], components[1], components[2]);
return components[3] == 0 vec : vec / components[3]; }
// Parse a OBJ face from a string definition. private WavefrontFace ParseFace(string str) { // Split the face definition at whitespace var segments = (new Char[0], EmptyEntries);
var vertices = new List
// Iterate over the segments foreach (string segment in segments) { // Parse and add the vertex (ParseVertex(segment)); }
// Create and return the face return new WavefrontFace()
{ Vertices = vertices, }; }
// Parse an OBJ vertex from a string definition in the forms of: // 1/2/3 // 1//3 // 1/2 // 1 private WavefrontVertex ParseVertex(string str) { // Split the string definition at the slash separator var segments = ('/');
// Store the vertex indices here var indices = new int[3];
// Iterate 3 times for (int i = 0; i < 3; i++) { // If no segment exists at the location or the segment can not be passed to an integer // Set the index to zero if ( <= i || !se(segments[i], out indices[i])) indices[i] = 0; }
// Create the new vertex return new WavefrontVertex() { Position = indices[0], Texcoord = indices[1], Normal = indices[2], }; } }
///
// Lists containing the vertex components public List
// List of sub meshes public List
///
public class WavefrontFaceGroup { public WavefrontFaceGroup() { Faces = new List
// Name of the sub mesh public string Name { get; set; }
// A list of faces public List
// Get the total number of triangles public int TriangleCount { get { var count = 0;
foreach (var face in Faces) count += leCount;
return count; } } }
///
// Number of triangles the face (polygon) consists of public int TriangleCount { get { return - 2; } }
// Number of vertices public int VertexCount { get { return ; } } }
///
{ public WavefrontVertex(int position, int texcoord, int normal) : this() { Position = position; Texcoord = texcoord; Normal = normal; }
// Inidices of the vertex components public int Position { get; set; } public int Normal { get; set; } public int Texcoord { get; set; } }}
版权声明:本文标题:C#OBJ模型解析的封装(网上看到的保留一份) 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.roclinux.cn/b/1709971591a551724.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论