/// For each node calculate the bounding box. /// This is used to align the viewport nicely when the scene is imported. private void MakeBoundingBoxes(IList <Mesh> scene_meshes, Node node) { foreach (int index in node.MeshIndices) { Mesh mesh = scene_meshes[index]; _mesh_id2box[index] = new MeshBounds(); } for (int i = 0; i < node.ChildCount; i++) { MakeBoundingBoxes(scene_meshes, node.Children[i]); } }
private MeshTreeNode MakeMeshTree(Entity ent, Node nd) { var current = new MeshTreeNode(nd.Name); var child_boxes = new List <MeshBounds>(); if (nd.MeshCount > 1) { foreach (int mesh_id in nd.MeshIndices) { MeshBounds aabb = ent._extra_geometry._mesh_id2box[mesh_id]; string mesh_name = _world._cur_scene._inner.Meshes[mesh_id].Name; var mesh_view_nd = new MeshTreeNode(mesh_name); var list = new List <MeshBounds>() { aabb }; mesh_view_nd.DrawData = new BoundingBoxGroup(list); child_boxes.Add(aabb); current.Nodes.Add(mesh_view_nd); } // get a bounding box that covers all of the meshes assigned to this node current.DrawData = new BoundingBoxGroup(child_boxes); } else { // Place the bounding box of mesh as self bounding box MeshBounds aabb = ent._extra_geometry._mesh_id2box[nd.MeshIndices[0]]; var list = new List <MeshBounds>() { aabb }; current.DrawData = new BoundingBoxGroup(list); } foreach (var child_nd in nd.Children) { var treeview_child = MakeMeshTree(ent, child_nd); current.Nodes.Add(treeview_child); } return(current); }
// Second pass: transform all vertices in a mesh according to bone // just apply the previously caluclated matrix public void RecursiveTransformVertices(Node nd) { foreach (int mesh_id in nd.MeshIndices) { MeshDraw mesh_draw = _mesh_id2mesh_draw[mesh_id]; // map data from VBO IntPtr data; int qty_vertices; mesh_draw.BeginModifyVertexData(out data, out qty_vertices); // iterate over inital vertex positions Mesh cur_mesh = _scene._inner.Meshes[mesh_id]; MeshBounds aabb = _extra_geometry._mesh_id2box[mesh_id]; // go over every vertex in the mesh unsafe { // array of floats: X,Y,Z..... int sz = 3; // size of step float *coords = (float *)data; for (int vertex_id = 0; vertex_id < qty_vertices; vertex_id++) { Matrix4x4 matrix_with_offset = mesh_draw._vertex_id2matrix[vertex_id]; // get the initial position of vertex when scene was loaded Vector3D vertex_default = cur_mesh.Vertices[vertex_id]; Vector3D vertex; Entity.TransformPositionAssimp(ref vertex_default, ref matrix_with_offset, out vertex); // write new coords back into array coords[vertex_id * sz + 0] = vertex.X; coords[vertex_id * sz + 1] = vertex.Y; coords[vertex_id * sz + 2] = vertex.Z; } } mesh_draw.EndModifyVertexData(); foreach (Node child in nd.Children) { RecursiveTransformVertices(child); } } }