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 shader2 = get_input_link(1, to_type_);
if (shader1 && !shader2) {
res = shader1;
}
else if (!shader1 && shader2) {
res = shader2;
}
else if (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;
NodeItem shader1 = get_input_link(0, to_type_);
NodeItem shader2 = get_input_link(1, to_type_);
if (!shader1 && !shader2) {
return empty();
}
if (shader1 && !shader2) {
return shader1;
}
if (!shader1 && shader2) {
return shader2;
}
return shader1 + shader2;
} }
#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 shader2 = get_input_link(2, to_type_);
if (shader1 && !shader2) {
res = shader1 * (val(1.0f) - fac);
}
else if (!shader1 && shader2) {
res = shader2 * fac;
}
else if (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;
NodeItem shader1 = get_input_link(1, 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) {
return shader1 * (val(1.0f) - fac);
}
if (!shader1 && shader2) {
return shader2 * fac;
}
return fac.mix(shader1, shader2);
} }
#endif #endif
NODE_SHADER_MATERIALX_END NODE_SHADER_MATERIALX_END

View File

@ -46,20 +46,19 @@ static int node_shader_gpu_output_material(GPUMaterial *mat,
NODE_SHADER_MATERIALX_BEGIN NODE_SHADER_MATERIALX_BEGIN
#ifdef WITH_MATERIALX #ifdef WITH_MATERIALX
{ {
NodeItem bsdf = get_input_link("Surface", NodeItem::Type::BSDF); NodeItem surface = get_input_link("Surface", NodeItem::Type::SurfaceShader);
NodeItem edf = get_input_link("Surface", NodeItem::Type::EDF); if (!surface) {
NodeItem surface = empty(); NodeItem bsdf = get_input_link("Surface", NodeItem::Type::BSDF);
if (bsdf || edf) { NodeItem edf = get_input_link("Surface", NodeItem::Type::EDF);
surface = create_node("surface", NodeItem::Type::SurfaceShader); if (bsdf || edf) {
if (bsdf) { surface = create_node("surface", NodeItem::Type::SurfaceShader);
surface.set_input("bsdf", bsdf); if (bsdf) {
surface.set_input("bsdf", bsdf);
}
if (edf) {
surface.set_input("edf", edf);
}
} }
if (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}});
} }