npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

three-pathfinding

v1.3.0

Published

Navigation mesh toolkit for three.js, based on PatrolJS

Downloads

38,922

Readme

three-pathfinding

Latest NPM release npm bundle size License Build Status

Navigation mesh toolkit for ThreeJS, based on PatrolJS. Computes paths between points on a 3D nav mesh, supports multiple zones, and clamps movement vectors for FPS controls. To learn how to create a navigation mesh using Blender, see Creating a Nav Mesh.

Thanks to Nick Janssen for creating PatrolJS, which was the basis for this library.

screenshot

Introduction

Traditionally games and 3D apps used waypoints to help their AI agents navigate. This is bad and has a lot of problems, but is generally easier to implement than navigation meshes. Navmeshes are far more accurate, faster, and take into account the size of the AI agent (e.g. tanks require move space to maneuver than soldiers).

For a thorough introduction to Navigation mesh pathfinding, see AI Blog's article, Fixing Pathfinding Once and For All.

Quickstart

Installation

npm install --save three-pathfinding

Creating a Navigation Mesh

This library does not build navigation meshes for you — instead, create a navigation mesh using Blender, Recast (CLI), or another tool.

Currently, this library does not accept the custom navigation mesh file formats created by tools like Recast. Instead, you will need to export the navigation mesh to a 3D model format (like OBJ or glTF) and then load it with one of the three.js loaders, like THREE.OBJLoader or THREE.GLTFLoader. The library accepts a THREE.BufferGeometry instance, and follows the +Y=Up convention of three.js and glTF.

Example

Loading a mesh from a .gltf file:

// For ES6, see: https://github.com/mrdoob/three.js/issues/9562
// CommonJS
const THREE = window.THREE = require('three');
require('three/examples/js/loaders/GLTFLoader.js');

let navmesh;

const loader = new THREE.GLTFLoader();
loader.load( 'navmesh.gltf', ({scene}) => {
    scene.traverse((node) => {
        if (node.isMesh) navmesh = node;
    });
}, undefined, (e) => {
    console.error(e);
});

Initializing the library, creating a level, and finding a path:

// ES6
import { Pathfinding } from 'three-pathfinding';
// CommonJS
const { Pathfinding } = require('three-pathfinding');
// UMD
const Pathfinding = window.threePathfinding.Pathfinding;

// Create level.
const pathfinding = new Pathfinding();
const ZONE = 'level1';
pathfinding.setZoneData(ZONE, Pathfinding.createZone(navmesh.geometry));

// Find path from A to B.
const groupID = pathfinding.getGroup(ZONE, a);
const path = pathfinding.findPath(a, b, ZONE, groupID);

The origin of an agent should initially be placed on the surface of the nav mesh. If needed, a dummy object can be used for pathfinding logic, and the rendered model for that agent may be placed at on offset as needed.

Running the demo locally

git clone https://github.com/donmccurdy/three-pathfinding.git
cd three-pathfinding

npm install
npm run dev

The demo will start at http://localhost:3000/.

API

Table of Contents

Pathfinding

Defines an instance of the pathfinding module, with one or more zones.

setZoneData

Sets data for the given zone.

Parameters

getRandomNode

Returns a random node within a given range of a given position.

Parameters

Returns Node

getClosestNode

Returns the closest node to the target position.

Parameters

Returns Node

findPath

Returns a path between given start and end points. If a complete path cannot be found, will return the nearest endpoint available.

Parameters

  • startPosition Vector3 Start position.
  • targetPosition Vector3 Destination.
  • zoneID string ID of current zone.
  • groupID number Current group ID.

Returns Array<Vector3> Array of points defining the path.

getGroup

Returns closest node group ID for given position.

Parameters

Returns number

clampStep

Clamps a step along the navmesh, given start and desired endpoint. May be used to constrain first-person / WASD controls.

Parameters

  • start Vector3
  • end Vector3 Desired endpoint.
  • node Node
  • zoneID string
  • groupID number
  • endTarget Vector3 Updated endpoint.

Returns Node Updated node.

createZone

(Static) Builds a zone/node set from navigation mesh geometry.

Parameters

  • geometry BufferGeometry
  • tolerance number Vertex welding tolerance. (optional, default 1e-4)

Returns Zone

PathfindingHelper

Extends Object3D

Helper for debugging pathfinding behavior.

setPath

Parameters

Returns this

setPlayerPosition

Parameters

  • position Vector3

Returns this

setTargetPosition

Parameters

  • position Vector3

Returns this

setNodePosition

Parameters

  • position Vector3

Returns this

setStepPosition

Parameters

  • position Vector3

Returns this

reset

Hides all markers.

Returns this

Zone

Defines a zone of interconnected groups on a navigation mesh.

Type: Object

Properties

Group

Defines a group within a navigation mesh.

Type: Object

Node

Defines a node (or polygon) within a group.

Type: Object

Properties