-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathwebgl.js
More file actions
61 lines (57 loc) · 1.67 KB
/
webgl.js
File metadata and controls
61 lines (57 loc) · 1.67 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
import { ImageLoader } from '@loaders.gl/images';
import { parse } from '@loaders.gl/core';
import { Texture2D } from '@luma.gl/core';
import GL from '@luma.gl/constants';
// WIP: Intended to work with 16 bit textures, but doesn't currently work
// See https://github.com/kylebarron/deck.gl-raster/issues/16
export async function bytesToTextures(
gl,
urls,
{ ArrayType = Uint8Array, scale = 1 }
) {
const tileSize = scale * 256;
const arrays = await Promise.all(
urls.map(url => fetch(url).then(res => res.arrayBuffer()))
);
const textures = arrays.map(array => {
const data = new ArrayType(array);
return new Texture2D(gl, {
data,
width: tileSize,
height: tileSize,
// Need to change these params to work with uints
// parameters: DEFAULT_TEXTURE_PARAMETERS,
format: GL.RED_INTEGER,
dataFormat: GL.R16UI,
type: GL.UNSIGNED_SHORT,
mipmaps: true,
});
});
return textures;
}
export async function loadRgbImage(url) {
const { image, assets } = await loadImageUrl(url);
const imageData = {
data: image,
format: GL.RGB,
};
return { imageData, assets };
}
export async function loadSingleBandImage(url) {
const { image, assets } = await loadImageUrl(url);
const imageData = {
data: image,
// Colormaps are 10 pixels high
// Load colormaps as RGB; all others as LUMINANCE
format: image && image.height === 10 ? GL.RGB : GL.LUMINANCE,
};
return { imageData, assets };
}
async function loadImageUrl(url) {
const res = await fetch(url);
const assets = JSON.parse(res.headers.get('x-assets')) || [];
return {
assets,
image: await parse(res.arrayBuffer(), ImageLoader),
};
}