glsl-sdf-sphere

1.0.0 • Public • Published

glsl-sdf-sphere

stable

GLSL SDF function for generating a sphere. Determines the distance between a point and the surface of a sphere at the origin.

view demo

Usage

NPM

float sphere(vec3 position, float radius)

Returns the signed distanced between position and a sphere at the origin with a given radius. For example, to draw a sphere in a raytracing shader:

#pragma glslify: sphere require('glsl-sdf-sphere') 
 
vec2 doModel(vec3 p) {
  float id     = 1.0;
  float radius = 0.5;
  float dist   = sphere(p, radius);
 
  return vec2(dist, id);
}

Note that you can move the sphere around in this case by offsetting the position vector, for example:

uniform float iGlobalTime;
 
#pragma glslify: sphere require('glsl-sdf-sphere') 
 
vec2 doModel(vec3 p) {
  vec3 offset = vec3(sin(iGlobalTime), 00);
 
  float id     = 1.0;
  float radius = 0.5;
  float dist   = sphere(p - offset, radius);
 
  return vec2(dist, id);
}

Contributing

See stackgl/contributing for details.

License

MIT. See LICENSE.md for details.

Dependencies (0)

    Dev Dependencies (11)

    Package Sidebar

    Install

    npm i glsl-sdf-sphere

    Weekly Downloads

    1

    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
    • dy