全局
MeshBVH
SerializedBVH
MeshBVHVisualizer
ExtendedTriangle
OrientedBox
Raycaster
StaticGeometryGenerator
GenerateMeshBVHWorker

StaticGeometryGenerator

简介

StaticGeometryGenerator是three-bvh-mesh库中的一个类,用于将模型网格(mesh)转换为静态几何体(StaticGeometry)。几何体是Three.js渲染引擎中的一种基本对象,表示场景中三维物体的形状和位置。而BVH(Bounding Volume Hierarchy)是一种快速的空间分割算法,可以用于加速碰撞检测和光线追踪等程序。

构造函数

StaticGeometryGenerator的构造函数是:

constructor(mesh: THREE.BufferGeometry, material?: THREE.Material)

其中,mesh参数是要转换的模型网格,必须是THREE.BufferGeometry类型;material参数是生成的几何体使用的材质(可选)。

方法

StaticGeometryGenerator类提供了以下方法:

generate()

将模型网格转换为静态几何体。

代码示例:

const generator = new StaticGeometryGenerator(mesh, material);
const geometry = generator.generate();

getBoundingBox()

获取几何体的边界框(BoundingBox)。

代码示例:

const bbox = generator.getBoundingBox();

getBoundingSphere()

获取几何体的包围球(BoundingSphere)。

代码示例:

const sphere = generator.getBoundingSphere();

getPartitioning()

获取BVH算法生成的空间分割层次结构。

代码示例:

const partitioning = generator.getPartitioning();

示例

import * as THREE from 'three';
import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader.js';
import { StaticGeometryGenerator } from 'three-bvh-mesh';

const loader = new GLTFLoader();
loader.load('my-model.glb', (gltf) => {
  const mesh = gltf.scene.children[0].geometry;
  const material = new THREE.MeshStandardMaterial();
  const generator = new StaticGeometryGenerator(mesh, material);
  const geometry = generator.generate();
  const mesh = new THREE.Mesh(geometry, material);
  scene.add(mesh);
});

参考文献

  • GitHub仓库:https://github.com/gkjohnson/three-bvh-mesh
  • Three.js文档:https://threejs.org/docs/index.html#api/en/core/Geometry
  • BVH算法介绍:https://en.wikipedia.org/wiki/Bounding_volume_hierarchy