38 BLI_assert(socket && socket->type == property_type);
39 switch (property_type) {
69 fprintf(stderr,
"Cannot load image file: '%s'\n", path.c_str());
72 fprintf(stderr,
"Loaded image from: '%s'\n", path.c_str());
81 const float color[4] = {0, 0, 0, 1};
105 if (
image !=
nullptr) {
111 if (
image !=
nullptr) {
115 std::string no_quote_path{tex_path};
116 auto end_pos =
std::remove(no_quote_path.begin(), no_quote_path.end(),
'"');
117 no_quote_path.erase(end_pos, no_quote_path.end());
118 if (no_quote_path != tex_path) {
120 if (
image !=
nullptr) {
125 std::string no_underscore_path{no_quote_path};
126 std::replace(no_underscore_path.begin(), no_underscore_path.end(),
'_',
' ');
127 if (no_underscore_path != no_quote_path && no_underscore_path != tex_path) {
129 if (
image !=
nullptr) {
135 if (base_path != tex_path) {
137 if (
image !=
nullptr) {
161 set_bsdf_socket_values(mat);
162 add_image_textures(bmain, mat, relative_paths);
163 link_sockets(bsdf_,
"BSDF", shader_output_,
"Surface", 4);
184 return nodetree_.release();
187 bNode *ShaderNodetreeWrap::add_node_to_tree(
const int node_type)
192 std::pair<float, float> ShaderNodetreeWrap::set_node_locations(
const int pos_x)
197 for (
Span<int> location : node_locations) {
198 if (location[0] == pos_x && location[1] == pos_y) {
207 node_locations.append({pos_x, pos_y});
208 return {pos_x * node_size_, pos_y * node_size_ * 2.0 / 3.0};
213 void ShaderNodetreeWrap::link_sockets(
bNode *from_node,
214 StringRef from_node_id,
216 StringRef to_node_id,
217 const int from_node_pos_x)
219 std::tie(from_node->
locx, from_node->
locy) = set_node_locations(from_node_pos_x);
220 std::tie(to_node->
locx, to_node->
locy) = set_node_locations(from_node_pos_x + 1);
224 nodeAddLink(nodetree_.get(), from_node, from_sock, to_node, to_sock);
227 void ShaderNodetreeWrap::set_bsdf_socket_values(
Material *mat)
230 bool do_highlight =
false;
231 bool do_tranparency =
false;
232 bool do_reflection =
false;
233 bool do_glass =
false;
247 do_reflection =
true;
253 do_reflection =
true;
254 do_tranparency =
true;
259 do_reflection =
true;
264 do_reflection =
true;
265 do_tranparency =
true;
270 do_reflection =
true;
271 do_tranparency =
true;
276 do_reflection =
true;
282 do_reflection =
false;
283 do_tranparency =
true;
287 std::cerr <<
"Warning! illum value = " <<
illum
288 <<
"is not supported by the Principled-BSDF shader." << std::endl;
295 float specular = (mtl_mat_.
Ks[0] + mtl_mat_.
Ks[1] + mtl_mat_.
Ks[2]) / 3;
297 specular = do_highlight ? 1.0f : 0.0f;
301 if (mtl_mat_.
Ns < 0.0f) {
309 float metallic = (mtl_mat_.
Ka[0] + mtl_mat_.
Ka[1] + mtl_mat_.
Ka[2]) / 3;
319 float ior = mtl_mat_.
Ni;
321 if (do_tranparency) {
328 float alpha = mtl_mat_.
d;
329 if (do_tranparency && alpha < 0) {
333 float3 base_color = {mtl_mat_.
Kd[0], mtl_mat_.
Kd[1], mtl_mat_.
Kd[2]};
334 if (base_color.
x >= 0 && base_color.
y >= 0 && base_color.
z >= 0) {
337 mat->
r = base_color.
x;
338 mat->
g = base_color.
y;
339 mat->
b = base_color.
z;
342 float3 emission_color = {mtl_mat_.
Ke[0], mtl_mat_.
Ke[1], mtl_mat_.
Ke[2]};
343 if (emission_color.
x >= 0 && emission_color.
y >= 0 && emission_color.
z >= 0) {
360 if (do_tranparency || (alpha >= 0.0f && alpha < 1.0f)) {
365 void ShaderNodetreeWrap::add_image_textures(
Main *bmain,
Material *mat,
bool relative_paths)
367 for (
const Map<const eMTLSyntaxElement, tex_map_XX>::Item texture_map :
369 if (!texture_map.value.is_valid()) {
377 if (
image ==
nullptr) {
380 image_texture->
id = &
image->id;
382 texture_map.value.projection_type;
385 bNode *normal_map =
nullptr;
393 if (texture_map.value.translation !=
float3(0, 0, 0) ||
394 texture_map.value.scale !=
float3(1, 1, 1)) {
400 link_sockets(texture_coordinate,
"UV", mapping,
"Vector", 0);
401 link_sockets(mapping,
"Vector", image_texture,
"Vector", 1);
405 link_sockets(image_texture,
"Color", normal_map,
"Color", 2);
406 link_sockets(normal_map,
"Normal", bsdf_,
"Normal", 3);
409 link_sockets(image_texture,
"Alpha", bsdf_, texture_map.value.dest_socket_id, 2);
413 link_sockets(image_texture,
"Color", bsdf_, texture_map.value.dest_socket_id, 2);
struct Image * BKE_image_load_exists(struct Main *bmain, const char *filepath)
struct Image * BKE_image_add_generated(struct 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 struct Main *bmain) ATTR_NONNULL()
#define SH_NODE_BSDF_PRINCIPLED
#define SH_NODE_OUTPUT_MATERIAL
void ntreeFreeEmbeddedTree(struct bNodeTree *ntree)
struct bNodeLink * nodeAddLink(struct bNodeTree *ntree, struct bNode *fromnode, struct bNodeSocket *fromsock, struct bNode *tonode, struct bNodeSocket *tosock)
struct bNodeSocket * nodeFindSocket(const struct bNode *node, eNodeSocketInOut in_out, const char *identifier)
struct bNodeTree * ntreeAddTree(struct Main *bmain, const char *name, const char *idname)
struct bNode * nodeAddStaticNode(const struct bContext *C, struct bNodeTree *ntree, int type)
void nodeSetActive(struct bNodeTree *ntree, struct bNode *node)
MINLINE void copy_v4_v4(float r[4], const float a[4])
MINLINE void copy_v3_v3(float r[3], const float a[3])
const char * BLI_path_basename(const char *path) ATTR_NONNULL() ATTR_WARN_UNUSED_RESULT
void BLI_path_rel(char *file, const char *relfile) ATTR_NONNULL()
#define STRNCPY(dst, src)
struct bNodeTreeType * ntreeType_Shader
in reality light always falls off quadratically Particle Retrieve the data of the particle that spawned the object for example to give variation to multiple instances of an object Point Retrieve information about points in a point cloud Retrieve the edges of an object as it appears to Cycles topology will always appear triangulated Convert a blackbody temperature to an RGB value Normal Generate a perturbed normal from an RGB normal map image Typically used for faking highly detailed surfaces Generate an OSL shader from a file or text data block Image Sample an image file as a texture Sky Generate a procedural sky texture Noise Generate fractal Perlin noise Wave Generate procedural bands or rings with noise Voronoi Generate Worley noise based on the distance to random points Typically used to generate textures such as or biological cells Brick Generate a procedural texture producing bricks SH_NODE_TEX_COORD
Group Output data from inside of a node group A color picker Mix two input colors RGB to Convert a color s luminance to a grayscale value Generate a normal vector and a dot product Bright Control the brightness and contrast of the input color Vector Map an input vectors to used to fine tune the interpolation of the input Camera Retrieve information about the camera and how it relates to the current shading point s position Clamp a value between a minimum and a maximum Vector Perform vector math operation Invert a producing a negative Combine Generate a color from its and blue Hue Saturation Apply a color transformation in the HSV color model Specular Similar to the Principled BSDF node but uses the specular workflow instead of metallic
in reality light always falls off quadratically Particle Retrieve the data of the particle that spawned the object for example to give variation to multiple instances of an object Point Retrieve information about points in a point cloud Retrieve the edges of an object as it appears to Cycles topology will always appear triangulated Convert a blackbody temperature to an RGB value Normal Generate a perturbed normal from an RGB normal map image Typically used for faking highly detailed surfaces Generate an OSL shader from a file or text data block SH_NODE_TEX_IMAGE
Group Output data from inside of a node group A color picker Mix two input colors RGB to Convert a color s luminance to a grayscale value Generate a normal vector and a dot product Bright Control the brightness and contrast of the input color Vector Map an input vectors to used to fine tune the interpolation of the input Camera Retrieve information about the camera and how it relates to the current shading point s position Clamp a value between a minimum and a maximum Vector Perform vector math operation Invert a color
in reality light always falls off quadratically Particle Retrieve the data of the particle that spawned the object for example to give variation to multiple instances of an object Point Retrieve information about points in a point cloud Retrieve the edges of an object as it appears to Cycles topology will always appear triangulated Convert a blackbody temperature to an RGB value SH_NODE_NORMAL_MAP
constexpr const T * data() const
constexpr int64_t size() const
constexpr const char * data() const
bNodeTree * get_nodetree()
ShaderNodetreeWrap(Main *bmain, const MTLMaterial &mtl_mat, Material *mat, bool relative_paths)
depth_tx normal_tx diffuse_light_tx specular_light_tx volume_light_tx environment_tx ambient_occlusion_tx aov_value_tx in_weight_img image(1, GPU_R32F, Qualifier::WRITE, ImageType::FLOAT_2D_ARRAY, "out_weight_img") .image(3
bool remove(void *owner, const AttributeIDRef &attribute_id)
static Image * load_image_at_path(Main *bmain, const std::string &path, bool relative_paths)
static Image * create_placeholder_image(Main *bmain, const std::string &path)
static void set_property_of_socket(eNodeSocketDatatype property_type, StringRef socket_id, Span< float > value, bNode *r_node)
static Image * load_texture_image(Main *bmain, const tex_map_XX &tex_map, bool relative_paths)
static const pxr::TfToken ior("ior", pxr::TfToken::Immortal)
static const pxr::TfToken roughness("roughness", pxr::TfToken::Immortal)
static const pxr::TfToken specular("specular", pxr::TfToken::Immortal)
Map< const eMTLSyntaxElement, tex_map_XX > texture_maps
const tex_map_XX & tex_map_of_type(const eMTLSyntaxElement key) const
void operator()(bNodeTree *node)