Skip to content

Commit 6c625ea

Browse files
authored
Merge pull request #14075 from kickblade/GLTFExporterImageCache
GLTFExporter image caching.
2 parents 0ee30a9 + e004c50 commit 6c625ea

File tree

1 file changed

+33
-14
lines changed

1 file changed

+33
-14
lines changed

examples/js/exporters/GLTFExporter.js

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,8 @@ THREE.GLTFExporter.prototype = {
106106

107107
attributes: new Map(),
108108
materials: new Map(),
109-
textures: new Map()
109+
textures: new Map(),
110+
images: new Map()
110111

111112
};
112113

@@ -622,32 +623,47 @@ THREE.GLTFExporter.prototype = {
622623

623624
/**
624625
* Process image
625-
* @param {Texture} map Texture to process
626+
* @param {Image} image to process
627+
* @param {Integer} format of the image (e.g. THREE.RGBFormat, THREE.RGBAFormat etc)
628+
* @param {Boolean} flipY before writing out the image
626629
* @return {Integer} Index of the processed texture in the "images" array
627630
*/
628-
function processImage( map ) {
631+
function processImage( image, format, flipY ) {
632+
633+
if ( ! cachedData.images.has( image ) ) {
634+
635+
cachedData.images.set( image, {} );
636+
637+
}
638+
639+
var cachedImages = cachedData.images.get( image );
640+
var mimeType = format === THREE.RGBAFormat ? 'image/png' : 'image/jpeg';
641+
var key = mimeType + ":flipY/" + flipY.toString();
629642

630-
// @TODO Cache
643+
if ( cachedImages[ key ] !== undefined ) {
644+
645+
return cachedImages[ key ];
646+
647+
}
631648

632649
if ( ! outputJSON.images ) {
633650

634651
outputJSON.images = [];
635652

636653
}
637654

638-
var mimeType = map.format === THREE.RGBAFormat ? 'image/png' : 'image/jpeg';
639655
var gltfImage = { mimeType: mimeType };
640656

641657
if ( options.embedImages ) {
642658

643659
var canvas = cachedCanvas = cachedCanvas || document.createElement( 'canvas' );
644660

645-
canvas.width = map.image.width;
646-
canvas.height = map.image.height;
661+
canvas.width = image.width;
662+
canvas.height = image.height;
647663

648-
if ( options.forcePowerOfTwoTextures && ! isPowerOfTwo( map.image ) ) {
664+
if ( options.forcePowerOfTwoTextures && ! isPowerOfTwo( image ) ) {
649665

650-
console.warn( 'GLTFExporter: Resized non-power-of-two image.', map.image );
666+
console.warn( 'GLTFExporter: Resized non-power-of-two image.', image );
651667

652668
canvas.width = THREE.Math.floorPowerOfTwo( canvas.width );
653669
canvas.height = THREE.Math.floorPowerOfTwo( canvas.height );
@@ -656,14 +672,14 @@ THREE.GLTFExporter.prototype = {
656672

657673
var ctx = canvas.getContext( '2d' );
658674

659-
if ( map.flipY === true ) {
675+
if ( flipY === true ) {
660676

661677
ctx.translate( 0, canvas.height );
662678
ctx.scale( 1, - 1 );
663679

664680
}
665681

666-
ctx.drawImage( map.image, 0, 0, canvas.width, canvas.height );
682+
ctx.drawImage( image, 0, 0, canvas.width, canvas.height );
667683

668684
if ( options.binary === true ) {
669685

@@ -691,13 +707,16 @@ THREE.GLTFExporter.prototype = {
691707

692708
} else {
693709

694-
gltfImage.uri = map.image.src;
710+
gltfImage.uri = image.src;
695711

696712
}
697713

698714
outputJSON.images.push( gltfImage );
699715

700-
return outputJSON.images.length - 1;
716+
var index = outputJSON.images.length - 1;
717+
cachedImages[ key ] = index;
718+
719+
return index;
701720

702721
}
703722

@@ -751,7 +770,7 @@ THREE.GLTFExporter.prototype = {
751770
var gltfTexture = {
752771

753772
sampler: processSampler( map ),
754-
source: processImage( map )
773+
source: processImage( map.image, map.format, map.flipY )
755774

756775
};
757776

0 commit comments

Comments
 (0)