JSPM

glsl-ray-sphere

0.0.1
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 2
  • Score
    100M100P100Q24633F
  • License MIT

Ray-sphere intersection in GLSL bypassing precision issues

Package Exports

    This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (glsl-ray-sphere) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

    Readme

    glsl-ray-sphere frozen

    GLSL Ray-Sphere intersection bypassing precision issues

    Many ray-sphere intersection implementations suffer from precision issues when the sphere is too small or the ray is too close, as described in this paper:

    "Precision Improvements for Ray/Sphere Intersection" By: Eric Haines, Johannes Gunther, and Tomas Akenine-Moller https://link.springer.com/content/pdf/10.1007/978-1-4842-4427-2_7.pdf

    This implementation uses the algorithm that bypasses those issues.

    Intended for use with glslify.

    Usage

    // Add these lines:
    #pragma glslify: Intersection = require(glsl-ray-sphere/intersection)
    #pragma glslify: sphereIntersection = require(glsl-ray-sphere/rsi, Intersection=Intersection)
    
    uniform vec3 ray; // normalized
    uniform vec3 rayOrigin; // relative to the sphere
    uniform float sphereRadius;
    
    // Use it:
    void main() {
      Intersection inter = sphereIntersection(rayOrigin, ray, sphereRadius);
    
      // number of hits in the forward direction:
      int numberOfHitsInFront = inter.hits;
      // is the ray inside?
      bool isInside = inter.isInside;
      // distances from ray origin are t0 and t1
      // so get the points by:
      vec3 first = rayOrigin + ray * inter.t0;
      vec3 second = rayOrigin + ray * inter.t1;
    }