Given a signed distance function and ray, trace a scene to find the first point of intersection.
raytrace = require(glsl-raytrace, <map>, <steps>)
Loads the raytrace function into your shader. Note that
steps are required to be defined when using this module.
vec2 map(vec3 position)
Your signed distance function, responsible for defining the
solid shapes in your scene. Accepts
position, and returns
- The signed distance from the surface, i.e. how far from the
surface that position is. If inside the surface, this
number should be negative. If a collision isn't made, this
number should be
-1.0. Fortunately there's a bunch of easily composable primitives so this isn't as scary as it sounds once you're familiar with it.
- A second attribute for assigning a property to the surface. Often, this is used as a material ID to apply different effects to different surfaces, but it could really be anything.
The maximum number of steps to attempt for each trace.
vec2# raytrace = require('glsl-raytrace', map = doModel, steps = 90)vec2
vec2 raytrace(vec3 ro, vec3 rd)
Once set up, you can then use the
raytrace function to
trace a ray and get how far it travels before making a
collision. You can then use this value to determine the point
of the collision, the surface normal and lighting conditions,
rois the ray origin.
rdis a unit vector (i.e. normalized) representing the ray direction.
# square = require('glsl-square-frame')# camera = require('glsl-camera-ray')# raytrace = require('glsl-raytrace')uniform vec2 iResolution;uniform float iGlobalTime;void
raytrace(vec3 ro, vec3 rd, float maxd, float precis)
For more control, you may optionally include:
float maxd: the maxium distance to trace. Defaults to 20.
float precis: the minimum closeness to the surface before considering the trace to be a collision. Defaults to 0.001. Increasing this number will improve performance.
See stackgl/contributing for details.
MIT. See LICENSE.md for details.