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 58 additions and 40 deletions
Showing only changes of commit 49f197ce5c - Show all commits

View File

@ -10,11 +10,13 @@ namespace blender::nodes::materialx {
NodeItem MathNodeParser::compute()
{
/* TODO: finish some math operations */
auto op = node_->custom1;
NodeMathOperation op = NodeMathOperation(node_->custom1);
NodeItem res = empty();
/* Single operand operations */
NodeItem x = get_input_value(0, NodeItem::Type::Any);
NodeItem x = get_input_value(0, NodeItem::Type::Float);
/* TODO: Seems we have to use average if Vector or Color are added */
switch (op) {
case NODE_MATH_SINE:
res = x.sin();
@ -82,7 +84,7 @@ NodeItem MathNodeParser::compute()
default: {
/* 2-operand operations */
NodeItem y = get_input_value(1, NodeItem::Type::Any);
NodeItem y = get_input_value(1, NodeItem::Type::Float);
switch (op) {
case NODE_MATH_ADD:
res = x + y;
@ -132,7 +134,7 @@ NodeItem MathNodeParser::compute()
default: {
/* 3-operand operations */
NodeItem z = get_input_value(2, NodeItem::Type::Any);
NodeItem z = get_input_value(2, NodeItem::Type::Float);
switch (op) {
case NODE_MATH_WRAP:
CLOG_WARN(LOG_MATERIALX_SHADER, "Unimplemented math operation %d", op);

View File

@ -9,21 +9,20 @@ namespace blender::nodes::materialx {
NodeItem TexCheckerNodeParser::compute()
{
NodeItem vector = get_input_link("Vector", NodeItem::Type::Vector2);
NodeItem color1 = get_input_value("Color1", NodeItem::Type::Color3);
NodeItem color2 = get_input_value("Color2", NodeItem::Type::Color3);
NodeItem scale = get_input_value("Scale", NodeItem::Type::Float);
if (!vector) {
vector = texcoord_node();
}
NodeItem value1 = val(1.0f);
NodeItem value2 = val(0.0f);
if (STREQ(socket_out_->name, "Color")) {
value1 = get_input_value("Color1", NodeItem::Type::Color3);
value2 = get_input_value("Color2", NodeItem::Type::Color3);
}
NodeItem scale = get_input_value("Scale", NodeItem::Type::Float);
vector = (vector * scale) % val(2.0f);
NodeItem mix = (vector.extract(0).floor() + vector.extract(1).floor())
.if_else(NodeItem::CompareOp::Eq, val(1.0f), val(1.0f), val(0.0f));
NodeItem res = create_node("mix", NodeItem::Type::Color3);
res.set_input("fg", color1);
res.set_input("bg", color2);
res.set_input("mix", mix);
return res;
return (vector.extract(0).floor() + vector.extract(1).floor())
.if_else(NodeItem::CompareOp::Eq, val(1.0f), value1, value2);
}
} // namespace blender::nodes::materialx

View File

@ -4,9 +4,7 @@
#include "node_parser.h"
#ifdef WITH_HYDRA
#include "hydra/image.h"
#endif
#include "DEG_depsgraph_query.h"
@ -14,22 +12,32 @@ namespace blender::nodes::materialx {
NodeItem TexEnvironmentNodeParser::compute()
{
NodeItem res = val(MaterialX::Color4(1.0f, 0.0f, 1.0f, 1.0f));
Image *image = (Image *)node_->id;
if (!image) {
return res;
}
NodeTexEnvironment *tex = static_cast<NodeTexEnvironment *>(node_->storage);
Scene *scene = DEG_get_input_scene(depsgraph_);
Main *bmain = DEG_get_bmain(depsgraph_);
std::string image_path;
#ifdef WITH_HYDRA
/* 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. */
image_path = io::hydra::cache_or_get_image_file(bmain, scene, image, &tex->iuser);
#endif
std::string image_path = io::hydra::cache_or_get_image_file(bmain, scene, image, &tex->iuser);
NodeItem texcoord = texcoord_node();
NodeItem res = create_node("image", NodeItem::Type::Color3);
NodeItem vector = get_input_link("Vector", NodeItem::Type::Vector2);
if (!vector) {
vector = texcoord_node();
}
/* TODO: fix texcoord of environment texture, some math should be applied */
res = create_node("image", NodeItem::Type::Color3);
res.set_input("file", image_path, NodeItem::Type::Filename);
res.set_input("texcoord", texcoord);
res.set_input("texcoord", vector);
/* TODO: get other Tex environment node parameters */
return res;
}

View File

@ -4,9 +4,7 @@
#include "node_parser.h"
#ifdef WITH_HYDRA
# include "hydra/image.h"
#endif
#include "hydra/image.h"
#include "DEG_depsgraph_query.h"
@ -14,22 +12,33 @@ namespace blender::nodes::materialx {
NodeItem TexImageNodeParser::compute()
{
NodeItem res = val(MaterialX::Color4(1.0f, 0.0f, 1.0f, 1.0f));
Image *image = (Image *)node_->id;
NodeTexImage *tex = static_cast<NodeTexImage *>(node_->storage);
if (image) {
NodeTexImage *tex_image = static_cast<NodeTexImage *>(node_->storage);
Scene *scene = DEG_get_input_scene(depsgraph_);
Main *bmain = DEG_get_bmain(depsgraph_);
std::string image_path;
#ifdef WITH_HYDRA
/* 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. */
image_path = io::hydra::cache_or_get_image_file(bmain, scene, image, &tex->iuser);
#endif
std::string image_path = io::hydra::cache_or_get_image_file(
bmain, scene, image, &tex_image->iuser);
NodeItem texcoord = texcoord_node();
NodeItem res = create_node("image", NodeItem::Type::Color3);
NodeItem texcoord = get_input_link("Vector", NodeItem::Type::Vector2);
if (!texcoord) {
texcoord = texcoord_node();
}
res = create_node("image", NodeItem::Type::Color4);
res.set_input("file", image_path, NodeItem::Type::Filename);
res.set_input("texcoord", texcoord);
/* TODO: get other Tex image node parameters */
}
if (STREQ(socket_out_->name, "Alpha")) {
res = res.extract(3);
}
return res;
}