public STGenericObject CreateGenericObject(Mesh msh, int Index, Matrix4 transform) { STGenericObject obj = new STGenericObject(); Console.WriteLine(msh.MaterialIndex); if (msh.MaterialIndex != -1) { obj.MaterialIndex = msh.MaterialIndex; } else { scene.Materials.Add(new Material() { Name = msh.Name }); } if (scene.Materials[msh.MaterialIndex].Name == "") { scene.Materials[msh.MaterialIndex].Name = msh.Name; } obj.HasPos = msh.HasVertices; obj.HasNrm = msh.HasNormals; obj.HasUv0 = msh.HasTextureCoords(0); obj.HasUv1 = msh.HasTextureCoords(1); obj.HasUv2 = msh.HasTextureCoords(2); obj.HasIndices = msh.HasBones; if (msh.HasBones) { obj.HasWeights = msh.Bones[0].HasVertexWeights; } obj.HasTans = msh.HasTangentBasis; obj.HasBitans = msh.HasTangentBasis; obj.HasVertColors = msh.HasVertexColors(0); obj.ObjectName = msh.Name; obj.boneList = GetBoneList(msh); obj.MaxSkinInfluenceCount = GetVertexSkinCount(msh); GenericObject.LOD_Mesh lod = new GenericObject.LOD_Mesh(); lod.faces = GetFaces(msh); lod.IndexFormat = STIndexFormat.UInt16; lod.PrimitiveType = STPolygonType.Triangle; lod.GenerateSubMesh(); obj.lodMeshes.Add(lod); obj.vertices = GetVertices(msh, transform, obj); obj.VertexBufferIndex = Index; return(obj); }
public void LoadFile(string FileName, bool IsModel = false) { if (!IsModel) { MessageBox.Show("Not valid model csv"); return; } string line = null; List <Model> models = new List <Model>(); TextReader csv = new StreamReader(FileName); Model model = new Model(); STGenericObject STobj = new STGenericObject(); Vertex vtx = new Vertex(); GenericObject.LOD_Mesh lod = new GenericObject.LOD_Mesh(); int Index = 0; int ww = 0; while (true) { line = csv.ReadLine(); if (line != null) { if (line.StartsWith("Obj Name")) { model = new Model(); STobj = new STGenericObject(); model.Name = line.Split(':')[1].Replace("\n", ""); model.subType = DataSubType.position; models.Add(model); STobj.ObjectName = model.Name; lod = new GenericObject.LOD_Mesh(); lod.IndexFormat = STIndexFormat.UInt16; lod.PrimitiveType = STPolygonType.Triangle; STobj.lodMeshes.Add(lod); STobj.VertexBufferIndex = Index; objects.Add(STobj); Index++; } else if (line.StartsWith("tex_Array:")) { } else if (line.StartsWith("Bone_Suport")) { } else if (line.StartsWith("Color_Suport")) { } else if (line.StartsWith("UV_Num:")) { int uvCount; int.TryParse(line.Split(':')[1].Replace("\n", ""), out uvCount); model.UVChannelCount = uvCount; } else if (line.StartsWith("vert_Array")) { model.type = DataType.vertex; } else if (line.StartsWith("face_Array")) { model.type = DataType.faces; } else if (line.StartsWith("bone_Array")) { model.type = DataType.bones; } else { string[] values = line.Replace("\n", "").Replace("\r", "").Split(','); if (model.type == DataType.vertex) { switch (model.subType) { case DataSubType.position: vtx = new Vertex(); STobj.vertices.Add(vtx); STobj.HasPos = true; float.TryParse(values[0], out X); float.TryParse(values[1], out Y); float.TryParse(values[2], out Z); vtx.pos = new Vector3(X, Y, Z); model.subType = DataSubType.normals; break; case DataSubType.normals: STobj.HasNrm = true; float.TryParse(values[0], out X); float.TryParse(values[1], out Y); float.TryParse(values[2], out Z); vtx.nrm = new Vector3(X, Y, Z); model.subType = DataSubType.colors; break; case DataSubType.colors: STobj.HasVertColors = true; float.TryParse(values[0], out X); float.TryParse(values[1], out Y); float.TryParse(values[2], out Z); float.TryParse(values[3], out W); vtx.col = new Vector4(X / 255, Y / 255, Z / 255, W / 255); model.subType = DataSubType.uv0; break; case DataSubType.uv0: STobj.HasUv0 = true; float.TryParse(values[0], out X); float.TryParse(values[1], out Y); vtx.uv0 = new Vector2(X, Y); if (model.UVChannelCount == 1) { model.subType = DataSubType.position; } else { model.subType = DataSubType.uv1; } break; case DataSubType.uv1: STobj.HasUv1 = true; float.TryParse(values[0], out X); float.TryParse(values[1], out Y); vtx.uv1 = new Vector2(X, Y); if (model.UVChannelCount == 2) { model.subType = DataSubType.position; } else { model.subType = DataSubType.uv2; } break; case DataSubType.uv2: STobj.HasUv2 = true; float.TryParse(values[0], out X); float.TryParse(values[1], out Y); vtx.uv2 = new Vector2(X, Y); if (model.UVChannelCount == 3) { model.subType = DataSubType.position; } else { model.subType = DataSubType.uv3; } break; case DataSubType.uv3: float.TryParse(values[0], out X); float.TryParse(values[1], out Y); model.subType = DataSubType.position; break; } } if (model.type == DataType.faces) { int face; foreach (string v in values) { var cleaned = v.Replace(".0", string.Empty); int.TryParse(cleaned, out face); lod.faces.Add(face - 1); } } if (model.type == DataType.bones) { STobj.HasWeights = true; STobj.HasIndices = true; Array.Resize(ref values, values.Length - 1); List <string> bones = new List <string>(); List <float> weights = new List <float>(); int bbs = 0; foreach (string obj in values) { if (bbs == 0) { bones.Add(obj); bbs += 1; } else { float.TryParse(obj, out X); weights.Add(X); bbs = 0; } } STobj.bones.Add(bones.ToArray()); STobj.weightsT.Add(weights.ToArray()); } } } else { break; } } if (objects[0].weightsT.Count != objects[0].vertices.Count) { throw new Exception("Incorrect vertex amount"); } foreach (GenericObject obj in objects) { obj.lodMeshes[0].GenerateSubMesh(); for (int v = 0; v < obj.vertices.Count; v++) { foreach (string bn in obj.bones[v]) { obj.vertices[v].boneNames.Add(bn); } foreach (float f in obj.weightsT[v]) { obj.vertices[v].boneWeights.Add(f); } } } csv.Close(); csv = null; }