three-instance-batch 0.1.0 → 0.1.2

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 (3) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +44 -4
  3. package/package.json +5 -4
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 qiao-coding
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -28,7 +28,7 @@ inst.castShadow = true
28
28
  batcher.addInstance(inst)
29
29
 
30
30
  function animate() {
31
- inst.position.x = Math.sin(Date.now() * 0.001) * 5
31
+ inst.position.setX(Math.sin(Date.now() * 0.001) * 5)
32
32
  batcher.update(camera)
33
33
  renderer.render(scene, camera)
34
34
  requestAnimationFrame(animate)
@@ -37,9 +37,35 @@ function animate() {
37
37
 
38
38
  No `setMatrixAt`. No `needsUpdate`. Just change properties and call `update()`.
39
39
 
40
+ ## Multiple Instances
41
+
42
+ ```ts
43
+ const count = 500
44
+ const instances: Instance[] = []
45
+
46
+ for (let i = 0; i < count; i++) {
47
+ const inst = new Instance(geo, mat)
48
+ const angle = (i / count) * Math.PI * 2
49
+ inst.position.set(Math.cos(angle) * 10, 0, Math.sin(angle) * 10)
50
+ inst.color.setHSL(i / count, 0.8, 0.5)
51
+ instances.push(inst)
52
+ }
53
+ batcher.addInstance(instances)
54
+
55
+ function animate() {
56
+ for (let i = 0; i < instances.length; i++) {
57
+ instances[i].rotation.set(0, Date.now() * 0.001 + i * 0.1, 0)
58
+ instances[i].color.setHSL((Date.now() * 0.0001 + i * 0.002) % 1, 0.8, 0.5)
59
+ }
60
+ batcher.update(camera)
61
+ renderer.render(scene, camera)
62
+ requestAnimationFrame(animate)
63
+ }
64
+ ```
65
+
40
66
  ## How It Works
41
67
 
42
- `Instance` wraps `Vector3`, `Quaternion`, and `Color` setter methods at construction time. Any mutation — `position.set()`, `quaternion.setFromEuler()`, `color.setHSL()`, etc. — automatically marks that instance dirty. On `batcher.update()`, only the changed matrix rows and color rows are written to the GPU buffer.
68
+ `Instance` wraps `Vector3`, `Euler`, `Quaternion`, and `Color` setter methods at construction time. Any mutation via setter — `position.set()`, `rotation.set()`, `quaternion.setFromEuler()`, `color.setHSL()`, etc. — automatically marks that instance dirty. Note that direct property assignment (`position.x = 5`) is **not** tracked; use `setX()` or `set()` instead.
43
69
 
44
70
  Instances are grouped into `InstancedMesh` objects by a **group key** derived from geometry UUID, material properties, and shadow flags. Instances sharing the same key share one draw call.
45
71
 
@@ -109,6 +135,7 @@ new Instance(geometry: BufferGeometry, material: Material)
109
135
  | `material` | `Material` | Readonly |
110
136
  | `position` | `Vector3` | Auto-tracked |
111
137
  | `scale` | `Vector3` | Default `(1,1,1)`, auto-tracked |
138
+ | `rotation` | `Euler` | Auto-tracked; syncs to `quaternion` on change |
112
139
  | `quaternion` | `Quaternion` | Auto-tracked |
113
140
  | `color` | `Color` | Default white, auto-tracked |
114
141
  | `visible` | `boolean` | Hidden instances write a zero matrix |
@@ -123,7 +150,7 @@ new Instance(geometry: BufferGeometry, material: Material)
123
150
  | `isAncestorOf(other)` | `boolean` | |
124
151
  | `dispose()` | `void` | Detaches from parent, detaches children, unsubscribes from all batchers |
125
152
 
126
- > **Note:** `rotation` (Euler) mutates are tracked and auto-sync to `quaternion`.
153
+ > **Note:** Only method calls (`.set()`, `.setX()`, `.copy()`, etc.) are tracked. Property assignment (`position.x = 5`) bypasses the wrapper. Use `setX()` / `setY()` / `setZ()` for single-component updates.
127
154
 
128
155
  ### `InstanceBatcher`
129
156
 
@@ -170,7 +197,20 @@ The real bottleneck at scale is GPU draw calls and vertex throughput, not JS-sid
170
197
 
171
198
  ## Known Limitations
172
199
 
173
- - Frustum culling is JS-side only; it does not write zero matrices for culled instances.
200
+ - Frustum culling collects per-instance visibility but does not yet skip culled instances in the render pass.
201
+
202
+ ## Development
203
+
204
+ ```bash
205
+ git clone https://github.com/qiao-coding/three-instance-batch.git
206
+ cd three-instance-batch
207
+ npm install
208
+ npm run test:run # 56 tests
209
+ npm run dev # start demo at localhost:5173
210
+ npm run build # build to dist/
211
+ ```
212
+
213
+ PRs welcome. Open an issue before starting on anything substantial.
174
214
 
175
215
  ## License
176
216
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "three-instance-batch",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
4
4
  "description": "Declarative instanced rendering for Three.js — Instance describes what, InstanceBatcher decides how to draw",
5
5
  "type": "module",
6
6
  "main": "./dist/three-instance-batch.js",
@@ -25,11 +25,12 @@
25
25
  },
26
26
  "repository": {
27
27
  "type": "git",
28
- "url": "https://github.com/xier123456/three-instance-batch"
28
+ "url": "https://github.com/qiao-coding/three-instance-batch.git"
29
29
  },
30
- "author": "xier123456",
30
+ "author": "qiao-coding",
31
+ "homepage": "https://github.com/qiao-coding/three-instance-batch#readme",
31
32
  "bugs": {
32
- "url": "https://github.com/xier123456/three-instance-batch/issues"
33
+ "url": "https://github.com/qiao-coding/three-instance-batch/issues"
33
34
  },
34
35
  "peerDependencies": {
35
36
  "three": ">=0.152.0"