glsl-camera-ray

1.0.0 • Public • Published

glsl-camera-ray

stable

Generates a ray for Shadertoy-style raycasting in GLSL. Accepts either either a camera origin/target or an arbitrary mat3 matrix.

Usage

NPM

vec3 cameraRay(vec3 ro, vec3 ta, vec2 screen, float lens)

  • vec3 ro is the position of the camera.
  • vec3 ta is the position the camera is pointing towards.
  • vec2 screen is the position of the fragment on the screen, generally between -1 and 1. For non-square frames you'll want to normalize this using something like glsl-square-frame
  • lens is the lens length of the camera. This works similarly to FOV, where 0.0 is horribly wide and 2.0 is a decent default.
#pragma glslify: square require('glsl-square-frame') 
#pragma glslify: camera require('glsl-camera-ray') 
 
uniform vec2  iResolution;
uniform float iGlobalTime;
 
void main() {
  // Bootstrap a Shadertoy-style raytracing scene: 
  float cameraAngle  = 0.8 * iGlobalTime;
  vec3  rayOrigin    = vec3(3.5 * sin(cameraAngle), 3.03.5 * cos(cameraAngle));
  vec3  rayTarget    = vec3(000);
  vec2  screenPos    = square(iResolution.xy);
  float lensLength   = 2.0;
 
  vec3  rayDirection = camera(rayOrigin, rayTarget, screenPos, lensLength);
  // ... 
}

vec3 cameraRay(mat3 camera, vec2 screen, float lens)

For more flexibility, you can supply mat3 camera in place of vec3 ro, vec3 ta. This way, you can use your own camera modules alongside glsl-camera-ray.

Contributing

See stackgl/contributing for details.

License

MIT. See LICENSE.md for details.

Readme

Keywords

Package Sidebar

Install

npm i glsl-camera-ray

Weekly Downloads

3

Version

1.0.0

License

MIT

Last publish

Collaborators

  • hughsk
  • mikolalysenko
  • mattdesl
  • chrisdickinson
  • yoshuawuyts
  • mikkoh
  • rezaali
  • tatumcreative
  • wwwtyro
  • thibauts
  • bpostlethwaite
  • dfcreative
  • erkaman
  • gre
  • rreusser
  • vorg
  • archmoj