Beast offers a Lua render pass, which allows users to take complete control over baking by providing a custom Lua script. This separate pass exists for both texture baking and surface transfer, as well as for point cloud baking. For details on creating the Lua pass, see Creating Render Passes.
When you create the Lua pass, you must provide the path and file name of a script file that will be called to carry out the baking. This page describes how to write that script, and details the Lua functions built in to Beast that you can use in your script.
The baking script that you provide in the call to ILBCreateLuaPass() should have the following structure:
function setup() -- Required. Sets configuration options for baking. -- See Lua Reference. end function basis(x, y, z) -- Optional. Defines the function basis to which data will be fit. -- See Lua Reference. end function bake() -- Optional. Computes the data output to texture or point cloud for each point. -- See Lua Reference. end
This function is called once before the baking process begins. Its main purpose is to set values for the different configuration options that will control the way the baking is carried out.
To set the value of an option, call the bset() function. Pass as arguments:
function setup() -- Sampling hemispherically in tangent space. bset("gather.sampletype", "indirect illumination") bset("gather.space", "tangent space") bset("gather.distribution", "equiareal") bset("gather.minsamples", 300) bset("gather.maxsamples", 600) bset("gather.coneangle", 180) bset("output.size", 9) end
The following table describes all available configuration options, with their accepted and default values.
option | type | description |
---|---|---|
gather.useRadianceCache | Boolean | Determines whether baking uses the radiance cache or brute force sampling. When enabled, all gather functionality is controlled via the final gather options described below. If you enable this setting, you must also set the basis.type option to sh.
|
gather.sampletype | string | Determines the type of light to gather. Accepts the following values:
The default value is direct dynamic illumination. |
gather.space | string | Specifies the coordinate system in which the gather will be performed. Accepts the following values:
The default value is tangent space. |
gather.distribution | string | Determines how samples are spread out over the sphere or hemisphere. Accepts the following values:
The default value is equiareal. |
gather.minsamples | int | Specifies the minimum number of samples to be used for the gather. This number is used to determine the number of sample cells to use in the gather distribution. Note that the actual number of sample cells used will not necessarily match the value of this setting, since the sample distribution needs to distribute the samples over the sphere or hemisphere in an efficient way. Accepts any value greater than 1. The default value is 128. |
gather.maxsamples | int | Specifies the maximum number of samples to be used for the gather. This setting provides an upper limit for super-sampling in the gather distribution. Accepts any value greater than the value of the gather.minsamples option. The default value is 256. |
gather.coneangle | int | Specifies the cone angle to use for the gather distribution. Accepts any value between 0 and 360 inclusive. In most cases, use 180 or 360. The default value is 180. |
gather.clamp | Boolean | Determines whether or not the values sampled during the gather are clamped. The default value is false. |
gather.cosweighdynamic | Boolean | Determines whether or not the gathered dynamic light should be cosine-weighed according
to the sample normal. The default value is false. |
gather.aomaxdistance | float | Sets the maximum distance a ray can travel before being considered un-occluded. This
will affect the occlusion appearance. If this option is not defined, the size of the whole scene will be used. Therefore, you must define this option for indoor scenes. If you do not, all surfaces will be fully occluded. |
gather.aoscale | float | Determines the scaling of the occlusion value for occlusion sampling. Can be used to boost the shadowing effect. |
gather.aocontrast | float | Controls the amount of contrast in the occlusion. A higher value will increase the contrast, making dark areas darker and bright areas brighter. |
gather.aoinfluence | float | Determines whether the output values from Lua should be scaled by ambient occlusion automatically. Accepts any value between 0.0 and 1.0:
|
gather.obeytransparency | Boolean | Determines whether or not rays will be transmitted through transparent surfaces. The default value is false. |
output.size | int | Specifies the number of elements in the array returned by the bake() function. The default value is 4. |
basis.type | string | Determines the type of function basis the gathered data will fit. Accepts the following values:
If set to any value other than none, the bake() function can be omitted from the script, and the coefficients for the specified function basis will be generated automatically. The default value is none. |
basis.rgb | Boolean | This option is taken into consideration when baking function coefficients.
The default value is false. |
basis.sh.bands | int | Specify the number of bands to use when generating spherical harmonics coefficients. Read only when the basis.type option is set to sh. Accepts any value from 1 to 10 inclusive. The default value is 2. |
This optional function allows you to define the values of the function basis that your gathered data should fit.
When defined, this function is called once for every direction used in the gather before baking. It is passed a vector that defines a point on the sampling sphere. Your implementation must determine what values this vector will produce in your custom basis, and must return an array with the correct number of coefficients. The sampled data will automatically be fitted to your basis during rendering.
In order to use this function:
For example, the following simple implementation defines a basis for polynomial texture mapping (PTM):
The bake() function is executed once for every pixel (or every super-sample if super-sampling is enabled). It must return an array that contains the data that should be output to the texture or point cloud. The size of the returned array must match the value of the output.size option set by the setup() function.
In your implementation of the bake() function, you may need to access information about the point under consideration, or to query Beast for additional information about the scene. The following sections describe the different functions and classes that you can use.
The vec3 class represents a vector of three floating-point values. It can be constructed by calling vec3(x,y,z).
This class implements the following operators:
The following table describes the functions available for obtaining information about the point currently being treated by the bake() function.
function | description |
---|---|
vec3 getPoint() | Returns the intersection point of the fragment in world space. |
vec3 getNormal() | Returns the normal of the fragment. |
vec3 getTangentU() | Returns the tangent of the fragment. |
vec3 getTangentV() | Returns the bitangent of the fragment. |
vec3 getViewDir() | Returns the vector from the fragment to the eye in world space. |
vec3 getUV() | Returns the texture coordinates of the fragment. Uses only the first two components of the vec3. |
vec3 getdTdX() | Returns the projection of the X axis of the screen space pixel in texture space. Uses only the first two components of the vec3. |
vec3 getdTdY() | Returns the projection of the Y axis of the screen space pixel in texture space. Uses only the first two components of the vec3. |
vec3 getT() | Returns the distance from the ray origin to the current fragment. |
The following table describes the functions that you can call to retrieve information about the light sources that are light-linked to the point being baked. The getLights() function returns the total number light sources linked to the point; the other functions retrieve information about a single linked light source in that list, specified by the index you provide.
function | description |
---|---|
int getLights() | Returns the number of lights linked to the current point. |
bool getLightName(i) | Returns the name of the light at the specified index. |
vec3 getLightDir(i) | Returns the direction in world space from the current point to the light at the specified index. |
float getLightDist(i) | Returns the distance in world space between the current point and the light at the specified index. |
vec3 getLightCol(i) | Returns the color of the light at the specified index. Includes attenuation and shadows. |
vec3 getLightColUnshadowed(i) | Returns the color of the light at the specified index. Does not include shadows. |
bool getLightAmb(i) | Returns true if the light at the specified index is ambient. |
bool getLightInd(i) | Returns true if the light at the specified index is indirect. |
bool getLightCastShadows(i) | Returns true if the light at the specified index casts shadows. |
float getLightLambertianReflectance(i, vec3) | Returns the Lambertian reflectance of the light with the specified index, around the normal direction specified by the vec3 argument. |
The following table describes the functions that you can call to cast rays into the scene.
The following table describes the other utility functions provided by Beast that you can call in your bake() function.