Skip to content

Shader

A Shader is an object that is used to modify the behavior of a ray. It is applied to the ray when it is traced. Each shader is applied to a ray in the order they are in the ray's shader table. Because a shader is applied to each ray individually, they cannot be used to modify the adjacent pixels.


Creating a Shader


Shader Shader.new(function shaderFunction, table attributes?)

Creates a new Shader. shaderFunction is the function to call when the shader is applied. attributes is a table of attributes to pass to the shader.


Properties


function Function

The function to call when the shader is applied.

Note

The shader function is expected to return an Out dictionary. If the shader function returns nothing, the ray will keep its current output.

For backwards compatibility, the shader can also return a Color3. Note that TracedRay.Color is deprecated and will be removed in a future version.


table Attributes

Optional user-defined attributes to pass to the shader.


Methods


dict Process(TracedRay tracedRay, RaycastResult raycastResult, ...)

Process the shader on the ray. tracedRay is the ray that is being processed. raycastResult is the result of the raycast. ... is any additional arguments passed to the shader. The dictionary returned by the shader is expected to match the format of Ray.Out.

Warning

Ray.Out should never contain values for non-existent buffers.


TracedRay Continue(TracedRay tracedRay, RaycastResult raycastResult)

Continues the ray after it has been processed by the shader. tracedRay is the ray that is being processed. raycastResult is the result of the raycast.

Note

This method will continue the ray in the same direction as the ray's previous direction. For more fine control over how the ray is continued, create a new TracedRay manually in a shader.


TracedRay Reflect(TracedRay tracedRay, RaycastResult raycastResult, Vector3 normal)

Reflects the ray. tracedRay is the ray that is being processed. raycastResult is the result of the raycast. normal is the normal of the surface the ray hit.


TracedRay Refract(TracedRay tracedRay, RaycastResult raycastResult, Vector3 normal, number indexOfRefraction)])

Refracts the ray. tracedRay is the ray that is being processed. raycastResult is the result of the raycast. normal is the normal of the surface the ray hit. indexOfRefraction is the index of refraction of the material the ray hit.


Vector3, float SimpleIntersect(Vector3 origin, Vector3 direction, Instance object)

Calculates the intersection end point and length of a ray through an object based on its outermost geometry. origin is the origin of the ray. direction is the direction of the ray.


Vector3, float ComplexIntersect(Vector3 origin, Vector3 direction, Instance object)

Calculates the first intersection end point and length of a ray through an object based on its true geometry. origin is the origin of the ray. direction is the direction of the ray. This method is more accurate than SimpleIntersect but is slower.


Vector3 Color3ToVector3(Color3 color)

Converts a Color3 to a Vector3 to make math operations easier.

Important

Shaders are expected to return a Color3. If you convert a Color3 to a Vector3, remember to convert it back to a Color3 when returning a value.


Color3 Vector3ToColor3(Vector3 vector, boolean clamp? [=false])

Converts a Vector3 to a Color3. If clamp is true, the vector will be clamped to the range [0, 1].


Color3 NumberToColor3(number number)

Converts a number to a greyscale Color3.