• Stars
    star
    1,168
  • Rank 40,005 (Top 0.8 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 7 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Navigation mesh utilities for three.js, based on PatrolJS.

three-pathfinding

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

  • zoneID string
  • position 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 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

Thanks to

More Repositories

1

three-gltf-viewer

Drag-and-drop preview for glTF 2.0 models in WebGL using three.js.
JavaScript
2,127
star
2

glTF-Transform

glTF 2.0 SDK for JavaScript and TypeScript, on Web and Node.js.
TypeScript
1,318
star
3

three-to-cannon

Convert a THREE.Mesh to a CANNON.Shape.
JavaScript
334
star
4

expression-eval

JavaScript expression parsing and evaluation.
TypeScript
198
star
5

msdf-bmfont-web

Web tool for creation of MSDF bitmap font spritesheets and JSON
JavaScript
143
star
6

simple-dropzone

A simple multi-file drag-and-drop input using vanilla JavaScript.
JavaScript
68
star
7

aframe-gamepad-controls

🎮 Gamepad controls for A-Frame VR.
JavaScript
67
star
8

glTF-Procedural-Trees

Create procedural glTF 2.0 trees with proctree.js and three.js.
JavaScript
66
star
9

aframe-inspector-plugin-recast

A-Frame Inspector plugin for creating navigation meshes.
JavaScript
64
star
10

glTF-Report-Feedback

Issues, feedback, and feature requests for the https://gltf.report website.
58
star
11

zstddec-wasm

ZSTD (Zstandard) decoder for Web and Node.js, using WebAssembly.
TypeScript
58
star
12

glTF-Transform-View

Syncs a glTF-Transform Document with a three.js scene graph.
TypeScript
49
star
13

aframe-proxy-controls

A-Frame component to proxy keyboard/gamepad controls between devices over WebRTC.
JavaScript
47
star
14

KTX-Parse

KTX 2.0 (.ktx2) parser and serializer.
TypeScript
47
star
15

three-filmic

Film emulsion-like rendering transforms for three.js
TypeScript
40
star
16

proxy-controls-server

Send keyboard/gamepad controls between devices, peer-to-peer, over WebRTC.
JavaScript
36
star
17

mikktspace-wasm

MikkTSpace vertex tangent calculation for JavaScript/TypeScript/Node.js, using Web Assembly.
JavaScript
32
star
18

tweakpane-plugin-thumbnail-list

Image thumbnail list plugin for Tweakpane
TypeScript
30
star
19

three-shadenodeloader

[EXPERIMENTAL] three.js loader for shaders created with Shade app for iOS
Lua
29
star
20

hex2dec

Arbitrary precision decimal↔️hexadecimal converter.
JavaScript
27
star
21

input-tokenizer

jQuery plugin that allows a user to type keywords, which will be broken up into tokens/tags and displayed, similarly to tagging a post on Tumblr or Stack-Overflow.
JavaScript
25
star
22

ndarray-pixels

Convert ndarray ↔ image data, for Web and Node.js.
TypeScript
24
star
23

aframe-keyboard-controls

⌨️ Keyboard controls and input events for A-Frame VR.
JavaScript
23
star
24

glTF-Browser-Extension

Adds preview button for glTF 2.0 models on GitHub.
JavaScript
16
star
25

webvr-experiments

JavaScript
16
star
26

property-graph

Base for creating objects that behave like a Property Graph
TypeScript
14
star
27

aframe-obj-loader-component

[Deprecated] A-Frame loader component for .OBJ models and .MTL materials.
11
star
28

three-color-grading

TypeScript
10
star
29

keyframe-resample-wasm

Resamples and optimizes keyframe data using WebAssembly.
TypeScript
10
star
30

greendoc

🌿 An adaptable system for generating documentation of TypeScript and JavaScript APIs.
TypeScript
9
star
31

KTX2-Samples

Collection of KTX2 sample textures, for testing and debugging.
Shell
8
star
32

typescript-library-template

Personal template for new TypeScript libraries.
JavaScript
8
star
33

github-contributions-scraper

Scrapes the GitHub contributions graph SVG and outputs JSON.
JavaScript
8
star
34

assemblyscript-library-template

Personal template for new AssemblyScript libraries.
TypeScript
6
star
35

ndarray-lanczos

Resize an ndarray with Lanczos resampling
TypeScript
5
star
36

circular-array

Simple circular array data structure, for storing a finite-length list of values
JavaScript
4
star
37

fp64-arithmetic

Testing FP64 arithmetic in GLSL on Apple GPUs.
JavaScript
4
star
38

blocks-sandbox

TypeScript
3
star
39

watch-exec

Runs a specified command when any files in the target directory change.
JavaScript
3
star
40

fbx2gltf-api

Dockerized API endpoint for FBX2glTF
JavaScript
2
star
41

THREE.MapControls

(DEPRECATED) THREE.js camera controls suitable for flat scenes — panning, zooming, and limited rotation.
JavaScript
2
star
42

2024-06-20_vite_lib_ts

Minimal reproduction of Vite library mode issue
TypeScript
2
star
43

glTF-Transform-View-Starter

TypeScript
2
star
44

20241010-stackblitz-dotfiles

Minimal reproduction for initializing Stackblitz with dotfiles
JavaScript
1
star
45

stack-overflow-feed-bot

Posts links in Slack for each new Stack Overflow question with a given tag.
JavaScript
1
star