Fix creating 2 <image> nodes in TexImage when Color and Output are connected #24

Merged
Bogdan Nagirniak merged 1 commits from BogdanNagirniak/blender:matx-fix-teximage-nodes into matx-export-material 2023-09-19 18:12:58 +02:00
3 changed files with 112 additions and 58 deletions
Showing only changes of commit 2d0ca4da6a - Show all commits

View File

@ -100,7 +100,7 @@ NodeItem GroupOutputNodeParser::compute_full()
res = compute(); res = compute();
return res; return res;
#else #else
return NodeParser::compute_full(); return compute();
#endif #endif
} }
@ -144,7 +144,7 @@ NodeItem GroupInputNodeParser::compute_full()
res = compute(); res = compute();
return res; return res;
#else #else
return NodeParser::compute_full(); return compute();
#endif #endif
} }

View File

@ -11,6 +11,10 @@
#include "IMB_colormanagement.h" #include "IMB_colormanagement.h"
#include "hydra/image.h"
#include "DEG_depsgraph_query.h"
namespace blender::nodes::node_shader_tex_environment_cc { namespace blender::nodes::node_shader_tex_environment_cc {
static void node_declare(NodeDeclarationBuilder &b) static void node_declare(NodeDeclarationBuilder &b)
@ -130,8 +134,50 @@ static int node_shader_gpu_tex_environment(GPUMaterial *mat,
NODE_SHADER_MATERIALX_BEGIN NODE_SHADER_MATERIALX_BEGIN
#ifdef WITH_MATERIALX #ifdef WITH_MATERIALX
{ {
/* TODO: Implement */ NodeItem res = val(MaterialX::Color4(1.0f, 0.0f, 1.0f, 1.0f));
return empty();
Image *image = (Image *)node_->id;
if (!image) {
return res;
}
NodeTexEnvironment *tex_env = static_cast<NodeTexEnvironment *>(node_->storage);
Scene *scene = DEG_get_input_scene(depsgraph_);
Main *bmain = DEG_get_bmain(depsgraph_);
/* TODO: What if Blender built without Hydra? Also io::hydra::cache_or_get_image_file contains
* pretty general code, so could be moved from bf_usd project. */
std::string image_path = io::hydra::cache_or_get_image_file(
bmain, scene, image, &tex_env->iuser);
NodeItem vector = get_input_link("Vector", NodeItem::Type::Vector2);
if (!vector) {
vector = texcoord_node();
}
/* TODO: texcoords should be translated to spherical coordinates */
std::string filtertype;
switch (tex_env->interpolation) {
case SHD_INTERP_LINEAR:
filtertype = "linear";
break;
case SHD_INTERP_CLOSEST:
filtertype = "closest";
break;
case SHD_INTERP_CUBIC:
case SHD_INTERP_SMART:
filtertype = "cubic";
break;
default:
BLI_assert_unreachable();
}
res = create_node("image", NodeItem::Type::Color4);
res.set_input("file", image_path, NodeItem::Type::Filename);
res.set_input("texcoord", vector);
res.set_input("filtertype", val(filtertype));
return res;
} }
#endif #endif
NODE_SHADER_MATERIALX_END NODE_SHADER_MATERIALX_END

View File

@ -179,7 +179,14 @@ static int node_shader_gpu_tex_image(GPUMaterial *mat,
NODE_SHADER_MATERIALX_BEGIN NODE_SHADER_MATERIALX_BEGIN
#ifdef WITH_MATERIALX #ifdef WITH_MATERIALX
{ {
NodeItem res = val(MaterialX::Color4(1.0f, 0.0f, 1.0f, 1.0f)); /* Getting node name for Color output. This name will be used for <image> node. */
std::string image_node_name = node_name();
image_node_name = image_node_name.substr(0, image_node_name.rfind('_')) + "_Color";
NodeItem res = empty();
res.node = graph_->getNode(image_node_name);
if (!res.node) {
res = val(MaterialX::Color4(1.0f, 0.0f, 1.0f, 1.0f));
Image *image = (Image *)node_->id; Image *image = (Image *)node_->id;
if (image) { if (image) {
@ -187,8 +194,8 @@ NODE_SHADER_MATERIALX_BEGIN
Scene *scene = DEG_get_input_scene(depsgraph_); Scene *scene = DEG_get_input_scene(depsgraph_);
Main *bmain = DEG_get_bmain(depsgraph_); Main *bmain = DEG_get_bmain(depsgraph_);
/* TODO: What if Blender built without Hydra? Also io::hydra::cache_or_get_image_file contains /* TODO: What if Blender built without Hydra? Also io::hydra::cache_or_get_image_file
* pretty general code, so could be moved from bf_usd project. */ * contains pretty general code, so could be moved from bf_usd project. */
std::string image_path = io::hydra::cache_or_get_image_file( std::string image_path = io::hydra::cache_or_get_image_file(
bmain, scene, image, &tex_image->iuser); bmain, scene, image, &tex_image->iuser);
@ -238,6 +245,7 @@ NODE_SHADER_MATERIALX_BEGIN
res.set_input("uaddressmode", val(addressmode)); res.set_input("uaddressmode", val(addressmode));
res.set_input("vaddressmode", val(addressmode)); res.set_input("vaddressmode", val(addressmode));
} }
}
if (STREQ(socket_out_->name, "Alpha")) { if (STREQ(socket_out_->name, "Alpha")) {
res = res.extract(3); res = res.extract(3);