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.
npm install fast-voxel-raycast
var raycast = require('fast-voxel-raycast')
raycast( getVoxel, start, direction, distance, hit_position, hit_normal )
getVoxel
- afunction(x,y,z)
that returns a truthy value for whether each voxel should block the raycaststart
- origin of the raydirection
- direction of the raydistance
- how far to check the ray before giving uphit_position
- result array, gets populated with the point of impacthit_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.
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')
}