@kubohiroya/turbowarp-camera-calibration 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +373 -0
- package/README.ja.md +289 -0
- package/README.md +452 -0
- package/THIRD_PARTY_NOTICES.md +29 -0
- package/dist/camera-calibration.js +8676 -0
- package/dist/extension-manifest.json +200 -0
- package/docs/.nojekyll +0 -0
- package/docs/architecture.ja.md +153 -0
- package/docs/architecture.md +159 -0
- package/docs/index.html +124 -0
- package/package.json +80 -0
- package/schemas/camera-intrinsics-v1.schema.json +72 -0
- package/schemas/extension-manifest.schema.json +49 -0
- package/src/block-definitions.json +180 -0
- package/src/calibration/camera-source.ts +107 -0
- package/src/calibration/controller.ts +717 -0
- package/src/calibration/opencv-backend.ts +304 -0
- package/src/calibration/profile.ts +356 -0
- package/src/calibration/types.ts +85 -0
- package/src/config.ts +12 -0
- package/src/extension-manifest.ts +149 -0
- package/src/extension.ts +217 -0
- package/src/globals.d.ts +38 -0
- package/src/index.ts +8 -0
- package/templates/README.ja.md +183 -0
- package/templates/README.md +198 -0
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
/** A printed chessboard target, measured by its inner corner grid. */
|
|
2
|
+
export interface CalibrationBoard {
|
|
3
|
+
columns: number;
|
|
4
|
+
rows: number;
|
|
5
|
+
squareSizeMeters: number;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
/** One still image taken from the leased camera. */
|
|
9
|
+
export interface CalibrationFrame {
|
|
10
|
+
element: HTMLVideoElement;
|
|
11
|
+
width: number;
|
|
12
|
+
height: number;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface CalibrationCorner {
|
|
16
|
+
x: number;
|
|
17
|
+
y: number;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** One accepted board view, kept until the session solves or is cancelled. */
|
|
21
|
+
export interface CalibrationSample {
|
|
22
|
+
corners: CalibrationCorner[];
|
|
23
|
+
quality: number;
|
|
24
|
+
coverage: number;
|
|
25
|
+
sharpness: number;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Distortion models this extension can produce and validate, named after the
|
|
30
|
+
* OpenCV coefficient layouts. The coefficient count identifies the model.
|
|
31
|
+
*/
|
|
32
|
+
export type DistortionModel =
|
|
33
|
+
| 'none'
|
|
34
|
+
| 'opencv-plumb-bob'
|
|
35
|
+
| 'opencv-rational'
|
|
36
|
+
| 'opencv-thin-prism'
|
|
37
|
+
| 'opencv-tilted';
|
|
38
|
+
|
|
39
|
+
/** Coefficient counts OpenCV emits for each supported distortion model. */
|
|
40
|
+
export const DISTORTION_COEFFICIENT_COUNTS: Readonly<Record<DistortionModel, readonly number[]>> =
|
|
41
|
+
Object.freeze({
|
|
42
|
+
'none': [0],
|
|
43
|
+
'opencv-plumb-bob': [4, 5],
|
|
44
|
+
'opencv-rational': [8],
|
|
45
|
+
'opencv-thin-prism': [12],
|
|
46
|
+
'opencv-tilted': [14]
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* The intrinsic solution. External pose is deliberately absent: this extension
|
|
51
|
+
* calibrates one camera's optics and never reports where that camera stands.
|
|
52
|
+
*/
|
|
53
|
+
export interface CalibrationSolveResult {
|
|
54
|
+
intrinsicMatrix: number[];
|
|
55
|
+
distortionModel: DistortionModel;
|
|
56
|
+
distortionCoefficients: number[];
|
|
57
|
+
reprojectionErrorPx: number;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* The solver seam. Everything above this interface is arithmetic-free control
|
|
62
|
+
* flow, so tests drive the whole procedure without loading OpenCV.
|
|
63
|
+
*/
|
|
64
|
+
export interface CalibrationBackendPort {
|
|
65
|
+
readonly name: string;
|
|
66
|
+
captureSample(
|
|
67
|
+
frame: CalibrationFrame,
|
|
68
|
+
board: CalibrationBoard
|
|
69
|
+
): Promise<CalibrationSample | undefined>;
|
|
70
|
+
solve(
|
|
71
|
+
samples: readonly CalibrationSample[],
|
|
72
|
+
board: CalibrationBoard,
|
|
73
|
+
imageWidth: number,
|
|
74
|
+
imageHeight: number
|
|
75
|
+
): Promise<CalibrationSolveResult>;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Creates the solver on first use. The name is available without creating it so
|
|
80
|
+
* that the backend reporter never loads the OpenCV runtime.
|
|
81
|
+
*/
|
|
82
|
+
export interface CalibrationBackendFactory {
|
|
83
|
+
readonly name: string;
|
|
84
|
+
create(): Promise<CalibrationBackendPort>;
|
|
85
|
+
}
|
package/src/config.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export const extensionConfig = {
|
|
2
|
+
id: 'kubohiroyacameracalibration',
|
|
3
|
+
slug: 'camera-calibration',
|
|
4
|
+
name: 'TurboWarp-Camera-Calibration',
|
|
5
|
+
description: 'Calibrate a shared Camera Source camera and publish its intrinsic calibration profile.',
|
|
6
|
+
author: 'Hiroya Kubo',
|
|
7
|
+
license: 'MPL-2.0',
|
|
8
|
+
unsandboxed: true,
|
|
9
|
+
docsURI: 'https://kubohiroya.github.io/turbowarp-camera-calibration/',
|
|
10
|
+
blockIconURI:
|
|
11
|
+
'data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCA0OCA0OCI+PHJlY3QgeD0iNCIgeT0iNCIgd2lkdGg9IjQwIiBoZWlnaHQ9IjQwIiByeD0iNCIgZmlsbD0iI2ZmZmZmZiIgc3Ryb2tlPSIjNEM5N0ZGIiBzdHJva2Utd2lkdGg9IjIiLz48ZyBmaWxsPSIjNEM5N0ZGIj48cmVjdCB4PSI2IiB5PSI2IiB3aWR0aD0iOSIgaGVpZ2h0PSI5Ii8+PHJlY3QgeD0iMjQiIHk9IjYiIHdpZHRoPSI5IiBoZWlnaHQ9IjkiLz48cmVjdCB4PSIxNSIgeT0iMTUiIHdpZHRoPSI5IiBoZWlnaHQ9IjkiLz48cmVjdCB4PSIzMyIgeT0iMTUiIHdpZHRoPSI5IiBoZWlnaHQ9IjkiLz48cmVjdCB4PSI2IiB5PSIyNCIgd2lkdGg9IjkiIGhlaWdodD0iOSIvPjxyZWN0IHg9IjI0IiB5PSIyNCIgd2lkdGg9IjkiIGhlaWdodD0iOSIvPjxyZWN0IHg9IjE1IiB5PSIzMyIgd2lkdGg9IjkiIGhlaWdodD0iOSIvPjxyZWN0IHg9IjMzIiB5PSIzMyIgd2lkdGg9IjkiIGhlaWdodD0iOSIvPjwvZz48L3N2Zz4='
|
|
12
|
+
} as const;
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import type {Plugin} from 'vite';
|
|
2
|
+
|
|
3
|
+
export const EXTENSION_MANIFEST_FORMAT_VERSION = 1 as const;
|
|
4
|
+
|
|
5
|
+
export interface ExtensionManifestArgument {
|
|
6
|
+
id: string;
|
|
7
|
+
type: string;
|
|
8
|
+
menu?: string;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface ExtensionManifestBlock {
|
|
12
|
+
opcode: string;
|
|
13
|
+
blockType: string;
|
|
14
|
+
arguments: ExtensionManifestArgument[];
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface ExtensionManifestMenu {
|
|
18
|
+
id: string;
|
|
19
|
+
acceptReporters: boolean;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface ExtensionManifest {
|
|
23
|
+
formatVersion: typeof EXTENSION_MANIFEST_FORMAT_VERSION;
|
|
24
|
+
id: string;
|
|
25
|
+
blocks: ExtensionManifestBlock[];
|
|
26
|
+
menus: ExtensionManifestMenu[];
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface ExtensionManifestPluginOptions {
|
|
30
|
+
id: string;
|
|
31
|
+
definitions: unknown;
|
|
32
|
+
fileName?: string;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function createExtensionManifest(id: string, definitions: unknown): ExtensionManifest {
|
|
36
|
+
if (!/^[a-z0-9]+$/.test(id)) {
|
|
37
|
+
throw new TypeError('Extension manifest ID must contain only lowercase letters and numbers.');
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const source = requireRecord(definitions, 'Block definitions');
|
|
41
|
+
const sourceBlocks = source.blocks;
|
|
42
|
+
if (!Array.isArray(sourceBlocks)) {
|
|
43
|
+
throw new TypeError('Block definitions must contain a blocks array.');
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const menus = normalizeMenus(source.menus);
|
|
47
|
+
const menuIds = new Set(menus.map((menu) => menu.id));
|
|
48
|
+
const seenOpcodes = new Set<string>();
|
|
49
|
+
const blocks = sourceBlocks.map((block, index) => {
|
|
50
|
+
const normalized = normalizeBlock(block, index, menuIds);
|
|
51
|
+
if (seenOpcodes.has(normalized.opcode)) {
|
|
52
|
+
throw new TypeError(`Duplicate block opcode: ${normalized.opcode}`);
|
|
53
|
+
}
|
|
54
|
+
seenOpcodes.add(normalized.opcode);
|
|
55
|
+
return normalized;
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
return {
|
|
59
|
+
formatVersion: EXTENSION_MANIFEST_FORMAT_VERSION,
|
|
60
|
+
id,
|
|
61
|
+
blocks: blocks.sort((left, right) => compareIds(left.opcode, right.opcode)),
|
|
62
|
+
menus
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export function serializeExtensionManifest(id: string, definitions: unknown): string {
|
|
67
|
+
return `${JSON.stringify(createExtensionManifest(id, definitions), null, 2)}\n`;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function extensionManifestPlugin(options: ExtensionManifestPluginOptions): Plugin {
|
|
71
|
+
return {
|
|
72
|
+
name: 'extension-api-manifest',
|
|
73
|
+
apply: 'build',
|
|
74
|
+
enforce: 'post',
|
|
75
|
+
generateBundle() {
|
|
76
|
+
this.emitFile({
|
|
77
|
+
type: 'asset',
|
|
78
|
+
fileName: options.fileName ?? 'extension-manifest.json',
|
|
79
|
+
source: serializeExtensionManifest(options.id, options.definitions)
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function normalizeBlock(
|
|
86
|
+
value: unknown,
|
|
87
|
+
index: number,
|
|
88
|
+
menuIds: ReadonlySet<string>
|
|
89
|
+
): ExtensionManifestBlock {
|
|
90
|
+
const block = requireRecord(value, `Block at index ${index}`);
|
|
91
|
+
const opcode = requireNonEmptyString(block.opcode, `Block at index ${index} opcode`);
|
|
92
|
+
const blockType = requireNonEmptyString(block.blockType, `Block ${opcode} blockType`);
|
|
93
|
+
const sourceArguments = block.arguments ?? {};
|
|
94
|
+
const argumentRecord = requireRecord(sourceArguments, `Block ${opcode} arguments`);
|
|
95
|
+
const argumentsList = Object.entries(argumentRecord).map(([argumentId, argument]) => {
|
|
96
|
+
requireNonEmptyString(argumentId, `Block ${opcode} argument ID`);
|
|
97
|
+
const definition = requireRecord(argument, `Block ${opcode} argument ${argumentId}`);
|
|
98
|
+
const type = requireNonEmptyString(
|
|
99
|
+
definition.type,
|
|
100
|
+
`Block ${opcode} argument ${argumentId} type`
|
|
101
|
+
);
|
|
102
|
+
const menu = definition.menu;
|
|
103
|
+
if (menu !== undefined && (typeof menu !== 'string' || !menuIds.has(menu))) {
|
|
104
|
+
throw new TypeError(`Block ${opcode} argument ${argumentId} references unknown menu: ${menu}`);
|
|
105
|
+
}
|
|
106
|
+
return menu === undefined ? {id: argumentId, type} : {id: argumentId, type, menu};
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
return {
|
|
110
|
+
opcode,
|
|
111
|
+
blockType,
|
|
112
|
+
arguments: argumentsList.sort((left, right) => compareIds(left.id, right.id))
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function normalizeMenus(value: unknown): ExtensionManifestMenu[] {
|
|
117
|
+
const menuRecord = requireRecord(value ?? {}, 'Block definition menus');
|
|
118
|
+
return Object.entries(menuRecord)
|
|
119
|
+
.map(([id, menu]) => {
|
|
120
|
+
requireNonEmptyString(id, 'Menu ID');
|
|
121
|
+
const definition = requireRecord(menu, `Menu ${id}`);
|
|
122
|
+
const acceptReporters = definition.acceptReporters ?? false;
|
|
123
|
+
if (typeof acceptReporters !== 'boolean') {
|
|
124
|
+
throw new TypeError(`Menu ${id} acceptReporters must be a boolean.`);
|
|
125
|
+
}
|
|
126
|
+
return {id, acceptReporters};
|
|
127
|
+
})
|
|
128
|
+
.sort((left, right) => compareIds(left.id, right.id));
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function requireRecord(value: unknown, label: string): Record<string, unknown> {
|
|
132
|
+
if (typeof value !== 'object' || value === null || Array.isArray(value)) {
|
|
133
|
+
throw new TypeError(`${label} must be an object.`);
|
|
134
|
+
}
|
|
135
|
+
return value as Record<string, unknown>;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function requireNonEmptyString(value: unknown, label: string): string {
|
|
139
|
+
if (typeof value !== 'string' || value.length === 0) {
|
|
140
|
+
throw new TypeError(`${label} must be a non-empty string.`);
|
|
141
|
+
}
|
|
142
|
+
return value;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
function compareIds(left: string, right: string): number {
|
|
146
|
+
if (left < right) return -1;
|
|
147
|
+
if (left > right) return 1;
|
|
148
|
+
return 0;
|
|
149
|
+
}
|
package/src/extension.ts
ADDED
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
import {featureFlags} from '../config/feature-flags.js';
|
|
2
|
+
import {extensionConfig} from './config';
|
|
3
|
+
import definitions from './block-definitions.json';
|
|
4
|
+
import {CameraCalibrationController} from './calibration/controller.js';
|
|
5
|
+
import {openCvBackendFactory} from './calibration/opencv-backend.js';
|
|
6
|
+
import type {CalibrationBackendFactory} from './calibration/types.js';
|
|
7
|
+
|
|
8
|
+
type BlockTypeName = 'COMMAND' | 'REPORTER' | 'BOOLEAN';
|
|
9
|
+
type ArgumentTypeName = 'STRING' | 'NUMBER';
|
|
10
|
+
type BlockFeature = 'always' | 'cameraCalibrationV1';
|
|
11
|
+
|
|
12
|
+
interface DefinitionArgument {
|
|
13
|
+
type: ArgumentTypeName;
|
|
14
|
+
defaultValue: string | number;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
interface BlockDefinition {
|
|
18
|
+
opcode: string;
|
|
19
|
+
feature: BlockFeature;
|
|
20
|
+
blockType: BlockTypeName;
|
|
21
|
+
text: string;
|
|
22
|
+
description: string;
|
|
23
|
+
arguments: Record<string, DefinitionArgument>;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface CameraCalibrationExtensionOptions {
|
|
27
|
+
runtime?: TurboWarpRuntime;
|
|
28
|
+
backend?: CalibrationBackendFactory;
|
|
29
|
+
enabled?: boolean;
|
|
30
|
+
nowMilliseconds?: () => number;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const blockDefinitions = definitions.blocks as readonly BlockDefinition[];
|
|
34
|
+
const defaultCameraId = 'default';
|
|
35
|
+
|
|
36
|
+
/** The state reported for a camera that has no calibration session. */
|
|
37
|
+
export const IDLE_CALIBRATION_STATE = 'idle';
|
|
38
|
+
|
|
39
|
+
function normalizeId(value: unknown, fallback = defaultCameraId): string {
|
|
40
|
+
const text = String(value ?? '').trim();
|
|
41
|
+
return text || fallback;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export class CameraCalibrationExtension implements TurboWarpExtension {
|
|
45
|
+
private readonly controller: CameraCalibrationController;
|
|
46
|
+
private readonly runtime: TurboWarpRuntime;
|
|
47
|
+
private readonly enabled: boolean;
|
|
48
|
+
|
|
49
|
+
public constructor(options: CameraCalibrationExtensionOptions = {}) {
|
|
50
|
+
this.runtime = options.runtime ?? Scratch.vm.runtime;
|
|
51
|
+
this.enabled = options.enabled ?? featureFlags.cameraCalibrationV1;
|
|
52
|
+
const nowMilliseconds = options.nowMilliseconds;
|
|
53
|
+
this.controller = new CameraCalibrationController({
|
|
54
|
+
runtime: this.runtime,
|
|
55
|
+
backend: options.backend ?? openCvBackendFactory,
|
|
56
|
+
...(nowMilliseconds ? {nowMilliseconds} : {})
|
|
57
|
+
});
|
|
58
|
+
this.runtime.on?.('PROJECT_STOP_ALL', this.handleProjectBoundary);
|
|
59
|
+
this.runtime.on?.('PROJECT_LOADED', this.handleProjectBoundary);
|
|
60
|
+
this.runtime.on?.('RUNTIME_DISPOSED', this.handleDisposed);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
public getInfo(): Record<string, unknown> {
|
|
64
|
+
return {
|
|
65
|
+
id: extensionConfig.id,
|
|
66
|
+
name: Scratch.translate(definitions.extensionName),
|
|
67
|
+
docsURI: extensionConfig.docsURI,
|
|
68
|
+
blockIconURI: extensionConfig.blockIconURI,
|
|
69
|
+
blocks: blockDefinitions
|
|
70
|
+
.filter((block) => this.blockEnabled(block.feature))
|
|
71
|
+
.map((block) => this.toScratchBlock(block))
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
public async startCameraCalibration(args: {
|
|
76
|
+
CAMERA_ID: unknown;
|
|
77
|
+
CALIBRATION_ID: unknown;
|
|
78
|
+
COLUMNS: unknown;
|
|
79
|
+
ROWS: unknown;
|
|
80
|
+
SQUARE_METERS: unknown;
|
|
81
|
+
MAX_ERROR_PX: unknown;
|
|
82
|
+
}): Promise<void> {
|
|
83
|
+
this.requireEnabled();
|
|
84
|
+
await this.controller.start({
|
|
85
|
+
cameraId: normalizeId(args.CAMERA_ID),
|
|
86
|
+
calibrationId: Scratch.Cast.toString(args.CALIBRATION_ID).trim(),
|
|
87
|
+
board: {
|
|
88
|
+
columns: Scratch.Cast.toNumber(args.COLUMNS),
|
|
89
|
+
rows: Scratch.Cast.toNumber(args.ROWS),
|
|
90
|
+
squareSizeMeters: Scratch.Cast.toNumber(args.SQUARE_METERS)
|
|
91
|
+
},
|
|
92
|
+
maximumReprojectionErrorPx: Scratch.Cast.toNumber(args.MAX_ERROR_PX)
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
public async addCameraCalibrationSample(args: {CAMERA_ID: unknown}): Promise<void> {
|
|
97
|
+
this.requireEnabled();
|
|
98
|
+
await this.controller.addSample(normalizeId(args.CAMERA_ID));
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
public async solveCameraCalibration(args: {CAMERA_ID: unknown}): Promise<void> {
|
|
102
|
+
this.requireEnabled();
|
|
103
|
+
await this.controller.solve(normalizeId(args.CAMERA_ID));
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
public async cancelCameraCalibration(args: {CAMERA_ID: unknown}): Promise<void> {
|
|
107
|
+
await this.controller.cancel(normalizeId(args.CAMERA_ID));
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
public async cleanupCameraCalibration(args: {CAMERA_ID: unknown}): Promise<void> {
|
|
111
|
+
await this.controller.cleanup(normalizeId(args.CAMERA_ID));
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
public async publishCameraCalibration(args: {CAMERA_ID: unknown}): Promise<void> {
|
|
115
|
+
this.requireEnabled();
|
|
116
|
+
await this.controller.publishProfile(normalizeId(args.CAMERA_ID));
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
public async importCameraCalibration(args: {
|
|
120
|
+
JSON: unknown;
|
|
121
|
+
CAMERA_ID: unknown;
|
|
122
|
+
}): Promise<void> {
|
|
123
|
+
this.requireEnabled();
|
|
124
|
+
await this.controller.importProfile(
|
|
125
|
+
normalizeId(args.CAMERA_ID),
|
|
126
|
+
Scratch.Cast.toString(args.JSON)
|
|
127
|
+
);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
public cameraCalibrationJsonValid(args: {JSON: unknown; CAMERA_ID: unknown}): boolean {
|
|
131
|
+
if (!this.enabled) return false;
|
|
132
|
+
return this.controller.validateProfile(
|
|
133
|
+
normalizeId(args.CAMERA_ID),
|
|
134
|
+
Scratch.Cast.toString(args.JSON)
|
|
135
|
+
);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
public cameraCalibrationReady(args: {CAMERA_ID: unknown}): boolean {
|
|
139
|
+
return this.enabled && this.controller.ready(normalizeId(args.CAMERA_ID));
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
public cameraCalibrationState(args: {CAMERA_ID?: unknown} = {}): string {
|
|
143
|
+
if (!this.enabled) return IDLE_CALIBRATION_STATE;
|
|
144
|
+
return this.controller.state(normalizeId(args.CAMERA_ID));
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
public cameraCalibrationBackend(): string {
|
|
148
|
+
return this.enabled ? this.controller.backend() : '';
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
public cameraCalibrationSampleCount(args: {CAMERA_ID: unknown}): number {
|
|
152
|
+
return this.enabled ? this.controller.sampleCount(normalizeId(args.CAMERA_ID)) : 0;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
public cameraCalibrationSampleQuality(args: {CAMERA_ID: unknown}): number {
|
|
156
|
+
return this.enabled ? this.controller.latestSampleQuality(normalizeId(args.CAMERA_ID)) : 0;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
public cameraCalibrationReprojectionError(args: {CAMERA_ID: unknown}): number {
|
|
160
|
+
return this.enabled
|
|
161
|
+
? this.controller.latestReprojectionError(normalizeId(args.CAMERA_ID))
|
|
162
|
+
: 0;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
public cameraCalibrationErrorCode(args: {CAMERA_ID: unknown}): string {
|
|
166
|
+
return this.enabled ? this.controller.errorCode(normalizeId(args.CAMERA_ID)) : '';
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
public cameraCalibrationError(args: {CAMERA_ID: unknown}): string {
|
|
170
|
+
return this.enabled ? this.controller.errorMessage(normalizeId(args.CAMERA_ID)) : '';
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
public cameraCalibrationJson(args: {CAMERA_ID: unknown}): string {
|
|
174
|
+
return this.enabled ? this.controller.profileJson(normalizeId(args.CAMERA_ID)) : '';
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/** Releases every camera lease when the project stops or is replaced. */
|
|
178
|
+
private readonly handleProjectBoundary = (): void => {
|
|
179
|
+
void this.controller.cancelAll();
|
|
180
|
+
};
|
|
181
|
+
|
|
182
|
+
private readonly handleDisposed = (): void => {
|
|
183
|
+
this.runtime.off?.('PROJECT_STOP_ALL', this.handleProjectBoundary);
|
|
184
|
+
this.runtime.off?.('PROJECT_LOADED', this.handleProjectBoundary);
|
|
185
|
+
this.runtime.off?.('RUNTIME_DISPOSED', this.handleDisposed);
|
|
186
|
+
void this.controller.cleanupAll();
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
private blockEnabled(feature: BlockFeature): boolean {
|
|
190
|
+
return feature === 'always' || this.enabled;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
private requireEnabled(): void {
|
|
194
|
+
if (!this.enabled) {
|
|
195
|
+
throw new Error(
|
|
196
|
+
'Camera calibration v1 is disabled. Enable it before the project starts.'
|
|
197
|
+
);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
private toScratchBlock(block: BlockDefinition): Record<string, unknown> {
|
|
202
|
+
return {
|
|
203
|
+
opcode: block.opcode,
|
|
204
|
+
blockType: Scratch.BlockType[block.blockType],
|
|
205
|
+
text: Scratch.translate(block.text),
|
|
206
|
+
arguments: Object.fromEntries(
|
|
207
|
+
Object.entries(block.arguments).map(([name, argument]) => [
|
|
208
|
+
name,
|
|
209
|
+
{
|
|
210
|
+
type: Scratch.ArgumentType[argument.type],
|
|
211
|
+
defaultValue: argument.defaultValue
|
|
212
|
+
}
|
|
213
|
+
])
|
|
214
|
+
)
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
}
|
package/src/globals.d.ts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
interface TurboWarpExtension {
|
|
2
|
+
getInfo(): Record<string, unknown>;
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* The TurboWarp VM runtime. Loaded extensions publish their capability objects
|
|
7
|
+
* on it under an `ext_<extension id>` key.
|
|
8
|
+
*/
|
|
9
|
+
interface TurboWarpRuntime extends Record<string, unknown> {
|
|
10
|
+
ext_kubohiroyacamerasource?: unknown;
|
|
11
|
+
on?: (event: string, listener: () => void) => void;
|
|
12
|
+
off?: (event: string, listener: () => void) => void;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
interface ScratchTranslate {
|
|
16
|
+
(text: string): string;
|
|
17
|
+
(message: {default: string; description?: string}, placeholders?: Record<string, string | number>): string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
interface ScratchApi {
|
|
21
|
+
extensions: {
|
|
22
|
+
unsandboxed: boolean;
|
|
23
|
+
register(extension: TurboWarpExtension): void;
|
|
24
|
+
};
|
|
25
|
+
vm: {
|
|
26
|
+
runtime: TurboWarpRuntime;
|
|
27
|
+
};
|
|
28
|
+
BlockType: Record<'COMMAND' | 'REPORTER' | 'BOOLEAN' | 'HAT', string>;
|
|
29
|
+
ArgumentType: Record<'STRING' | 'NUMBER' | 'BOOLEAN', string>;
|
|
30
|
+
Cast: {
|
|
31
|
+
toString(value: unknown): string;
|
|
32
|
+
toNumber(value: unknown): number;
|
|
33
|
+
toBoolean(value: unknown): boolean;
|
|
34
|
+
};
|
|
35
|
+
translate: ScratchTranslate;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
declare const Scratch: ScratchApi;
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import {extensionConfig} from './config.js';
|
|
2
|
+
import {CameraCalibrationExtension} from './extension.js';
|
|
3
|
+
|
|
4
|
+
if (extensionConfig.unsandboxed && !Scratch.extensions.unsandboxed) {
|
|
5
|
+
throw new Error(`${extensionConfig.name} must run unsandboxed.`);
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
Scratch.extensions.register(new CameraCalibrationExtension());
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
# TurboWarp-<Extension-Name>
|
|
2
|
+
|
|
3
|
+
[English](README.md) | [日本語](README.ja.md)
|
|
4
|
+
|
|
5
|
+
<何をするかを一文で説明>ためのTurboWarp機能拡張です。
|
|
6
|
+
|
|
7
|
+
**利用ガイド:** [English](<PAGES_URL>/)
|
|
8
|
+
|
|
9
|
+
## できること
|
|
10
|
+
|
|
11
|
+
- <利用者ができること1>
|
|
12
|
+
- <利用者ができること2>
|
|
13
|
+
- <利用者ができること3>
|
|
14
|
+
|
|
15
|
+
実装ファイルの説明ではなく、利用者から見た結果を書きます。
|
|
16
|
+
|
|
17
|
+
## ドキュメントとブロックアイコン
|
|
18
|
+
|
|
19
|
+
この機能拡張は、英語版の利用者向けドキュメントをGitHub Pagesで公開します。
|
|
20
|
+
|
|
21
|
+
- `docsURI` はGitHub Pages上の英語版ドキュメント `<PAGES_URL>/` を指します。
|
|
22
|
+
- `blockIconURI` は `data:image/svg+xml;base64,...` 形式の自己完結したSVGとします。
|
|
23
|
+
- ブロックアイコンは機能拡張の内容を直感的に表す、単純で識別しやすい図柄にします。公開版では汎用の仮アイコンをそのまま使用しません。
|
|
24
|
+
|
|
25
|
+
## 動作条件と安全上の注意
|
|
26
|
+
|
|
27
|
+
- <TurboWarp Desktop/Web/Packagerの対応>
|
|
28
|
+
- <Browser APIまたはcompanion extension>
|
|
29
|
+
- <sandbox条件>
|
|
30
|
+
|
|
31
|
+
> [!IMPORTANT]
|
|
32
|
+
> この機能拡張は<理由>のため、サンドボックスなしで実行する必要があります。
|
|
33
|
+
> 信頼できる配布元の機能拡張だけを読み込んでください。
|
|
34
|
+
|
|
35
|
+
sandbox互換の場合は上の注意書きを削除します。
|
|
36
|
+
|
|
37
|
+
## インストール
|
|
38
|
+
|
|
39
|
+
### 完成済みJavaScript
|
|
40
|
+
|
|
41
|
+
1. [`dist/<file>.js`](dist/<file>.js?raw=1)をダウンロードします。
|
|
42
|
+
2. TurboWarpで**機能拡張**を開きます。
|
|
43
|
+
3. **カスタム機能拡張**からfileを読み込みます。
|
|
44
|
+
4. 必要な場合は**サンドボックスなしで実行する**を許可します。
|
|
45
|
+
|
|
46
|
+
完成済みの検証対象JavaScriptをrepositoryへcommitしているため、利用者が
|
|
47
|
+
Node.jsやbuild環境を用意する必要はありません。
|
|
48
|
+
|
|
49
|
+
### npm package
|
|
50
|
+
|
|
51
|
+
検証済みのversionをexact pinします。
|
|
52
|
+
|
|
53
|
+
```bash
|
|
54
|
+
pnpm add --save-exact @kubohiroya/<package>@<version>
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
standalone bundle:
|
|
58
|
+
|
|
59
|
+
```text
|
|
60
|
+
node_modules/@kubohiroya/<package>/dist/<file>.js
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
version固定CDN URL:
|
|
64
|
+
|
|
65
|
+
```text
|
|
66
|
+
https://cdn.jsdelivr.net/npm/@kubohiroya/<package>@<version>/dist/<file>.js
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
`<version>`を手作業で保守する場合は、`package.json`との一致をCIで検査します。
|
|
70
|
+
|
|
71
|
+
## クイックスタート
|
|
72
|
+
|
|
73
|
+
1. <手順1>
|
|
74
|
+
2. <手順2>
|
|
75
|
+
3. <手順3>
|
|
76
|
+
|
|
77
|
+
```text
|
|
78
|
+
<最小のTurboWarp block flow>
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
## ブロックリファレンス
|
|
82
|
+
|
|
83
|
+
block referenceは
|
|
84
|
+
[`src/block-definitions.json`](src/block-definitions.json)から生成します。
|
|
85
|
+
生成範囲を直接編集しないでください。
|
|
86
|
+
|
|
87
|
+
<!-- BEGIN GENERATED BLOCKS -->
|
|
88
|
+
|
|
89
|
+
<自動生成block文書>
|
|
90
|
+
|
|
91
|
+
<!-- END GENERATED BLOCKS -->
|
|
92
|
+
|
|
93
|
+
## 重要な動作
|
|
94
|
+
|
|
95
|
+
| 状況 | 動作 |
|
|
96
|
+
|---|---|
|
|
97
|
+
| <case> | <正確な動作> |
|
|
98
|
+
| project停止 | <cleanup> |
|
|
99
|
+
| project再読込 | <cleanupまたは復元> |
|
|
100
|
+
| 無効な入力 | <errorまたはfallback> |
|
|
101
|
+
|
|
102
|
+
## Composition API
|
|
103
|
+
|
|
104
|
+
Composition APIがないpackageでは、このsectionを削除します。
|
|
105
|
+
|
|
106
|
+
Composition APIをimportしても、standalone TurboWarp機能拡張は自動登録されません。
|
|
107
|
+
|
|
108
|
+
```ts
|
|
109
|
+
import {create<Feature>Composition} from '@kubohiroya/<package>/composition';
|
|
110
|
+
|
|
111
|
+
const feature = create<Feature>Composition({
|
|
112
|
+
// 明示的な依存
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
// capabilityを利用する。
|
|
116
|
+
|
|
117
|
+
feature.releaseAll();
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
所有、取消し、`AbortSignal`、project lifecycle、副作用、browser/network/storage/
|
|
121
|
+
renderer境界、`release()`/`releaseAll()`を説明します。
|
|
122
|
+
|
|
123
|
+
完全なmethod referenceは
|
|
124
|
+
[`docs/composition-api.md`](docs/composition-api.md)へ分けます。
|
|
125
|
+
|
|
126
|
+
## 連携
|
|
127
|
+
|
|
128
|
+
| 利用側 | 関係 |
|
|
129
|
+
|---|---|
|
|
130
|
+
| `<consumer package>` | <required/optional/capability injection> |
|
|
131
|
+
|
|
132
|
+
依存方向はconsumerからproviderとし、private fieldを公開連携方法として書きません。
|
|
133
|
+
|
|
134
|
+
## 互換性
|
|
135
|
+
|
|
136
|
+
| 識別子 | 値 | 安定性 |
|
|
137
|
+
|---|---|---|
|
|
138
|
+
| 製品名 | `TurboWarp-<Extension-Name>` | 利用者向け表示 |
|
|
139
|
+
| repository | `kubohiroya/<repository>` | current source |
|
|
140
|
+
| npm package | `@kubohiroya/<package>` | public package contract |
|
|
141
|
+
| extension ID | `<extensionid>` | SB3に保存。変更にはmigrationが必要 |
|
|
142
|
+
|
|
143
|
+
複数語からなるTurboWarp機能拡張の正式表示名では、すべての構成語をハイフンで連結します。例: `TurboWarp-Asset-Manager`、`TurboWarp-Runtime-Expression`、`TurboWarp-Diagnostic-Overlay`。
|
|
144
|
+
|
|
145
|
+
extension IDまたはopcode変更時はschema-aware migrationを説明し、保存済み識別子が変わった場合にJavaScriptだけの差替えを案内しません。
|
|
146
|
+
|
|
147
|
+
## 開発
|
|
148
|
+
|
|
149
|
+
`engines`のNode.jsと、`packageManager`に宣言したpnpmを使用します。
|
|
150
|
+
|
|
151
|
+
```bash
|
|
152
|
+
corepack enable
|
|
153
|
+
pnpm install --frozen-lockfile
|
|
154
|
+
pnpm check
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
| command | 用途 |
|
|
158
|
+
|---|---|
|
|
159
|
+
| `pnpm dev` | source変更中の継続build |
|
|
160
|
+
| `pnpm test` | test |
|
|
161
|
+
| `pnpm docs` | block文書生成 |
|
|
162
|
+
| `pnpm check:dist` | commit済みbuild artifactの検証 |
|
|
163
|
+
| `pnpm pack:check` | npm package内容の確認 |
|
|
164
|
+
| `pnpm release:check` | release dry run |
|
|
165
|
+
|
|
166
|
+
## リリース
|
|
167
|
+
|
|
168
|
+
1. `CHANGELOG.md`がある場合は更新する。
|
|
169
|
+
2. `package.json`のversionを更新する。
|
|
170
|
+
3. `pnpm check`とrepository固有のrelease checkを実行する。
|
|
171
|
+
4. release PRをmergeする。
|
|
172
|
+
5. 検証済みcommitへ`v<version>`tagを付ける。
|
|
173
|
+
6. 必要に応じて同じversionをnpmへpublishする。
|
|
174
|
+
7. GitHub Release、npm tarball、GitHub Pages、`docsURI`、block icon、CDNを確認する。
|
|
175
|
+
|
|
176
|
+
release consistency checkでは、exact-version README/CDN例に加え、`docsURI`が英語版Pagesを指すこと、`blockIconURI`が`data:image/svg+xml;base64,...`形式であることを検証します。
|
|
177
|
+
|
|
178
|
+
## ライセンス
|
|
179
|
+
|
|
180
|
+
[Mozilla Public License 2.0](LICENSE)(SPDX: `MPL-2.0`)で提供します。
|
|
181
|
+
|
|
182
|
+
third-party componentを含む場合は
|
|
183
|
+
[`THIRD_PARTY_NOTICES.md`](THIRD_PARTY_NOTICES.md)に記載します。
|