diff options
author | RĂ©mi Verschelde <remi@verschelde.fr> | 2023-01-13 00:37:14 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-01-13 00:37:14 +0100 |
commit | 14cca21256f77e4eda921211d5a75b803a642b72 (patch) | |
tree | f08d7a9be3d8c2ab2499109a6b4b612f979f9f70 /editor | |
parent | f79c58aac5547d3ad6bcced7b499c4015130f579 (diff) | |
parent | 649857416dc1080d1b049da22d11a9bc20f84fc4 (diff) |
Merge pull request #71033 from scurest/obj-vertex-color
Add vertex color support to OBJ importer
Diffstat (limited to 'editor')
-rw-r--r-- | editor/import/resource_importer_obj.cpp | 23 |
1 files changed, 22 insertions, 1 deletions
diff --git a/editor/import/resource_importer_obj.cpp b/editor/import/resource_importer_obj.cpp index a67da0c2b1..f1fd1d5a03 100644 --- a/editor/import/resource_importer_obj.cpp +++ b/editor/import/resource_importer_obj.cpp @@ -217,6 +217,7 @@ static Error _parse_obj(const String &p_path, List<Ref<Mesh>> &r_meshes, bool p_ Vector<Vector3> vertices; Vector<Vector3> normals; Vector<Vector2> uvs; + Vector<Color> colors; String name; HashMap<String, HashMap<String, Ref<StandardMaterial3D>>> material_map; @@ -249,6 +250,19 @@ static Error _parse_obj(const String &p_path, List<Ref<Mesh>> &r_meshes, bool p_ vtx.y = v[2].to_float() * scale_mesh.y + offset_mesh.y; vtx.z = v[3].to_float() * scale_mesh.z + offset_mesh.z; vertices.push_back(vtx); + //vertex color + if (v.size() == 7) { + while (colors.size() < vertices.size() - 1) { + colors.push_back(Color(1.0, 1.0, 1.0)); + } + Color c; + c.r = v[4].to_float(); + c.g = v[5].to_float(); + c.b = v[6].to_float(); + colors.push_back(c); + } else if (!colors.is_empty()) { + colors.push_back(Color(1.0, 1.0, 1.0)); + } } else if (l.begins_with("vt ")) { //uv Vector<String> v = l.split(" ", false); @@ -317,6 +331,9 @@ static Error _parse_obj(const String &p_path, List<Ref<Mesh>> &r_meshes, bool p_ ERR_FAIL_INDEX_V(vtx, vertices.size(), ERR_FILE_CORRUPT); Vector3 vertex = vertices[vtx]; + if (!colors.is_empty()) { + surf_tool->set_color(colors[vtx]); + } if (!smoothing) { smooth_group++; } @@ -356,7 +373,11 @@ static Error _parse_obj(const String &p_path, List<Ref<Mesh>> &r_meshes, bool p_ print_verbose("OBJ: 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]); + Ref<StandardMaterial3D> &material = material_map[current_material_library][current_material]; + if (!colors.is_empty()) { + material->set_flag(StandardMaterial3D::FLAG_SRGB_VERTEX_COLOR, true); + } + surf_tool->set_material(material); } mesh = surf_tool->commit(mesh, mesh_flags); |