Export other channels and parameters from texture nodes #16

Merged
Bogdan Nagirniak merged 8 commits from BogdanNagirniak/blender:matx-images-alpha-1 into matx-export-material 2023-09-08 18:28:56 +02:00
4 changed files with 77 additions and 20 deletions
Showing only changes of commit f17671fb87 - Show all commits

View File

@ -346,15 +346,6 @@ NodeItem NodeItem::exp() const
return arithmetic("exp", [](float a) { return std::expf(a); }); return arithmetic("exp", [](float a) { return std::expf(a); });
} }
NodeItem NodeItem::extract(const int index) const
{
NodeItem res = empty();
res = create_node("extract", Type::Float);
res.set_input("in", *this);
res.set_input("index", val(index));
return res;
}
NodeItem NodeItem::convert(Type to_type) const NodeItem NodeItem::convert(Type to_type) const
{ {
Type from_type = type(); Type from_type = type();
@ -600,6 +591,15 @@ NodeItem NodeItem::if_else(CompareOp op,
return res; return res;
} }
NodeItem NodeItem::extract(const int index) const
{
NodeItem res = empty();
res = create_node("extract", Type::Float);
res.set_input("in", *this);
res.set_input("index", val(index));
return res;
}
NodeItem NodeItem::empty() const NodeItem NodeItem::empty() const
{ {
return NodeItem(graph_); return NodeItem(graph_);

View File

@ -121,4 +121,6 @@ DECLARE_SHADER_NODE_PARSER(EmissionNodeParser)
DECLARE_SHADER_NODE_PARSER(MixShaderNodeParser) DECLARE_SHADER_NODE_PARSER(MixShaderNodeParser)
DECLARE_SHADER_NODE_PARSER(SubsurfaceScatteringNodeParser) DECLARE_SHADER_NODE_PARSER(SubsurfaceScatteringNodeParser)
extern const MaterialX::Color4 TEX_ERROR_COLOR;
} // namespace blender::nodes::materialx } // namespace blender::nodes::materialx

View File

@ -12,31 +12,48 @@ namespace blender::nodes::materialx {
NodeItem TexEnvironmentNodeParser::compute() NodeItem TexEnvironmentNodeParser::compute()
{ {
NodeItem res = val(MaterialX::Color4(1.0f, 0.0f, 1.0f, 1.0f)); NodeItem res = val(TEX_ERROR_COLOR);
Image *image = (Image *)node_->id; Image *image = (Image *)node_->id;
if (!image) { if (!image) {
return res; return res;
} }
NodeTexEnvironment *tex = static_cast<NodeTexEnvironment *>(node_->storage); NodeTexEnvironment *tex_env = static_cast<NodeTexEnvironment *>(node_->storage);
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 contains
* pretty general code, so could be moved from bf_usd project. */ * 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->iuser); 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); NodeItem vector = get_input_link("Vector", NodeItem::Type::Vector2);
if (!vector) { if (!vector) {
vector = texcoord_node(); vector = texcoord_node();
} }
/* TODO: fix texcoord of environment texture, some math should be applied */ /* 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::Color3); res = create_node("image", NodeItem::Type::Color3);
res.set_input("file", image_path, NodeItem::Type::Filename); res.set_input("file", image_path, NodeItem::Type::Filename);
res.set_input("texcoord", vector); res.set_input("texcoord", vector);
/* TODO: get other Tex environment node parameters */ res.set_input("filtertype", val(filtertype));
return res; return res;
} }

View File

@ -10,9 +10,11 @@
namespace blender::nodes::materialx { namespace blender::nodes::materialx {
const MaterialX::Color4 TEX_ERROR_COLOR(1.0f, 0.0f, 1.0f, 1.0f);
NodeItem TexImageNodeParser::compute() NodeItem TexImageNodeParser::compute()
{ {
NodeItem res = val(MaterialX::Color4(1.0f, 0.0f, 1.0f, 1.0f)); NodeItem res = val(TEX_ERROR_COLOR);
Image *image = (Image *)node_->id; Image *image = (Image *)node_->id;
if (image) { if (image) {
@ -25,15 +27,51 @@ NodeItem TexImageNodeParser::compute()
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);
NodeItem texcoord = get_input_link("Vector", NodeItem::Type::Vector2); NodeItem vector = get_input_link("Vector", NodeItem::Type::Vector2);
if (!texcoord) { if (!vector) {
texcoord = texcoord_node(); vector = texcoord_node();
}
/* TODO: add math to vector depending of tex_image->projection */
std::string filtertype;
switch (tex_image->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();
}
std::string addressmode;
switch (tex_image->extension) {
case SHD_IMAGE_EXTENSION_REPEAT:
addressmode = "periodic";
break;
case SHD_IMAGE_EXTENSION_EXTEND:
addressmode = "clamp";
break;
case SHD_IMAGE_EXTENSION_CLIP:
addressmode = "constant";
break;
case SHD_IMAGE_EXTENSION_MIRROR:
addressmode = "mirror";
break;
default:
BLI_assert_unreachable();
} }
res = create_node("image", NodeItem::Type::Color4); res = create_node("image", NodeItem::Type::Color4);
res.set_input("file", image_path, NodeItem::Type::Filename); res.set_input("file", image_path, NodeItem::Type::Filename);
res.set_input("texcoord", texcoord); res.set_input("texcoord", vector);
/* TODO: get other Tex image node parameters */ res.set_input("filtertype", val(filtertype));
res.set_input("uaddressmode", val(addressmode));
res.set_input("vaddressmode", val(addressmode));
} }
if (STREQ(socket_out_->name, "Alpha")) { if (STREQ(socket_out_->name, "Alpha")) {