Open Shading Language

C’est aussi possible de créer vos propres nodes en utilisant le Open Shading Language (OSL). Notez que ces nodes fonctionneront seulement dans le rendu CPU : il n’existe pas de prise en charge pour lancer le code OSL sur le GPU.

Pour l’activer, sélectionnez Open Shading Language comme système de shading dans le réglages de rendu.

Note

Sous Linux, les outils de compilation C/C++ (en particulier /usr/bin/cpp) doivent être installés pour compiler les scripts OSL.

Script Node

../../../_images/render_cycles_nodes_osl_script-node.png

Script Node.

OSL was designed for node-based shading, and each OSL shader corresponds to one node in a node setup. To add an OSL shader, add a script node and link it to a text data-block or an external file. Input and output sockets will be created from the shader parameters on clicking the update button in the Node or the Text editor.

Les OSL shaders peuvent être liés au node de plusieurs manières. Avec le mode Internal, un data-block texte est utilisé pour enregistrer le OSL shader, et le bytecode OSL est enregistré dans le node lui-même. C’est pratique pour distribuer un fichier blend avec le tout empaqueté dedans.

The External mode can be used to specify a .osl file from a drive, and this will then be automatically compiled into a .oso file in the same directory. It is also possible to specify a path to a .oso file, which will then be used directly, with compilation done manually by the user. The third option is to specify just the module name, which will be looked up in the shader search path.

le chemin de recherche du shader est au même emplacement que celui des scripts ou de la configuration, sous :

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

Astuce

Pour une utilisation en production, nous suggérons l’utilisation d’un groupe de nodes pour envelopper les script nodes du shader. Ceci permet plus facilement les modifications du node après coup car des sockets sont ajoutés ou supprimés, sans avoir à mettre à jour les script nodes dans tous les fichiers.

Writing Shaders

Pour plus de détails sur l’écriture des shaders, voir les spécifications OSL. ci-après un exemple simple :

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);
}

Closures

OSL is different from, for example, RSL or GLSL, in that it does not have a light loop. There is no access to lights in the scene, and the material must be built from closures that are implemented in the renderer itself. This is more limited, but also makes it possible for the renderer to do optimizations and ensure all shaders can be importance sampled.

Les closures disponibles dans Cycles correspondent à des shader nodes et leurs sockets ; pour plus de détail sur leur fonctionnement et la signification des paramètres, voit le manuel des shader nodes.

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

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

Volume

  • henyey_greenstein(g)
  • absorption()

Other

  • emission()
  • ambient_occlusion()
  • holdout()
  • background()

Attributes

Some object, particle and mesh attributes are available to the built-in getattribute() function. UV maps and vertex colors can be retrieved using their name. Other attributes are listed below:

geom:generated
Generated texture coordinates.
geom:uv
Default render UV map.
geom:dupli_generated
For instances, generated coordinate from duplicator object.
geom:dupli_uv
For instances, UV coordinate from duplicator object.
geom:trianglevertices
3 vertex coordinates of the triangle.
geom:numpolyvertices
Number of vertices in the polygon (always returns three currently).
geom:polyvertices
Vertex coordinates array of the polygon (always three vertices currently).
geom:name
Name of the object.
geom:is_curve
Is object a strand or not.
geom:curve_intercept
Point along the strand, from root to tip.
geom:curve_thickness
Thickness of the strand.
geom:curve_tangent_normal
Tangent Normal of the strand.
path:ray_length
Ray distance since last hit.
object:location
Object location.
object:index
Object index number.
object:random
Per object random number generated from object index and name.
material:index
Material index number.
particle:index
Particle instance number.
particle:age
Particle age in frames.
particle:lifetime
Total lifespan of particle in frames.
particle:location
Location of the particle.
particle:size
Size of the particle.
particle:velocity
Velocity of the particle.
particle:angular_velocity
Angular velocity of the particle.

Trace

Nous prenons en charge la fonction trace(point pos, vector dir, ...), pour tracer les rayons du OSL shader. Le paramètre shade n’est pas pris en charge actuellement, mais les attributs peuvent être récupérés depuis l’objet qui est touché en utilisant la fonction getmessage("trace", ..). Voir les spécifications OSL pour les détails d’utilisation.

Cette fonction ne peut pas être utilisée à la place de l’éclairage : l’objectif principal est de permettre aux shaders de la géométrie voisine, par exemple pour appliquer un etexture projetée qui peut être bloquée par la géométrie, appliquez plus de à la géométrie exposée, ou faites d’autres effets de type ambient occlusion.