@@ -214,6 +214,7 @@ static Error _parse_obj(const String &p_path, List<Ref<Mesh>> &r_meshes, bool p_
214
214
Vector<Vector3> vertices;
215
215
Vector<Vector3> normals;
216
216
Vector<Vector2> uvs;
217
+ Vector<Color> colors;
217
218
String name;
218
219
219
220
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_
244
245
vtx.y = v[2 ].to_float () * scale_mesh.y + offset_mesh.y ;
245
246
vtx.z = v[3 ].to_float () * scale_mesh.z + offset_mesh.z ;
246
247
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
+ }
247
261
} else if (l.begins_with (" vt " )) {
248
262
// uv
249
263
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_
312
326
ERR_FAIL_INDEX_V (vtx, vertices.size (), ERR_FILE_CORRUPT);
313
327
314
328
Vector3 vertex = vertices[vtx];
329
+ if (!colors.empty ()) {
330
+ surf_tool->add_color (colors[vtx]);
331
+ }
315
332
// if (weld_vertices)
316
333
// vertex.snap(Vector3(weld_tolerance, weld_tolerance, weld_tolerance));
317
334
surf_tool->add_vertex (vertex);
@@ -344,7 +361,11 @@ static Error _parse_obj(const String &p_path, List<Ref<Mesh>> &r_meshes, bool p_
344
361
print_verbose (" OBJ: Current material " + current_material + " has " + itos (material_map.has (current_material_library) && material_map[current_material_library].has (current_material)));
345
362
346
363
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);
348
369
}
349
370
350
371
mesh = surf_tool->commit (mesh, p_compress_flags);
0 commit comments