Optimizes images, optionally resizing or converting to JPEG, PNG, WebP, or AVIF formats.
For best results use a Node.js environment, install the sharp module, and
provide an encoder. When the encoder is omitted — sharp works only in Node.js —
the implementation will use a platform-specific fallback encoder, and most
quality- and compression-related options are ignored.
Example:
import { textureCompress } from'@gltf-transform/functions';
import sharp from'sharp';
// (A) Optimize without conversion.awaitdocument.transform(
textureCompress({encoder: sharp})
);
// (B) Optimize and convert images to WebP.awaitdocument.transform(
textureCompress({
encoder: sharp,
targetFormat: 'webp',
slots: /^(?!normalTexture).*$/// exclude normal maps
})
);
// (C) Resize and convert images to WebP in a browser, without a Sharp// encoder. Most quality- and compression-related options are ignored.awaitdocument.transform(
textureCompress({ targetFormat: 'webp', resize: [1024, 1024] })
);
Optimizes images, optionally resizing or converting to JPEG, PNG, WebP, or AVIF formats.
For best results use a Node.js environment, install the
sharp
module, and provide an encoder. When the encoder is omitted —sharp
works only in Node.js — the implementation will use a platform-specific fallback encoder, and most quality- and compression-related options are ignored.Example:
import { textureCompress } from '@gltf-transform/functions'; import sharp from 'sharp'; // (A) Optimize without conversion. await document.transform( textureCompress({encoder: sharp}) ); // (B) Optimize and convert images to WebP. await document.transform( textureCompress({ encoder: sharp, targetFormat: 'webp', slots: /^(?!normalTexture).*$/ // exclude normal maps }) ); // (C) Resize and convert images to WebP in a browser, without a Sharp // encoder. Most quality- and compression-related options are ignored. await document.transform( textureCompress({ targetFormat: 'webp', resize: [1024, 1024] }) );