AABB
AlignmentBehavior
ArriveBehavior
AStar
BFS
BoundingSphere
BVH
BVHNode
Cell
CellSpacePartitioning
CohesionBehavior
CompositeGoal
ConvexHull
Corridor
CostTable
DFS
Dijkstra
Edge
EntityManager
EvadeBehavior
EventDispatcher
Behavior
FollowPathBehavior
FuzzyAND
FuzzyCompositeTerm
FuzzyFAIRLY
FuzzyModule
FuzzyOR
FuzzyRule
FuzzySet
FuzzyTerm
FuzzyVariable
FuzzyVERY
GameEntity
Goal
GoalEvaluator
Graph
GraphUtils
HalfEdge
HeuristicPolicyDijkstra
HeuristicPolicyEuclid
HeuristicPolicyEuclidSquared
HeuristicPolicyManhattan
InterposeBehavior
LeftSCurveFuzzySet
LeftShoulderFuzzySet
LineSegment
Logger
MathUtils
Matrix3
Matrix4
MemoryRecord
MemorySystem
MeshGeometry
MessageDispatcher
MovingEntity
NavEdge
NavMesh
NavMeshLoader
NavNode
Node
NormalDistFuzzySet
OBB
ObstacleAvoidanceBehavior
OffsetPursuitBehavior
OnPathBehavior
Path
Plane
Polygon
Polyhedron
PriorityQueue
PursuitBehavior
Quaternion
Ray
RectangleTriggerRegion
Regular
RightSCurveFuzzySet
RightShoulderFuzzySet
SAT
SeekBehavior
SeparationBehavior
SingletonFuzzySet
Smoother
SphericalTriggerRegion
State
StateMachine
SteeringBehavior
SteeringManager
Task
TaskQueue
Telegram
Think
Time
TriangularFuzzySet
Trigger
TriggerRegion
Vector3
Vehicle
Version
WanderBehavior

computeBoundingVolume

描述

computeBoundingVolume是MeshGeometry类的方法,用于计算几何体的包围盒。

语法

MeshGeometry.prototype.computeBoundingVolume()

参数

该方法没有参数。

返回值

该方法没有返回值,但是会修改MeshGeometry对象的boundingVolume属性。

使用方法

const mesh = new MeshGeometry(vertices, indices);
mesh.computeBoundingVolume();

示例

import { MeshGeometry } from 'yuka';

const meshData = {
  vertices: [
    // vertex positions
  ],
  indices: [
    // indices to form triangles
  ]
};
const mesh = new MeshGeometry(meshData.vertices, meshData.indices);
mesh.computeBoundingVolume();
console.log(mesh.boundingVolume);

实现细节

计算包围盒需要遍历所有的顶点,获取其中的最大坐标和最小坐标,最后根据这两个坐标建立一个包围盒。具体实现可以参考以下代码片段:

computeBoundingVolume() {
  const positions = this.attributes.position.array;
  const positionStride = this.attributes.position.stride
  const verticesCount = positions.length / positionStride;

  const vec3 = new Vector3();
  const boundingBox = new Box3();

  for (let i = 0; i < verticesCount; i++) {
    vec3.fromArray(positions, i * positionStride);
    boundingBox.expandByPoint(vec3);
  }

  this.boundingVolume.fromBox3(boundingBox);
}

以上代码首先获取网格几何体的顶点坐标,利用Vector3将其转化为向量,然后通过Box3获取当前所有向量的最大坐标和最小坐标,并最终将其构建为包围盒。

注意事项

  • 该方法需要在构造模型之后调用。
  • 如果模型的顶点数量较大,则计算过程可能较慢,需要注意性能。