-
Notifications
You must be signed in to change notification settings - Fork 406
Expand file tree
/
Copy pathTiledImageSource.js
More file actions
91 lines (63 loc) · 1.91 KB
/
Copy pathTiledImageSource.js
File metadata and controls
91 lines (63 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import { DataCache } from '../utils/DataCache.js';
import { TilingScheme } from '../utils/TilingScheme.js';
import { SRGBColorSpace, Texture, TextureUtils } from 'three';
// TODO: support queries for detail at level - ie projected pixel size for geometric error mapping
// Goes here or in "TilingScheme"?
export class TiledImageSource extends DataCache {
constructor( options = {} ) {
super();
const {
fetchOptions = {}
} = options;
this.tiling = new TilingScheme();
this.fetchOptions = fetchOptions;
this.fetchData = ( ...args ) => fetch( ...args );
}
// async function for initializing the tiled image set
init() {}
// helper for processing the buffer into a texture
async processBufferToTexture( buffer ) {
// pre-flip the y axis
const blob = new Blob( [ buffer ] );
const imageBitmap = await createImageBitmap( blob, {
premultiplyAlpha: 'none',
colorSpaceConversion: 'none',
imageOrientation: 'flipY',
} );
const texture = new Texture( imageBitmap );
texture.generateMipmaps = false;
texture.colorSpace = SRGBColorSpace;
texture.needsUpdate = true;
return texture;
}
getMemoryUsage( tex ) {
const { format, type, image, generateMipmaps } = tex;
const { width, height } = image;
const bytes = TextureUtils.getByteLength( width, height, format, type );
return generateMipmaps ? bytes * 4 / 3 : bytes;
}
// fetch the item with the given key fields
fetchItem( tokens, signal ) {
const fetchOptions = {
...this.fetchOptions,
signal,
};
const url = this.getUrl( ...tokens );
return this
.fetchData( url, fetchOptions )
.then( res => res.arrayBuffer() )
.then( buffer => this.processBufferToTexture( buffer ) );
}
// dispose of the item that was fetched
disposeItem( texture ) {
if ( ! texture ) {
return;
}
texture.dispose();
if ( texture.image instanceof ImageBitmap ) {
texture.image.close();
}
}
getUrl( ...args ) {
}
}