# Documentation/Raytracing in SL

### From PixieWiki

< Documentation

# [edit] Raytracing in shading language

For an object to be raytraced, it must be marked as raytraceable by setting the visibility attribute.

    Attribute "visibility" "trace" 1

The following new shading language constructs and functions are available:

## [edit] trace

    color trace(point P,vector R);
    float trace(point P,vector R);

The `trace` shadeop fires a single ray from `P` in direction `R` and returns either the color hit (the color version) or the percentage visibility (the float version). You can use the explicit prefix in situations which are ambiguous. Trace also takes these optional arguments:

`uniform float bias` | The self intersection bias.
`uniform string label` | A label for the ray
`uniform float numSamples` | The number of samples for the ray
`varying float coneAngle` | The cone in which to spread the rays
`varying float blur` | Alternate way to specify the cone in which to spread the rays (coneAngle=blur*Pi)

## [edit] gather

    gather(	string category,
        point P,
        vector dir,
        float angle,
        float numSamples,
        ...)
        statement
        [else statement]

This construct is very similar to PrMan 11's gather construct. It will shoot `numSamples` rays rooted at `P` distributed in a cone centered at `dir` making `angle` angles. For each ray that hits something, the first statement will be executed. Otherwise else statement will be executed. The following optional parameters can be provided:

`uniform float bias` | The self intersection bias.
`uniform float maxdist` | The maximum intersection distance
`uniform string distribution` | The sample distribution (`"cosine"` or `"uniform"`)
`uniform string label` | The ray label
`varying type shadertype:variablename` | The query value (output)
`varying float ray:length` | Length of the ray (output)
`varying vector ray:direction` | The ray direction (output)

For example:

    color totalColor = 0;
    color hitColor;
    float numHits = 0;
    gather("irradiance",P,N,PI/2,256,"surface:Ci",hitColor) {
        totalColor += hitColor;
        numHits++;
    }
    totalColor /= numHits;

is approximately equivalent (for single bounce color bleeding) to

    totalColor = indirectdiffuse(P,N,256);

## [edit] tranmission

    color transmission(point P1,point P2,...);

This function gives the transmission between the two points. This function can be used to get raytraced shadows in light source shaders:

    illuminate (from) {
        Cl = transmission(Ps,from) * intensity * lightcolor / (L . L);
    }

## [edit] indirectdiffuse

    color indirectdiffuse(point P,vector N,float numSamples ...);

This function gives the average irradiance as seen be a hemisphere centered at `P` `N`. The irradiance is computed by raytracing. numSamples gives the number of rays to shoot. Depending on the irradiance attributes of the current primitive, this value can be cached. The following optional parameters can be provided:

`uniform float minR` | The minimum distance between samples.
`uniform float maxR` | The maximum distance between samples.
`uniform float bias` | Self intersection bias
`varying float occlusion` | The occlusion amount (output)
`uniform float maxdist` | The maximum intersection distance
`uniform vector backgroundColor` | This color will be used for rays that don't hit anything
`uniform float maxBrightness` | The ray brightness will be clamped by this number to avoid splotchy results
`uniform string environmentmap` | The incoming color for rays which don't hit anything will be estimated by sampling this map if specified
`output varying vector environmentdir` | The argument takes a variable, into which the average unoccluded environment direction will be placed
`output varying color occlusion` | The argument takes a variable, into which the value of occlusion will be placed

## [edit] occlusion

    float occlusion(point P,vector N,float numSamples ...);

This function computes the fraction of the hemisphere that is not occluded by another geometry. The interpretation of the parameters is the same with indirectdiffuse. The following parameters can be passed:

`uniform float minR` | The closest distance between samples.
`uniform float maxR` | The maximum distance between samples.
`uniform float bias` | Self intersection bias
`varying color irradiance` | The irradiance amount (output)
`uniform float maxdist` | The maximum intersection distance

## [edit] raydepth

    uniform float raydepth();

This function returns the depth of the current shading point. It returns 0 for the camera rays.

## [edit] raylabel

    uniform string raylabel();

This function returns the label of ray that caused the shading. You can assign labels to rays that are generated by the gather statement.

## [edit] rayinfo

    uniform float rayinfo(uniform string arg,varying <type> val);

The `rayinfo` shadeop returns information about a ray. The `arg` parameter specifies which piece of information to fetch. The value will be placed in `val`. If no value can be found, `rayinfo` returns 0. The table below shows what information can be obtained. Note it's that values are premoted to varying as each ray hitting the surface can have different values.

`varying string label` | The label of the ray
`varying float depth` | The depth of the ray
`varying point origin` | The origin of the ray (`P` where it was fired from)
`varying vector direction` | The direction of the ray (`R` when it was fired)
`varying float length` | The length of the ray (distance from `P`)

Retrieved from "http://www.george-graphics.co.uk/pixiewiki/Documentation/Raytracing_in_SL"

Category: Documentation

##### Views

- Article
- Discussion
- Edit
- History

##### Personal tools

- Log in / create account

##### Search

##### Toolbox

- What links here
- Related changes
- Upload file
- Special pages
- Printable version
- Permanent link