Skip to content

Commit 95c59d7

Browse files
authored
Merge pull request #8 from satoshi7190/fix/inline-webworker
Add inline web worker for image processing
2 parents c1cb3d8 + 720eef4 commit 95c59d7

File tree

2 files changed

+20
-16
lines changed

2 files changed

+20
-16
lines changed

src/terrain.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import type { RasterDEMSourceSpecification } from 'maplibre-gl';
22
import maplibregl from 'maplibre-gl';
3+
import { workerCode } from './worker';
4+
35

46
const loadImage = async (
57
src: string,
@@ -82,9 +84,8 @@ class WorkerProtocol {
8284
};
8385
}
8486

85-
const worker = new Worker(new URL('./worker.ts', import.meta.url), {
86-
type: 'module',
87-
});
87+
const blob = new Blob([workerCode], { type: 'application/javascript' });
88+
const worker = new Worker(URL.createObjectURL(blob));
8889
const workerProtocol = new WorkerProtocol(worker);
8990

9091
type Options = {

src/worker.ts

+16-13
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
const vsSource = `#version 300 es
1+
// インラインWebワーカーのコードを文字列で定義
2+
export const workerCode = `
3+
const vsSource = \`#version 300 es
24
in vec4 a_position;
35
out vec2 v_tex_coord;
46
57
void main() {
68
gl_Position = a_position;
79
v_tex_coord = vec2(a_position.x * 0.5 + 0.5, a_position.y * -0.5 + 0.5);
810
}
9-
`;
11+
\`;
1012
11-
const fsSource = `#version 300 es
13+
const fsSource = \`#version 300 es
1214
#ifdef GL_FRAGMENT_PRECISION_HIGH
1315
precision highp float;
1416
#else
@@ -44,24 +46,24 @@ const fsSource = `#version 300 es
4446
float(is_valid)
4547
);
4648
}
47-
`;
49+
\`;
4850
49-
let gl: WebGL2RenderingContext | null = null;
50-
let program: WebGLProgram | null = null;
51-
let positionBuffer: WebGLBuffer | null = null;
52-
let heightMapLocation: WebGLUniformLocation | null = null;
51+
let gl = null;
52+
let program = null;
53+
let positionBuffer = null;
54+
let heightMapLocation = null;
5355
54-
const initWebGL = (canvas: OffscreenCanvas) => {
56+
const initWebGL = (canvas) => {
5557
gl = canvas.getContext('webgl2');
5658
if (!gl) {
5759
throw new Error('WebGL not supported');
5860
}
5961
6062
const loadShader = (
61-
gl: WebGL2RenderingContext,
62-
type: number,
63-
source: string,
64-
): WebGLShader | null => {
63+
gl,
64+
type,
65+
source,
66+
) => {
6567
const shader = gl.createShader(type);
6668
if (!shader) {
6769
console.error('Unable to create shader');
@@ -155,3 +157,4 @@ self.onmessage = async (e) => {
155157
}
156158
}
157159
};
160+
`;

0 commit comments

Comments
 (0)