public List<Vector3> GetVertices(Model3D input) { List<Vector3> vertices = new List<Vector3>(input.Meshes.Count * 100); foreach (Model3DMesh mesh in input.Meshes) { for (int i = 0; i < mesh.Vertices.Count; i++) { vertices.Add(mesh.Vertices[i]); } } return vertices; }
public List<Vector3> GetCollisionVertices(Model3D input) { List<Vector3> vertices = new List<Vector3>(input.Meshes.Count * 100); bool colOnly = false; foreach (Model3DMesh mesh in input.Meshes) { if (mesh.Name.ToLowerFast().Contains("collision")) { colOnly = true; break; } } foreach (Model3DMesh mesh in input.Meshes) { if ((!colOnly || mesh.Name.ToLowerFast().Contains("collision")) && !mesh.Name.ToLowerFast().Contains("nocollide")) { for (int i = 0; i < mesh.Indices.Count; i ++) { vertices.Add(mesh.Vertices[mesh.Indices[i]]); } } } return vertices; }
/// <summary> /// Loads a model from .VMD (Voxalia Model Data) input. /// </summary> public Model3D LoadModel(byte[] data) { if (data.Length < 3 || data[0] != 'V' || data[1] != 'M' || data[2] != 'D') { throw new Exception("Model3D: Invalid header bits."); } byte[] dat_filt = new byte[data.Length - "VMD001".Length]; Array.ConstrainedCopy(data, "VMD001".Length, dat_filt, 0, dat_filt.Length); dat_filt = FileHandler.UnGZip(dat_filt); DataStream ds = new DataStream(dat_filt); DataReader dr = new DataReader(ds); Model3D mod = new Model3D(); mod.MatrixA = ReadMat(dr); int meshCount = dr.ReadInt(); mod.Meshes = new List<Model3DMesh>(meshCount); for (int m = 0; m < meshCount; m++) { Model3DMesh mesh = new Model3DMesh(); mod.Meshes.Add(mesh); mesh.Name = dr.ReadFullString(); int vertexCount = dr.ReadInt(); mesh.Vertices = new List<Vector3>(vertexCount); for (int v = 0; v < vertexCount; v++) { double f1 = dr.ReadFloat(); double f2 = dr.ReadFloat(); double f3 = dr.ReadFloat(); mesh.Vertices.Add(new Vector3(f1, f2, f3)); } int indiceCount = dr.ReadInt() * 3; mesh.Indices = new List<int>(indiceCount); for (int i = 0; i < indiceCount; i++) { mesh.Indices.Add(dr.ReadInt()); } int tcCount = dr.ReadInt(); mesh.TexCoords = new List<Vector2>(tcCount); for (int t = 0; t < tcCount; t++) { double f1 = dr.ReadFloat(); double f2 = dr.ReadFloat(); mesh.TexCoords.Add(new Vector2(f1, f2)); } int normCount = dr.ReadInt(); mesh.Normals = new List<Vector3>(normCount); for (int n = 0; n < normCount; n++) { double f1 = dr.ReadFloat(); double f2 = dr.ReadFloat(); double f3 = dr.ReadFloat(); mesh.Normals.Add(new Vector3(f1, f2, f3)); } int boneCount = dr.ReadInt(); mesh.Bones = new List<Model3DBone>(boneCount); for (int b = 0; b < boneCount; b++) { Model3DBone bone = new Model3DBone(); mesh.Bones.Add(bone); bone.Name = dr.ReadFullString(); int weights = dr.ReadInt(); bone.IDs = new List<int>(weights); bone.Weights = new List<double>(weights); for (int w = 0; w < weights; w++) { bone.IDs.Add(dr.ReadInt()); bone.Weights.Add(dr.ReadFloat()); } bone.MatrixA = ReadMat(dr); } } mod.RootNode = ReadSingleNode(null, dr); return mod; }
public ConvexHullShape MeshToBepuConvex(Model3D input, out int verts) { List<Vector3> vertices = GetCollisionVertices(input); ConvexHullHelper.RemoveRedundantPoints(vertices); verts = vertices.Count; return new ConvexHullShape(vertices); }
public MobileMeshShape MeshToBepu(Model3D input, out int verts) { List<Vector3> vertices = GetCollisionVertices(input); List<int> indices = new List<int>(vertices.Count); for (int i = 0; i < vertices.Count; i++) { indices.Add(indices.Count); } verts = vertices.Count; return new MobileMeshShape(vertices.ToArray(), indices.ToArray(), AffineTransform.Identity, MobileMeshSolidity.DoubleSided); }
public Model FromScene(Model3D scene, string name, AnimationEngine engine) { if (scene.Meshes.Count == 0) { throw new Exception("Scene has no meshes! (" + name + ")"); } Model model = new Model(name); model.Engine = this; model.Original = scene; model.Root = convert(scene.MatrixA); foreach (Model3DMesh mesh in scene.Meshes) { if (mesh.Name.ToLowerFast().Contains("collision") || mesh.Name.ToLowerFast().Contains("norender")) { continue; } ModelMesh modmesh = new ModelMesh(mesh.Name); modmesh.vbo.Prepare(); bool hastc = mesh.TexCoords.Count == mesh.Vertices.Count; bool hasn = mesh.Normals.Count == mesh.Vertices.Count; if (!hasn) { SysConsole.Output(OutputType.WARNING, "Mesh has no normals! (" + name + ")"); } if (!hastc) { SysConsole.Output(OutputType.WARNING, "Mesh has no texcoords! (" + name + ")"); } for (int i = 0; i < mesh.Vertices.Count; i++) { BEPUutilities.Vector3 vertex = mesh.Vertices[i]; modmesh.vbo.Vertices.Add(new Vector3((float)vertex.X, (float)vertex.Y, (float)vertex.Z)); if (!hastc) { modmesh.vbo.TexCoords.Add(new Vector3(0, 0, 0)); } else { BEPUutilities.Vector2 texCoord = mesh.TexCoords[i]; modmesh.vbo.TexCoords.Add(new Vector3((float)texCoord.X, 1 - (float)texCoord.Y, 0)); } if (!hasn) { modmesh.vbo.Normals.Add(new Vector3(0, 0, 1)); } else { modmesh.vbo.Normals.Add(new Vector3((float)mesh.Normals[i].X, (float)mesh.Normals[i].Y, (float)mesh.Normals[i].Z)); } modmesh.vbo.Colors.Add(new Vector4(1, 1, 1, 1)); // TODO: From the mesh? } for (int i = 0; i < mesh.Indices.Count; i++) { modmesh.vbo.Indices.Add((uint)mesh.Indices[i]); } int bc = mesh.Bones.Count; if (bc > 200) { SysConsole.Output(OutputType.WARNING, "Mesh has " + bc + " bones! (" + name + ")"); bc = 200; } modmesh.vbo.BoneIDs = new Vector4[modmesh.vbo.Vertices.Count].ToList(); modmesh.vbo.BoneWeights = new Vector4[modmesh.vbo.Vertices.Count].ToList(); modmesh.vbo.BoneIDs2 = new Vector4[modmesh.vbo.Vertices.Count].ToList(); modmesh.vbo.BoneWeights2 = new Vector4[modmesh.vbo.Vertices.Count].ToList(); int[] pos = new int[modmesh.vbo.Vertices.Count]; for (int i = 0; i < bc; i++) { for (int x = 0; x < mesh.Bones[i].Weights.Count; x++) { int IDa = mesh.Bones[i].IDs[x]; float Weighta = (float)mesh.Bones[i].Weights[x]; int spot = pos[IDa]++; if (spot > 7) { //SysConsole.Output(OutputType.WARNING, "Too many bones influencing " + vw.VertexID + "!"); ForceSet(modmesh.vbo.BoneWeights, IDa, 3, modmesh.vbo.BoneWeights[IDa][3] + Weighta); } else if (spot > 3) { ForceSet(modmesh.vbo.BoneIDs2, IDa, spot - 4, i); ForceSet(modmesh.vbo.BoneWeights2, IDa, spot - 4, Weighta); } else { ForceSet(modmesh.vbo.BoneIDs, IDa, spot, i); ForceSet(modmesh.vbo.BoneWeights, IDa, spot, Weighta); } } } model.Meshes.Add(modmesh); } model.RootNode = new ModelNode() { Parent = null, Name = scene.RootNode.Name.ToLowerFast() }; List<ModelNode> allNodes = new List<ModelNode>(); PopulateChildren(model.RootNode, scene.RootNode, model, engine, allNodes); for (int i = 0; i < model.Meshes.Count; i++) { for (int x = 0; x < scene.Meshes[i].Bones.Count; x++) { ModelNode nodet = null; string nl = scene.Meshes[i].Bones[x].Name.ToLowerFast(); for (int n = 0; n < allNodes.Count; n++) { if (allNodes[n].Name == nl) { nodet = allNodes[n]; break; } } ModelBone mb = new ModelBone() { Offset = convert(scene.Meshes[i].Bones[x].MatrixA) }; nodet.Bones.Add(mb); model.Meshes[i].Bones.Add(mb); } } return model; }
/// <summary> /// Loads a model from .VMD (Voxalia Model Data) input. /// </summary> public Model3D LoadModel(byte[] data) { if (data.Length < 3 || data[0] != 'V' || data[1] != 'M' || data[2] != 'D') { throw new Exception("Model3D: Invalid header bits."); } byte[] dat_filt = new byte[data.Length - "VMD001".Length]; Array.ConstrainedCopy(data, "VMD001".Length, dat_filt, 0, dat_filt.Length); dat_filt = FileHandler.UnGZip(dat_filt); DataStream ds = new DataStream(dat_filt); DataReader dr = new DataReader(ds); Model3D mod = new Model3D(); Matrix matA = ReadMat(dr); mod.MatrixA = matA; int meshCount = dr.ReadInt(); mod.Meshes = new List <Model3DMesh>(meshCount); for (int m = 0; m < meshCount; m++) { Model3DMesh mesh = new Model3DMesh(); mod.Meshes.Add(mesh); mesh.Name = dr.ReadFullString(); int vertexCount = dr.ReadInt(); mesh.Vertices = new List <Vector3>(vertexCount); for (int v = 0; v < vertexCount; v++) { double f1 = dr.ReadFloat(); double f2 = dr.ReadFloat(); double f3 = dr.ReadFloat(); mesh.Vertices.Add(new Vector3(f1, f2, f3)); } int indiceCount = dr.ReadInt() * 3; mesh.Indices = new List <int>(indiceCount); for (int i = 0; i < indiceCount; i++) { mesh.Indices.Add(dr.ReadInt()); } int tcCount = dr.ReadInt(); mesh.TexCoords = new List <Vector2>(tcCount); for (int t = 0; t < tcCount; t++) { double f1 = dr.ReadFloat(); double f2 = dr.ReadFloat(); mesh.TexCoords.Add(new Vector2(f1, f2)); } int normCount = dr.ReadInt(); mesh.Normals = new List <Vector3>(normCount); for (int n = 0; n < normCount; n++) { double f1 = dr.ReadFloat(); double f2 = dr.ReadFloat(); double f3 = dr.ReadFloat(); mesh.Normals.Add(new Vector3(f1, f2, f3)); } int boneCount = dr.ReadInt(); mesh.Bones = new List <Model3DBone>(boneCount); for (int b = 0; b < boneCount; b++) { Model3DBone bone = new Model3DBone(); mesh.Bones.Add(bone); bone.Name = dr.ReadFullString(); int weights = dr.ReadInt(); bone.IDs = new List <int>(weights); bone.Weights = new List <double>(weights); for (int w = 0; w < weights; w++) { bone.IDs.Add(dr.ReadInt()); bone.Weights.Add(dr.ReadFloat()); } bone.MatrixA = ReadMat(dr); } } mod.RootNode = ReadSingleNode(null, dr); return(mod); }