blender/intern/cycles/kernel/osl/shaders/node_attribute.osl
Alex Fuller 3ff1f508e6 Cycles: Fallback value for missing image or attribute in shader
The implements internal support in Cycles for fallback values when an
attribute or image texture is missing in a shader node. It is not yet
exposed in Blender shader nodes.

This is useful to provide an appropriate default value when a shader is
used across objects with different attributes, or when UDIMs don't cover
the entire UV space.

Pull Request: https://projects.blender.org/blender/blender/pulls/162785
2026-09-15 19:45:36 +02:00

51 lines
1.5 KiB
Text

/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
*
* SPDX-License-Identifier: Apache-2.0 */
#include "node_math.h"
#include "stdcycles.h"
shader node_attribute(string bump_offset = "center",
float bump_filter_width = BUMP_FILTER_WIDTH,
string name = "",
color Missing = 0.0,
float MissingAlpha = 1.0,
output point Vector = point(0.0, 0.0, 0.0),
output color Color = 0.0,
output float Fac = 0.0,
output float Alpha = 0.0)
{
float data[4] = {0.0, 0.0, 0.0, 0.0};
int success = getattribute(name, data);
if (!success) {
if (name == "geom:generated") {
/* No generated attribute, fall back to object coordinates. */
Color = transform("object", P);
data[3] = 1.0;
}
else {
Color = Missing;
data[3] = MissingAlpha;
}
}
else {
Color = color(data[0], data[1], data[2]);
}
Vector = point(Color);
Fac = average(Color);
Alpha = data[3];
if (bump_offset == "dx") {
Color += Dx(Color) * bump_filter_width;
Vector += Dx(Vector) * bump_filter_width;
Fac += Dx(Fac) * bump_filter_width;
Alpha += Dx(Alpha) * bump_filter_width;
}
else if (bump_offset == "dy") {
Color += Dy(Color) * bump_filter_width;
Vector += Dy(Vector) * bump_filter_width;
Fac += Dy(Fac) * bump_filter_width;
Alpha += Dy(Alpha) * bump_filter_width;
}
}