Skip to content

Commit 567128b

Browse files
authored
Merge pull request #76671 from dioptryk/obj-vertex-colors
[3.x] Add vertex color support to OBJ importer
2 parents 716fef7 + ab8792c commit 567128b

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

editor/import/resource_importer_obj.cpp

+22-1
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ static Error _parse_obj(const String &p_path, List<Ref<Mesh>> &r_meshes, bool p_
214214
Vector<Vector3> vertices;
215215
Vector<Vector3> normals;
216216
Vector<Vector2> uvs;
217+
Vector<Color> colors;
217218
String name;
218219

219220
Map<String, Map<String, Ref<SpatialMaterial>>> material_map;
@@ -244,6 +245,19 @@ static Error _parse_obj(const String &p_path, List<Ref<Mesh>> &r_meshes, bool p_
244245
vtx.y = v[2].to_float() * scale_mesh.y + offset_mesh.y;
245246
vtx.z = v[3].to_float() * scale_mesh.z + offset_mesh.z;
246247
vertices.push_back(vtx);
248+
//vertex colors
249+
if (v.size() >= 7) {
250+
while (colors.size() < vertices.size() - 1) {
251+
colors.push_back(Color(1.0, 1.0, 1.0));
252+
}
253+
Color c;
254+
c.r = v[4].to_float();
255+
c.g = v[5].to_float();
256+
c.b = v[6].to_float();
257+
colors.push_back(c);
258+
} else if (!colors.empty()) {
259+
colors.push_back(Color(1.0, 1.0, 1.0));
260+
}
247261
} else if (l.begins_with("vt ")) {
248262
//uv
249263
Vector<String> v = l.split(" ", false);
@@ -312,6 +326,9 @@ static Error _parse_obj(const String &p_path, List<Ref<Mesh>> &r_meshes, bool p_
312326
ERR_FAIL_INDEX_V(vtx, vertices.size(), ERR_FILE_CORRUPT);
313327

314328
Vector3 vertex = vertices[vtx];
329+
if (!colors.empty()) {
330+
surf_tool->add_color(colors[vtx]);
331+
}
315332
//if (weld_vertices)
316333
// vertex.snap(Vector3(weld_tolerance, weld_tolerance, weld_tolerance));
317334
surf_tool->add_vertex(vertex);
@@ -344,7 +361,11 @@ static Error _parse_obj(const String &p_path, List<Ref<Mesh>> &r_meshes, bool p_
344361
print_verbose("OBJ: Current material " + current_material + " has " + itos(material_map.has(current_material_library) && material_map[current_material_library].has(current_material)));
345362

346363
if (material_map.has(current_material_library) && material_map[current_material_library].has(current_material)) {
347-
surf_tool->set_material(material_map[current_material_library][current_material]);
364+
Ref<SpatialMaterial> &material = material_map[current_material_library][current_material];
365+
if (!colors.empty()) {
366+
material->set_flag(SpatialMaterial::FLAG_SRGB_VERTEX_COLOR, true);
367+
}
368+
surf_tool->set_material(material);
348369
}
349370

350371
mesh = surf_tool->commit(mesh, p_compress_flags);

0 commit comments

Comments
 (0)