// processes a node in a recursive fashion. Processes each individual mesh located at the node and repeats this process on its children nodes (if any). void processNode(Node node, Scene scene, MObject parent) { MMesh NewNode = null; if (parent == null) { parent = this; } // process each mesh located at the current node for (int i = 0; i < node.MeshCount; i++) { // the node object only contains indices to index the actual objects in the scene. // the scene contains all the data, node is just to keep stuff organized (like relations between nodes). Mesh m = scene.Meshes[node.MeshIndices[i]]; // meshes.Add(processMesh(m, scene)); Matrix4d tr = FromMatrixd(node.Transform); // tr.Transpose(); NewNode = processMesh(m, scene, node.Name, tr); if (parent != null) { NewNode.OwnerID = parent.OwnerID; NewNode.FaceCount = m.FaceCount; parent.Add(NewNode); } } // after we've processed all of the meshes (if any) we then recursively process each of the children nodes for (int i = 0; i < node.ChildCount; i++) { processNode(node.Children[i], scene, NewNode); } }
void SetupTree() { tree = new MModel(EType.Model, "InstanceTree"); tree.DistanceThreshold = 15000; tree.Load(TreeModel); treemesh = (MMesh)tree.FindModuleByType(EType.Mesh); treemesh.transform.Scale = new Vector3d(1, 1, 1); treemesh.Setup(); //MScene.Background.Add(tree); }
void SetupMesh() { tree = new MModel(EType.Model, "BaseModel"); tree.DistanceThreshold = 5000; tree.Load(ModelPath); mesh = (MMesh)tree.FindModuleByType(EType.Mesh); if (mesh == null) { return; } mesh.transform.Scale = new Vector3d(1, 1, 1); mesh.Setup(); }
public static MModel SpawnModel(MObject parent, string TemplateID, string OwnerID, string sName, Vector3d pos) { MModel mo = (MModel)MScene.TemplateRoot.FindModuleByInstanceID(TemplateID); //MMesh sm = (MMesh)mo.FindModuleByType(MObject.EType.Mesh); MModel m = new MModel(MObject.EType.Model, sName); m.OwnerID = OwnerID; m.transform.Position = pos; parent.Add(m); for (int i = 0; i < mo.Modules.Count; i++) { if (mo.Modules[i].Type != MObject.EType.Mesh) { continue; } MMesh mr = (MMesh)mo.Modules[i]; MMesh mesh = new MMesh(sName); mesh.OwnerID = mr.OwnerID; m.Add(mesh); mesh.VBO = mr.VBO; mesh.VAO = mr.VAO; mesh.EBO = mr.EBO; mesh.Indices = mr.Indices; mesh.IndicesLength = mr.IndicesLength; mesh.Vertices = mr.Vertices; mesh.VerticesLength = mr.VerticesLength; mesh.Normals = mr.Normals; //mesh.material = mo.material; m.material = mo.material; } return(m); }
MMesh processMesh(Mesh mesh, Scene scene, string sName, Matrix4d trans) { // data to fill List <TexturedVertex> vertices = new List <TexturedVertex>(); List <int> indices = new List <int>(); List <MTexture> textures = new List <MTexture>(); // Walk through each of the mesh's vertices for (int i = 0; i < mesh.VertexCount; i++) { TexturedVertex vertex = new TexturedVertex(); Vector3 vector = new Vector3(); // we declare a placeholder vector since assimp uses its own vector class that doesn't directly convert to glm's vec3 class so we transfer the data to this placeholder glm::vec3 first. // positions vector.X = mesh.Vertices[i].X; vector.Y = mesh.Vertices[i].Y; vector.Z = mesh.Vertices[i].Z; vertex._position = vector; // normals if (mesh.HasNormals) { vector.X = mesh.Normals[i].X; vector.Y = mesh.Normals[i].Y; vector.Z = mesh.Normals[i].Z; vertex._normal = vector; } // texture coordinates if (mesh.HasTextureCoords(0)) // does the mesh contain texture coordinates? { Vector2 vec = new Vector2(); // a vertex can contain up to 8 different texture coordinates. We thus make the assumption that we won't // use models where a vertex can have multiple texture coordinates so we always take the first set (0). vec.X = mesh.TextureCoordinateChannels[0][i].X; vec.Y = 1 - mesh.TextureCoordinateChannels[0][i].Y; vertex._textureCoordinate = vec; } else { vertex._textureCoordinate = new Vector2(0.0f, 0.0f); } if (mesh.HasTangentBasis) { // tangent vector.X = mesh.Tangents[i].X; vector.Y = mesh.Tangents[i].Y; vector.Z = mesh.Tangents[i].Z; /// vertex.Tangent = vector; // bitangent vector.X = mesh.BiTangents[i].X; vector.Y = mesh.BiTangents[i].Y; vector.Z = mesh.BiTangents[i].Z; /// vertex.BiTangent = vector; } vertices.Add(vertex); } // now wak through each of the mesh's faces (a face is a mesh its triangle) and retrieve the corresponding vertex indices. for (int i = 0; i < mesh.FaceCount; i++) { Face face = mesh.Faces[i]; // retrieve all indices of the face and store them in the indices vector for (int j = 0; j < face.IndexCount; j++) { indices.Add(face.Indices[j]); } } // process materials Material material = scene.Materials[mesh.MaterialIndex]; // we assume a convention for sampler names in the shaders. Each diffuse texture should be named // as 'texture_diffuseN' where N is a sequential number ranging from 1 to MAX_SAMPLER_NUMBER. // Same applies to other texture as the following list summarizes: // diffuse: texture_diffuseN // specular: texture_specularN // normal: texture_normalN // 1. diffuse maps /* * List<MTexture> diffuseMaps = loadMaterialTextures(material, TextureType_DIFFUSE, "texture_diffuse"); * textures.insert(textures.end(), diffuseMaps.begin(), diffuseMaps.end()); * // 2. specular maps * List<Texture> specularMaps = loadMaterialTextures(material, aiTextureType_SPECULAR, "texture_specular"); * textures.insert(textures.end(), specularMaps.begin(), specularMaps.end()); * // 3. normal maps * List<Texture> normalMaps = loadMaterialTextures(material, aiTextureType_HEIGHT, "texture_normal"); * textures.insert(textures.end(), normalMaps.begin(), normalMaps.end()); * // 4. height maps * List<Texture> heightMaps = loadMaterialTextures(material, aiTextureType_AMBIENT, "texture_height"); * textures.insert(textures.end(), heightMaps.begin(), heightMaps.end()); */ // return a mesh object created from the extracted mesh data MMesh m = new MMesh(sName); // trans.Transpose(); m.transform.Position = trans.ExtractTranslation(); m.transform.Rotation = trans.ExtractRotation(); m.transform.Scale = trans.ExtractScale(); m.AddMaterial(this.material); m.SetupMesh(vertices, indices, textures); return(m); }