37 if (tot_verts_object <= 0) {
42 this->fixup_invalid_faces();
46 mesh_geometry_.
edges_.size(),
50 this->create_vertices(mesh);
52 this->create_edges(mesh);
53 this->create_uv_verts(mesh);
54 this->create_normals(mesh);
55 this->create_colors(mesh);
58 bool verbose_validate =
false;
60 verbose_validate =
true;
70 Map<std::string, std::unique_ptr<MTLMaterial>> &materials,
76 if (mesh ==
nullptr) {
82 if (ob_name.empty()) {
89 this->create_materials(bmain, materials, created_materials, obj, import_params.
relative_paths);
96 this->create_vertex_groups(obj);
101void MeshFromGeometry::fixup_invalid_faces()
120 int vertex_idx = mesh_geometry_.
face_corners_[corner_idx].vert_index;
121 if (used_verts.
contains(vertex_idx)) {
125 used_verts.
add(vertex_idx);
133 Vector<int, 8> face_verts;
134 Vector<int, 8> face_uvs;
135 Vector<int, 8> face_normals;
141 const FaceCorner &corner = mesh_geometry_.
face_corners_[corner_idx];
142 face_verts.append(corner.vert_index);
143 face_normals.append(corner.vertex_normal_index);
144 face_uvs.append(corner.uv_vert_index);
158 for (Span<int> face : new_faces) {
159 if (face.size() < 3) {
163 new_face.vertex_group_index = face_vertex_group;
164 new_face.material_index = face_material;
165 new_face.shaded_smooth = face_shaded_smooth;
167 new_face.corner_count_ = face.size();
168 for (
int idx : face) {
169 BLI_assert(idx >= 0 && idx < face_verts.size());
170 mesh_geometry_.
face_corners_.append({face_verts[idx], face_uvs[idx], face_normals[idx]});
178void MeshFromGeometry::create_vertices(
Mesh *mesh)
180 MutableSpan<float3> positions = mesh->vert_positions_for_write();
193 BLI_assert(local_vi >= 0 && local_vi < mesh->verts_num);
199void MeshFromGeometry::create_faces(
Mesh *mesh,
bool use_vertex_groups)
201 MutableSpan<MDeformVert> dverts;
204 dverts = mesh->deform_verts_for_write();
207 MutableSpan<int> face_offsets = mesh->face_offsets_for_write();
208 MutableSpan<int> corner_verts = mesh->corner_verts_for_write();
209 bke::MutableAttributeAccessor attributes = mesh->attributes_for_write();
210 bke::SpanAttributeWriter<int> material_indices =
213 const bool do_sharp = !has_normals();
214 bke::SpanAttributeWriter<bool> sharp_faces;
216 sharp_faces = attributes.lookup_or_add_for_write_span<
bool>(
"sharp_face",
220 int corner_index = 0;
222 for (
int face_idx = 0; face_idx < mesh->faces_num; ++face_idx) {
223 const FaceElem &curr_face = mesh_geometry_.
face_elements_[face_idx];
224 if (curr_face.corner_count_ < 3) {
226 std::cerr <<
"Face with less than 3 vertices found, skipping." << std::endl;
230 face_offsets[face_idx] = corner_index;
232 sharp_faces.span[face_idx] = !curr_face.shaded_smooth;
234 material_indices.span[face_idx] = curr_face.material_index;
237 if (material_indices.span[face_idx] < 0) {
238 material_indices.span[face_idx] = 0;
241 for (
int idx = 0; idx < curr_face.corner_count_; ++idx) {
242 const FaceCorner &curr_corner = mesh_geometry_.
face_corners_[curr_face.start_index_ + idx];
244 curr_corner.vert_index, 0);
247 if (!dverts.is_empty()) {
248 const int group_index = curr_face.vertex_group_index;
250 if (group_index >= 0 ||
true) {
261 material_indices.finish();
263 sharp_faces.finish();
267void MeshFromGeometry::create_vertex_groups(
Object *obj)
270 if (mesh->deform_verts().is_empty()) {
273 for (
const std::string &name : mesh_geometry_.group_order_) {
278void MeshFromGeometry::create_edges(
Mesh *mesh)
280 MutableSpan<int2> edges = mesh->edges_for_write();
285 for (
int i = 0; i < tot_edges; ++i) {
286 const int2 &src_edge = mesh_geometry_.
edges_[i];
287 int2 &dst_edge = edges[i];
290 BLI_assert(dst_edge[0] < total_verts && dst_edge[1] < total_verts);
298void MeshFromGeometry::create_uv_verts(
Mesh *mesh)
304 bke::MutableAttributeAccessor attributes = mesh->attributes_for_write();
305 bke::SpanAttributeWriter<float2> uv_map = attributes.lookup_or_add_for_write_only_span<
float2>(
308 int corner_index = 0;
309 bool added_uv =
false;
311 for (
const FaceElem &curr_face : mesh_geometry_.face_elements_) {
312 for (
int idx = 0; idx < curr_face.corner_count_; ++idx) {
313 const FaceCorner &curr_corner = mesh_geometry_.
face_corners_[curr_face.start_index_ + idx];
314 if (curr_corner.uv_vert_index >= 0 &&
315 curr_corner.uv_vert_index < global_vertices_.
uv_vertices.size())
317 uv_map.span[corner_index] = global_vertices_.
uv_vertices[curr_corner.uv_vert_index];
321 uv_map.span[corner_index] = {0.0f, 0.0f};
337 attributes.remove(
"UVMap");
342 const std::string &name,
343 Map<std::string, std::unique_ptr<MTLMaterial>> &materials,
349 if (found_mat !=
nullptr) {
356 const MTLMaterial &mtl = *materials.lookup_or_add(name, std::make_unique<MTLMaterial>());
365 created_materials.
add_new(name, mat);
369void MeshFromGeometry::create_materials(
Main *bmain,
370 Map<std::string, std::unique_ptr<MTLMaterial>> &materials,
375 for (
const std::string &name : mesh_geometry_.material_order_) {
377 bmain, name, materials, created_materials, relative_paths);
378 if (mat ==
nullptr) {
388bool MeshFromGeometry::has_normals()
const
393void MeshFromGeometry::create_normals(
Mesh *mesh)
395 if (!has_normals()) {
400 int corner_index = 0;
401 for (
const FaceElem &curr_face : mesh_geometry_.face_elements_) {
402 for (
int idx = 0; idx < curr_face.corner_count_; ++idx) {
403 const FaceCorner &curr_corner = mesh_geometry_.
face_corners_[curr_face.start_index_ + idx];
404 int n_index = curr_corner.vertex_normal_index;
406 if (n_index >= 0 && n_index < global_vertices_.
vert_normals.size()) {
409 corner_normals[corner_index] = normal;
416void MeshFromGeometry::create_colors(
Mesh *mesh)
424 for (
int vi : mesh_geometry_.vertices_) {
438 for (
auto item : mesh_geometry_.global_to_local_vertices_.items()) {
439 const int vi = item.key;
440 const int local_vi = item.value;
442 BLI_assert(local_vi >= 0 && local_vi < mesh->verts_num);
444 colors[local_vi] =
float4(c.
x, c.
y, c.
z, 1.0);
void BKE_id_attributes_default_color_set(struct ID *id, const char *name)
void BKE_id_attributes_active_color_set(struct ID *id, const char *name)
struct CustomDataLayer * BKE_attribute_new(AttributeOwner &owner, const char *name, eCustomDataType type, blender::bke::AttrDomain domain, struct ReportList *reports)
General operations, lookup, etc. for materials.
void BKE_object_material_assign_single_obdata(struct Main *bmain, struct Object *ob, struct Material *ma, short act)
struct Material * BKE_material_add(struct Main *bmain, const char *name)
void BKE_mesh_set_custom_normals(Mesh *mesh, float(*r_custom_loop_normals)[3])
bool BKE_mesh_validate(Mesh *mesh, bool do_verbose, bool cddata_check_mask)
Mesh * BKE_mesh_new_nomain(int verts_num, int edges_num, int faces_num, int corners_num)
void BKE_mesh_nomain_to_mesh(Mesh *mesh_src, Mesh *mesh_dst, Object *ob)
void BKE_ntree_update_main_tree(Main *bmain, bNodeTree *ntree, NodeTreeUpdateExtraParams *params)
General operations, lookup, etc. for blender objects.
void * BKE_object_obdata_add_from_type(Main *bmain, int type, const char *name) ATTR_NONNULL(1)
Object * BKE_object_add_only_object(Main *bmain, int type, const char *name) ATTR_RETURNS_NONNULL
MINLINE void copy_v3_v3(float r[3], const float a[3])
#define UNUSED_VARS_NDEBUG(...)
static AttributeOwner from_id(ID *id)
const Value * lookup_ptr(const Key &key) const
Value lookup_default(const Key &key, const Value &default_value) const
void add_new(const Key &key, const Value &value)
bool contains(const Key &key) const
Mesh * create_mesh(const OBJImportParams &import_params)
Object * create_mesh_object(Main *bmain, Map< std::string, std::unique_ptr< MTLMaterial > > &materials, Map< std::string, Material * > &created_materials, const OBJImportParams &import_params)
draw_view push_constant(Type::INT, "radiance_src") .push_constant(Type capture_info_buf storage_buf(1, Qualifier::READ, "ObjectBounds", "bounds_buf[]") .push_constant(Type draw_view int
void mesh_calc_edges(Mesh &mesh, bool keep_existing_edges, bool select_new_edges)
static Material * get_or_create_material(Main *bmain, const std::string &name, Map< std::string, std::unique_ptr< MTLMaterial > > &materials, Map< std::string, Material * > &created_materials, bool relative_paths)
bNodeTree * create_mtl_node_tree(Main *bmain, const MTLMaterial &mtl_mat, Material *mat, bool relative_paths)
void transform_object(Object *object, const OBJImportParams &import_params)
Vector< Vector< int > > fixup_invalid_face(Span< float3 > vert_positions, Span< int > face_verts)
std::string get_geometry_name(const std::string &full_name, char separator)
VecBase< float, 4 > float4
struct bNodeTree * nodetree
bool import_vertex_groups
char collection_separator
int get_vertex_count() const
Map< int, int > global_to_local_vertices_
Vector< FaceCorner > face_corners_
std::string geometry_name_
Vector< FaceElem > face_elements_
Vector< float3 > vertex_colors
bool has_vertex_color(size_t index) const
Vector< float3 > vertices
Vector< float2 > uv_vertices
Vector< float3 > vert_normals