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, 1,
"Loaded image from: '%s'", path.c_str());
86 const float color[4] = {0, 0, 0, 1};
110 Image *image =
nullptr;
114 auto end_pos = std::remove(image_path.begin(), image_path.end(),
'"');
115 image_path.erase(end_pos, image_path.end());
118 std::string tex_path{tex_map.
mtl_dir_path + image_path};
120 if (image !=
nullptr) {
125 if (image !=
nullptr) {
129 std::string no_underscore_path{image_path};
130 std::replace(no_underscore_path.begin(), no_underscore_path.end(),
'_',
' ');
131 if (!
ELEM(no_underscore_path, image_path, tex_path)) {
133 if (image !=
nullptr) {
139 if (base_path != tex_path) {
141 if (image !=
nullptr) {
174 const char *from_node_id,
176 const char *to_node_id)
187 bool do_highlight =
false;
188 bool do_tranparency =
false;
189 bool do_reflection =
false;
190 bool do_glass =
false;
204 do_reflection =
true;
210 do_reflection =
true;
211 do_tranparency =
true;
216 do_reflection =
true;
221 do_reflection =
true;
222 do_tranparency =
true;
227 do_reflection =
true;
228 do_tranparency =
true;
233 do_reflection =
true;
239 do_reflection =
false;
240 do_tranparency =
true;
245 "Material illum value '%d' is not supported by the Principled BSDF shader.",
254 if (specular < 0.0f) {
255 specular = do_highlight ? 1.0f : 0.0f;
260 roughness = do_highlight ? 0.0f : 1.0f;
263 float clamped_ns = std::max(0.0f, std::min(1000.0f, mtl_mat.
spec_exponent));
264 roughness = 1.0f -
sqrt(clamped_ns / 1000.0f);
271 if (metallic < 0.0f) {
279 float ior = mtl_mat.
ior;
281 if (do_tranparency) {
288 float alpha = mtl_mat.
alpha;
289 if (do_tranparency && alpha < 0) {
302 if (base_color.x >= 0 && base_color.y >= 0 && base_color.z >= 0) {
305 mat->
r = base_color.x;
306 mat->
g = base_color.y;
307 mat->
b = base_color.z;
315 if (emission_color.x >= 0 && emission_color.y >= 0 && emission_color.z >= 0) {
316 float emission_strength = fmax(emission_color.x, fmax(emission_color.y, emission_color.z));
317 if (emission_strength > 1.0f) {
321 SOCK_RGBA,
"Emission Color", {emission_color / emission_strength, 3}, bsdf);
342 if (do_tranparency || (alpha >= 0.0f && alpha < 1.0f)) {
347 if (mtl_mat.
sheen >= 0) {
357 if (mtl_mat.
aniso >= 0) {
368 if (transmission >= 0) {
389 if (image ==
nullptr) {
395 image_node->
id = &image->
id;
399 bNode *normal_map =
nullptr;
414 link_sockets(ntree, mapping,
"Vector", image_node,
"Vector");
418 link_sockets(ntree, image_node,
"Color", normal_map,
"Color");
419 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, level,...)
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)]
void * BKE_image_free_buffers