Browse Source

Merge pull request #72437 from lyuma/lightmap_custom_uv

Implement custom uvs for Static Lightmap imported gltf
Rémi Verschelde 2 years ago
parent
commit
5a5e262f5b
1 changed files with 17 additions and 5 deletions
  1. 17 5
      scene/resources/importer_mesh.cpp

+ 17 - 5
scene/resources/importer_mesh.cpp

@@ -1058,6 +1058,8 @@ struct EditorSceneFormatImporterMeshLightmapSurface {
 	String name;
 	String name;
 };
 };
 
 
+static const uint32_t custom_shift[RS::ARRAY_CUSTOM_COUNT] = { Mesh::ARRAY_FORMAT_CUSTOM0_SHIFT, Mesh::ARRAY_FORMAT_CUSTOM1_SHIFT, Mesh::ARRAY_FORMAT_CUSTOM2_SHIFT, Mesh::ARRAY_FORMAT_CUSTOM3_SHIFT };
+
 Error ImporterMesh::lightmap_unwrap_cached(const Transform3D &p_base_transform, float p_texel_size, const Vector<uint8_t> &p_src_cache, Vector<uint8_t> &r_dst_cache) {
 Error ImporterMesh::lightmap_unwrap_cached(const Transform3D &p_base_transform, float p_texel_size, const Vector<uint8_t> &p_src_cache, Vector<uint8_t> &r_dst_cache) {
 	ERR_FAIL_COND_V(!array_mesh_lightmap_unwrap_callback, ERR_UNCONFIGURED);
 	ERR_FAIL_COND_V(!array_mesh_lightmap_unwrap_callback, ERR_UNCONFIGURED);
 	ERR_FAIL_COND_V_MSG(blend_shapes.size() != 0, ERR_UNAVAILABLE, "Can't unwrap mesh with blend shapes.");
 	ERR_FAIL_COND_V_MSG(blend_shapes.size() != 0, ERR_UNAVAILABLE, "Can't unwrap mesh with blend shapes.");
@@ -1178,9 +1180,6 @@ Error ImporterMesh::lightmap_unwrap_cached(const Transform3D &p_base_transform,
 		return ERR_CANT_CREATE;
 		return ERR_CANT_CREATE;
 	}
 	}
 
 
-	//remove surfaces
-	clear();
-
 	//create surfacetools for each surface..
 	//create surfacetools for each surface..
 	LocalVector<Ref<SurfaceTool>> surfaces_tools;
 	LocalVector<Ref<SurfaceTool>> surfaces_tools;
 
 
@@ -1190,9 +1189,16 @@ Error ImporterMesh::lightmap_unwrap_cached(const Transform3D &p_base_transform,
 		st->begin(Mesh::PRIMITIVE_TRIANGLES);
 		st->begin(Mesh::PRIMITIVE_TRIANGLES);
 		st->set_material(lightmap_surfaces[i].material);
 		st->set_material(lightmap_surfaces[i].material);
 		st->set_meta("name", lightmap_surfaces[i].name);
 		st->set_meta("name", lightmap_surfaces[i].name);
+
+		for (int custom_i = 0; custom_i < RS::ARRAY_CUSTOM_COUNT; custom_i++) {
+			st->set_custom_format(custom_i, (SurfaceTool::CustomFormat)((lightmap_surfaces[i].format >> custom_shift[custom_i]) & RS::ARRAY_FORMAT_CUSTOM_MASK));
+		}
 		surfaces_tools.push_back(st); //stay there
 		surfaces_tools.push_back(st); //stay there
 	}
 	}
 
 
+	//remove surfaces
+	clear();
+
 	print_verbose("Mesh: Gen indices: " + itos(gen_index_count));
 	print_verbose("Mesh: Gen indices: " + itos(gen_index_count));
 
 
 	//go through all indices
 	//go through all indices
@@ -1229,6 +1235,11 @@ Error ImporterMesh::lightmap_unwrap_cached(const Transform3D &p_base_transform,
 			if (lightmap_surfaces[surface].format & Mesh::ARRAY_FORMAT_WEIGHTS) {
 			if (lightmap_surfaces[surface].format & Mesh::ARRAY_FORMAT_WEIGHTS) {
 				surfaces_tools[surface]->set_weights(v.weights);
 				surfaces_tools[surface]->set_weights(v.weights);
 			}
 			}
+			for (int custom_i = 0; custom_i < RS::ARRAY_CUSTOM_COUNT; custom_i++) {
+				if ((lightmap_surfaces[surface].format >> custom_shift[custom_i]) & RS::ARRAY_FORMAT_CUSTOM_MASK) {
+					surfaces_tools[surface]->set_custom(custom_i, v.custom[custom_i]);
+				}
+			}
 
 
 			Vector2 uv2(gen_uvs[gen_indices[i + j] * 2 + 0], gen_uvs[gen_indices[i + j] * 2 + 1]);
 			Vector2 uv2(gen_uvs[gen_indices[i + j] * 2 + 0], gen_uvs[gen_indices[i + j] * 2 + 1]);
 			surfaces_tools[surface]->set_uv2(uv2);
 			surfaces_tools[surface]->set_uv2(uv2);
@@ -1238,10 +1249,11 @@ Error ImporterMesh::lightmap_unwrap_cached(const Transform3D &p_base_transform,
 	}
 	}
 
 
 	//generate surfaces
 	//generate surfaces
-	for (Ref<SurfaceTool> &tool : surfaces_tools) {
+	for (int i = 0; i < lightmap_surfaces.size(); i++) {
+		Ref<SurfaceTool> &tool = surfaces_tools[i];
 		tool->index();
 		tool->index();
 		Array arrays = tool->commit_to_arrays();
 		Array arrays = tool->commit_to_arrays();
-		add_surface(tool->get_primitive_type(), arrays, Array(), Dictionary(), tool->get_material(), tool->get_meta("name"));
+		add_surface(tool->get_primitive_type(), arrays, Array(), Dictionary(), tool->get_material(), tool->get_meta("name"), lightmap_surfaces[i].format);
 	}
 	}
 
 
 	set_lightmap_size_hint(Size2(size_x, size_y));
 	set_lightmap_size_hint(Size2(size_x, size_y));