Materials: support true float4 attributes in the Attribute node.

Add a new Alpha socket to the Attribute node that outputs the
fourth component of the attribute. Currently the only such
attribute is vertex color, but there may be more in the future.
If the attribute has no alpha channel, the expected value is 1.

The Cycles code is already refactored and committed by Brecht.

Ref D2057
This commit is contained in:
Alexander Gavrilov 2020-08-04 17:56:39 +03:00
parent da93da45ee
commit 9bc177d8de
4 changed files with 15 additions and 6 deletions

View File

@ -478,7 +478,7 @@ static void codegen_call_functions(DynStr *ds, GPUNodeGraph *graph, GPUOutput *f
BLI_dynstr_appendf(ds, "cons%d", input->id);
}
else if (input->source == GPU_SOURCE_ATTR) {
BLI_dynstr_appendf(ds, "var%d", input->attr->id);
codegen_convert_datatype(ds, input->attr->gputype, input->type, "var", input->attr->id);
}
BLI_dynstr_append(ds, ", ");

View File

@ -132,7 +132,10 @@ static void gpu_node_input_link(GPUNode *node, GPUNodeLink *link, const eGPUType
case GPU_NODE_LINK_ATTR:
input->source = GPU_SOURCE_ATTR;
input->attr = link->attr;
input->attr->gputype = type;
/* Failsafe handling if the same attribute is used with different datatypes for
* some reason (only really makes sense with float/vec2/vec3/vec4 though). This
* can happen if mixing the generic Attribute node with specialized ones. */
CLAMP_MIN(input->attr->gputype, type);
break;
case GPU_NODE_LINK_CONSTANT:
input->source = (type == GPU_CLOSURE) ? GPU_SOURCE_STRUCT : GPU_SOURCE_CONSTANT;

View File

@ -1,6 +1,7 @@
void node_attribute(vec3 attr, out vec4 outcol, out vec3 outvec, out float outf)
void node_attribute(vec4 attr, out vec4 outcol, out vec3 outvec, out float outf, out float outalpha)
{
outcol = vec4(attr, 1.0);
outvec = attr;
outf = avg(attr);
outcol = vec4(attr.xyz, 1.0);
outvec = attr.xyz;
outf = avg(attr.xyz);
outalpha = attr.w;
}

View File

@ -25,6 +25,7 @@ static bNodeSocketTemplate sh_node_attribute_out[] = {
{SOCK_RGBA, N_("Color")},
{SOCK_VECTOR, N_("Vector"), 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
{SOCK_FLOAT, N_("Fac"), 0.0f, 0.0f, 0.0f, 0.0f, -FLT_MAX, FLT_MAX, PROP_FACTOR},
{SOCK_FLOAT, N_("Alpha"), 0.0f, 0.0f, 0.0f, 0.0f, -FLT_MAX, FLT_MAX, PROP_FACTOR},
{-1, ""},
};
@ -52,6 +53,10 @@ static int node_shader_gpu_attribute(GPUMaterial *mat,
if (out[2].hasoutput) {
out[2].link = GPU_volume_grid(mat, attr->name, GPU_VOLUME_DEFAULT_0);
}
if (out[3].hasoutput) {
static const float default_alpha = 1.0f;
out[3].link = GPU_constant(&default_alpha);
}
return 1;
}