Getting Started
Sections
Get Involved
In addition to the built-in camera types (Perspective, Orthographic and Panoramic cameras),Cycles also supports implementing custom cameras usingOpen Shading Language (OSL).
Custom cameras are implemented as OSL shaders. A camera shader receives a sensor positionas its input and outputs the corresponding ray’s position, direction and throughput.
OSL for shading and custom cameras are independent, so the latter can be used even when OSL shadingis disabled.
In order to use a custom camera, set thelens type to Custom.
This enables the selection of a text data-block or external file, similar to theScript node in shaders.
If the selected camera shader has parameters, they will be displayed below the Lens panel.
The primary input to the camera shader is the sensor position. This is provided by the functioncamera_shader_raster_position(), which returns a point whose X and Y components store theposition within the image in the range of 0-1.
In order to support random sampling in the shader, a pair of random numbers is provided bycamera_shader_random_sample(), which returns a vector containing random numbers in itsX and Y components. For the particular case of sampling the aperture, it’s betterto use thecam:aperture_position attribute (see below) to be compatible with Blender’s usualaperture options.
position, a variable of typepoint which contains the origin of the generated ray.
direction, a variable of typevector which contains the normalized direction of the generated ray.
throughput, a variable of typecolor which contains the throughput of the ray - a weighting factor
that can be used to dim or tint the resulting color seen by the camera.
Bothposition anddirection are in camera coordinates, where the origin is the position ofthe camera itself, the positive Z axis is the view direction, and the positive Y axis is up.
Ifthroughput is black, the resulting ray is skipped. This can be used to e.g. indicateinvalid rays for panoramic mappings.
Since camera shaders are not shaders in the traditional sense, many of OSL’s features such as closuresor geometry-related attributes are not available.
Instead, the following camera-specific attributes are available throughgetattribute():
cam:sensor_sizeSize of the camera sensor in millimeters, as set in theCamera properties.
cam:image_resolutionResolution of the rendered image.
cam:focal_distanceFocal distance of the camera in millimeters, as set in theDepth of Field properties.
cam:aperture_aspect_ratioAspect ratio of the camera aperture, as set in theDepth of Field properties.
cam:aperture_sizeSize of the camera aperture, as set in theDepth of Field properties.
cam:aperture_positionA random position on the aperture, taking into account its size, shape and aspect ratio as setin theDepth of Field properties. Note that this uses thesame random numbers as provided bycamera_shader_random_sample(), so avoid using both as itwould lead to correlation issues.
For some features such as the Wireframe node, Cycles needs derivatives of the ray origin anddirection with respect to image X and Y coordinates.
By default, OSL auto-differentiation will be used to compute these. For advanced cases where youcan compute the derivatives more accurately or efficiently, you can make your shader output fouradditional variables nameddPdx,dPdy,dDdx anddDdy. If any of these are present, theirvalues will be used instead. Note that you can not mix both options - either all or none must beexplicitly provided.
Shaders can define additional input parameters. These will be exposed to the user in the Cameraproperties panel, under the Lens options.
To further control how they are presented, the following OSL metadata can be used:
[[stringhelp="Thisisaparameter"]]Description of the parameter, shown in the tooltip.
[[floatsensitivity=0.25]]How far to increment/decrement the parameter when dragging/clicking.
[[intdigits=2]]How many digits are displayed for numerical parameters.
[[floatmin=-5,floatmax=5]]What range the property can take on.
[[intslider=1,floatslidermin=-4,floatslidermax=4]]Display the property as a slider with the given range.
[[stringwidget="boolean"]]Display the`int` property as a checkbox, resulting in values 0 or 1.
This is a very basic shader implementing a perspective camera:
shaderperspective_camera(floatfocal_length=90.0[[floatsensitivity=0.2,floatmin=0]],outputpointposition=0.0,outputvectordirection=0.0,outputcolorthroughput=1.0){vectorsensor_size;getattribute("cam:sensor_size",sensor_size);pointPcam=camera_shader_raster_position()-point(0.5);Pcam*=sensor_size/focal_length;direction=normalize(vector(Pcam.x,Pcam.y,1.0));}
More examples can be found inText Editor ‣ Templates ‣ Open Shading Language.
Important
Custom cameras are not supported with GPU rendering unless using the OptiX backend.
Some features in Cycles, in particular the Vector pass and Window texture coordinates, requireinverse mappings from rays to image coordinates. This is not yet supported with custom cameras.