开放着色语言#

仅 Cycles

It is also possible to create your own nodes using Open Shading Language (OSL). These nodes will only work with the CPU and OptiX rendering backend.

要开启这块功能,在渲染设置的着色器系统中选择开放式着色语言。

Note

Some OSL features are not available when using the OptiX backend. Examples include:

  • Memory usage reductions offered by features like on-demand texture loading and mip-mapping are not available.

  • Texture lookups require OSL to be able to determine a constant image file path for each texture call.

  • Some noise functions are not available. Examples include Cell, Simplex, and Gabor.

  • The trace function is not functional. As a result of this, the Ambient Occlusion and Bevel nodes do not work.

脚本节点#

脚本节点。

OSL是专门为节点着色器式的渲染模式设计的,节点图里面一个着色器节点就对应着一个OSL着色器。要添加一个OSL着色器,需要在Blender的文本编辑器中添加脚本或者一个外部文件的链接。节点的输入和输出接口会由代码中的各种参数,在Update按钮按下时即时生成。

OSL着色器可以通过几种不同的方式连接到节点。使用 内部 模式,将使用文本数据块存储OSL着色器,而OSO字节码则存储在节点本身中。这对于分配一个包含所有内容的bl末端文件非常有用。

外部 模式可用于从驱动器指定 .osl 文件,然后它将自动编译成在同一个目录下的 .osl 文件。也可以指定一条路径到 .osl 文件,然后将直接使用,由用户手工完成编译。第三个选项是指定模块名称,它将在着色器搜索路径中查找。

着色器的搜索地址可以在OSL脚本内所指定的地址下找到,如下:

Linux
$HOME/.config/blender/4.2/shaders/
Windows
C:\Users\$user\AppData\Roaming\Blender Foundation\Blender\4.2\shaders\
macOS
/Users/$USER/Library/Application Support/Blender/4.2/shaders/

Tip

为了制作方便,我们建议利用节点组包装着色器脚本节点,并连接到其他的.blend文件。这样一来,后续可以更容易更改(增加或删除)节点的插口,而不需要更新全部文件里面的代码节点。

着色器写入#

有关如何编写着色器的更多详情,可参阅 OSL 文档

这是一个简单的示例:

shader simple_material(
    color Diffuse_Color = color(0.6, 0.8, 0.6),
    float Noise_Factor = 0.5,
    output closure color BSDF = diffuse(N))
{
    color material_color = Diffuse_Color * mix(1.0, noise(P * 10.0), Noise_Factor);
    BSDF = material_color * diffuse(N);
}

闭包#

OSL 不同于RSL 或 GLSL等,它没有光回路。场景中没有入射光源,而且必须由在渲染引擎本身的闭包中构建。这么一来限制挺多的,但这也使渲染引擎能够进行优化,并确保所有的着色器都能被取样。

在Cycles中可用的闭包对应于着色器节点和套接口;对他们所做的更多的细节和参数的意义更多详细信息,请参阅 着色器节点手册

See also

Documentation on OSL's built-in closures.

BSDF#

  • diffuse(N)

  • oren_nayar(N, roughness)

  • diffuse_ramp(N, colors[8])

  • phong_ramp(N, exponent, colors[8])

  • diffuse_toon(N, size, smooth)

  • glossy_toon(N, size, smooth)

  • translucent(N)

  • reflection(N)

  • refraction(N, ior)

  • transparent()

  • microfacet_ggx(N, roughness)

  • microfacet_ggx_aniso(N, T, ax, ay)

  • microfacet_ggx_refraction(N, roughness, ior)

  • microfacet_beckmann(N, roughness)

  • microfacet_beckmann_aniso(N, T, ax, ay)

  • microfacet_beckmann_refraction(N, roughness, ior)

  • ashikhmin_shirley(N, T, ax, ay)

  • ashikhmin_velvet(N, roughness)

毛发#

  • hair_reflection(N, roughnessu, roughnessv, T, offset)

  • hair_transmission(N, roughnessu, roughnessv, T, offset)

  • principled_hair(N, absorption, roughness, radial_roughness, coat, offset, IOR)

BSSRDF#

用于模拟次表面散射。

bssrdf(method, N, radius, albedo)#
参数:
  • method (string) --

    模拟次表面散射的渲染方式。

    • burley:基于物理的体积散射的近似值。此方法不如 随机游走 准确,但是,在某些情况下,此方法将更快地解决噪声。

    • random_walk_skin:为薄而弯曲的物体提供准确的结果。随机游走在网格内使用真正的体积散射,这意味着它最适合封闭网格。网格中的重叠面和孔可能会导致问题。

    • random_walk: Behaves similarly to random_walk_skin but modulates the Radius based on the Color, Anisotropy, and IOR. This method thereby attempts to retain greater surface detail and color than random_walk_skin.

  • N (vector) -- Normal vector of the surface point being shaded.

  • radius (vector) -- 光散射到表面下方的平均距离。较高的半径可以使外观更柔和,因为光线会流入阴影区域并穿过物体。散射距离是针对RGB通道单独指定的,对于具有较强红光散射的面板材质,渲染效果较佳。X,Y和Z的数值会分别映射到R,G和B的值。

  • albedo (color) -- 物体表面的颜色,从物理学角度讲,既光线中的不同波长被反射的概率。

体积/音量#

  • henyey_greenstein(g)

  • absorption()

其他#

  • emission()

  • ambient_occlusion()

  • holdout()

  • background()

属性#

Geometry attributes can be read through the getattribute() function. This includes UV maps, color attributes and any attributes output from geometry nodes.

The following built-in attributes are available through getattribute() as well.

geom:generated

自动生成的纹理坐标,来自于未变形的网格。

geom:uv

默认渲染UV贴图。

geom:tangent

默认的沿表面切向矢量,于物体空间。

geom:undisplaced

置换前的位置,于物体空间。

geom:dupli_generated

对于实例,从instancer对象中生成坐标。

geom:dupli_uv

对于实例来说,UV坐标来自于instancer对象。

geom:trianglevertices

三角形的三个顶点坐标。

geom:numpolyvertices

多边形的顶点数(通常返回值是3)。

geom:polyvertices

多边形的顶点坐标阵列(通常返回值是3)。

geom:name

物件名称。

geom:is_smooth

网格面平滑或平直与否。

geom:is_curve

物件是否为曲线。

geom:curve_intercept

0..1 coordinate for point along the curve, from root to tip.

geom:curve_thickness

物体空间中曲线的厚度。

geom:curve_length

物体空间中曲线的长度。

geom:curve_tangent_normal

线股的切向法线。

geom:is_point

点是否为点云。

geom:point_radius

点云中点的半径。

geom:point_position

点云中点的中心位置。

geom:point_random

Random number, different for every point in point cloud.

path:ray_length

光线到达距离。

object:random

随机数,对每个对象实例都不同。

object:index

物体唯一实例编号。

object:location

物件位置。

material:index

材质的唯一索引号。

particle:index

粒子的唯一实例编号。

particle:age

粒子帧寿命。

particle:lifetime

粒子在帧的总寿命。

particle:location

粒子的位置。

particle:size

粒子的尺寸。

particle:velocity

粒子的速度。

particle:angular_velocity

粒子的角速度。

跟踪#

仅限CPU

We support the trace(point pos, vector dir, ...) function, to trace rays from the OSL shader. The "shade" parameter is not supported currently, but attributes can be retrieved from the object that was hit using the getmessage("trace", ..) function. See the OSL specification for details on how to use this.

这个功能不能代替照明;其主要目的是使着色器能够 "探测" 附近的几何形状,例如,应用可以被几何形状阻挡的投影纹理,应用更多的 "磨损" 来暴露几何图形,或者使其他环境遮挡效果类似。

元数据#

参数的元数据控制其在用户界面中如何显示。支持以下元数据:

[[ string label = "My Label" ]]

用户界面中参数的名称

[[ string widget = "null" ]]

在用户界面中隐藏参数。

[[ string widget = "boolean" ]][[ string widget = "checkbox" ]]

将整型参数显示为布尔复选框。