@ludicon/spark.js 0.1.2 → 0.1.3

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/README.md CHANGED
@@ -179,6 +179,17 @@ registerSparkLoader(loader, spark)
179
179
 
180
180
  After registration, the loader will automatically encode textures with Spark whenever applicable.
181
181
 
182
+ To use the Spark plugins with the three.js 3DTilesRenderer, you can pass them to the renderer's `GLTFExtensionsPlugin` as follows:
183
+
184
+ ```js
185
+ import { createSparkPlugins } from "@ludicon/spark.js/three-gltf";
186
+
187
+ tiles = new TilesRenderer();
188
+ tiles.registerPlugin( new GLTFExtensionsPlugin( {
189
+ plugins: createSparkPlugins( spark, { generateMipmaps: false } )
190
+ } ) );
191
+ ```
192
+
182
193
 
183
194
  ## License
184
195
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ludicon/spark.js",
3
- "version": "0.1.2",
3
+ "version": "0.1.3",
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",
package/src/three-gltf.js CHANGED
@@ -10,7 +10,7 @@ const Channel = {
10
10
  RGBA: 15 // 1111
11
11
  }
12
12
 
13
- class GLTFSparkPlugin {
13
+ export class GLTFSparkPlugin {
14
14
  constructor(name, parser, spark, options) {
15
15
  this.name = name
16
16
  this.parser = parser
@@ -216,19 +216,26 @@ class SparkLoader extends THREE.TextureLoader {
216
216
  }
217
217
  }
218
218
 
219
- export function registerSparkLoader(loader, spark, options = {}) {
220
- // Remove existing webp and avif plugins:
221
- for (let i = 0; i < loader.pluginCallbacks.length; i++) {
222
- const plugin = loader.pluginCallbacks[i](loader)
219
+ export function createSparkPlugins(spark, options = {}) {
220
+ const pluginCache = new WeakMap()
223
221
 
224
- if (plugin.name == "EXT_texture_webp" || plugin.name == "EXT_texture_avif") {
225
- loader.unregister(loader.pluginCallbacks[i])
226
- i--
222
+ function getOrCreatePlugin(parser) {
223
+ let plugin = pluginCache.get(parser)
224
+ if (!plugin) {
225
+ plugin = new GLTFSparkPlugin("spark", parser, spark, options)
226
+ pluginCache.set(parser, plugin)
227
227
  }
228
+ return plugin
228
229
  }
229
230
 
230
- // Install plugin for standard textures, and textures using webp and avif extensions.
231
- loader.register(parser => new GLTFSparkPlugin("spark", parser, spark, options))
232
- loader.register(parser => new GLTFSparkPlugin("EXT_texture_webp", parser, spark, options))
233
- loader.register(parser => new GLTFSparkPlugin("EXT_texture_avif", parser, spark, options))
231
+ return ["spark", "EXT_texture_webp", "EXT_texture_avif"].map(name => parser => {
232
+ const plugin = getOrCreatePlugin(parser)
233
+ return { name, loadTexture: idx => plugin.loadTexture(idx) }
234
+ })
235
+ }
236
+
237
+ export function registerSparkLoader(loader, spark, options = {}) {
238
+ for (const callback of createSparkPlugins(spark, options)) {
239
+ loader.register(callback)
240
+ }
234
241
  }