Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

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

License

NotificationsYou must be signed in to change notification settings

donmccurdy/three-pathfinding

Repository files navigation

Latest NPM releasenpm bundle sizeLicenseBuild Status

Navigation mesh toolkit for ThreeJS, based onPatrolJS. 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, seeCreating a Nav Mesh.

Thanks toNick Janssen for creatingPatrolJS, 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 usingBlender,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 itwith one of the three.js loaders, like THREE.OBJLoader or THREE.GLTFLoader. The library accepts aTHREE.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// CommonJSconstTHREE=window.THREE=require('three');require('three/examples/js/loaders/GLTFLoader.js');letnavmesh;constloader=newTHREE.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:

// ES6import{Pathfinding}from'three-pathfinding';// CommonJSconst{ Pathfinding}=require('three-pathfinding');// UMDconstPathfinding=window.threePathfinding.Pathfinding;// Create level.constpathfinding=newPathfinding();constZONE='level1';pathfinding.setZoneData(ZONE,Pathfinding.createZone(navmesh.geometry));// Find path from A to B.constgroupID=pathfinding.getGroup(ZONE,a);constpath=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.gitcd three-pathfindingnpm installnpm run dev

The demo will start athttp://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

ReturnsNode

getClosestNode

Returns the closest node to the target position.

Parameters

ReturnsNode

findPath

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

Parameters

  • startPositionVector3 Start position.
  • targetPositionVector3 Destination.
  • zoneIDstring ID of current zone.
  • groupIDnumber Current group ID.

ReturnsArray<Vector3> Array of points defining the path.

getGroup

Returns closest node group ID for given position.

Parameters

Returnsnumber

clampStep

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

Parameters

  • startVector3
  • endVector3 Desired endpoint.
  • nodeNode
  • zoneIDstring
  • groupIDnumber
  • endTargetVector3 Updated endpoint.

ReturnsNode Updated node.

createZone

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

Parameters

  • geometryBufferGeometry
  • tolerancenumber Vertex welding tolerance. (optional, default1e-4)

ReturnsZone

PathfindingHelper

Extends Object3D

Helper for debugging pathfinding behavior.

setPath

Parameters

Returnsthis

setPlayerPosition

Parameters

  • positionVector3

Returnsthis

setTargetPosition

Parameters

  • positionVector3

Returnsthis

setNodePosition

Parameters

  • positionVector3

Returnsthis

setStepPosition

Parameters

  • positionVector3

Returnsthis

reset

Hides all markers.

Returnsthis

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

About

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

Resources

License

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp