开放式着色语言

开放式着色语言 (OSL) 是一种可编程着色系统,专为高级渲染引擎开发。它允许技术艺术家和开发人员使用类似 C 语言的脚本语言编写自定义着色器代码。

在 Blender 中,OSL 可在 Cycles 中用于定义自定义表面、体积和置换着色器。这样,用户就可以完全控制着色行为,实现程序化效果、高级照明模型和基于几何的自定义材质逻辑,而仅使用内置着色器节点可能无法实现这些效果。

与基于节点的材质不同,OSL 着色器是使用 Blender 的内部文本编辑器,或者从外部的 .osl.oso 文件中加载的文本脚本。然后通过脚本节点在着色器编辑器中编译和使用这些脚本。

Tip

OSL 对于生成程序纹理、自定义 BRDF 或实现研究原型特别有用。它还允许在支持 OSL 标准的兼容渲染应用程序之间共享着色器。

用法

要在 Blender 中使用开放式着色语言(OSL),请按照以下步骤操作:

  1. 启用 OSL 渲染

    渲染属性 中启用 开放式着色语言

  2. 添加一个脚本节点

    在着色器编辑器中添加脚本节点,然后在节点的属性中:

    • 模式 设为 内置,以使用 Blender 文本数据块,或者

    • 将其设置为 外部,可从磁盘加载着色器文件(.osl 或编译后的 .oso)。

    对于内置模式,请在文本编辑器中新建一个文本数据块,然后在其中编写或粘贴 OSL 代码。

    Blender 会自动编译 OSL 源文件。如果源文件是 .osl,则会编译成 .oso 字节码。编译错误将显示在系统控制台中。

  3. 使用着色器输出

    编译后,节点的输出将反映 OSL 代码中定义的 output 参数。这些输出可以连接到材质节点树的任何部分。

着色器写入

有关如何编写着色器的更多详情,可参阅 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

关于 OSL 内建闭包的文档。

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 准确,但是,在某些情况下,此方法将更快地解决噪声。

    • 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) -- 着色表面点的法向量。

  • 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" ]]

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

局限

Important

OSL 不支持 GPU 渲染,除非使用 OptiX 后端。

使用 OptiX 后端时,某些 OSL 功能不可用。例如:

  • 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.