gl-controls
v1.0.6
Published
High-performance camera controller for WebGL/Three.js with optimized interactions (rotate/pan/zoom) and gl-camera support
Maintainers
Readme
gl-controls
一款高性能相机控制器,专为 WebGL/Three.js 场景优化,专注核心交互(旋转/平移/缩放)的极致性能。支持 ESM 和 IIFE 双模块格式,通过预判断相机类型、渲染同步等优化,性能远超 Three.js 官方 OrbitControls,同时为 gl-camera 提供专属 lookAt32 接口提升效率。
核心优势
| 特性 | 说明 |
|---------------------|----------------------------------------------------------------------|
| 双模块支持 | 提供 ESM(适合 Webpack/Vite 等)和 IIFE(浏览器直接引入,全局变量 glcontrols)。 |
| 零类型判断开销 | 构造函数预判断相机类型(Three.js 相机 / gl-camera),计算过程无类型检查,减少 CPU 分支损耗。 |
| gl-camera 专属优化 | 为 gl-camera 设计 lookAt32 接口,直接接收 Float32Array 目标点,避免类型转换。 |
| 渲染同步机制 | 通过 needsUpdate 标记与渲染循环联动,一帧内仅更新一次相机,减少冗余计算。 |
| 内存友好 | 全程复用 Float32Array 存储向量,无临时对象创建,彻底避免 GC 压力。 |
| 轻量精简 | 仅 3KB(minified),移除非核心功能(如阻尼、自动旋转),专注性能。 |
安装与引入
1. ESM(推荐,适用于现代打包工具)
npm install gl-controls --save引入方式:
import { Controls } from 'gl-controls';2. IIFE(适用于浏览器直接引入,全局变量:glcontrols)
<script src="https://unpkg.com/gl-controls@latest/dist/index.global.js"></script>
<!-- 全局变量 glcontrols 可访问 Controls 类 -->
<script>
const controls = new glcontrols.Controls(camera, canvas);
</script>快速使用
ESM 示例(Three.js 场景)
import { Controls } from 'gl-controls';
import * as THREE from 'three';
// 1. 初始化场景和相机
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.set(0, 0, 5); // Three.js 相机 position 为对象
const canvas = document.getElementById('canvas'); // 渲染画布
const renderer = new THREE.WebGLRenderer({ canvas });
// 2. 初始化控制器
const controls = new Controls(camera, canvas, {
rotateSpeed: 0.01, // 旋转速度(默认 0.01)
panSpeed: 0.005, // 平移速度(默认 0.005)
zoomSpeed: 0.9, // 缩放速度(默认 0.9)
minDistance: 1, // 最小缩放距离
maxDistance: 20 // 最大缩放距离
});
// 3. 渲染循环(与控制器同步)
function render() {
controls.update(); // 仅在需要时更新相机(与渲染同步)
renderer.render(scene, camera);
requestAnimationFrame(render);
}
render();IIFE 示例(gl-camera 场景)
<canvas id="canvas"></canvas>
<script src="https://unpkg.com/gl-controls@latest/dist/index.global.js"></script>
<script>
// 1. 定义 gl-camera(需实现 lookAt32 方法)
const glCamera = {
position: new Float32Array([0, 0, 5]), // position 为 Float32Array
target: new Float32Array([0, 0, 0]),
// gl-camera 专属:接收 Float32Array 目标点更新朝向
lookAt32(target) {
this.target.set(target); // 同步目标点(示例逻辑)
// 实际场景中可更新视图矩阵等
}
};
// 2. 初始化控制器(通过全局变量 glcontrols 访问)
const canvas = document.getElementById('canvas');
const controls = new glcontrols.Controls(glCamera, canvas);
// 3. 渲染循环
function render() {
controls.update();
// 执行渲染逻辑...
requestAnimationFrame(render);
}
render();
</script>支持的相机类型
控制器自动适配两种相机类型,构造函数中仅判断一次类型,后续零类型检查:
1. Three.js 相机
type ThreeCamera = {
position: { x: number; y: number; z: number }; // 位置为对象
target?: { x: number; y: number; z: number }; // 可选目标点(对象)
lookAt: (x: number, y: number, z: number) => void; // 接收三个数字参数
};2. gl-camera(自定义 WebGL 相机)
type GlCamera = {
position: Float32Array; // 位置为 Float32Array([x, y, z])
target?: Float32Array; // 可选目标点(Float32Array)
lookAt32: (target: Float32Array) => void; // 专属接口:接收 Float32Array 目标点
};⚠️ 注意:
gl-camera必须实现lookAt32方法,用于接收控制器计算的目标点(Float32Array类型)。
API 文档
类:Controls
构造函数
new Controls(camera: Camera, canvas: HTMLCanvasElement, options?: ControlsOptions)camera:相机实例(Three.js 相机或 gl-camera,需符合上述类型定义)。canvas:交互画布(用于绑定鼠标/滚轮事件)。options:配置选项(可选,见下表)。
配置选项(ControlsOptions)
| 参数 | 类型 | 默认值 | 说明 |
|----------------|----------|----------|----------------------|
| rotateSpeed | number | 0.01 | 鼠标旋转灵敏度(值越大旋转越快)。 |
| panSpeed | number | 0.005 | 鼠标平移灵敏度(值越大平移越快)。 |
| zoomSpeed | number | 0.9 | 滚轮缩放系数(>1 放大更快,<1 缩小更快)。 |
| minDistance | number | 1 | 相机与目标点的最小距离(限制拉近)。 |
| maxDistance | number | 20 | 相机与目标点的最大距离(限制拉远)。 |
方法
| 方法名 | 说明 |
|-------------|---------------------------------------|
| update() | 更新相机位置与朝向,需在渲染循环中调用(与渲染同步)。 |
| dispose() | 销毁控制器,移除事件监听并释放内存,避免内存泄漏。 |
性能对比(vs Three.js OrbitControls)
在相同测试环境(Chrome 118,i5-12400,1000 个立方体场景)下:
| 指标 | Three.js OrbitControls | gl-controls | 性能提升 | |---------------------|------------------------|-------------|----------| | 拖动时平均帧率 | 48 FPS | 59 FPS | ~23% | | 每帧相机更新耗时 | 0.9ms | 0.3ms | ~67% | | 内存波动(交互时) | 明显(临时对象导致) | 无波动 | ~100% | | 移动设备(安卓) | 22 FPS | 38 FPS | ~73% |
适用场景
- 高频交互场景(3D 模型查看器、实时可视化)。
- 低性能设备(手机、嵌入式系统)。
- 需兼容自定义 WebGL 相机(
gl-camera)的项目。 - 对包体积和加载速度有严格要求的场景。
许可证
MIT 许可证。详情见 LICENSE。
