Commit: 928736b173b426f8780c9c109588827647b47c16
Author: Ankit Meel
Date: Thu Aug 6 01:39:14 2020 +0530
Branches: soc-2020-io-performance
https://developer.blender.org/rB928736b173b426f8780c9c109588827647b47c16
Replace enum and its functions with Map for maps
The enum approach was adding lots of repetitive functions
and switch-case blocks. Use `blender::Map` instead.
Also fix nodes overlapping.
Connect Texture nodes with correct sockets of p-BSDF.
===================================================================
M source/blender/io/wavefront_obj/intern/wavefront_obj_im_file_reader.cc
M source/blender/io/wavefront_obj/intern/wavefront_obj_im_mtl.cc
M source/blender/io/wavefront_obj/intern/wavefront_obj_im_objects.cc
M source/blender/io/wavefront_obj/intern/wavefront_obj_im_objects.hh
===================================================================
diff --git
a/source/blender/io/wavefront_obj/intern/wavefront_obj_im_file_reader.cc
b/source/blender/io/wavefront_obj/intern/wavefront_obj_im_file_reader.cc
index a52c28b1e53..631aa4229b4 100644
--- a/source/blender/io/wavefront_obj/intern/wavefront_obj_im_file_reader.cc
+++ b/source/blender/io/wavefront_obj/intern/wavefront_obj_im_file_reader.cc
@@ -409,35 +409,6 @@ Span<std::string> OBJParser::mtl_libraries() const
return mtl_libraries_;
}
-/**
- * Get the texture map from the MTLMaterial struct corresponding to the given
string.
- */
-static tex_map_XX *get_tex_map_of_type(MTLMaterial *mtl_mat, StringRef
tex_map_str)
-{
- if (tex_map_str == "map_Kd") {
- return &mtl_mat->map_Kd;
- }
- if (tex_map_str == "map_Ks") {
- return &mtl_mat->map_Ks;
- }
- if (tex_map_str == "map_Ns") {
- return &mtl_mat->map_Ns;
- }
- if (tex_map_str == "map_d") {
- return &mtl_mat->map_d;
- }
- if (tex_map_str == "map_refl") {
- return &mtl_mat->map_refl;
- }
- if (tex_map_str == "map_Ke") {
- return &mtl_mat->map_Ke;
- }
- if (tex_map_str == "map_Bump") {
- return &mtl_mat->map_Bump;
- }
- return nullptr;
-}
-
MTLParser::MTLParser(StringRef mtl_library, StringRef obj_filepath) :
mtl_library_(mtl_library)
{
char obj_file_dir[FILE_MAXDIR];
@@ -498,11 +469,11 @@ void MTLParser::parse_and_store(Map<string, MTLMaterial>
&mtl_materials)
}
/* Image Textures. */
else if (line_key.find("map_") != string::npos) {
- tex_map_XX *tex_map = get_tex_map_of_type(curr_mtlmat, line_key);
- if (!tex_map) {
+ if (!curr_mtlmat->texture_maps.contains_as(line_key)) {
/* No supported map_xx found. */
continue;
}
+ tex_map_XX &tex_map = curr_mtlmat->texture_maps.lookup(line_key);
Vector<string> str_map_xx_split{};
split_by_char(rest_line, ' ', str_map_xx_split);
@@ -512,7 +483,7 @@ void MTLParser::parse_and_store(Map<string, MTLMaterial>
&mtl_materials)
str_map_xx_split[pos_o + 2],
str_map_xx_split[pos_o + 3]},
0.0f,
- {tex_map->translation, 3});
+ {tex_map.translation, 3});
}
int64_t pos_s{str_map_xx_split.first_index_of_try("-s")};
if (pos_s != string::npos && pos_s + 3 < str_map_xx_split.size()) {
@@ -520,7 +491,7 @@ void MTLParser::parse_and_store(Map<string, MTLMaterial>
&mtl_materials)
str_map_xx_split[pos_s + 2],
str_map_xx_split[pos_s + 3]},
1.0f,
- {tex_map->scale, 3});
+ {tex_map.scale, 3});
}
/* Only specific to Normal Map node. */
int64_t pos_bm{str_map_xx_split.first_index_of_try("-bm")};
@@ -528,7 +499,7 @@ void MTLParser::parse_and_store(Map<string, MTLMaterial>
&mtl_materials)
copy_string_to_float(str_map_xx_split[pos_bm + 1], 0.0f,
curr_mtlmat->map_Bump_value);
}
- tex_map->image_path = str_map_xx_split.last();
+ tex_map.image_path = str_map_xx_split.last();
}
}
}
diff --git a/source/blender/io/wavefront_obj/intern/wavefront_obj_im_mtl.cc
b/source/blender/io/wavefront_obj/intern/wavefront_obj_im_mtl.cc
index 16fd0475272..17d43a3ab24 100644
--- a/source/blender/io/wavefront_obj/intern/wavefront_obj_im_mtl.cc
+++ b/source/blender/io/wavefront_obj/intern/wavefront_obj_im_mtl.cc
@@ -169,11 +169,11 @@ void ShaderNodetreeWrap::link_sockets(unique_node_ptr
from_node,
bNode *to_node,
StringRef to_node_id)
{
+ to_node->locx = from_node->locx + 300;
bNodeSocket *from_sock{nodeFindSocket(from_node.get(), SOCK_OUT,
from_node_id.data())};
bNodeSocket *to_sock{nodeFindSocket(to_node, SOCK_IN, to_node_id.data())};
BLI_assert(from_sock && to_sock);
nodeAddLink(nodetree_.get(), from_node.get(), from_sock, to_node, to_sock);
- /* TODO ankitm add node positioning. */
static_cast<void>(from_node.release());
}
@@ -190,26 +190,24 @@ void ShaderNodetreeWrap::set_bsdf_socket_values()
void ShaderNodetreeWrap::add_image_textures(Main *bmain)
{
-
- for (const eTextureMapType map_type : mtl_mat_->all_tex_map_types()) {
- const tex_map_XX &texture_map = mtl_mat_->tex_map_of_type(map_type);
- if (texture_map.image_path.empty()) {
+ for (const Map<std::string, tex_map_XX>::Item texture_map :
mtl_mat_->texture_maps.items()) {
+ if (texture_map.value.image_path.empty()) {
/* No Image texture node of this map type to add in this material. */
continue;
}
unique_node_ptr tex_node{add_node_to_tree(SH_NODE_TEX_IMAGE)};
unique_node_ptr vector_node{add_node_to_tree(SH_NODE_MAPPING)};
unique_node_ptr normal_map_node = nullptr;
- if (map_type == MAP_BUMP) {
+ if (texture_map.key == "map_Bump") {
normal_map_node.reset(add_node_to_tree(SH_NODE_NORMAL_MAP));
set_property_of_socket(
SOCK_FLOAT, "Strength", {mtl_mat_->map_Bump_value},
normal_map_node.get());
}
- set_img_filepath(bmain, texture_map.image_path, tex_node.get());
+ set_img_filepath(bmain, texture_map.value.image_path, tex_node.get());
set_property_of_socket(
- SOCK_VECTOR, "Location", {texture_map.translation, 3},
vector_node.get());
- set_property_of_socket(SOCK_VECTOR, "Scale", {texture_map.scale, 3},
vector_node.get());
+ SOCK_VECTOR, "Location", {texture_map.value.translation, 3},
vector_node.get());
+ set_property_of_socket(SOCK_VECTOR, "Scale", {texture_map.value.scale, 3},
vector_node.get());
link_sockets(std::move(vector_node), "Vector", tex_node.get(), "Vector");
if (normal_map_node) {
@@ -217,7 +215,7 @@ void ShaderNodetreeWrap::add_image_textures(Main *bmain)
link_sockets(std::move(normal_map_node), "Normal", bsdf_.get(),
"Normal");
}
else {
- link_sockets(std::move(tex_node), "Color", bsdf_.get(), "Base Color");
+ link_sockets(std::move(tex_node), "Color", bsdf_.get(),
texture_map.value.dest_socket_id);
}
}
}
diff --git a/source/blender/io/wavefront_obj/intern/wavefront_obj_im_objects.cc
b/source/blender/io/wavefront_obj/intern/wavefront_obj_im_objects.cc
index 2205d9a9c03..8d3b33ed408 100644
--- a/source/blender/io/wavefront_obj/intern/wavefront_obj_im_objects.cc
+++ b/source/blender/io/wavefront_obj/intern/wavefront_obj_im_objects.cc
@@ -122,47 +122,9 @@ const std::string &OBJRawObject::group() const
return nurbs_element_.group_;
}
-Span<eTextureMapType> MTLMaterial::all_tex_map_types() const
-{
- return {MAP_KD, MAP_KS, MAP_KE, MAP_D, MAP_REFL, MAP_NS, MAP_BUMP};
-}
-
-const tex_map_XX &MTLMaterial::tex_map_of_type(eTextureMapType type) const
-{
- switch (type) {
- case MAP_KD: {
- return map_Kd;
- break;
- }
- case MAP_KS: {
- return map_Ks;
- break;
- }
- case MAP_KE: {
- return map_Ke;
- break;
- }
- case MAP_D: {
- return map_d;
- break;
- }
- case MAP_REFL: {
- return map_refl;
- break;
- }
- case MAP_NS: {
- return map_Ns;
- break;
- }
- case MAP_BUMP: {
- return map_Bump;
- break;
- }
- default: {
- BLI_assert(0);
- break;
- }
- }
+tex_map_XX &MTLMaterial::tex_map_of_type(StringRef map_string)
+{
+ return texture_maps.lookup_as(map_string);
}
/**
diff --git a/source/blender/io/wavefront_obj/intern/wavefront_obj_im_objects.hh
b/source/blender/io/wavefront_obj/intern/wavefront_obj_im_objects.hh
index dbbce9462d6..927b0c14ef0 100644
--- a/source/blender/io/wavefront_obj/intern/wavefront_obj_im_objects.hh
+++ b/source/blender/io/wavefront_obj/intern/wavefront_obj_im_objects.hh
@@ -29,6 +29,7 @@
#include "BLI_float2.hh"
#include "BLI_float3.hh"
+#include "BLI_map.hh"
#include "BLI_string_ref.hh"
#include "BLI_vector.hh"
@@ -136,20 +137,13 @@ class OBJRawObject {
friend class OBJParser;
};
-enum eTextureMapType {
- MAP_KD = 1,
- MAP_KS = 2,
- MAP_KE = 3,
- MAP_D = 4,
- MAP_REFL = 5,
- MAP_NS = 6,
- MAP_BUMP = 7,
-};
-
/**
* Used for storing parameters for all kinds of texture maps from MTL file.
*/
struct tex_map_XX {
+ tex_map_XX(StringRef to_socket_id) : dest_socket_id(to_socket_id){};
+
+ const std::string dest_socket_id{};
float3 translation = {0.0f, 0.0f, 0.0f};
float3 scale = {1.0f, 1.0f, 1.0f};
std::string image_path{};
@@ -159,6 +153,17 @@ struct tex_map_XX {
* Store material data parsed from MTL file.
*/
struct MTLMaterial {
+ MTLMaterial()
+ {
+ texture_maps.add("map_Kd", tex_map_XX("Base Color"));
+ texture_maps.add("map_Ks", tex_map_XX("Specular"));
+ texture_maps.add("map_Ns", tex_map_XX("Roughness"));
+ texture_maps.add("map_d", tex_map_XX("Alpha"));
+ texture_maps.add("map_refl", tex_map_XX("Metallic"));
+ texture_maps.add("map_Ke", tex_map_XX("Emission"));
+ }
+ tex_map_XX &tex_map_of_type(StringRef map_string);
+
std::string name{};
float Ns{1.0f};
float3 Ka{0.0f};
@@ -168,18 +173,9 @@ struct MTLMaterial {
float Ni{1.0f};
float d{1.0f};
int illum{0};
- tex_map_XX map_Kd;
- tex_map_XX map_Ks;
- tex_map_XX map_Ke;
- tex_map_XX map_d;
- tex_map_XX map_refl;
- tex_map_XX map_Ns;
- tex_map_XX map_Bump;
+ Map<std::string, tex_map_XX> texture_maps;
/** Only used for Normal Map node: map_Bump. */
float map_Bump_value = 0.0f;
-
- Span<eTextureMapType> all_tex_map_types() const;
- const tex_map_XX &tex_map_of_type(eTextureMapType type) const;
};
struct UniqueObjectDeleter {
_______________________________________________
Bf-blender-cvs mailing list
[email protected]
https://lists.blender.org/mailman/listinfo/bf-blender-cvs