Commit: ae1c5f16cb7a7397d1a2508761fd863d62a0c3e6
Author: Ankit Meel
Date: Sun Aug 16 19:28:16 2020 +0530
Branches: soc-2020-io-performance
https://developer.blender.org/rBae1c5f16cb7a7397d1a2508761fd863d62a0c3e6
Move MTL writer code near OBJ writer code.
===================================================================
M source/blender/io/wavefront_obj/intern/wavefront_obj_ex_file_writer.cc
M source/blender/io/wavefront_obj/intern/wavefront_obj_ex_file_writer.hh
M source/blender/io/wavefront_obj/intern/wavefront_obj_ex_mtl.cc
M source/blender/io/wavefront_obj/intern/wavefront_obj_ex_mtl.hh
===================================================================
diff --git
a/source/blender/io/wavefront_obj/intern/wavefront_obj_ex_file_writer.cc
b/source/blender/io/wavefront_obj/intern/wavefront_obj_ex_file_writer.cc
index 869278dca43..37e743db0dd 100644
--- a/source/blender/io/wavefront_obj/intern/wavefront_obj_ex_file_writer.cc
+++ b/source/blender/io/wavefront_obj/intern/wavefront_obj_ex_file_writer.cc
@@ -29,6 +29,8 @@
#include "DNA_object_types.h"
#include "wavefront_obj_ex_file_writer.hh"
+#include "wavefront_obj_ex_mtl.hh"
+#include "wavefront_obj_im_mtl.hh"
namespace blender::io::obj {
@@ -419,4 +421,78 @@ void OBJWriter::update_index_offsets(const OBJMesh
&obj_mesh_data)
index_offset_[UV_VERTEX_OFF] += obj_mesh_data.tot_uv_vertices();
index_offset_[NORMAL_OFF] += obj_mesh_data.tot_polygons();
}
+
+MTLWriter::MTLWriter(const char *obj_filepath)
+{
+ BLI_strncpy(mtl_filepath_, obj_filepath, FILE_MAX);
+ BLI_path_extension_replace(mtl_filepath_, FILE_MAX, ".mtl");
+}
+
+MTLWriter::~MTLWriter()
+{
+ fclose(mtl_outfile_);
+}
+
+void MTLWriter::append_materials(const OBJMesh &mesh_to_export)
+{
+ mtl_outfile_ = fopen(mtl_filepath_, "a");
+ if (!mtl_outfile_) {
+ fprintf(stderr, "Error in opening file at %s\n", mtl_filepath_);
+ return;
+ }
+ Vector<MTLMaterial> mtl_materials;
+ MaterialWrap mat_wrap(mesh_to_export, mtl_materials);
+
+ for (const MTLMaterial &mtl_material : mtl_materials) {
+ fprintf(mtl_outfile_, "\nnewmtl %s\n", mtl_material.name.c_str());
+ fprintf(mtl_outfile_, "Ns %.6f\n", mtl_material.Ns);
+ fprintf(mtl_outfile_,
+ "Ka %.6f %.6f %.6f\n",
+ mtl_material.Ka[0],
+ mtl_material.Ka[1],
+ mtl_material.Ka[2]);
+ fprintf(mtl_outfile_,
+ "Kd %.6f %.6f %.6f\n",
+ mtl_material.Kd[0],
+ mtl_material.Kd[1],
+ mtl_material.Kd[2]);
+ fprintf(mtl_outfile_,
+ "Ks %0.6f %0.6f %0.6f\n",
+ mtl_material.Ks[0],
+ mtl_material.Ks[0],
+ mtl_material.Ks[0]);
+ fprintf(mtl_outfile_,
+ "Ke %0.6f %0.6f %0.6f\n",
+ mtl_material.Ke[0],
+ mtl_material.Ke[1],
+ mtl_material.Ke[2]);
+ fprintf(mtl_outfile_,
+ "Ni %0.6f\nd %.6f\nillum %d\n",
+ mtl_material.Ni,
+ mtl_material.d,
+ mtl_material.illum);
+ for (const Map<const std::string, tex_map_XX>::Item &texture_map :
+ mtl_material.texture_maps.items()) {
+ if (texture_map.value.image_path.empty()) {
+ continue;
+ }
+ std::string map_bump_strength{"", 12};
+ if (texture_map.key == "map_Bump" && mtl_material.map_Bump_strength >
-0.9f) {
+ map_bump_strength = " -bm " +
std::to_string(mtl_material.map_Bump_strength);
+ }
+ /* Always keep only one space between options. map_Bump string has its
leading space. */
+ fprintf(mtl_outfile_,
+ "%s -o %.6f %.6f %.6f -s %.6f %.6f %.6f%s %s\n",
+ texture_map.key.c_str(),
+ texture_map.value.translation[0],
+ texture_map.value.translation[1],
+ texture_map.value.translation[2],
+ texture_map.value.scale[0],
+ texture_map.value.scale[1],
+ texture_map.value.scale[2],
+ map_bump_strength.c_str(),
+ texture_map.value.image_path.c_str());
+ }
+ }
+}
} // namespace blender::io::obj
diff --git
a/source/blender/io/wavefront_obj/intern/wavefront_obj_ex_file_writer.hh
b/source/blender/io/wavefront_obj/intern/wavefront_obj_ex_file_writer.hh
index 628497071a8..a4f10a3f14c 100644
--- a/source/blender/io/wavefront_obj/intern/wavefront_obj_ex_file_writer.hh
+++ b/source/blender/io/wavefront_obj/intern/wavefront_obj_ex_file_writer.hh
@@ -98,4 +98,16 @@ class OBJWriter {
Span<uint> normal_indices,
const MPoly &poly_to_write) const;
};
+
+class MTLWriter {
+ private:
+ FILE *mtl_outfile_;
+ char mtl_filepath_[FILE_MAX];
+
+ public:
+ MTLWriter(const char *obj_filepath);
+ ~MTLWriter();
+
+ void append_materials(const OBJMesh &mesh_to_export);
+};
} // namespace blender::io::obj
diff --git a/source/blender/io/wavefront_obj/intern/wavefront_obj_ex_mtl.cc
b/source/blender/io/wavefront_obj/intern/wavefront_obj_ex_mtl.cc
index 5df109ec601..f24ce0f14aa 100644
--- a/source/blender/io/wavefront_obj/intern/wavefront_obj_ex_mtl.cc
+++ b/source/blender/io/wavefront_obj/intern/wavefront_obj_ex_mtl.cc
@@ -89,7 +89,7 @@ static void copy_property_from_node(MutableSpan<float>
r_property,
static void linked_sockets_to_dest_id(Vector<const nodes::OutputSocketRef *>
&r_linked_sockets,
const bNode *dest_node,
nodes::NodeTreeRef &node_tree,
- const char *dest_socket_id)
+ StringRefNull dest_socket_id)
{
if (!dest_node) {
return;
@@ -98,7 +98,7 @@ static void linked_sockets_to_dest_id(Vector<const
nodes::OutputSocketRef *> &r_
Span<const nodes::InputSocketRef *> dest_inputs =
object_dest_nodes.first()->inputs();
const nodes::InputSocketRef *dest_socket = nullptr;
for (const nodes::InputSocketRef *curr_socket : dest_inputs) {
- if (STREQ(curr_socket->bsocket()->identifier, dest_socket_id)) {
+ if (STREQ(curr_socket->bsocket()->identifier, dest_socket_id.data())) {
dest_socket = curr_socket;
break;
}
@@ -157,26 +157,16 @@ static const char *get_image_filepath(const bNode
*tex_node)
return nullptr;
}
-MTLWriter::MTLWriter(const char *obj_filepath)
-{
- BLI_strncpy(mtl_filepath_, obj_filepath, FILE_MAX);
- BLI_path_extension_replace(mtl_filepath_, FILE_MAX, ".mtl");
- /* File is opened when a material is appended. */
-}
-
-MTLWriter::~MTLWriter()
-{
- fclose(mtl_outfile_);
-}
-
/**
* Find the Principled-BSDF from the object's node tree & initialise class
member.
*/
-void MTLWriter::init_bsdf_node(const char *object_name)
+void MaterialWrap::init_bsdf_node(StringRefNull object_name)
{
+ BLI_assert(export_mtl_);
if (!export_mtl_->use_nodes) {
- fprintf(
- stderr, "No Principled-BSDF node found in the material node tree of:
%s.\n", object_name);
+ fprintf(stderr,
+ "No Principled-BSDF node found in the material node tree of:
%s.\n",
+ object_name.data());
bsdf_node_ = nullptr;
return;
}
@@ -187,17 +177,14 @@ void MTLWriter::init_bsdf_node(const char *object_name)
return;
}
}
- fprintf(
- stderr, "No Principled-BSDF node found in the material node tree of:
%s.\n", object_name);
+ fprintf(stderr,
+ "No Principled-BSDF node found in the material node tree of: %s.\n",
+ object_name.data());
bsdf_node_ = nullptr;
}
-void MTLWriter::write_curr_material(const char *object_name)
+void MaterialWrap::store_bsdf_properties(MTLMaterial &r_mtl_mat) const
{
- fprintf(mtl_outfile_, "\nnewmtl %s\n", export_mtl_->id.name + 2);
-
- init_bsdf_node(object_name);
-
/* Empirical, and copied from original python exporter. */
float spec_exponent = (1.0f - export_mtl_->roughness) * 30;
spec_exponent *= spec_exponent;
@@ -238,36 +225,52 @@ void MTLWriter::write_curr_material(const char
*object_name)
/* Transparency: Glass on, Reflection: Ray trace off */
illum = 9;
}
+ r_mtl_mat.Ns = spec_exponent;
+ r_mtl_mat.Ka = {metallic, metallic, metallic};
+ r_mtl_mat.Kd = diffuse_col;
+ r_mtl_mat.Ks = {specular, specular, specular};
+ r_mtl_mat.Ke = emission_col;
+ r_mtl_mat.Ni = refraction_index;
+ r_mtl_mat.d = dissolved;
+ r_mtl_mat.illum = illum;
+}
- fprintf(mtl_outfile_, "Ns %.6f\n", spec_exponent);
- fprintf(mtl_outfile_, "Ka %.6f %.6f %.6f\n", metallic, metallic, metallic);
- fprintf(mtl_outfile_, "Kd %.6f %.6f %.6f\n", diffuse_col[0], diffuse_col[1],
diffuse_col[2]);
- fprintf(mtl_outfile_, "Ks %0.6f %0.6f %0.6f\n", specular, specular,
specular);
- fprintf(
- mtl_outfile_, "Ke %0.6f %0.6f %0.6f\n", emission_col[0],
emission_col[1], emission_col[2]);
- fprintf(mtl_outfile_, "Ni %0.6f\n", refraction_index);
- fprintf(mtl_outfile_, "d %.6f\n", dissolved);
- fprintf(mtl_outfile_, "illum %d\n", illum);
-
- /* Image Textures. */
- Map<const std::string, const std::string> texture_map_types;
- texture_map_types.add("map_Kd", "Base Color");
- texture_map_types.add("map_Ks", "Specular");
- texture_map_types.add("map_Ns", "Roughness");
- texture_map_types.add("map_d", "Alpha");
- texture_map_types.add("map_refl", "Metallic");
- texture_map_types.add("map_Ke", "Emission");
-
+void MaterialWrap::store_image_textures(MTLMaterial &r_mtl_mat) const
+{
/* Need to create a NodeTreeRef for a faster way to find linked sockets, as
opposed to
* looping over all the links in a node tree to match two sockets of our
interest. */
nodes::NodeTreeRef node_tree(export_mtl_->nodetree);
- Vector<const nodes::OutputSocketRef *> linked_sockets;
- for (Map<const std::string, const std::string>::Item map_type_id :
texture_map_types.items()) {
- /* Find sockets linked to the destination socket of interest, in p-bsdf
node. */
- linked_sockets_to_dest_id(linked_sockets, bsdf_node_, node_tree,
map_type_id.value.c_str());
+ /* Normal Map Texture has two extra tasks of:
+ * - finding a Normal Map node before finding a texture node.
+ * - finding "Strength" property of the node for `-bm` option.
+ */
+
+ for (Map<const std::string, tex_map_XX>::MutableItem texture_map :
+ r_mtl_mat.texture_maps.items()) {
+ Vector<const nodes::OutputSocketRef *> linked_sockets;
+ const bNode *normal_map_node{nullptr};
+
+ if (texture_map.key == "map_Bump") {
+ /* Find sockets linked to destination "Normal" socket in p-bsdf node. */
+ linked_sockets_to_dest_id(linked_sockets, bsdf_node_, node_tree,
"Normal");
+ /* Among the linked sockets, find Normal Map shader node. */
+ normal_map_node = get_node_of_type(linked_sockets, SH_NODE_NORMAL_MAP);
+
+ /* Find sockets linked to "Color" socket in normal map node. */
+ linked_sockets_to_dest_id(linked_sockets, normal_map_node, node_tree,
"Col
@@ Diff output truncated at 10240 characters. @@
_______________________________________________
Bf-blender-cvs mailing list
[email protected]
https://lists.blender.org/mailman/listinfo/bf-blender-cvs