wgpu-kit/three
Three.js CPU-snapshot adapter, with explicit ownership and current-version limits.
Auxiliary inputs, dispatch counts, managed batches and typed kernels are released in 2.1.0. The original API retains the published npm 2.0.0 declarations and implementation as its historical audit baseline; Additions in 2.1.0 and 2.2.0 are identified on affected APIs and cite src files, without claiming a fresh audit of the entire API. Only actual public exports of this entry point are listed.
threePointsfunction
Create a Three.js Points object driven by CPU snapshots of a particle position buffer.
threePoints(
sim: ParticlesSim,
THREE: ThreeAPI,
opts?: { size?: number; color?: number },
): ThreePointsHandle;Parameters
| Name | Type | Required | Default | Meaning |
|---|---|---|---|---|
sim | ParticlesSim | Yes | — | Particle simulation; its position-buffer length determines point count. The handle dispose() also destroys this simulation. |
THREE | ThreeAPI | Yes | — | Injected Three.js constructors; no hard Three.js dependency is installed by this adapter. |
opts | { size?: number; color?: number } | No | {} | Point material options. |
opts.size | number | No | 0.015 | Passed to PointsMaterial.size with sizeAttenuation=true. |
opts.color | number | No | 0x8fb4ff | Uniform material color; this adapter does not copy per-species colors. |
Returns
ThreePointsHandle A Points handle with asynchronous update and disposal. positions initially contain zeroes until update() completes.
Behavior & constraints
- Each update reads GPU data back to CPU and marks a Three BufferAttribute dirty; it is not a zero-copy WebGPU/TSL bridge. z is fixed at 0.
- Version 2.0.0 captures sim.buffers().pos only once at construction. Particle simulation ping-pong changes the current buffer after tick(), so update() may read the previous step on alternating ticks. Do not promise every update is the newest step.
- The geometry must provide setAttribute at runtime although ThreeAPI types BufferGeometry as returning unknown. There is no automatic resize, render loop, scene removal, geometry disposal, or material disposal.
Examples
import * as THREE from 'three';
import { particles } from 'wgpu-kit/particles';
import { threePoints, type ThreeAPI } from 'wgpu-kit/three';
const sim = await particles({ count: 1024 });
const handle = threePoints(sim, THREE as unknown as ThreeAPI, { size: 0.02 });
const points = handle.points as THREE.Points;
const scene = new THREE.Scene();
scene.add(points);
await handle.update(); // Initial snapshot; no tick has changed the buffer.
// Render scene with your camera and renderer.
function dispose() {
scene.remove(points);
points.geometry.dispose();
const materials = Array.isArray(points.material)
? points.material
: [points.material];
materials.forEach((material) => material.dispose());
handle.dispose(); // Also destroys sim.
}ThreeAPIinterface
Minimal structural constructor surface injected into threePoints.
interface ThreeAPIPointsconstructor property
Construct the returned point cloud.
Points: new (geometry: unknown, material: unknown) => unknown;Parameters
| Name | Type | Required | Default | Meaning |
|---|---|---|---|---|
geometry | unknown | Yes | — | Geometry constructed below, with a position attribute. |
material | unknown | Yes | — | Constructed point material. |
Returns
unknown Usually THREE.Points; retain/cast its concrete type in the application.
BufferGeometryconstructor property
Construct a geometry; runtime must support setAttribute(name, attribute).
BufferGeometry: new () => unknown;Parameters
This call takes no arguments.
Returns
unknown Geometry object.
BufferAttributeconstructor property
Construct a position attribute backed by a Float32Array.
BufferAttribute: new (
array: Float32Array,
itemSize: number,
) => { set: (arr: ArrayLike<number>) => void; needsUpdate: boolean };Parameters
| Name | Type | Required | Default | Meaning |
|---|---|---|---|---|
array | Float32Array | Yes | — | Backing positions array of count*3 components. |
itemSize | number | Yes | — | Components per item; adapter passes 3. |
Returns
{ set: (arr: ArrayLike<number>) => void; needsUpdate: boolean } Attribute whose needsUpdate is set true after each snapshot. The declared set(arr) method is not called by this adapter.
Fields & related types
| Name | Type | Required | Default | Meaning |
|---|---|---|---|---|
set(arr) | (arr: ArrayLike<number>) => void | — | — | Required by the structural declaration; array-like numbers to copy. Unused internally. |
needsUpdate | boolean | — | — | Writable flag set true after snapshot copy. |
PointsMaterialconstructor property
Construct the point material.
PointsMaterial: new (params: {
size?: number;
color?: number;
sizeAttenuation?: boolean;
}) => unknown;Parameters
| Name | Type | Required | Default | Meaning |
|---|---|---|---|---|
params | { size?: number; color?: number; sizeAttenuation?: boolean } | Yes | — | Material option object. |
params.size | number | No | — | Point size; adapter supplies opts.size ?? 0.015. |
params.color | number | No | — | Color; adapter supplies opts.color ?? 0x8fb4ff. |
params.sizeAttenuation | boolean | No | — | Perspective size attenuation; adapter explicitly passes true. |
Returns
unknown Material object.
ThreePointsHandleinterface
Point object and lifecycle returned by threePoints.
interface ThreePointsHandlepointsproperty
Constructed THREE.Points instance, ready to add to a scene.
points: unknown;Returns
unknown Concrete object from injected THREE.Points.
updatemethod
Read the captured position buffer and copy x/y into the attribute; z=0.
update(): Promise<void>;Parameters
This call takes no arguments.
Returns
Promise<void> Resolves after readback and attribute update. It does not render the scene.
Behavior & constraints
- Await updates serially; Buffer.read mapping and captured-buffer limitations apply.
Errors & failure cases
- GPU readback failures propagate as rejected promises.
disposemethod
Calls sim.destroy().
dispose(): void;Parameters
This call takes no arguments.
Returns
void No return value.
Behavior & constraints
- This destroys the supplied simulation, but does not remove points from its scene or call geometry/material.dispose(). Clean those up separately.
Three.js CPU 快照适配器,明确资源所有权和当前版本限制。
辅助输入、dispatch count、托管批次与类型化内核已随 2.1.0 发布。原 API 仍以 npm 2.0.0 声明与实现为历史核对基线;2.1.0 和 2.2.0 新增能力在对应 API 中注明并引用 src 文件,不代表整份 API 重新审计。只列出此入口实际导出的公开 API。
threePointsfunction
创建由粒子位置缓冲 CPU 快照驱动的 Three.js Points 对象。
threePoints(
sim: ParticlesSim,
THREE: ThreeAPI,
opts?: { size?: number; color?: number },
): ThreePointsHandle;参数
| 名称 | 类型 | 必填 | 默认值 | 含义 |
|---|---|---|---|---|
sim | ParticlesSim | 是 | — | 粒子模拟;其位置缓冲长度决定点数。handle.dispose() 也会销毁此模拟。 |
THREE | ThreeAPI | 是 | — | 注入的 Three.js 构造器;适配器不强制安装 Three.js 依赖。 |
opts | { size?: number; color?: number } | 否 | {} | 点材质选项。 |
opts.size | number | 否 | 0.015 | 传给 PointsMaterial.size,同时使用 sizeAttenuation=true。 |
opts.color | number | 否 | 0x8fb4ff | 统一材质颜色;此适配器不复制各物种颜色。 |
返回值
ThreePointsHandle 包含异步 update 和清理方法的 Points 句柄。update() 完成前位置数组初始为零。
行为与约束
- 每次 update 将 GPU 数据读回 CPU,并标记 Three BufferAttribute 需要更新;这不是零拷贝 WebGPU/TSL 桥接。z 固定为 0。
- 2.0.0 仅在创建时捕获一次 sim.buffers().pos。粒子模拟 tick() 后切换双缓冲当前侧,因此 update() 在交替帧上可能读到上一步。不能承诺每次 update 都是最新一步。
- 运行时 geometry 必须提供 setAttribute,虽然 ThreeAPI 将 BufferGeometry 返回值声明为 unknown。不自动处理 resize、渲染循环、移出场景、geometry 清理或 material 清理。
示例
import * as THREE from 'three';
import { particles } from 'wgpu-kit/particles';
import { threePoints, type ThreeAPI } from 'wgpu-kit/three';
const sim = await particles({ count: 1024 });
const handle = threePoints(sim, THREE as unknown as ThreeAPI, { size: 0.02 });
const points = handle.points as THREE.Points;
const scene = new THREE.Scene();
scene.add(points);
await handle.update(); // Initial snapshot; no tick has changed the buffer.
// Render scene with your camera and renderer.
function dispose() {
scene.remove(points);
points.geometry.dispose();
const materials = Array.isArray(points.material)
? points.material
: [points.material];
materials.forEach((material) => material.dispose());
handle.dispose(); // Also destroys sim.
}ThreeAPIinterface
注入 threePoints 的最小构造器接口。
interface ThreeAPIPointsconstructor property
构造返回的点云。
Points: new (geometry: unknown, material: unknown) => unknown;参数
| 名称 | 类型 | 必填 | 默认值 | 含义 |
|---|---|---|---|---|
geometry | unknown | 是 | — | 下方创建并设置 position 属性的 geometry。 |
material | unknown | 是 | — | 构造出的点材质。 |
返回值
unknown 通常为 THREE.Points,应用可保留或转换到具体类型。
BufferGeometryconstructor property
构造 geometry;运行时必须支持 setAttribute(name, attribute)。
BufferGeometry: new () => unknown;参数
此调用不接收参数。
返回值
unknown Geometry 对象。
BufferAttributeconstructor property
创建由 Float32Array 支撑的位置属性。
BufferAttribute: new (
array: Float32Array,
itemSize: number,
) => { set: (arr: ArrayLike<number>) => void; needsUpdate: boolean };参数
| 名称 | 类型 | 必填 | 默认值 | 含义 |
|---|---|---|---|---|
array | Float32Array | 是 | — | 含 count*3 分量的底层位置数组。 |
itemSize | number | 是 | — | 每个元素的分量数,适配器传 3。 |
返回值
{ set: (arr: ArrayLike<number>) => void; needsUpdate: boolean } 每次快照后 needsUpdate 被设为 true。声明中的 set(arr) 不被该适配器调用。
字段与关联类型
| 名称 | 类型 | 必填 | 默认值 | 含义 |
|---|---|---|---|---|
set(arr) | (arr: ArrayLike<number>) => void | — | — | 结构声明要求的方法;arr 是待复制的类数值数组。内部未调用。 |
needsUpdate | boolean | — | — | 快照复制后设为 true 的可写标记。 |
PointsMaterialconstructor property
构造点材质。
PointsMaterial: new (params: {
size?: number;
color?: number;
sizeAttenuation?: boolean;
}) => unknown;参数
| 名称 | 类型 | 必填 | 默认值 | 含义 |
|---|---|---|---|---|
params | { size?: number; color?: number; sizeAttenuation?: boolean } | 是 | — | 材质选项对象。 |
params.size | number | 否 | — | 点大小;适配器传 opts.size ?? 0.015。 |
params.color | number | 否 | — | 颜色;适配器传 opts.color ?? 0x8fb4ff。 |
params.sizeAttenuation | boolean | 否 | — | 透视尺寸衰减;适配器明确传 true。 |
返回值
unknown 材质对象。
ThreePointsHandleinterface
threePoints 返回的点对象和生命周期方法。
interface ThreePointsHandlepointsproperty
构造出的 THREE.Points 实例,可加入场景。
points: unknown;返回值
unknown 注入的 THREE.Points 构造器返回的具体对象。
updatemethod
读取捕获的位置缓冲,将 x/y 复制到属性,z=0。
update(): Promise<void>;参数
此调用不接收参数。
返回值
Promise<void> 读回并更新属性后完成;不渲染场景。
行为与约束
- 串行 await 更新;需遵守 Buffer.read 映射限制和上述固定缓冲引用限制。
错误与失败情况
- GPU 读回失败会传播为 Promise 拒绝。
disposemethod
调用 sim.destroy()。
dispose(): void;参数
此调用不接收参数。
返回值
void 无返回值。
行为与约束
- 会销毁传入的模拟,但不会把 points 移出场景,也不会调用 geometry/material.dispose()。这些资源须单独清理。