Code improvements + Mix node #30

Merged
Bogdan Nagirniak merged 18 commits from BogdanNagirniak/blender:matx-code-improvements into matx-export-material 2023-09-22 18:23:13 +02:00
3 changed files with 46 additions and 72 deletions
Showing only changes of commit 9bcb2ff0fb - Show all commits

View File

@ -25,36 +25,23 @@ static int node_shader_gpu_add_shader(GPUMaterial *mat,
NODE_SHADER_MATERIALX_BEGIN NODE_SHADER_MATERIALX_BEGIN
#ifdef WITH_MATERIALX #ifdef WITH_MATERIALX
{ {
NodeItem res = empty(); if (!ELEM(to_type_, NodeItem::Type::BSDF, NodeItem::Type::EDF)) {
switch (to_type_) { return empty();
case NodeItem::Type::BSDF: }
case NodeItem::Type::EDF: {
NodeItem shader1 = get_input_link(0, to_type_); NodeItem shader1 = get_input_link(0, to_type_);
NodeItem shader2 = get_input_link(1, to_type_); NodeItem shader2 = get_input_link(1, to_type_);
if (!shader1 && !shader2) {
return empty();
}
if (shader1 && !shader2) { if (shader1 && !shader2) {
res = shader1; return shader1;
} }
else if (!shader1 && shader2) { if (!shader1 && shader2) {
res = shader2; return shader2;
} }
else if (shader1 && shader2) { return shader1 + shader2;
res = shader1 + shader2;
}
break;
}
case NodeItem::Type::SurfaceShader: {
/* SurfaceShaders can't be added, returning the first one connected */
res = get_input_link(0, to_type_);
if (!res) {
res = get_input_link(1, to_type_);
}
break;
}
default:
BLI_assert_unreachable();
}
return res;
} }
#endif #endif
NODE_SHADER_MATERIALX_END NODE_SHADER_MATERIALX_END

View File

@ -26,37 +26,25 @@ static int node_shader_gpu_mix_shader(GPUMaterial *mat,
NODE_SHADER_MATERIALX_BEGIN NODE_SHADER_MATERIALX_BEGIN
#ifdef WITH_MATERIALX #ifdef WITH_MATERIALX
{ {
NodeItem res = empty(); if (!ELEM(to_type_, NodeItem::Type::BSDF, NodeItem::Type::EDF)) {
switch (to_type_) { return empty();
case NodeItem::Type::BSDF: }
case NodeItem::Type::EDF: {
NodeItem fac = get_input_value(0, NodeItem::Type::Float);
NodeItem shader1 = get_input_link(1, to_type_); NodeItem shader1 = get_input_link(1, to_type_);
NodeItem shader2 = get_input_link(2, to_type_); NodeItem shader2 = get_input_link(2, to_type_);
if (!shader1 && !shader2) {
return empty();
}
NodeItem fac = get_input_value(0, NodeItem::Type::Float);
if (shader1 && !shader2) { if (shader1 && !shader2) {
res = shader1 * (val(1.0f) - fac); return shader1 * (val(1.0f) - fac);
} }
else if (!shader1 && shader2) { if (!shader1 && shader2) {
res = shader2 * fac; return shader2 * fac;
} }
else if (shader1 && shader2) { return fac.mix(shader1, shader2);
res = fac.mix(shader1, shader2);
}
break;
}
case NodeItem::Type::SurfaceShader: {
/* SurfaceShaders can't be mixed, returning the first one connected */
res = get_input_link(1, NodeItem::Type::SurfaceShader);
if (!res) {
res = get_input_link(2, NodeItem::Type::SurfaceShader);
}
break;
}
default:
BLI_assert_unreachable();
}
return res;
} }
#endif #endif
NODE_SHADER_MATERIALX_END NODE_SHADER_MATERIALX_END

View File

@ -46,9 +46,10 @@ static int node_shader_gpu_output_material(GPUMaterial *mat,
NODE_SHADER_MATERIALX_BEGIN NODE_SHADER_MATERIALX_BEGIN
#ifdef WITH_MATERIALX #ifdef WITH_MATERIALX
{ {
NodeItem surface = get_input_link("Surface", NodeItem::Type::SurfaceShader);
if (!surface) {
NodeItem bsdf = get_input_link("Surface", NodeItem::Type::BSDF); NodeItem bsdf = get_input_link("Surface", NodeItem::Type::BSDF);
NodeItem edf = get_input_link("Surface", NodeItem::Type::EDF); NodeItem edf = get_input_link("Surface", NodeItem::Type::EDF);
NodeItem surface = empty();
if (bsdf || edf) { if (bsdf || edf) {
surface = create_node("surface", NodeItem::Type::SurfaceShader); surface = create_node("surface", NodeItem::Type::SurfaceShader);
if (bsdf) { if (bsdf) {
@ -58,8 +59,6 @@ NODE_SHADER_MATERIALX_BEGIN
surface.set_input("edf", edf); surface.set_input("edf", edf);
} }
} }
else {
surface = get_input_link("Surface", NodeItem::Type::SurfaceShader);
} }
return create_node("surfacematerial", NodeItem::Type::Material, {{"surfaceshader", surface}}); return create_node("surfacematerial", NodeItem::Type::Material, {{"surfaceshader", surface}});
} }