@lijuhong1981/three.instancedsprite 1.0.0 → 1.1.0
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.
- package/.gitattributes +2 -0
- package/API.md +61 -3
- package/README.md +125 -55
- package/index.js +5 -5
- package/package.json +1 -1
- package/src/InstancedSprite.js +25 -2
- package/src/InstancedSpriteCollection.js +16 -0
- package/src/InstancedSpriteMaterial.js +146 -133
- package/src/InstancedSpriteMesh.js +425 -406
- package/src/InstancedSpriteNodeMaterial.js +21 -6
- package/.claude/settings.local.json +0 -7
package/.gitattributes
ADDED
package/API.md
CHANGED
|
@@ -22,6 +22,12 @@
|
|
|
22
22
|
<dt><a href="#InstancedSpriteMesh">InstancedSpriteMesh</a> ⇐ <code>Mesh</code></dt>
|
|
23
23
|
<dd><p>InstancedSpriteMesh类,基于InstancedBufferGeometry实现的高性能InstancedSprite渲染组件</p>
|
|
24
24
|
</dd>
|
|
25
|
+
<dt><a href="#InstancedSpriteNodeMaterial">InstancedSpriteNodeMaterial</a> ⇐ <code>NodeMaterial</code></dt>
|
|
26
|
+
<dd><p>InstancedSpriteNodeMaterial 材质类,基于 Three.js TSL (Three Shading Language) 语法实现</p>
|
|
27
|
+
<p>功能与 InstancedSpriteMaterial (ShaderMaterial) 完全相同,但使用 TSL 节点系统构建,
|
|
28
|
+
可更好地与 Three.js 的 NodeMaterial 管线集成(自动处理色调映射、色彩空间转换等)。</p>
|
|
29
|
+
<p><strong>注意</strong>:使用此类需要 Three.js 的 WebGPU/TSL 构建(<code>three/webgpu</code>),非标准 <code>three</code> 构建。</p>
|
|
30
|
+
</dd>
|
|
25
31
|
</dl>
|
|
26
32
|
|
|
27
33
|
## Constants
|
|
@@ -62,6 +68,7 @@ InstancedSprite 数据模型类,用于存储单个 InstancedSprite 的所有
|
|
|
62
68
|
* [.color](#InstancedSprite+color) : <code>Color</code>
|
|
63
69
|
* [.opacity](#InstancedSprite+opacity) : <code>number</code>
|
|
64
70
|
* [.imageSize](#InstancedSprite+imageSize) : <code>Vector2</code>
|
|
71
|
+
* [.userData](#InstancedSprite+userData) : <code>object</code>
|
|
65
72
|
* [.isInstancedSprite](#InstancedSprite+isInstancedSprite) : <code>boolean</code>
|
|
66
73
|
* [.visible](#InstancedSprite+visible) : <code>boolean</code>
|
|
67
74
|
* [.rotationDegrees](#InstancedSprite+rotationDegrees) : <code>number</code>
|
|
@@ -70,7 +77,7 @@ InstancedSprite 数据模型类,用于存储单个 InstancedSprite 的所有
|
|
|
70
77
|
* [.imageWidth](#InstancedSprite+imageWidth) : <code>number</code>
|
|
71
78
|
* [.imageHeight](#InstancedSprite+imageHeight) : <code>number</code>
|
|
72
79
|
* [.geometry](#InstancedSprite+geometry) : <code>InstancedBufferGeometry</code>
|
|
73
|
-
* [.material](#InstancedSprite+material) : [<code>InstancedSpriteMaterial</code>](#InstancedSpriteMaterial)
|
|
80
|
+
* [.material](#InstancedSprite+material) : [<code>InstancedSpriteMaterial</code>](#InstancedSpriteMaterial) \| [<code>InstancedSpriteNodeMaterial</code>](#InstancedSpriteNodeMaterial)
|
|
74
81
|
* [.setValues(options)](#InstancedSprite+setValues) ⇒ [<code>InstancedSprite</code>](#InstancedSprite)
|
|
75
82
|
* [.remove()](#InstancedSprite+remove) ⇒ [<code>InstancedSprite</code>](#InstancedSprite)
|
|
76
83
|
* [.raycast(raycaster, intersects, modelViewMatrix)](#InstancedSprite+raycast)
|
|
@@ -151,6 +158,13 @@ InstancedSprite 数据模型类,用于存储单个 InstancedSprite 的所有
|
|
|
151
158
|
### instancedSprite.imageSize : <code>Vector2</code>
|
|
152
159
|
图片尺寸(像素),图片加载完成后可用
|
|
153
160
|
|
|
161
|
+
**Kind**: instance property of [<code>InstancedSprite</code>](#InstancedSprite)
|
|
162
|
+
**Read only**: true
|
|
163
|
+
<a name="InstancedSprite+userData"></a>
|
|
164
|
+
|
|
165
|
+
### instancedSprite.userData : <code>object</code>
|
|
166
|
+
用户自定义数据存储对象,InstancedSprite本身不使用该属性,用户可以自由使用它来存储任意数据
|
|
167
|
+
|
|
154
168
|
**Kind**: instance property of [<code>InstancedSprite</code>](#InstancedSprite)
|
|
155
169
|
**Read only**: true
|
|
156
170
|
<a name="InstancedSprite+isInstancedSprite"></a>
|
|
@@ -208,8 +222,8 @@ InstancedSprite对象的几何体属性
|
|
|
208
222
|
**Read only**: true
|
|
209
223
|
<a name="InstancedSprite+material"></a>
|
|
210
224
|
|
|
211
|
-
### instancedSprite.material : [<code>InstancedSpriteMaterial</code>](#InstancedSpriteMaterial)
|
|
212
|
-
InstancedSprite
|
|
225
|
+
### instancedSprite.material : [<code>InstancedSpriteMaterial</code>](#InstancedSpriteMaterial) \| [<code>InstancedSpriteNodeMaterial</code>](#InstancedSpriteNodeMaterial)
|
|
226
|
+
InstancedSprite对象的材质属性,可能是InstancedSpriteMaterial或InstancedSpriteNodeMaterial
|
|
213
227
|
|
|
214
228
|
**Kind**: instance property of [<code>InstancedSprite</code>](#InstancedSprite)
|
|
215
229
|
**Read only**: true
|
|
@@ -256,6 +270,8 @@ InstancedSpriteCollection类,批量管理InstancedSprite实例
|
|
|
256
270
|
**Extends**: <code>Object3D</code>
|
|
257
271
|
|
|
258
272
|
* [InstancedSpriteCollection](#InstancedSpriteCollection) ⇐ <code>Object3D</code>
|
|
273
|
+
* [new InstancedSpriteCollection([useNodeMaterial])](#new_InstancedSpriteCollection_new)
|
|
274
|
+
* [.useNodeMaterial](#InstancedSpriteCollection+useNodeMaterial) : <code>boolean</code>
|
|
259
275
|
* [.type](#InstancedSpriteCollection+type) : <code>string</code>
|
|
260
276
|
* [.isInstancedSpriteCollection](#InstancedSpriteCollection+isInstancedSpriteCollection) : <code>boolean</code>
|
|
261
277
|
* [.depthTest](#InstancedSpriteCollection+depthTest) : <code>boolean</code>
|
|
@@ -266,9 +282,26 @@ InstancedSpriteCollection类,批量管理InstancedSprite实例
|
|
|
266
282
|
* [.getByUuid(uuid)](#InstancedSpriteCollection+getByUuid) ⇒ [<code>InstancedSprite</code>](#InstancedSprite) \| <code>undefined</code>
|
|
267
283
|
* [.add()](#InstancedSpriteCollection+add) ⇒ [<code>InstancedSprite</code>](#InstancedSprite)
|
|
268
284
|
* [.remove(sprite)](#InstancedSpriteCollection+remove) ⇒ [<code>InstancedSpriteCollection</code>](#InstancedSpriteCollection)
|
|
285
|
+
* [.clear()](#InstancedSpriteCollection+clear) ⇒ [<code>InstancedSpriteCollection</code>](#InstancedSpriteCollection)
|
|
269
286
|
* [.forEach(callback)](#InstancedSpriteCollection+forEach) ⇒ [<code>InstancedSpriteCollection</code>](#InstancedSpriteCollection)
|
|
270
287
|
* [.raycast(raycaster, intersects)](#InstancedSpriteCollection+raycast)
|
|
271
288
|
|
|
289
|
+
<a name="new_InstancedSpriteCollection_new"></a>
|
|
290
|
+
|
|
291
|
+
### new InstancedSpriteCollection([useNodeMaterial])
|
|
292
|
+
|
|
293
|
+
| Param | Type | Default | Description |
|
|
294
|
+
| --- | --- | --- | --- |
|
|
295
|
+
| [useNodeMaterial] | <code>boolean</code> | <code>false</code> | 是否使用TSL的NodeMaterial,默认false |
|
|
296
|
+
|
|
297
|
+
<a name="InstancedSpriteCollection+useNodeMaterial"></a>
|
|
298
|
+
|
|
299
|
+
### instancedSpriteCollection.useNodeMaterial : <code>boolean</code>
|
|
300
|
+
是否使用TSL的NodeMaterial,默认false
|
|
301
|
+
|
|
302
|
+
**Kind**: instance property of [<code>InstancedSpriteCollection</code>](#InstancedSpriteCollection)
|
|
303
|
+
**Default**: <code>false</code>
|
|
304
|
+
**Read only**: true
|
|
272
305
|
<a name="InstancedSpriteCollection+type"></a>
|
|
273
306
|
|
|
274
307
|
### instancedSpriteCollection.type : <code>string</code>
|
|
@@ -353,6 +386,12 @@ InstancedSprite实例数量
|
|
|
353
386
|
| --- | --- |
|
|
354
387
|
| sprite | [<code>InstancedSprite</code>](#InstancedSprite) |
|
|
355
388
|
|
|
389
|
+
<a name="InstancedSpriteCollection+clear"></a>
|
|
390
|
+
|
|
391
|
+
### instancedSpriteCollection.clear() ⇒ [<code>InstancedSpriteCollection</code>](#InstancedSpriteCollection)
|
|
392
|
+
移除所有InstancedSprite,并清空所有Mesh的实例数据(Mesh本身保留以便复用)
|
|
393
|
+
|
|
394
|
+
**Kind**: instance method of [<code>InstancedSpriteCollection</code>](#InstancedSpriteCollection)
|
|
356
395
|
<a name="InstancedSpriteCollection+forEach"></a>
|
|
357
396
|
|
|
358
397
|
### instancedSpriteCollection.forEach(callback) ⇒ [<code>InstancedSpriteCollection</code>](#InstancedSpriteCollection)
|
|
@@ -485,6 +524,24 @@ Computes intersection points between a casted ray and this sprite.
|
|
|
485
524
|
| raycaster | <code>Raycaster</code> | The raycaster. |
|
|
486
525
|
| intersects | <code>Array.<Object></code> | The target array that holds the intersection points. |
|
|
487
526
|
|
|
527
|
+
<a name="InstancedSpriteNodeMaterial"></a>
|
|
528
|
+
|
|
529
|
+
## InstancedSpriteNodeMaterial ⇐ <code>NodeMaterial</code>
|
|
530
|
+
InstancedSpriteNodeMaterial 材质类,基于 Three.js TSL (Three Shading Language) 语法实现
|
|
531
|
+
|
|
532
|
+
功能与 InstancedSpriteMaterial (ShaderMaterial) 完全相同,但使用 TSL 节点系统构建,
|
|
533
|
+
可更好地与 Three.js 的 NodeMaterial 管线集成(自动处理色调映射、色彩空间转换等)。
|
|
534
|
+
|
|
535
|
+
**注意**:使用此类需要 Three.js 的 WebGPU/TSL 构建(`three/webgpu`),非标准 `three` 构建。
|
|
536
|
+
|
|
537
|
+
**Kind**: global class
|
|
538
|
+
**Extends**: <code>NodeMaterial</code>
|
|
539
|
+
<a name="InstancedSpriteNodeMaterial+texture"></a>
|
|
540
|
+
|
|
541
|
+
### instancedSpriteNodeMaterial.texture : <code>Texture</code> \| <code>null</code>
|
|
542
|
+
图片纹理
|
|
543
|
+
|
|
544
|
+
**Kind**: instance property of [<code>InstancedSpriteNodeMaterial</code>](#InstancedSpriteNodeMaterial)
|
|
488
545
|
<a name="imageLoader"></a>
|
|
489
546
|
|
|
490
547
|
## imageLoader
|
|
@@ -492,6 +549,7 @@ Computes intersection points between a casted ray and this sprite.
|
|
|
492
549
|
**Import**: InstancedSpriteCollection from "./InstancedSpriteCollection.js";
|
|
493
550
|
**Import**: InstancedSpriteMesh from "./InstancedSpriteMesh.js";
|
|
494
551
|
**Import**: InstancedSpriteMaterial from "./InstancedSpriteMaterial.js";
|
|
552
|
+
**Import**: InstancedSpriteNodeMaterial from "./InstancedSpriteNodeMaterial.js";
|
|
495
553
|
<a name="InstancedSpriteOptions"></a>
|
|
496
554
|
|
|
497
555
|
## InstancedSpriteOptions : <code>object</code>
|
package/README.md
CHANGED
|
@@ -1,55 +1,125 @@
|
|
|
1
|
-
# InstancedSprite
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
);
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
//
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
//
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
1
|
+
# InstancedSprite
|
|
2
|
+
|
|
3
|
+
Three.js 自带的 `Sprite` 不支持 GPU 实例化渲染。当场景中需要同时展示大量标签、图标或提示时,逐个绘制 `Sprite` 会产生大量 draw call,导致帧率明显下降。
|
|
4
|
+
|
|
5
|
+
`InstancedSprite` 基于 `InstancedBufferGeometry` + 自定义 `ShaderMaterial`(或 TSL `NodeMaterial`)实现实例化渲染。**相同图片的 Sprite 会自动合并到同一个 Mesh,一次 draw call 全部绘制**,极大提升大批量标签的渲染效率。
|
|
6
|
+
|
|
7
|
+
## 特性
|
|
8
|
+
|
|
9
|
+
- 🚀 **GPU 实例化**:基于 `InstancedBufferGeometry`,数千个 Sprite 仅需一次 draw call
|
|
10
|
+
- 🏷️ **按图自动合批**:`image` 相同的 Sprite 自动归入同一个 `InstancedSpriteMesh` 统一渲染
|
|
11
|
+
- ✏️ **属性自动同步**:直接修改 Sprite 的 `position` / `rotation` / `scale` / `color` 等属性,每帧 `update()` 时通过脏检查自动上传至 GPU,无需手动刷新
|
|
12
|
+
- 🎯 **射线拾取**:内置射线检测,可直接用 `Raycaster` 拾取到具体的 `InstancedSprite` 实例
|
|
13
|
+
- 🖼️ **像素级透明**:片元着色器自动丢弃完全透明的像素
|
|
14
|
+
- 🎨 **双渲染后端**:同时支持 WebGL(GLSL `ShaderMaterial`)与 WebGPU(TSL `NodeMaterial`)
|
|
15
|
+
- 📏 **动态扩容**:实例缓冲区按需自动扩容,实例数量无硬性上限
|
|
16
|
+
|
|
17
|
+
## 安装
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
npm install @lijuhong1981/three.instancedsprite
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
依赖 `three`(`>= 0.171.0`)。若使用 WebGPU / TSL 材质,需从 `three/webgpu` 引入相关构建。
|
|
24
|
+
|
|
25
|
+
## 使用
|
|
26
|
+
|
|
27
|
+
```js
|
|
28
|
+
import * as THREE from "three";
|
|
29
|
+
import { InstancedSpriteCollection } from "@lijuhong1981/three.instancedsprite";
|
|
30
|
+
|
|
31
|
+
// 初始化(继承自 Object3D,直接加入场景)
|
|
32
|
+
const collection = new InstancedSpriteCollection();
|
|
33
|
+
scene.add(collection);
|
|
34
|
+
|
|
35
|
+
// 若使用 WebGPURenderer,传入 true 以启用 TSL 的 NodeMaterial
|
|
36
|
+
// const collection = new InstancedSpriteCollection(true);
|
|
37
|
+
|
|
38
|
+
// 每帧更新:内部做脏检查,把发生变化的属性同步到 GPU
|
|
39
|
+
const onAnimate = () => {
|
|
40
|
+
window.requestAnimationFrame(onAnimate);
|
|
41
|
+
collection.update();
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
// 添加一个 Sprite
|
|
45
|
+
const sprite = collection.add({
|
|
46
|
+
position: [100, 100, 100], // 位置(世界空间)
|
|
47
|
+
rotation: 0, // 旋转(弧度)
|
|
48
|
+
scale: 1.0, // 缩放
|
|
49
|
+
sizeAttenuation: false, // 尺寸是否跟随相机深度变化(默认 true)
|
|
50
|
+
center: [0.5, 0], // 中心锚点(0-1,默认 (0.5, 0.5))
|
|
51
|
+
color: 0xff0000, // 颜色(默认 0xffffff)
|
|
52
|
+
opacity: 0.5, // 不透明度(0-1,默认 1)
|
|
53
|
+
image: './res/icon.png', // 图片地址,相同 image 会合并到同一个 Mesh 一次渲染
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
// 运行时修改属性(无需手动刷新,update() 会自动同步)
|
|
57
|
+
sprite.position.set(200, 100, 0);
|
|
58
|
+
sprite.color.set(0x00ff00);
|
|
59
|
+
sprite.show = false; // 隐藏(等同于 sprite.visible)
|
|
60
|
+
|
|
61
|
+
// 移除
|
|
62
|
+
sprite.remove();
|
|
63
|
+
// 或
|
|
64
|
+
collection.remove(sprite);
|
|
65
|
+
|
|
66
|
+
// 清空所有 Sprite
|
|
67
|
+
collection.clear();
|
|
68
|
+
|
|
69
|
+
// 射线检测
|
|
70
|
+
const ndc = new THREE.Vector2(/* ... */);
|
|
71
|
+
raycaster.setFromCamera(ndc, camera);
|
|
72
|
+
const intersects = raycaster.intersectObject(collection);
|
|
73
|
+
if (intersects.length > 0) {
|
|
74
|
+
const pickedSprite = intersects[0].object; // 拾取到的 InstancedSprite
|
|
75
|
+
const instanceId = intersects[0].instanceId; // 实例索引
|
|
76
|
+
}
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
## 主要 API
|
|
80
|
+
|
|
81
|
+
| 类 | 说明 |
|
|
82
|
+
| --- | --- |
|
|
83
|
+
| `InstancedSpriteCollection` | 继承自 `Object3D`,批量管理所有 Sprite;按 `image` 分组并生成对应的 `InstancedSpriteMesh` |
|
|
84
|
+
| `InstancedSprite` | 数据模型类,保存单个 Sprite 的全部属性;**不继承 `Object3D`**,不能直接加入 Scene,由 Collection 统一渲染 |
|
|
85
|
+
| `InstancedSpriteMesh` | 继承自 `Mesh`,基于 `InstancedBufferGeometry`,同一图片的所有 Sprite 由其统一绘制 |
|
|
86
|
+
| `InstancedSpriteMaterial` | 继承自 `ShaderMaterial`,以 instanced attribute 形式接收每个 Sprite 的属性(WebGL) |
|
|
87
|
+
| `InstancedSpriteNodeMaterial` | 继承自 `NodeMaterial`,基于 TSL 实现,功能与 `InstancedSpriteMaterial` 一致(WebGPU) |
|
|
88
|
+
|
|
89
|
+
### InstancedSpriteCollection
|
|
90
|
+
|
|
91
|
+
- `add(options)` → `InstancedSprite`:添加并返回一个 Sprite
|
|
92
|
+
- `remove(sprite)`:移除指定 Sprite
|
|
93
|
+
- `clear()`:移除所有 Sprite
|
|
94
|
+
- `get(index)` / `getByUuid(uuid)`:按索引 / uuid 获取
|
|
95
|
+
- `forEach(callback)`:遍历所有 Sprite
|
|
96
|
+
- `update()`:每帧调用,同步属性变化
|
|
97
|
+
- `raycast(raycaster, intersects)`:射线拾取
|
|
98
|
+
- `instancedSprites`:Sprite 数组(只读);`size`:数量(只读)
|
|
99
|
+
- `depthTest`:深度测试开关(默认 `true`;关闭后所有 Sprite 始终渲染在最前,适合 UI 元素)
|
|
100
|
+
|
|
101
|
+
### InstancedSprite 属性
|
|
102
|
+
|
|
103
|
+
| 属性 | 类型 | 默认值 | 说明 |
|
|
104
|
+
| --- | --- | --- | --- |
|
|
105
|
+
| `position` | `Vector3` | `(0, 0, 0)` | 世界坐标位置 |
|
|
106
|
+
| `scale` | `number` | `1` | 缩放 |
|
|
107
|
+
| `rotation` | `number` | `0` | 旋转(弧度);可用 `rotationDegrees` 以角度读写 |
|
|
108
|
+
| `sizeAttenuation` | `boolean` | `true` | 尺寸是否跟随相机深度变化 |
|
|
109
|
+
| `center` | `Vector2` | `(0.5, 0.5)` | 锚点中心(0-1) |
|
|
110
|
+
| `color` | `Color` | `0xffffff` | 颜色 |
|
|
111
|
+
| `opacity` | `number` | `1` | 不透明度(0-1) |
|
|
112
|
+
| `show` / `visible` | `boolean` | `true` | 是否显示 |
|
|
113
|
+
| `image` | `string \| HTMLImageElement \| HTMLCanvasElement` | - | 图片资源 |
|
|
114
|
+
| `imageSize` / `imageWidth` / `imageHeight` | - | - | 图片尺寸,图片加载完成后可用(只读) |
|
|
115
|
+
| `userData` | `object` | `{}` | 用户自定义数据 |
|
|
116
|
+
| `uuid` | `string` | - | 唯一标识(只读) |
|
|
117
|
+
|
|
118
|
+
## 工作原理
|
|
119
|
+
|
|
120
|
+
1. `InstancedSpriteCollection.add()` 创建 `InstancedSprite` 数据对象,并根据其 `image` 找到(或新建)对应的 `InstancedSpriteMesh`。
|
|
121
|
+
2. `InstancedSpriteMesh` 持有 `InstancedBufferGeometry`,为每个实例分配一组 instanced attribute(位置+显示、锚点+尺寸、缩放+旋转+衰减、颜色+透明度、拾取颜色)。
|
|
122
|
+
3. 顶点着色器根据实例属性计算 billboard 位置(对齐、旋转、透视缩放),片元着色器采样纹理并应用颜色 / 透明度。
|
|
123
|
+
4. 每帧调用 `update()`:仅当属性实际发生变化(脏检查)时才更新对应缓冲区,减少 CPU→GPU 传输开销;缓冲区按需自动扩容。
|
|
124
|
+
|
|
125
|
+
## [API 文档](./API.md)
|
package/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
export * from "./src/InstancedSprite.js";
|
|
2
|
-
export * from "./src/InstancedSpriteCollection.js";
|
|
3
|
-
export * from "./src/InstancedSpriteMaterial.js";
|
|
4
|
-
export * from "./src/InstancedSpriteMesh.js";
|
|
5
|
-
export * from "./src/InstancedSpriteNodeMaterial.js";
|
|
1
|
+
export * from "./src/InstancedSprite.js";
|
|
2
|
+
export * from "./src/InstancedSpriteCollection.js";
|
|
3
|
+
export * from "./src/InstancedSpriteMaterial.js";
|
|
4
|
+
export * from "./src/InstancedSpriteMesh.js";
|
|
5
|
+
export * from "./src/InstancedSpriteNodeMaterial.js";
|
package/package.json
CHANGED
package/src/InstancedSprite.js
CHANGED
|
@@ -8,6 +8,7 @@ import { Color, InstancedBufferGeometry, MathUtils, Matrix4, Triangle, Vector2,
|
|
|
8
8
|
* @import InstancedSpriteCollection from "./InstancedSpriteCollection.js";
|
|
9
9
|
* @import InstancedSpriteMesh from "./InstancedSpriteMesh.js";
|
|
10
10
|
* @import InstancedSpriteMaterial from "./InstancedSpriteMaterial.js";
|
|
11
|
+
* @import InstancedSpriteNodeMaterial from "./InstancedSpriteNodeMaterial.js";
|
|
11
12
|
*/
|
|
12
13
|
|
|
13
14
|
const imageLoader = new ImageLoader({ cacheType: Loader.CacheType.ALONE, });
|
|
@@ -169,6 +170,28 @@ class InstancedSprite {
|
|
|
169
170
|
this.imageSize = new Vector2(0, 0);
|
|
170
171
|
this._imageSize = new Vector2(0, 0);
|
|
171
172
|
this._image = null;
|
|
173
|
+
/**
|
|
174
|
+
* 拾取颜色,用于GPU拾取,由Picking管理器设置和使用,用户无需关心
|
|
175
|
+
* @type {Color}
|
|
176
|
+
* @readonly
|
|
177
|
+
* @private
|
|
178
|
+
*/
|
|
179
|
+
this.pickColor = new Color();
|
|
180
|
+
this._pickColor = new Color();
|
|
181
|
+
/**
|
|
182
|
+
* 是否启用拾取颜色,用于GPU拾取,由Picking管理器设置和使用,用户无需关心
|
|
183
|
+
* @type {boolean}
|
|
184
|
+
* @readonly
|
|
185
|
+
* @private
|
|
186
|
+
*/
|
|
187
|
+
this.enablePickColor = false;
|
|
188
|
+
this._enablePickColor = false;
|
|
189
|
+
/**
|
|
190
|
+
* 用户自定义数据存储对象,InstancedSprite本身不使用该属性,用户可以自由使用它来存储任意数据
|
|
191
|
+
* @type {object}
|
|
192
|
+
* @readonly
|
|
193
|
+
*/
|
|
194
|
+
this.userData = {};
|
|
172
195
|
|
|
173
196
|
this.setValues(options);
|
|
174
197
|
}
|
|
@@ -249,8 +272,8 @@ class InstancedSprite {
|
|
|
249
272
|
return this._mesh ? this._mesh.geometry : undefined;
|
|
250
273
|
}
|
|
251
274
|
/**
|
|
252
|
-
* InstancedSprite
|
|
253
|
-
* @type {InstancedSpriteMaterial}
|
|
275
|
+
* InstancedSprite对象的材质属性,可能是InstancedSpriteMaterial或InstancedSpriteNodeMaterial
|
|
276
|
+
* @type {InstancedSpriteMaterial|InstancedSpriteNodeMaterial}
|
|
254
277
|
* @readonly
|
|
255
278
|
*/
|
|
256
279
|
get material() {
|
|
@@ -187,6 +187,22 @@ class InstancedSpriteCollection extends Object3D {
|
|
|
187
187
|
}
|
|
188
188
|
return this;
|
|
189
189
|
}
|
|
190
|
+
/**
|
|
191
|
+
* 移除所有InstancedSprite,并清空所有Mesh的实例数据(Mesh本身保留以便复用)
|
|
192
|
+
* @returns {InstancedSpriteCollection}
|
|
193
|
+
*/
|
|
194
|
+
clear() {
|
|
195
|
+
for (const sprite of this._instancedSprites) {
|
|
196
|
+
sprite._mesh = undefined;
|
|
197
|
+
sprite._instanceIndex = -1;
|
|
198
|
+
}
|
|
199
|
+
this._instancedSprites.length = 0;
|
|
200
|
+
const meshes = this._meshes.values();
|
|
201
|
+
for (const mesh of meshes) {
|
|
202
|
+
mesh.clear();
|
|
203
|
+
}
|
|
204
|
+
return this;
|
|
205
|
+
}
|
|
190
206
|
/**
|
|
191
207
|
* 遍历所有的InstancedSprite
|
|
192
208
|
* @param {Function} callback
|