voxljs 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (72) hide show
  1. package/dist/audio/SpatialAudioEngine.d.ts +10 -0
  2. package/dist/audio/SpatialAudioEngine.js +80 -0
  3. package/dist/audio/SpatialAudioEngine.js.map +1 -1
  4. package/dist/core/VoxelEngine.d.ts +99 -0
  5. package/dist/core/VoxelEngine.js +181 -0
  6. package/dist/core/VoxelEngine.js.map +1 -0
  7. package/dist/core/index.d.ts +1 -0
  8. package/dist/core/index.js +1 -0
  9. package/dist/core/index.js.map +1 -1
  10. package/dist/index.d.ts +2 -1
  11. package/dist/index.js +2 -1
  12. package/dist/index.js.map +1 -1
  13. package/dist/input/InputManager.d.ts +45 -6
  14. package/dist/input/InputManager.js +178 -31
  15. package/dist/input/InputManager.js.map +1 -1
  16. package/dist/input/index.d.ts +1 -1
  17. package/dist/input/index.js +1 -1
  18. package/dist/input/index.js.map +1 -1
  19. package/dist/materials/Material.d.ts +6 -0
  20. package/dist/materials/Material.js +12 -0
  21. package/dist/materials/Material.js.map +1 -1
  22. package/dist/perf/EntityTickScheduler.d.ts +71 -0
  23. package/dist/perf/EntityTickScheduler.js +202 -0
  24. package/dist/perf/EntityTickScheduler.js.map +1 -0
  25. package/dist/perf/SpatialEntityGrid.d.ts +47 -0
  26. package/dist/perf/SpatialEntityGrid.js +200 -0
  27. package/dist/perf/SpatialEntityGrid.js.map +1 -0
  28. package/dist/perf/index.d.ts +2 -0
  29. package/dist/perf/index.js +2 -0
  30. package/dist/perf/index.js.map +1 -1
  31. package/dist/physics/VoxelPhysicsWorld.d.ts +308 -0
  32. package/dist/physics/VoxelPhysicsWorld.js +1020 -0
  33. package/dist/physics/VoxelPhysicsWorld.js.map +1 -0
  34. package/dist/physics/index.d.ts +1 -0
  35. package/dist/physics/index.js +1 -0
  36. package/dist/physics/index.js.map +1 -1
  37. package/dist/renderers/WebGLRenderer.js +1 -0
  38. package/dist/renderers/WebGLRenderer.js.map +1 -1
  39. package/dist/renderers/webgl/WebGLProgram.js +18 -19
  40. package/dist/renderers/webgl/WebGLProgram.js.map +1 -1
  41. package/dist/renderers/webgl/WebGLState.d.ts +17 -0
  42. package/dist/renderers/webgl/WebGLState.js +115 -1
  43. package/dist/renderers/webgl/WebGLState.js.map +1 -1
  44. package/dist/voxel/GrassFieldSystem.d.ts +186 -0
  45. package/dist/voxel/GrassFieldSystem.js +723 -0
  46. package/dist/voxel/GrassFieldSystem.js.map +1 -0
  47. package/dist/voxel/SilhouetteLightVolumes.d.ts +13 -2
  48. package/dist/voxel/SilhouetteLightVolumes.js +384 -78
  49. package/dist/voxel/SilhouetteLightVolumes.js.map +1 -1
  50. package/dist/voxel/VoxImporter.d.ts +32 -0
  51. package/dist/voxel/VoxImporter.js +228 -0
  52. package/dist/voxel/VoxImporter.js.map +1 -1
  53. package/dist/voxel/VoxelBrushes.d.ts +110 -0
  54. package/dist/voxel/VoxelBrushes.js +248 -0
  55. package/dist/voxel/VoxelBrushes.js.map +1 -0
  56. package/dist/voxel/VoxelPotatoShader.d.ts +8 -2
  57. package/dist/voxel/VoxelPotatoShader.js +68 -4
  58. package/dist/voxel/VoxelPotatoShader.js.map +1 -1
  59. package/dist/voxel/VoxelSilhouetteShadows.d.ts +69 -11
  60. package/dist/voxel/VoxelSilhouetteShadows.js +653 -32
  61. package/dist/voxel/VoxelSilhouetteShadows.js.map +1 -1
  62. package/dist/voxel/index.d.ts +4 -2
  63. package/dist/voxel/index.js +4 -2
  64. package/dist/voxel/index.js.map +1 -1
  65. package/dist/world/VoxelMobAISystem.d.ts +183 -0
  66. package/dist/world/VoxelMobAISystem.js +492 -0
  67. package/dist/world/VoxelMobAISystem.js.map +1 -0
  68. package/dist/world/index.d.ts +1 -0
  69. package/dist/world/index.js +1 -0
  70. package/dist/world/index.js.map +1 -1
  71. package/llms.txt +301 -0
  72. package/package.json +2 -1
package/llms.txt ADDED
@@ -0,0 +1,301 @@
1
+ # voxljs — AI-Ready Engine API Reference (`llms.txt`)
2
+
3
+ > Zero-dependency TypeScript WebGL2 & WebGPU voxel game engine.
4
+ > Import everything directly from `'voxljs'` (`import { ... } from 'voxljs'`).
5
+ > TypeScript declarations are bundled at `dist/index.d.ts`.
6
+
7
+ ---
8
+
9
+ ## 1. Engine Lifecycle, Fixed-Timestep Loop, Entity Budgets & Disposal
10
+
11
+ ### `VoxelEngine`
12
+ Deterministic runtime loop with frame-delta clamping, spatial entity tick throttling, O(1) spatial hash grid, and guaranteed reverse-order resource disposal (`engine.destroy()`).
13
+
14
+ ```ts
15
+ import { VoxelEngine } from 'voxljs';
16
+
17
+ const engine = new VoxelEngine({
18
+ fixedDt: 1 / 60, // Deterministic physics substep (default 1/60s)
19
+ maxFrameDelta: 0.05, // Tab-switch clamp (default 50ms)
20
+ maxSubsteps: 4, // Max fixed steps per frame
21
+ getReferencePosition: () => playerPos, // Reference point for sleepDistance
22
+ tickScheduler: { maxTicksPerFrame: 256, maxBudgetMs: 4.0 },
23
+ spatialGrid: { cellSize: 8 },
24
+ onFixedUpdate: (dt, fixedElapsed) => physics.step(dt),
25
+ onRender: (frameDt, alpha, elapsed) => renderer.render(scene, camera),
26
+ });
27
+
28
+ // Spatial / Entity Budget Guards
29
+ const stopAi = engine.registerTick(
30
+ (dt, elapsed) => updateNpc(dt),
31
+ { id: 'npc_1', interval: 0.1, sleepDistance: 48, getPosition: () => npcPos, priority: 1 },
32
+ );
33
+
34
+ // Track GPU renderers, buffers, AudioContexts, Workers, InputManagers, or cleanup callbacks
35
+ engine.track(renderer);
36
+ engine.track(input);
37
+ engine.track(() => window.removeEventListener('resize', onResize));
38
+
39
+ engine.start(); // Starts requestAnimationFrame loop
40
+ engine.stepFrame(dt); // Manual deterministic frame step (headless/tests)
41
+ engine.stop(); // Pauses loop
42
+ engine.destroy(); // Idempotent reverse-order cleanup of all tracked resources
43
+ ```
44
+
45
+ ### `EntityTickScheduler`
46
+ Standalone budget-guarded scheduler (`engine.tickScheduler`):
47
+ - `registerTick(fn, { id?, interval?, sleepDistance?, wakeHysteresis?, getPosition?, priority?, enabled? }): () => void`
48
+ - `unregisterTick(id)`, `setTickEnabled(id, enabled)`, `isSleeping(id)`, `update(dt, referencePos?)`, `clear()`
49
+ - `lastStats`: `{ totalRegistered, executed, sleeping, throttledByInterval, deferredByBudget, elapsedMs }`
50
+
51
+ ### `SpatialEntityGrid<T>`
52
+ O(1) 3D hash grid (`engine.spatialGrid`):
53
+ - `new SpatialEntityGrid<T>({ cellSize: 8 })`
54
+ - `upsert(id, x, y, z, data)` / `upsertAt(id, pos, data)` / `get(id)` / `has(id)` / `remove(id)` / `clear()`
55
+ - `queryRadius(x, y, z, radius, out?)` -> `SpatialEntityRecord<T>[]`
56
+ - `queryBox(minX, minY, minZ, maxX, maxY, maxZ, out?)` -> `SpatialEntityRecord<T>[]`
57
+ - `nearest(x, y, z, maxRadius, excludeId?)` -> `SpatialEntityRecord<T> | null`
58
+
59
+ ---
60
+
61
+ ## 2. Rigid-Body Voxel Physics, Wheel Raycasts & Multi-Part Joints
62
+
63
+ ### `VoxelPhysicsWorld`
64
+ Zero-dependency rigid-body physics against arbitrary-scale voxel grids (`voxelSize`: `1.0`, `0.5`, `0.25`, `0.1`, `0.05` meters) with multi-part joints and terrain raycasts.
65
+
66
+ ```ts
67
+ import { VoxelPhysicsWorld } from 'voxljs';
68
+
69
+ const physics = new VoxelPhysicsWorld({
70
+ voxelSize: 0.5, // Supports 1m, 50cm, 25cm, 10cm, 5cm voxels
71
+ gravity: { x: 0, y: -9.81, z: 0 },
72
+ getVoxel: (vx, vy, vz) => world.getVoxel(vx, vy, vz),
73
+ isSolid: (id) => id > 0,
74
+ onImpact: ({ bodyId, speed, normal, point, voxel, blockId }) => {},
75
+ });
76
+
77
+ // Add box or sphere rigid bodies
78
+ const chassis = physics.addBody({
79
+ id: 'buggy',
80
+ shape: 'box',
81
+ position: { x: 0, y: 4, z: 0 },
82
+ halfExtents: { x: 0.9, y: 0.35, z: 1.6 },
83
+ mass: 12,
84
+ friction: 0.7,
85
+ });
86
+
87
+ // Add joints: 'wheel' | 'hinge' | 'spring' | 'distance' | 'thruster' | 'fixed'
88
+ const flWheel = physics.addJoint({
89
+ type: 'wheel',
90
+ bodyA: 'buggy',
91
+ chassisAnchor: { x: -0.8, y: -0.2, z: -1.1 },
92
+ suspensionRestLength: 0.55,
93
+ wheelRadius: 0.35,
94
+ stiffness: 180,
95
+ damping: 18,
96
+ driveForce: 140,
97
+ steerAngle: 0.25,
98
+ lateralGrip: 10,
99
+ });
100
+
101
+ const booster = physics.addJoint({
102
+ type: 'thruster',
103
+ bodyA: 'buggy',
104
+ localOffset: { x: 0, y: 0, z: 1.6 },
105
+ localDirection: { x: 0, y: 0, z: -1 },
106
+ thrust: 220,
107
+ active: true,
108
+ });
109
+
110
+ const doorHinge = physics.addJoint({
111
+ type: 'hinge',
112
+ bodyA: 'frame',
113
+ bodyB: 'door',
114
+ axis: { x: 0, y: 1, z: 0 },
115
+ minAngle: 0,
116
+ maxAngle: Math.PI * 0.6,
117
+ motorSpeed: 2.0,
118
+ maxMotorTorque: 40,
119
+ });
120
+
121
+ const weld = physics.addJoint({
122
+ type: 'fixed',
123
+ bodyA: 'chassis',
124
+ bodyB: 'bumper',
125
+ breakForce: 350, // Sets weld.broken = true if impact exceeds threshold
126
+ });
127
+
128
+ const tether = physics.addJoint({
129
+ type: 'spring', // or 'distance'
130
+ bodyA: 'crane',
131
+ bodyB: 'crate',
132
+ restLength: 3.0,
133
+ stiffness: 90,
134
+ damping: 10,
135
+ });
136
+
137
+ // Voxel DDA terrain raycast
138
+ const hit = physics.raycastTerrain(origin, direction, 50);
139
+ // -> { distance, point, normal, voxel: [vx, vy, vz], blockId } | null
140
+
141
+ physics.step(1 / 60);
142
+ ```
143
+
144
+ Methods: `addBody`, `getBody`, `removeBody`, `applyForce`, `applyTorque`, `applyImpulse`, `addJoint`, `getJoint`, `removeJoint`, `raycastTerrain`, `setTerrainSampler`, `step(dt)`, `clear()`, `dispose()`.
145
+
146
+ ---
147
+
148
+ ## 3. Voxel World, Meshing, Prefab Importers & Destruction Brushes
149
+
150
+ ### `VoxelWorld` (SoA Chunk, Body & Debris Engine)
151
+ - `new VoxelWorld({ maxChunks, maxBodies, maxDebris, chunkX, chunkY, chunkZ, fixedDt })`
152
+ - `acquireChunkSlot`, `releaseChunkSlot`, `getVoxel`, `setVoxel`, `acquireBodySlot`, `releaseBodySlot`
153
+ - `spawnDebris(x, y, z, vx, vy, vz, color, size, lifetime)`
154
+ - `step(dt)` — fixed-timestep simulation for debris, gravity, and structural events.
155
+
156
+ ### `.vox` (MagicaVoxel) & `.qb` (Qubicle) Importers
157
+ ```ts
158
+ import {
159
+ parseVoxFile,
160
+ parseQubicleFile,
161
+ loadMagicaVoxel,
162
+ loadQubicle,
163
+ buildVoxMeshGeometry,
164
+ } from 'voxljs';
165
+
166
+ const tank = await loadMagicaVoxel('/models/tank.vox'); // Y-up ParsedVoxModel
167
+ const prop = await loadQubicle('/models/tree.qb'); // RLE + uncompressed multi-matrix .qb
168
+ const geo = buildVoxMeshGeometry(tank, { voxelSize: 0.1, bakeAO: true });
169
+ ```
170
+
171
+ ### Destructible Prefab Stamping & Crater Brushes
172
+ ```ts
173
+ import {
174
+ stampVoxelModel,
175
+ carveSphere,
176
+ carveBox,
177
+ spawnDestructiblePrefab,
178
+ shatterDestructiblePrefab,
179
+ } from 'voxljs';
180
+
181
+ // Stamp .vox/.qb model into terrain with rotationY (0|90|180|270) and mirrorX/mirrorZ
182
+ stampVoxelModel(tank, world, { origin: { x: 10, y: 4, z: 10 }, rotationY: 90 });
183
+
184
+ // Carve craters / trenches with optional bedrock minY and automatic debris cubes
185
+ carveSphere(world, { x: 12, y: 5, z: 12 }, 4, { minY: 1, raggedRim: true, debrisWorld: world });
186
+ carveBox(world, { x: 0, y: 2, z: 0 }, { x: 4, y: 5, z: 4 }, { debrisWorld: world });
187
+
188
+ // Spawn & shatter dynamic voxel prefabs
189
+ const crate = spawnDestructiblePrefab(prop, world, { position: { x: 4, y: 6, z: 4 }, voxelSize: 0.25 });
190
+ shatterDestructiblePrefab(crate, world, { maxDebris: 48, burstSpeed: 7 });
191
+ ```
192
+
193
+ ### Chunk Meshing & Culling
194
+ - `buildVoxelChunkGeometry(options)`, `buildVoxelSubChunkGeometries(options)`, `raycastVoxels(...)`
195
+ - `ChunkBatchRenderer`, `VoxelMesherWorkerPool`, `GPUChunkCuller`, `HorizonOcclusionCuller`, `ChunkStreamManager`
196
+ - `VoxelBlockRegistry`, `VoxelLightEngine`, `VoxelDeltaStore`, `detectDisconnectedIslands`
197
+
198
+ ---
199
+
200
+ ## 4. Action-Based Input & HTML5 Gamepad Bindings
201
+
202
+ ### `InputManager`
203
+ ```ts
204
+ import { InputManager, DEFAULT_VOXEL_BINDINGS } from 'voxljs';
205
+
206
+ const input = new InputManager({ targetElement: canvas });
207
+ input.bind('boost', ['ShiftLeft', 'GamepadRT']);
208
+ input.bind('detonate', ['KeyE', 'GamepadX']);
209
+
210
+ // Per-frame:
211
+ input.pollGamepads(); // Reads Gamepad0..Gamepad15 (GamepadA/B/X/Y/LB/RB/LT/RT/Start/Up/Down/Left/Right) + sticks
212
+ const move = input.getMoveAxes(); // { forward, right } keyboard + left stick
213
+ const look = input.getLookAxes(); // { x, y } right stick
214
+ const mouse = input.consumeLookDelta();
215
+ const steer = input.getAxis('moveLeft', 'moveRight'); // -1 | 0 | 1
216
+
217
+ if (input.wasActionPressed('boost')) startBoost();
218
+ if (input.wasActionReleased('boost')) stopBoost();
219
+ input.endFrame(); // Clear per-frame press/release edges
220
+ ```
221
+
222
+ ---
223
+
224
+ ## 5. Rendering, Materials, Atmosphere & Environment Systems
225
+
226
+ - **Renderers**: `WebGLRenderer`, `WebGPURenderer`, `EffectComposer`, `DynamicResolutionScaler`
227
+ - **Scene Graph & Math**: `Scene`, `Group`, `Mesh`, `InstancedMesh`, `PerspectiveCamera`, `OrthographicCamera`, `Vector2`, `Vector3`, `Vector4`, `Matrix3`, `Matrix4`, `Quaternion`, `Euler`, `Color`, `Box3`, `Sphere`, `Frustum`, `Ray`, `Raycaster`, `MathUtils`
228
+ - **Voxel Materials & Lighting**: `createVoxelPotatoMaterial`, `DirectionalLight`, `AmbientLight`, `HemisphereLight`, `PointLight`
229
+ - **Shadows & Volumetric Sunbeams**: `VoxelSilhouetteShadows`, `SilhouetteLightVolumes`, `VOXEL_GRAPHICS_PRESETS`
230
+ - **Clouds & Wakes**: `CloudLODSystem`, `VolumeWakeField` (shared by clouds & grass for shockwaves/trampling)
231
+ - **Single-Draw-Call Instanced Grass**: `GrassFieldSystem` (`setDensity('off' | 'low' | 'high')`, `setAmount(0..1)`, `update(dt, cameraPos, wakeField)`)
232
+ - **Character Controller, Audio, HUD & Net**: `VoxelCharacterController`, `SpatialAudioEngine`, `InGameHud`, `MultiplayerClient`
233
+
234
+ ---
235
+
236
+ ## 6. Minimal Complete Starter Template
237
+
238
+ ```ts
239
+ import {
240
+ VoxelEngine,
241
+ VoxelPhysicsWorld,
242
+ InputManager,
243
+ Scene,
244
+ PerspectiveCamera,
245
+ WebGLRenderer,
246
+ DirectionalLight,
247
+ AmbientLight,
248
+ Mesh,
249
+ BoxGeometry,
250
+ MeshStandardMaterial,
251
+ } from 'voxljs';
252
+
253
+ const canvas = document.querySelector('canvas')!;
254
+ const renderer = new WebGLRenderer({ canvas, antialias: true });
255
+ renderer.setSize(window.innerWidth, window.innerHeight);
256
+
257
+ const scene = new Scene();
258
+ const camera = new PerspectiveCamera(65, window.innerWidth / window.innerHeight, 0.1, 250);
259
+ camera.position.set(0, 6, 12);
260
+ camera.lookAt(0, 1, 0);
261
+
262
+ scene.add(new AmbientLight(0xffffff, 0.6));
263
+ const sun = new DirectionalLight(0xfff4d6, 1.2);
264
+ sun.position.set(20, 40, 15);
265
+ scene.add(sun);
266
+
267
+ // Flat voxel floor at y = 0
268
+ const physics = new VoxelPhysicsWorld({
269
+ voxelSize: 1,
270
+ getVoxel: (_vx, vy, _vz) => (vy <= 0 ? 1 : 0),
271
+ });
272
+ const cubeBody = physics.addBody({ id: 'hero', position: { x: 0, y: 5, z: 0 }, halfExtents: { x: 0.5, y: 0.5, z: 0.5 } });
273
+ const cubeMesh = new Mesh(new BoxGeometry(1, 1, 1), new MeshStandardMaterial({ color: 0x38bdf8 }));
274
+ scene.add(cubeMesh);
275
+
276
+ const input = new InputManager({ targetElement: canvas });
277
+
278
+ const engine = new VoxelEngine({
279
+ fixedDt: 1 / 60,
280
+ onFixedUpdate: (dt) => {
281
+ input.pollGamepads();
282
+ const { forward, right } = input.getMoveAxes();
283
+ physics.applyForce('hero', { x: right * 20, y: 0, z: -forward * 20 });
284
+ if (input.wasActionPressed('jump') && cubeBody.grounded) {
285
+ physics.applyImpulse('hero', { x: 0, y: 7, z: 0 });
286
+ }
287
+ physics.step(dt);
288
+ input.endFrame();
289
+ },
290
+ onRender: () => {
291
+ cubeMesh.position.copy(cubeBody.position);
292
+ cubeMesh.quaternion.copy(cubeBody.quaternion);
293
+ renderer.render(scene, camera);
294
+ },
295
+ });
296
+
297
+ engine.track(renderer);
298
+ engine.track(input);
299
+ engine.track(physics);
300
+ engine.start();
301
+ ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "voxljs",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "Zero-dependency TypeScript WebGL2 & WebGPU voxel game engine",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -17,6 +17,7 @@
17
17
  },
18
18
  "files": [
19
19
  "dist",
20
+ "llms.txt",
20
21
  "README.md",
21
22
  "LICENSE"
22
23
  ],