@@ -19,8 +19,11 @@ export default class TextureAtlas extends AsyncFactory {
1919 config ;
2020 resourcePackStack ;
2121
22- /** When makeAtlas() is called, this will contain UV coordinates and sizes for texture references passed as input, as well as cropping information. */
23- textures ;
22+ /**
23+ * When makeAtlas() is called, this will contain UV coordinates and sizes for texture references passed as input, as well as cropping information.
24+ * @type {Array<{ uv: Vec2, uv_size: Vec2, crop?: Rectangle }> }
25+ */
26+ uvs ;
2427
2528 /**
2629 * Contains the actual texture atlas images: [textureName, imageBlob]
@@ -123,8 +126,7 @@ export default class TextureAtlas extends AsyncFactory {
123126 "tint_like_png" : tintLikePng ,
124127 "opacity" : opacity ,
125128 "uv" : textureRef [ "uv" ] ,
126- "uv_size" : textureRef [ "uv_size" ] ,
127- "croppable" : textureRef [ "croppable" ]
129+ "uv_size" : textureRef [ "uv_size" ]
128130 } ;
129131 allTextureFragments . add ( textureFragment ) ;
130132 textureImageIndices . push ( allTextureFragments . indexOf ( textureFragment ) ) ;
@@ -146,7 +148,7 @@ export default class TextureAtlas extends AsyncFactory {
146148 let imageUvs = await this . #stitchTextureAtlas( imageFragments ) ;
147149 console . log ( "Image UVs:" , imageUvs ) ;
148150
149- this . textures = textureImageIndices . map ( i => imageUvs [ i ] ) ;
151+ this . uvs = textureImageIndices . map ( i => imageUvs [ i ] ) ;
150152 }
151153
152154 /**
@@ -306,7 +308,7 @@ export default class TextureAtlas extends AsyncFactory {
306308 return { imageData, imageIsTga, imageNotFound } ;
307309 } ) ) ;
308310 let imageDataByTexturePath = new Map ( allTexturePaths . map ( ( texturePath , i ) => [ texturePath , allImageData [ i ] ] ) ) ;
309- return await Promise . all ( Array . from ( textureFragments ) . map ( async ( { texturePath, tint, tint_like_png : tintLikePng , opacity, uv : sourceUv , uv_size : uvSize , croppable } ) => {
311+ return await Promise . all ( Array . from ( textureFragments ) . map ( async ( { texturePath, tint, tint_like_png : tintLikePng , opacity, uv : sourceUv , uv_size : uvSize } ) => {
310312 let { imageData, imageIsTga, imageNotFound } = imageDataByTexturePath . get ( texturePath ) ;
311313 if ( imageNotFound ) {
312314 sourceUv = [ 0 , 0 ] ;
@@ -336,9 +338,8 @@ export default class TextureAtlas extends AsyncFactory {
336338 let sourceY = sourceUv [ 1 ] * imageH ;
337339 let w = uvSize [ 0 ] * imageW ;
338340 let h = uvSize [ 1 ] * imageH ;
339- let crop ;
340- // croppable = false;
341- if ( croppable ) {
341+ let crop = null ;
342+ if ( true ) {
342343 let old = { sourceX, sourceY, w, h } ;
343344 let extremePixels = this . #findMostExtremePixels( image , sourceX , sourceY , w , h ) ;
344345 sourceX = extremePixels [ "minX" ] ;
@@ -353,6 +354,8 @@ export default class TextureAtlas extends AsyncFactory {
353354 } ;
354355 if ( crop [ "x" ] != 0 || crop [ "y" ] != 0 || crop [ "w" ] != 1 || crop [ "h" ] != 1 ) {
355356 console . debug ( `Cropped part of image ${ texturePath } to` , crop ) ;
357+ } else {
358+ crop = null ;
356359 }
357360 }
358361 let imageFragment = {
@@ -362,7 +365,7 @@ export default class TextureAtlas extends AsyncFactory {
362365 "w" : w ,
363366 "h" : h
364367 } ;
365- if ( croppable ) {
368+ if ( crop ) {
366369 imageFragment [ "crop" ] = crop ;
367370 }
368371 return imageFragment ;
@@ -396,7 +399,7 @@ export default class TextureAtlas extends AsyncFactory {
396399 imageFragment [ "h" ] = ceil ( imageFragment [ "h" ] ) ;
397400 }
398401 } ) ;
399- let imageFragments2 = imageFragments . map ( imageFragment => ( { ... imageFragment } ) ) ;
402+ let imageFragments2 = imageFragments . map ( imageFragment => ( { ...imageFragment } ) ) ;
400403 let packing1 = potpack ( imageFragments ) ;
401404 let packing2 = potpack ( imageFragments2 . sort ( ( a , b ) => b . h - a . h || b . w - a . w ) ) ; // width presort
402405 let packing = packing1 . fill > packing2 . fill ? packing1 : packing2 ; // In my testing on 100 structures, 10 times no width presort was better, 17 times width presort was better, and the rest they were equal. On average, width presorting improved space efficiency by 0.1385%. Since potpack takes just a couple ms, it's best to look at both and take the better one.
@@ -432,10 +435,14 @@ export default class TextureAtlas extends AsyncFactory {
432435 return imageUv ;
433436 } ) ;
434437
438+ ctx = can . getContext ( "2d" ) ;
439+ ctx . fillStyle = "#00f3" ;
440+ // ctx.fillRect(0, 0, can.width, can.height);
435441 if ( this . config . TEXTURE_OUTLINE_WIDTH != 0 ) {
436442 can = TextureAtlas . addTextureOutlines ( can , imageFragments , this . config ) ;
437443 }
438444
445+
439446 if ( this . config . MULTIPLE_OPACITIES ) {
440447 let opacities = range ( 4 , 10 ) . map ( x => x / 10 ) ; // lowest is 40% opacity. note that we do division after to avoid floating-point errors.
441448 this . imageBlobs = await Promise . all ( opacities . map ( async opacity => [ `hologram_opacity_${ opacity } ` , await this . #setCanvasOpacity( can , opacity ) . convertToBlob ( ) ] ) ) ;
@@ -461,6 +468,7 @@ export default class TextureAtlas extends AsyncFactory {
461468 let can = new OffscreenCanvas ( imageW , imageH ) ;
462469 let ctx = can . getContext ( "2d" ) ;
463470 ctx . drawImage ( image , startX , startY , imageW , imageH , 0 , 0 , imageW , imageH ) ;
471+ // console.log(imageW, imageH, can.width, can.height)
464472 let imageData = ctx . getImageData ( 0 , 0 , imageW , imageH ) . data ;
465473
466474 let minX = imageW , minY = imageH , maxX = 0 , maxY = 0 ;
@@ -599,18 +607,4 @@ export default class TextureAtlas extends AsyncFactory {
599607 }
600608}
601609
602- /**
603- * @typedef {import("./HoloPrint.js").TextureReference } TextureReference
604- */
605- /**
606- * @typedef {import("./HoloPrint.js").TextureFragment } TextureFragment
607- */
608- /**
609- * @typedef {import("./HoloPrint.js").ImageFragment } ImageFragment
610- */
611- /**
612- * @typedef {import("./HoloPrint.js").HoloPrintConfig } HoloPrintConfig
613- */
614- /**
615- * @typedef {import("./HoloPrint.js").Vec3 } Vec3
616- */
610+ /** @import { TextureReference, TextureFragment, ImageFragment, HoloPrintConfig, Vec3, Vec2, Rectangle } from "./HoloPrint.js" */
0 commit comments