-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCanvasTextureTiledLayer.ts
More file actions
53 lines (44 loc) · 1.7 KB
/
Copy pathCanvasTextureTiledLayer.ts
File metadata and controls
53 lines (44 loc) · 1.7 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
/**
* This is a demo of how to extend ShaderTiledLayer
* TextureTiledLayer is a layer that simply contains a texture per tile
*/
import { type RawShaderMaterial, CanvasTexture, type ShaderMaterialParameters } from "three";
import { BaseShaderTiledLayer } from "../core/BaseShaderTiledLayer";
import type { TileIndex } from "../core/tools";
// @ts-ignore
import fragmentShader from "../shaders/texture-tile.f.glsl?raw";
import { type TextureMaker, TileTextureManager } from "../core/TileTextureManager";
export type CanvasTextureTiledLayerOptions = {
minZoom?: number;
maxZoom?: number;
canvasMaker: (tileIndex: TileIndex) => Promise<HTMLCanvasElement | OffscreenCanvas>;
};
export class CanvasTextureTiledLayer extends BaseShaderTiledLayer {
private readonly tileTextureManager: TileTextureManager;
private readonly textureMaker: TextureMaker;
constructor(id: string, options: CanvasTextureTiledLayerOptions) {
const canvasMaker = options.canvasMaker;
super(id, {
minZoom: options.minZoom ?? 0,
maxZoom: options.maxZoom ?? 22,
});
this.textureMaker = async (tileIndex: TileIndex) => {
const canvas = await canvasMaker(tileIndex);
return new CanvasTexture(canvas);
};
this.tileTextureManager = new TileTextureManager();
}
// Must be implemented
protected onSetTileShaderParameters(_tileIndex: TileIndex): ShaderMaterialParameters {
return {
uniforms: {
u_tex: { value: null },
},
fragmentShader: fragmentShader,
};
}
// Must be implemented
async onTileUpdate(tileIndex: TileIndex, material: RawShaderMaterial) {
material.uniforms.u_tex.value = await this.tileTextureManager.getTexture(tileIndex, this.textureMaker);
}
}