spatialIndex是Yuka js库中用于NavMesh(导航网格)的空间索引模块。它可以对NavMesh中的三角形建立空间索引,提供快速的区域查询和碰撞检测功能。
import { NavMesh, NavMeshBuilder, spatialIndex } from 'yuka';
const navMesh = new NavMesh();
const builder = new NavMeshBuilder( navMesh );
builder.fromTriangles( triangles );
// 构建空间索引
navMesh.spatialIndex = new spatialIndex( navMesh );
// 获取与包围球相交的所有三角形
const spheres = [{
center: new Vector3( 0, 0, 0 ),
radius: 1
}];
const result = navMesh.spatialIndex.collisionTest( spheres );
// 获取射线相交的第一个三角形的信息
const ray = new Ray( new Vector3( -10, 0, 0 ), new Vector3( 1, 0, 0 ) );
const result = navMesh.spatialIndex.raycast( ray );
new spatialIndex( navMesh: NavMesh )navMesh - 类型:NavMesh,导航网格对象。update()更新空间索引。
collisionTest( spheres: Array<Object> )查询与包围球数组中任意一个包围球相交的所有三角形。
spheres - 类型:Array<Object>,包含多个包围球参数的数组:
center - 类型:Vector3,包围球中心坐标。radius - 类型:Number,包围球半径。Array<CollisionData>,包含每个相交三角形信息的数组。raycast( ray: Ray )查询与射线相交的第一个三角形。
ray - 类型:Ray,射线对象。CollisionData,相交三角形的信息。