开放着色语言

仅 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

  1. $HOME/.config/blender/3.6/shaders/

Windows

  1. C:\Users\$user\AppData\Roaming\Blender Foundation\Blender\3.6\shaders\

macOS

  1. /Users/$USER/Library/Application Support/Blender/3.6/shaders/

Tip

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

着色器写入

For more details on how to write shaders, see the OSL specification. Here is a simple example:

  1. shader simple_material(
  2. color Diffuse_Color = color(0.6, 0.8, 0.6),
  3. float Noise_Factor = 0.5,
  4. output closure color BSDF = diffuse(N))
  5. {
  6. color material_color = Diffuse_Color * mix(1.0, noise(P * 10.0), Noise_Factor);
  7. BSDF = material_color * diffuse(N);
  8. }

闭包

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

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

双向散射分布函数

  • 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_cubic(N, radius, texture_blur, sharpness)

  • bssrdf_gaussian(N, radius, texture_blur)

体积光渲染

  • 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

Automatically generated texture coordinates, from undeformed mesh.

geom:uv

默认渲染UV贴图。

geom:tangent

Default tangent vector along surface, in object space.

geom:undisplaced

Position before displacement, in object space.

geom:dupli_generated

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

geom:dupli_uv

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

geom:trianglevertices

三角形的三个顶点坐标。

geom:numpolyvertices

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

geom:polyvertices

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

geom:name

物件名称。

geom:is_smooth

Is mesh face smooth or flat shaded.

geom:is_curve

Is object a curve or not.

geom:curve_intercept

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

geom:curve_thickness

Thickness of the curve in object space.

geom:curve_length

Length of the curve in object space.

geom:curve_tangent_normal

线股的切向法线。

geom:is_point

Is point in a point cloud or not.

geom:point_radius

Radius of point in point cloud.

geom:point_position

Center position of point in point cloud.

geom:point_random

Random number, different for every point in point cloud.

path:ray_length

光线到达距离。

object:random

Random number, different for every object instance.

object:index

Object unique instance index.

object:location

物件位置。

material:index

Material unique index number.

particle:index

Particle unique instance number.

particle:age

粒子帧寿命。

particle:lifetime

粒子在帧的总寿命。

particle:location

粒子的位置。

particle:size

粒子的尺寸。

particle:velocity

粒子的速度。

particle:angular_velocity

粒子的角速度。

跟踪

CPU Only

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.

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

元数据

Metadata on parameters controls their display in the user interface. The following metadata is supported:

[[ string label = "My Label" ]]

Name of parameter in in the user interface

[[ string widget = "null" ]]

Hide parameter in the user interface.

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

Display integer parameter as a boolean checkbox.