Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

three-pathfinding

Package Overview
Dependencies
Maintainers
1
Versions
30
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

three-pathfinding

Navigation mesh toolkit for three.js, based on PatrolJS

  • 0.9.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
9.8K
increased by17.78%
Maintainers
1
Weekly downloads
 
Created
Source

three-pathfinding

Latest NPM release 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.

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);

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:9966/demo/.

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

  • position THREE.Vector3
  • zoneID string
  • groupID number
  • checkPolygon boolean (optional, default false)

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 THREE.Vector3 Start position.
  • targetPosition THREE.Vector3 Destination.
  • zoneID string ID of current zone.
  • groupID number Current group ID.

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

getGroup

Returns closest node group ID for given position.

Parameters

  • zoneID string
  • position THREE.Vector3

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 THREE.Vector3
  • end THREE.Vector3 Desired endpoint.
  • node Node
  • zoneID string
  • groupID number
  • endTarget THREE.Vector3 Updated endpoint.

Returns Node Updated node.

createZone

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

Parameters

  • geometry THREE.BufferGeometry

Returns Zone

PathfindingHelper

Extends THREE.Object3D

Helper for debugging pathfinding behavior.

setPath

Parameters

  • path

Returns this

setPlayerPosition

Parameters

  • position THREE.Vector3

Returns this

setTargetPosition

Parameters

  • position THREE.Vector3

Returns this

setNodePosition

Parameters

  • position THREE.Vector3

Returns this

setStepPosition

Parameters

  • position THREE.Vector3

Returns this

reset

Hides all markers.

Returns this

Zone

Defines a zone of interconnected groups on a navigation mesh.

Properties

Group

Defines a group within a navigation mesh.

Node

Defines a node (or polygon) within a group.

Properties

Thanks to

Keywords

FAQs

Package last updated on 21 Oct 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc