Skip to content

Latest commit

 

History

History
60 lines (42 loc) · 1.75 KB

README.md

File metadata and controls

60 lines (42 loc) · 1.75 KB

fast-voxel-raycast

Fast raycasting through voxels.

This is a fork of voxel-raycast, using the faster, simpler algorithm described in this paper by Amanatides and Woo.

In general it functions equivalently to the parent library, except for edge cases (e.g. when the raycast precisely touches the corner of a solid voxel), in which case no particular behavior is defined.

For 2D raycasting, also see noffle's wrapped implementation of this module.

Installation

npm install fast-voxel-raycast

Usage

var raycast = require('fast-voxel-raycast')

raycast( getVoxel, start, direction, distance, hit_position, hit_normal )
  • getVoxel - a function(x,y,z) that returns a truthy value for whether each voxel should block the raycast
  • start - origin of the ray
  • direction - direction of the ray
  • distance - how far to check the ray before giving up
  • hit_position - result array, gets populated with the point of impact
  • hit_normal - gets populated with a normal vector pointing away from the voxel that was struck

Returns: whatever value was returned by the getVoxel function for the struck voxel, or 0 if no voxel was struck.

Example

var raycast = require('fast-voxel-raycast')

var getVoxel = function(x,y,z) {
    // return a truthy value here for voxels that block the raycast
    return (y<0) ? 1 : 0
}

var hit_position = []
var hit_normal = []

var result = raycast( getVoxel, [3,4,5], [1,-1,-1], 20, hit_position, hit_normal )

if (result != 0) {
    console.log('hit:', result, hit_position, hit_normal)
} else {
    console.log('no truthy voxel was struck')
}