Node *EditorOBJImporter::import_scene(const String &p_path, uint32_t p_flags, int p_bake_fps, List<String> *r_missing_deps, Error *r_err) { List<Ref<Mesh> > meshes; Error err = _parse_obj(p_path, meshes, false, p_flags & IMPORT_GENERATE_TANGENT_ARRAYS, Vector3(1, 1, 1), r_missing_deps); if (err != OK) { if (r_err) { *r_err = err; } return NULL; } Spatial *scene = memnew(Spatial); for (List<Ref<Mesh> >::Element *E = meshes.front(); E; E = E->next()) { MeshInstance *mi = memnew(MeshInstance); mi->set_mesh(E->get()); mi->set_name(E->get()->get_name()); scene->add_child(mi); mi->set_owner(scene); } if (r_err) { *r_err = OK; } return scene; }
void MeshInstance::create_debug_tangents() { Vector<Vector3> lines; Vector<Color> colors; Ref<Mesh> mesh = get_mesh(); if (!mesh.is_valid()) return; for (int i = 0; i < mesh->get_surface_count(); i++) { Array arrays = mesh->surface_get_arrays(i); Vector<Vector3> verts = arrays[Mesh::ARRAY_VERTEX]; Vector<Vector3> norms = arrays[Mesh::ARRAY_NORMAL]; if (norms.size() == 0) continue; Vector<float> tangents = arrays[Mesh::ARRAY_TANGENT]; if (tangents.size() == 0) continue; for (int j = 0; j < verts.size(); j++) { Vector3 v = verts[j]; Vector3 n = norms[j]; Vector3 t = Vector3(tangents[j * 4 + 0], tangents[j * 4 + 1], tangents[j * 4 + 2]); Vector3 b = (n.cross(t)).normalized() * tangents[j * 4 + 3]; lines.push_back(v); //normal colors.push_back(Color(0, 0, 1)); //color lines.push_back(v + n * 0.04); //normal colors.push_back(Color(0, 0, 1)); //color lines.push_back(v); //tangent colors.push_back(Color(1, 0, 0)); //color lines.push_back(v + t * 0.04); //tangent colors.push_back(Color(1, 0, 0)); //color lines.push_back(v); //binormal colors.push_back(Color(0, 1, 0)); //color lines.push_back(v + b * 0.04); //binormal colors.push_back(Color(0, 1, 0)); //color } } if (lines.size()) { Ref<SpatialMaterial> sm; sm.instance(); sm->set_flag(SpatialMaterial::FLAG_UNSHADED, true); sm->set_flag(SpatialMaterial::FLAG_SRGB_VERTEX_COLOR, true); sm->set_flag(SpatialMaterial::FLAG_ALBEDO_FROM_VERTEX_COLOR, true); Ref<ArrayMesh> am; am.instance(); Array a; a.resize(Mesh::ARRAY_MAX); a[Mesh::ARRAY_VERTEX] = lines; a[Mesh::ARRAY_COLOR] = colors; am->add_surface_from_arrays(Mesh::PRIMITIVE_LINES, a); am->surface_set_material(0, sm); MeshInstance *mi = memnew(MeshInstance); mi->set_mesh(am); mi->set_name("DebugTangents"); add_child(mi); #ifdef TOOLS_ENABLED if (this == get_tree()->get_edited_scene_root()) mi->set_owner(this); else mi->set_owner(get_owner()); #endif } }
Node *EditorOBJImporter::import_scene(const String &p_path, uint32_t p_flags, int p_bake_fps, List<String> *r_missing_deps, Error *r_err) { FileAccessRef f = FileAccess::open(p_path, FileAccess::READ); if (r_err) { *r_err = ERR_CANT_OPEN; } ERR_FAIL_COND_V(!f, NULL); if (r_err) { *r_err = OK; } Spatial *scene = memnew(Spatial); Ref<ArrayMesh> mesh; mesh.instance(); Map<String, Ref<Material> > name_map; bool generate_tangents = p_flags & IMPORT_GENERATE_TANGENT_ARRAYS; bool flip_faces = false; //bool flip_faces = p_options["force/flip_faces"]; //bool force_smooth = p_options["force/smooth_shading"]; //bool weld_vertices = p_options["force/weld_vertices"]; //float weld_tolerance = p_options["force/weld_tolerance"]; Vector<Vector3> vertices; Vector<Vector3> normals; Vector<Vector2> uvs; String name; Map<String, Map<String, Ref<SpatialMaterial> > > material_map; Ref<SurfaceTool> surf_tool = memnew(SurfaceTool); surf_tool->begin(Mesh::PRIMITIVE_TRIANGLES); String current_material_library; String current_material; String current_group; while (true) { String l = f->get_line().strip_edges(); if (l.begins_with("v ")) { //vertex Vector<String> v = l.split(" ", false); ERR_FAIL_COND_V(v.size() < 4, NULL); Vector3 vtx; vtx.x = v[1].to_float(); vtx.y = v[2].to_float(); vtx.z = v[3].to_float(); vertices.push_back(vtx); } else if (l.begins_with("vt ")) { //uv Vector<String> v = l.split(" ", false); ERR_FAIL_COND_V(v.size() < 3, NULL); Vector2 uv; uv.x = v[1].to_float(); uv.y = 1.0 - v[2].to_float(); uvs.push_back(uv); } else if (l.begins_with("vn ")) { //normal Vector<String> v = l.split(" ", false); ERR_FAIL_COND_V(v.size() < 4, NULL); Vector3 nrm; nrm.x = v[1].to_float(); nrm.y = v[2].to_float(); nrm.z = v[3].to_float(); normals.push_back(nrm); } else if (l.begins_with("f ")) { //vertex Vector<String> v = l.split(" ", false); ERR_FAIL_COND_V(v.size() < 4, NULL); //not very fast, could be sped up Vector<String> face[3]; face[0] = v[1].split("/"); face[1] = v[2].split("/"); ERR_FAIL_COND_V(face[0].size() == 0, NULL); ERR_FAIL_COND_V(face[0].size() != face[1].size(), NULL); for (int i = 2; i < v.size() - 1; i++) { face[2] = v[i + 1].split("/"); ERR_FAIL_COND_V(face[0].size() != face[2].size(), NULL); for (int j = 0; j < 3; j++) { int idx = j; if (!flip_faces && idx < 2) { idx = 1 ^ idx; } if (face[idx].size() == 3) { int norm = face[idx][2].to_int() - 1; if (norm < 0) norm += normals.size() + 1; ERR_FAIL_INDEX_V(norm, normals.size(), NULL); surf_tool->add_normal(normals[norm]); } if (face[idx].size() >= 2 && face[idx][1] != String()) { int uv = face[idx][1].to_int() - 1; if (uv < 0) uv += uvs.size() + 1; ERR_FAIL_INDEX_V(uv, uvs.size(), NULL); surf_tool->add_uv(uvs[uv]); } int vtx = face[idx][0].to_int() - 1; if (vtx < 0) vtx += vertices.size() + 1; ERR_FAIL_INDEX_V(vtx, vertices.size(), NULL); Vector3 vertex = vertices[vtx]; //if (weld_vertices) // vertex.snap(Vector3(weld_tolerance, weld_tolerance, weld_tolerance)); surf_tool->add_vertex(vertex); } face[1] = face[2]; } } else if (l.begins_with("s ")) { //smoothing String what = l.substr(2, l.length()).strip_edges(); if (what == "off") surf_tool->add_smooth_group(false); else surf_tool->add_smooth_group(true); } else if (/*l.begins_with("g ") ||*/ l.begins_with("usemtl ") || (l.begins_with("o ") || f->eof_reached())) { //commit group to mesh //groups are too annoying if (surf_tool->get_vertex_array().size()) { //another group going on, commit it if (normals.size() == 0) { surf_tool->generate_normals(); } if (generate_tangents && uvs.size()) { surf_tool->generate_tangents(); } surf_tool->index(); print_line("current material library " + current_material_library + " has " + itos(material_map.has(current_material_library))); print_line("current material " + current_material + " has " + itos(material_map.has(current_material_library) && material_map[current_material_library].has(current_material))); if (material_map.has(current_material_library) && material_map[current_material_library].has(current_material)) { surf_tool->set_material(material_map[current_material_library][current_material]); } mesh = surf_tool->commit(mesh); if (current_material != String()) { mesh->surface_set_name(mesh->get_surface_count() - 1, current_material.get_basename()); } else if (current_group != String()) { mesh->surface_set_name(mesh->get_surface_count() - 1, current_group); } print_line("Added surface :" + mesh->surface_get_name(mesh->get_surface_count() - 1)); surf_tool->clear(); surf_tool->begin(Mesh::PRIMITIVE_TRIANGLES); } if (l.begins_with("o ") || f->eof_reached()) { MeshInstance *mi = memnew(MeshInstance); mi->set_name(name); mi->set_mesh(mesh); scene->add_child(mi); mi->set_owner(scene); mesh.instance(); current_group = ""; current_material = ""; } if (f->eof_reached()) { break; } if (l.begins_with("o ")) { name = l.substr(2, l.length()).strip_edges(); } if (l.begins_with("usemtl ")) { current_material = l.replace("usemtl", "").strip_edges(); } if (l.begins_with("g ")) { current_group = l.substr(2, l.length()).strip_edges(); } } else if (l.begins_with("mtllib ")) { //parse material current_material_library = l.replace("mtllib", "").strip_edges(); if (!material_map.has(current_material_library)) { Map<String, Ref<SpatialMaterial> > lib; Error err = _parse_material_library(current_material_library, lib, r_missing_deps); if (err == ERR_CANT_OPEN) { String dir = p_path.get_base_dir(); err = _parse_material_library(dir.plus_file(current_material_library), lib, r_missing_deps); } if (err == OK) { material_map[current_material_library] = lib; } } } } /* TODO, check existing materials and merge? //re-apply materials if exist for(int i=0;i<mesh->get_surface_count();i++) { String n = mesh->surface_get_name(i); if (name_map.has(n)) mesh->surface_set_material(i,name_map[n]); } */ return scene; }