@ludicon/spark.js 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.
- package/package.json +10 -3
- package/src/index.d.ts +301 -0
- package/src/three-gltf.d.ts +62 -0
- package/src/three-gltf.js +1 -1
package/package.json
CHANGED
|
@@ -1,19 +1,26 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ludicon/spark.js",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"description": "Real-Time GPU Texture Codecs for the Web",
|
|
5
5
|
"main": "dist/spark.esm.js",
|
|
6
6
|
"module": "dist/spark.esm.js",
|
|
7
7
|
"type": "module",
|
|
8
|
+
"types": "src/index.d.ts",
|
|
8
9
|
"exports": {
|
|
9
10
|
".": {
|
|
10
|
-
"import": "./dist/spark.esm.js"
|
|
11
|
+
"import": "./dist/spark.esm.js",
|
|
12
|
+
"types": "./src/index.d.ts"
|
|
11
13
|
},
|
|
12
|
-
"./three-gltf":
|
|
14
|
+
"./three-gltf": {
|
|
15
|
+
"import": "./src/three-gltf.js",
|
|
16
|
+
"types": "./src/three-gltf.d.ts"
|
|
17
|
+
}
|
|
13
18
|
},
|
|
14
19
|
"files": [
|
|
15
20
|
"dist",
|
|
16
21
|
"src/three-gltf.js",
|
|
22
|
+
"src/three-gltf.d.ts",
|
|
23
|
+
"src/index.d.ts",
|
|
17
24
|
"README.md",
|
|
18
25
|
"LICENSE"
|
|
19
26
|
],
|
package/src/index.d.ts
ADDED
|
@@ -0,0 +1,301 @@
|
|
|
1
|
+
// Type definitions for @ludicon/spark.js
|
|
2
|
+
// Project: https://github.com/ludicon/spark.js
|
|
3
|
+
// Definitions by: Ludicon LLC
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Options for initializing Spark (WebGPU) encoder
|
|
7
|
+
*/
|
|
8
|
+
export interface SparkCreateOptions {
|
|
9
|
+
/**
|
|
10
|
+
* Whether to preload all encoder pipelines or an array of format names to preload.
|
|
11
|
+
* Pipelines that are not preloaded are compiled on-demand when first used.
|
|
12
|
+
* @default false
|
|
13
|
+
*/
|
|
14
|
+
preload?: boolean | string[]
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Whether to cache temporary resources for reuse across encodeTexture calls.
|
|
18
|
+
* Improves performance when encoding multiple textures, but uses more GPU memory.
|
|
19
|
+
* @default false
|
|
20
|
+
*/
|
|
21
|
+
cacheTempResources?: boolean
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Enable verbose logging for debugging.
|
|
25
|
+
* @default false
|
|
26
|
+
*/
|
|
27
|
+
verbose?: boolean
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Enable GPU timestamp queries for performance profiling.
|
|
31
|
+
* Requires `timestamp-query` feature and enabling unsafe WebGPU features in the browser.
|
|
32
|
+
* @default false
|
|
33
|
+
*/
|
|
34
|
+
useTimestampQueries?: boolean
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Options for encoding textures with Spark
|
|
39
|
+
*/
|
|
40
|
+
export interface SparkEncodeOptions {
|
|
41
|
+
/**
|
|
42
|
+
* Desired block compression format. Can be specified in several ways:
|
|
43
|
+
* - Channel mask: "r", "rg", "rgb", "rgba" - Auto-selects the best format based on device capabilities
|
|
44
|
+
* - Explicit format: "bc1-rgb", "bc7-rgba", "astc-4x4-rgb", "etc2-rgb", "eac-r", etc.
|
|
45
|
+
* - Substring: "bc1", "bc7", "astc", "etc2" - Chooses the first matching format
|
|
46
|
+
* - Auto-detect: "auto" - Analyzes image to determine channel count (WebGPU only, has overhead)
|
|
47
|
+
* @default "rgb"
|
|
48
|
+
*/
|
|
49
|
+
format?: string
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Hint for the automatic format selector. When the input format is "rgb" it chooses
|
|
53
|
+
* 8 bit per block formats like "bc1" or "etc2" instead of "bc7" or "astc".
|
|
54
|
+
* @default false
|
|
55
|
+
*/
|
|
56
|
+
preferLowQuality?: boolean
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Whether to generate mipmaps.
|
|
60
|
+
* @default false
|
|
61
|
+
*/
|
|
62
|
+
mips?: boolean
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Alias for mips. Whether to generate mipmaps.
|
|
66
|
+
* @default false
|
|
67
|
+
*/
|
|
68
|
+
generateMipmaps?: boolean
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* The filter to use for mipmap generation:
|
|
72
|
+
* - "box" - Simple 2x2 box filter
|
|
73
|
+
* - "magic" - Higher quality 4x4 filter with sharpening properties
|
|
74
|
+
* @default "magic"
|
|
75
|
+
*/
|
|
76
|
+
mipmapFilter?: "box" | "magic"
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Optional array of alpha scale values to apply to each generated mipmap level.
|
|
80
|
+
* The array should contain one value per mipmap level (starting with mip level 1).
|
|
81
|
+
* Each value multiplies the alpha channel of the corresponding mipmap level.
|
|
82
|
+
* Values greater than 1.0 increase opacity, while values less than 1.0 increase transparency.
|
|
83
|
+
* If the array is shorter than the number of mipmap levels, the last value is used for remaining levels.
|
|
84
|
+
* Only applies when mips is true.
|
|
85
|
+
*/
|
|
86
|
+
mipsAlphaScale?: number[]
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Whether to encode the image using an sRGB format.
|
|
90
|
+
* This also affects mipmap generation. The srgb mode can also be inferred from the format.
|
|
91
|
+
* @default false
|
|
92
|
+
*/
|
|
93
|
+
srgb?: boolean
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Whether to interpret the image as a normal map.
|
|
97
|
+
* This affects automatic format selection favoring the use of "bc5" and "eac-rg" formats.
|
|
98
|
+
* @default false
|
|
99
|
+
*/
|
|
100
|
+
normal?: boolean
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Whether to vertically flip the image before encoding.
|
|
104
|
+
* @default false
|
|
105
|
+
*/
|
|
106
|
+
flipY?: boolean
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* WebGPU-based texture encoder
|
|
111
|
+
*/
|
|
112
|
+
export class Spark {
|
|
113
|
+
/**
|
|
114
|
+
* Creates a new Spark instance for WebGPU.
|
|
115
|
+
* @param device - WebGPU device with required features enabled
|
|
116
|
+
* @param options - Configuration options
|
|
117
|
+
* @returns Initialized Spark instance
|
|
118
|
+
*/
|
|
119
|
+
static create(device: GPUDevice, options?: SparkCreateOptions): Promise<Spark>
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Determines the set of WebGPU features to request when initializing the device.
|
|
123
|
+
* This function inspects the given adapter to see which texture compression and shader
|
|
124
|
+
* features are available, and returns a list of those that are both supported and safe to enable.
|
|
125
|
+
* @param adapter - The WebGPU adapter returned from navigator.gpu.requestAdapter()
|
|
126
|
+
* @returns Array of WebGPU feature names to request during adapter.requestDevice()
|
|
127
|
+
*/
|
|
128
|
+
static getRequiredFeatures(adapter: GPUAdapter): GPUFeatureName[]
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Destroys the Spark instance and all associated GPU resources.
|
|
132
|
+
*/
|
|
133
|
+
dispose(): void
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Load an image and encode it to a compressed GPU texture.
|
|
137
|
+
* @param source - The image to encode. Can be a URL string, DOM image element, ImageBitmap, or GPUTexture
|
|
138
|
+
* @param options - Optional configuration for encoding
|
|
139
|
+
* @returns Promise resolving to the encoded GPU texture
|
|
140
|
+
*/
|
|
141
|
+
encodeTexture(source: string | HTMLImageElement | ImageBitmap | GPUTexture, options?: SparkEncodeOptions): Promise<GPUTexture>
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Returns list of compression formats supported on the current device.
|
|
145
|
+
* @returns Array of format name strings (e.g., "bc7-rgba", "bc1-rgb", "etc2-rgb")
|
|
146
|
+
*/
|
|
147
|
+
getSupportedFormats(): string[]
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Checks if a specific format is supported.
|
|
151
|
+
* @param format - Format name or format constant
|
|
152
|
+
* @returns True if format is supported
|
|
153
|
+
*/
|
|
154
|
+
isFormatSupported(format: string | number): boolean
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Frees cached temporary GPU resources when cacheTempResources option is enabled.
|
|
158
|
+
* Call this when you're done encoding textures to free up GPU memory.
|
|
159
|
+
*/
|
|
160
|
+
freeTempResources(): void
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Try to determine the best compression options automatically.
|
|
164
|
+
* Do not use this in production, this is for convenience only.
|
|
165
|
+
* @param source - Image input
|
|
166
|
+
* @param options - Encoding options
|
|
167
|
+
* @returns Recommended encoding options with an explicit encoding format
|
|
168
|
+
*/
|
|
169
|
+
selectPreferredOptions(source: string | HTMLImageElement | ImageBitmap | GPUTexture, options?: SparkEncodeOptions): Promise<SparkEncodeOptions>
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Get elapsed time for the last encoding operation (requires useTimestampQueries option).
|
|
173
|
+
* @returns Promise resolving to elapsed time in milliseconds
|
|
174
|
+
*/
|
|
175
|
+
getTimeElapsed(): Promise<number>
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Options for initializing SparkGL (WebGL2) encoder
|
|
180
|
+
*/
|
|
181
|
+
export interface SparkGLCreateOptions {
|
|
182
|
+
/**
|
|
183
|
+
* Whether to preload shader programs. Can be:
|
|
184
|
+
* - false: Load shaders on-demand
|
|
185
|
+
* - true: Preload all supported formats
|
|
186
|
+
* - string[]: Array of format names to preload (e.g., ["bc7", "astc"])
|
|
187
|
+
* @default false
|
|
188
|
+
*/
|
|
189
|
+
preload?: boolean | string[]
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Whether to cache temporary resources for reuse across encodeTexture calls.
|
|
193
|
+
* @default false
|
|
194
|
+
*/
|
|
195
|
+
cacheTempResources?: boolean
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* Enable verbose logging for debugging.
|
|
199
|
+
* @default false
|
|
200
|
+
*/
|
|
201
|
+
verbose?: boolean
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* Enable WebGL shader validation. Only enable this for debugging,
|
|
205
|
+
* as it disables async shader compilation.
|
|
206
|
+
* @default false
|
|
207
|
+
*/
|
|
208
|
+
validateShaders?: boolean
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* Result object returned by SparkGL.encodeTexture()
|
|
213
|
+
*/
|
|
214
|
+
export interface SparkGLTextureResult {
|
|
215
|
+
/**
|
|
216
|
+
* The compressed WebGL texture
|
|
217
|
+
*/
|
|
218
|
+
texture: WebGLTexture
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* WebGL internal format constant
|
|
222
|
+
*/
|
|
223
|
+
format: number
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* Spark format name
|
|
227
|
+
*/
|
|
228
|
+
sparkFormat: number
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* Human-readable Spark format name
|
|
232
|
+
*/
|
|
233
|
+
sparkFormatName: string
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* Texture width in pixels
|
|
237
|
+
*/
|
|
238
|
+
width: number
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* Texture height in pixels
|
|
242
|
+
*/
|
|
243
|
+
height: number
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* Number of mipmap levels
|
|
247
|
+
*/
|
|
248
|
+
mipLevels: number
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* Size of the texture data in bytes
|
|
252
|
+
*/
|
|
253
|
+
byteLength: number
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* WebGL2-based texture encoder
|
|
258
|
+
*/
|
|
259
|
+
export class SparkGL {
|
|
260
|
+
/**
|
|
261
|
+
* Creates a new SparkGL instance for WebGL2.
|
|
262
|
+
* @param gl - WebGL2 context. Required extensions are automatically enabled.
|
|
263
|
+
* @param options - Configuration options
|
|
264
|
+
* @returns Initialized SparkGL instance
|
|
265
|
+
*/
|
|
266
|
+
static create(gl: WebGLRenderingContext | WebGL2RenderingContext, options?: SparkGLCreateOptions): SparkGL
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* Destroys the SparkGL instance and all associated GPU resources.
|
|
270
|
+
*/
|
|
271
|
+
dispose(): void
|
|
272
|
+
|
|
273
|
+
/**
|
|
274
|
+
* Load an image and encode it to a compressed WebGL texture.
|
|
275
|
+
* @param source - The image to encode. Can be a URL string, DOM image element, ImageBitmap, or WebGLTexture
|
|
276
|
+
* @param options - Optional configuration for encoding
|
|
277
|
+
* @returns Promise resolving to an object containing the encoded texture and metadata
|
|
278
|
+
*/
|
|
279
|
+
encodeTexture(source: string | HTMLImageElement | ImageBitmap | WebGLTexture, options?: SparkEncodeOptions): Promise<SparkGLTextureResult>
|
|
280
|
+
|
|
281
|
+
/**
|
|
282
|
+
* Returns list of compression formats supported on the current device.
|
|
283
|
+
* @returns Array of format name strings (e.g., "bc7-rgba", "bc1-rgb", "etc2-rgb")
|
|
284
|
+
*/
|
|
285
|
+
getSupportedFormats(): string[]
|
|
286
|
+
|
|
287
|
+
/**
|
|
288
|
+
* Checks if a specific format is supported.
|
|
289
|
+
* @param format - Format name or format constant
|
|
290
|
+
* @returns True if format is supported
|
|
291
|
+
*/
|
|
292
|
+
isFormatSupported(format: string | number): boolean
|
|
293
|
+
|
|
294
|
+
/**
|
|
295
|
+
* Frees cached temporary GPU resources when cacheTempResources option is enabled.
|
|
296
|
+
* Call this when you're done encoding textures to free up GPU memory.
|
|
297
|
+
*/
|
|
298
|
+
freeTempResources(): void
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
export default Spark
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
// Type definitions for @ludicon/spark.js/three-gltf
|
|
2
|
+
// Project: https://github.com/ludicon/spark.js
|
|
3
|
+
|
|
4
|
+
import type { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader.js'
|
|
5
|
+
import type { Spark, SparkGL, SparkEncodeOptions } from '@ludicon/spark.js'
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Options for the Spark GLTF loader plugin
|
|
9
|
+
*/
|
|
10
|
+
export interface SparkGLTFOptions extends Omit<SparkEncodeOptions, 'format'> {
|
|
11
|
+
/**
|
|
12
|
+
* Whether to generate mipmaps for textures.
|
|
13
|
+
* @default true
|
|
14
|
+
*/
|
|
15
|
+
mips?: boolean
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Alias for mips. Whether to generate mipmaps.
|
|
19
|
+
* @default true
|
|
20
|
+
*/
|
|
21
|
+
generateMipmaps?: boolean
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Hint for the automatic format selector. When true, chooses lower quality
|
|
25
|
+
* formats like "bc1" or "etc2" instead of "bc7" or "astc".
|
|
26
|
+
* @default false
|
|
27
|
+
*/
|
|
28
|
+
preferLowQuality?: boolean
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Registers the Spark texture compression plugin with a Three.js GLTFLoader.
|
|
33
|
+
* This plugin automatically compresses textures during GLTF loading using Spark.
|
|
34
|
+
*
|
|
35
|
+
* The plugin analyzes material usage to determine optimal compression formats for each texture:
|
|
36
|
+
* - Base color textures use RGB or RGBA formats with sRGB encoding
|
|
37
|
+
* - Normal maps use RG formats
|
|
38
|
+
* - Metallic/roughness and other PBR textures use appropriate channel formats
|
|
39
|
+
*
|
|
40
|
+
* @param loader - The Three.js GLTFLoader instance to register the plugin with
|
|
41
|
+
* @param spark - A Spark or SparkGL instance to use for texture compression
|
|
42
|
+
* @param options - Optional encoding options to apply to all textures
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* ```typescript
|
|
46
|
+
* import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader.js';
|
|
47
|
+
* import { SparkGL } from '@ludicon/spark.js';
|
|
48
|
+
* import { registerSparkLoader } from '@ludicon/spark.js/three-gltf';
|
|
49
|
+
*
|
|
50
|
+
* const canvas = document.createElement('canvas');
|
|
51
|
+
* const gl = canvas.getContext('webgl2');
|
|
52
|
+
* const spark = SparkGL.create(gl, { cacheTempResources: true });
|
|
53
|
+
*
|
|
54
|
+
* const loader = new GLTFLoader();
|
|
55
|
+
* registerSparkLoader(loader, spark, { mips: true });
|
|
56
|
+
*
|
|
57
|
+
* loader.load('model.gltf', (gltf) => {
|
|
58
|
+
* scene.add(gltf.scene);
|
|
59
|
+
* });
|
|
60
|
+
* ```
|
|
61
|
+
*/
|
|
62
|
+
export function registerSparkLoader(loader: GLTFLoader, spark: Spark | SparkGL, options?: SparkGLTFOptions): void
|
package/src/three-gltf.js
CHANGED
|
@@ -185,7 +185,7 @@ class SparkLoader extends THREE.TextureLoader {
|
|
|
185
185
|
const texture = new THREE.ExternalTexture(gpuTexture)
|
|
186
186
|
if (textureObject.texture !== undefined) {
|
|
187
187
|
texture.format = textureObject.format
|
|
188
|
-
texture.byteLength = textureObject.byteLength
|
|
188
|
+
texture.userData.byteLength = textureObject.byteLength
|
|
189
189
|
}
|
|
190
190
|
if (this.format == "rg" && "NormalRGPacking" in THREE) {
|
|
191
191
|
// This is not understood by stock three.js
|