private static unsafe AiMesh[] Parse(EZMMesh ezmMesh) { EZMVertexbuffer vertexbuffer = ezmMesh.Vertexbuffer; vec3[] vertexes = GetPositions(vertexbuffer); vec3[] normals = GetNormals(vertexbuffer); vec2[] texCoords = GetTexCoords(vertexbuffer); vec4[] boneWeights = GetBoneWeights(vertexbuffer); uvec4[] boneIndexes = GetBoneIndexes(vertexbuffer); EZMMeshSection[] meshSections = ezmMesh.MeshSections; var aiMeshes = new AiMesh[meshSections.Length]; for (int i = 0; i < aiMeshes.Length; i++) { EZMMeshSection section = meshSections[i]; var aiMesh = new AiMesh(); aiMesh.Vertexes = vertexes; aiMesh.Normals = normals; aiMesh.TexCoords = texCoords; aiMesh.boneWeights = boneWeights; aiMesh.boneIndexes = boneIndexes; aiMesh.indexes = section.Indexbuffer; aiMesh.materialName = section.MaterialName; aiMeshes[i] = aiMesh; } return(aiMeshes); }
unsafe private static vec3[] GetNormals(EZMVertexbuffer vertexbuffer) { Passbuffer passbuffer = vertexbuffer.GetBuffer("normal"); IntPtr address = passbuffer.Mapbuffer(); vec3 * pointer = (vec3 *)address.ToPointer(); int length = passbuffer.Length(); var normals = new vec3[length]; for (int i = 0; i < length; i++) { normals[i] = pointer[i]; } passbuffer.Unmapbuffer(); return(normals); }
unsafe private static vec2[] GetTexCoords(EZMVertexbuffer vertexbuffer) { Passbuffer passbuffer = vertexbuffer.GetBuffer("texcoord1"); IntPtr address = passbuffer.Mapbuffer(); vec2 * pointer = (vec2 *)address.ToPointer(); int length = passbuffer.Length(); var texCoords = new vec2[length]; for (int i = 0; i < length; i++) { texCoords[i] = pointer[i]; } passbuffer.Unmapbuffer(); return(texCoords); }
unsafe private static vec4[] GetBoneWeights(EZMVertexbuffer vertexbuffer) { Passbuffer passbuffer = vertexbuffer.GetBuffer("blendweights"); IntPtr address = passbuffer.Mapbuffer(); vec4 * pointer = (vec4 *)address.ToPointer(); int length = passbuffer.Length(); var boneWeights = new vec4[length]; for (int i = 0; i < length; i++) { boneWeights[i] = pointer[i]; } passbuffer.Unmapbuffer(); return(boneWeights); }
unsafe private static uvec4[] GetBoneIndexes(EZMVertexbuffer vertexbuffer) { Passbuffer passbuffer = vertexbuffer.GetBuffer("blendindices"); IntPtr address = passbuffer.Mapbuffer(); uvec4 * pointer = (uvec4 *)address.ToPointer(); int length = passbuffer.Length(); var boneIndexes = new uvec4[length]; for (int i = 0; i < length; i++) { boneIndexes[i] = pointer[i]; } passbuffer.Unmapbuffer(); return(boneIndexes); }
unsafe private static vec3[] GetPositions(EZMVertexbuffer vertexbuffer) { Passbuffer passbuffer = vertexbuffer.GetBuffer("position"); IntPtr address = passbuffer.Mapbuffer(); vec3 * pointer = (vec3 *)address.ToPointer(); int length = passbuffer.Length(); var vertexes = new vec3[length]; for (int i = 0; i < length; i++) { vertexes[i] = pointer[i]; } passbuffer.Unmapbuffer(); return(vertexes); }
// <Mesh name="him" skeleton="null" submesh_count="5"> /// <summary> /// /// </summary> /// <param name="xElement"></param> /// <returns></returns> public static EZMMesh Parse(XElement xElement) { EZMMesh result = null; if (xElement.Name == "Mesh") { result = new EZMMesh(); { var attr = xElement.Attribute("name"); if (attr != null) { result.Name = attr.Value; } } { var attr = xElement.Attribute("skeleton"); if (attr != null) { result.SkeletonName = attr.Value; } } { result.Vertexbuffer = EZMVertexbuffer.Parse(xElement.Element("vertexbuffer")); } { var xMeshSections = xElement.Elements("MeshSection"); var meshSections = new EZMMeshSection[xMeshSections.Count()]; int index = 0; foreach (var xMeshSection in xMeshSections) { meshSections[index++] = EZMMeshSection.Parse(xMeshSection); } result.MeshSections = meshSections; } } return(result); }
// <vertexbuffer count="13114" ctype="fff fff ff ff ff ffff hhhh" semantic="position normal texcoord1 texcoord2 texcoord3 blendweights blendindices"> /// <summary> /// /// </summary> /// <param name="xElement"></param> /// <returns></returns> public static unsafe EZMVertexbuffer Parse(System.Xml.Linq.XElement xElement) { EZMVertexbuffer result = null; if (xElement.Name == "vertexbuffer") { result = new EZMVertexbuffer(); int groupCount = int.Parse(xElement.Attribute("count").Value); result.Ctypes = xElement.Attribute("ctype").Value.Split(' '); result.Semantics = xElement.Attribute("semantic").Value.Split(' '); if (result.Ctypes.Length != result.Semantics.Length) { throw new Exception("EZMVertexbuffer.Ctypes.Length != EZMVertexbuffer.Semantics.Length"); } int groupSize = result.Ctypes.Length; // how many elements in a group. string[] parts = xElement.Value.Split(Separator.separators, StringSplitOptions.RemoveEmptyEntries); var lineLength = result.Ctypes[0].Length; var charCounts = new int[groupSize]; for (int i = 1; i < groupSize; i++) { charCounts[i] = charCounts[i - 1] + result.Ctypes[i - 1].Length; lineLength += result.Ctypes[i].Length; } var buffers = new Passbuffer[groupSize]; var pointers = new void *[groupSize]; for (int i = 0; i < groupSize; i++) { int index = charCounts[i]; buffers[i] = new Passbuffer(result.Ctypes[i], groupCount); pointers[i] = buffers[i].Mapbuffer().ToPointer(); switch (buffers[i].type) { case PassType.vec4: for (int t = 0; t < groupCount; t++) { float x = float.Parse(parts[t * lineLength + index + 0]); float y = float.Parse(parts[t * lineLength + index + 1]); float z = float.Parse(parts[t * lineLength + index + 2]); float w = float.Parse(parts[t * lineLength + index + 3]); var array = (vec4 *)pointers[i]; array[t] = new vec4(x, y, z, w); if (!(0 <= x && x <= 1)) { Console.WriteLine("Error"); } if (!(0 <= y && y <= 1)) { Console.WriteLine("Error"); } if (!(0 <= z && z <= 1)) { Console.WriteLine("Error"); } if (!(0 <= w && w <= 1)) { Console.WriteLine("Error"); } } break; case PassType.vec3: for (int t = 0; t < groupCount; t++) { float x = float.Parse(parts[t * lineLength + index + 0]); float y = float.Parse(parts[t * lineLength + index + 1]); float z = float.Parse(parts[t * lineLength + index + 2]); var array = (vec3 *)pointers[i]; array[t] = new vec3(x, y, z); } break; case PassType.vec2: var vec2xList = new List <float>(); var vec2yList = new List <float>(); for (int t = 0; t < groupCount; t++) { float x = float.Parse(parts[t * lineLength + index + 0]); if (x < 0) { x = 0; } if (1 < x) { x = 1; } float y = float.Parse(parts[t * lineLength + index + 1]); if (y < 0) { y = 0; } if (1 < y) { y = 1; } var array = (vec2 *)pointers[i]; array[t] = new vec2(x, y); if (!(0 <= x && x <= 1)) { vec2xList.Add(x); } if (!(0 <= y && y <= 1)) { vec2yList.Add(y); } } if (vec2xList.Count > 0 || vec2yList.Count > 0) { Console.WriteLine("Error"); } break; case PassType.ivec4: var list = new List <int>(); for (int t = 0; t < groupCount; t++) { int x = int.Parse(parts[t * lineLength + index + 0]); int y = int.Parse(parts[t * lineLength + index + 1]); int z = int.Parse(parts[t * lineLength + index + 2]); int w = int.Parse(parts[t * lineLength + index + 3]); var array = (ivec4 *)pointers[i]; array[t] = new ivec4(x, y, z, w); if (!list.Contains(x)) { list.Add(x); } if (!list.Contains(y)) { list.Add(y); } if (!list.Contains(z)) { list.Add(z); } if (!list.Contains(w)) { list.Add(w); } } break; default: break; } buffers[i].Unmapbuffer(); } result.Buffers = buffers; } return(result); }