Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3.x] Add vertex color support to OBJ importer #76671

Merged
merged 1 commit into from
May 15, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion editor/import/resource_importer_obj.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,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;

Map<String, Map<String, Ref<SpatialMaterial>>> material_map;
Expand Down Expand Up @@ -244,6 +245,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 colors
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.empty()) {
colors.push_back(Color(1.0, 1.0, 1.0));
}
} else if (l.begins_with("vt ")) {
//uv
Vector<String> v = l.split(" ", false);
Expand Down Expand Up @@ -312,6 +326,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.empty()) {
surf_tool->add_color(colors[vtx]);
}
//if (weld_vertices)
// vertex.snap(Vector3(weld_tolerance, weld_tolerance, weld_tolerance));
surf_tool->add_vertex(vertex);
Expand Down Expand Up @@ -344,7 +361,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<SpatialMaterial> &material = material_map[current_material_library][current_material];
if (!colors.empty()) {
material->set_flag(SpatialMaterial::FLAG_SRGB_VERTEX_COLOR, true);
}
surf_tool->set_material(material);
}

mesh = surf_tool->commit(mesh, p_compress_flags);
Expand Down
Loading