36 const char *socket_id,
43 switch (property_type) {
73 CLOG_WARN(&
LOG,
"Cannot load image file: '%s'", path.c_str());
76 CLOG_INFO(&
LOG,
"Loaded image from: '%s'", path.c_str());
86 const float color[4] = {0, 0, 0, 1};
105 Image *image =
nullptr;
109 auto end_pos = std::remove(image_path.begin(), image_path.end(),
'"');
110 image_path.erase(end_pos, image_path.end());
113 std::string tex_path{tex_map.
mtl_dir_path + image_path};
115 if (image !=
nullptr) {
120 if (image !=
nullptr) {
124 std::string no_underscore_path{image_path};
125 std::replace(no_underscore_path.begin(), no_underscore_path.end(),
'_',
' ');
126 if (!
ELEM(no_underscore_path, image_path, tex_path)) {
128 if (image !=
nullptr) {
134 if (base_path != tex_path) {
136 if (image !=
nullptr) {
169 const char *from_node_id,
171 const char *to_node_id)
182 bool do_highlight =
false;
183 bool do_tranparency =
false;
184 bool do_reflection =
false;
185 bool do_glass =
false;
199 do_reflection =
true;
205 do_reflection =
true;
206 do_tranparency =
true;
211 do_reflection =
true;
216 do_reflection =
true;
217 do_tranparency =
true;
222 do_reflection =
true;
223 do_tranparency =
true;
228 do_reflection =
true;
234 do_reflection =
false;
235 do_tranparency =
true;
240 "Material illum value '%d' is not supported by the Principled BSDF shader.",
249 if (specular < 0.0f) {
250 specular = do_highlight ? 1.0f : 0.0f;
255 roughness = do_highlight ? 0.0f : 1.0f;
258 float clamped_ns = std::max(0.0f, std::min(1000.0f, mtl_mat.
spec_exponent));
259 roughness = 1.0f -
sqrt(clamped_ns / 1000.0f);
266 if (metallic < 0.0f) {
274 float ior = mtl_mat.
ior;
276 if (do_tranparency) {
283 float alpha = mtl_mat.
alpha;
284 if (do_tranparency && alpha < 0) {
297 if (base_color.x >= 0 && base_color.y >= 0 && base_color.z >= 0) {
300 mat->
r = base_color.x;
301 mat->
g = base_color.y;
302 mat->
b = base_color.z;
310 if (emission_color.x >= 0 && emission_color.y >= 0 && emission_color.z >= 0) {
311 float emission_strength = fmax(emission_color.x, fmax(emission_color.y, emission_color.z));
312 if (emission_strength > 1.0f) {
316 SOCK_RGBA,
"Emission Color", {emission_color / emission_strength, 3}, bsdf);
337 if (do_tranparency || (alpha >= 0.0f && alpha < 1.0f)) {
342 if (mtl_mat.
sheen >= 0) {
352 if (mtl_mat.
aniso >= 0) {
363 if (transmission >= 0) {
384 if (image ==
nullptr) {
390 image_node->
id = &image->
id;
394 bNode *normal_map =
nullptr;
409 link_sockets(ntree, mapping,
"Vector", image_node,
"Vector");
413 link_sockets(ntree, image_node,
"Color", normal_map,
"Color");
414 link_sockets(ntree, normal_map,
"Normal", bsdf,
"Normal");
Image * BKE_image_load_exists(Main *bmain, const char *filepath, bool *r_exists=nullptr)
Image * BKE_image_add_generated(Main *bmain, unsigned int width, unsigned int height, const char *name, int depth, int floatbuf, short gen_type, const float color[4], bool stereo3d, bool is_data, bool tiled)
const char * BKE_main_blendfile_path(const Main *bmain) ATTR_NONNULL()
#define SH_NODE_TEX_IMAGE
#define SH_NODE_BSDF_PRINCIPLED
#define SH_NODE_TEX_COORD
#define SH_NODE_NORMAL_MAP
#define SH_NODE_OUTPUT_MATERIAL
MINLINE void copy_v4_v4(float r[4], const float a[4])
MINLINE void copy_v3_v3(float r[3], const float a[3])
void void void const char * BLI_path_basename(const char *path) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
int BLI_path_normalize(char *path) ATTR_NONNULL(1)
bool void BLI_path_rel(char path[FILE_MAX], const char *basepath) ATTR_NONNULL(1)
char * STRNCPY(char(&dst)[N], const char *src)
#define CLOG_WARN(clg_ref,...)
#define CLOG_INFO(clg_ref,...)
struct blender::bke::bNodeTreeType * ntreeType_Shader
constexpr const T * data() const
constexpr int64_t size() const
bNodeTree * node_tree_add_tree_embedded(Main *bmain, ID *owner_id, StringRefNull name, StringRefNull idname)
bNodeSocket * node_find_socket(bNode &node, eNodeSocketInOut in_out, StringRef identifier)
bNode * node_add_static_node(const bContext *C, bNodeTree &ntree, int type)
bNodeLink & node_add_link(bNodeTree &ntree, bNode &fromnode, bNodeSocket &fromsock, bNode &tonode, bNodeSocket &tosock)
void node_set_active(bNodeTree &ntree, bNode &node)
const char * tex_map_type_to_socket_id[]
const float node_locy_step
const float node_locx_normalmap
static void add_image_textures(Main *bmain, bNodeTree *ntree, bNode *bsdf, Material *mat, const MTLMaterial &mtl_mat, bool relative_paths)
const float node_locx_image
static bNode * add_node(bNodeTree *ntree, int type, float x, float y)
const float node_locx_mapping
const float node_locx_bsdf
static void set_bsdf_socket_values(bNode *bsdf, Material *mat, const MTLMaterial &mtl_mat)
static Image * create_placeholder_image(Main *bmain, const std::string &path)
bNodeTree * create_mtl_node_tree(Main *bmain, const MTLMaterial &mtl_mat, Material *mat, bool relative_paths)
const float node_locx_texcoord
static void set_property_of_socket(eNodeSocketDatatype property_type, const char *socket_id, Span< float > value, bNode *r_node)
const float node_locx_output
const float node_locy_top
static void link_sockets(bNodeTree *ntree, bNode *from_node, const char *from_node_id, bNode *to_node, const char *to_node_id)
static Image * load_image_at_path(Main *bmain, const std::string &path, bool relative_paths)
static Image * load_texture_image(Main *bmain, const MTLTexMap &tex_map, bool relative_paths)
VecBase< float, 3 > float3
const MTLTexMap & tex_map_of_type(MTLTexMapType key) const
MTLTexMap texture_maps[int(MTLTexMapType::Count)]