skia-motion-runtime 0.0.0-stage → 3.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.
@@ -0,0 +1,70 @@
1
+ import type { SkImage } from 'react-native-skia';
2
+ import { assetClips, bindAssetPalette, parseAsset, type MotionAsset } from './asset';
3
+ import { decodeAssetImages, prepareAssetDrawing, rebindAssetMeshShaders, type AssetImages, type DrawableAsset } from './asset-draw';
4
+
5
+ let nextAssetKey = 0;
6
+
7
+ export type LoadedMotionAsset = Readonly<{
8
+ key: number;
9
+ machine: MotionAsset['machine'];
10
+ markers: MotionAsset['markers'];
11
+ drawing: DrawableAsset;
12
+ images: AssetImages;
13
+ clips: ReturnType<typeof assetClips>;
14
+ semantics?: MotionAsset['semantics'];
15
+ audio?: MotionAsset['audio'];
16
+ }>;
17
+
18
+ /** Prepare one file for reuse by independent player instances. Keep the result while those players are mounted. */
19
+ export type MotionPrepareOptions = { artboard?: string; imageSlots?: AssetImages; palette?: Readonly<Record<string, string>> };
20
+
21
+ export function loadMotionAsset(source: string, { artboard, imageSlots, palette }: MotionPrepareOptions = {}): LoadedMotionAsset {
22
+ const asset = bindAssetPalette(parseAsset(source, artboard), palette);
23
+ const images = decodeAssetImages(asset, imageSlots);
24
+ return {
25
+ key: nextAssetKey++,
26
+ machine: asset.machine,
27
+ markers: asset.markers,
28
+ drawing: prepareAssetDrawing(asset, images),
29
+ images,
30
+ clips: assetClips(asset),
31
+ semantics: asset.semantics,
32
+ audio: asset.audio,
33
+ };
34
+ }
35
+
36
+ /**
37
+ * Prepare a file before mounting it. Skia versions with asynchronous raster decoding perform
38
+ * that work off the JS thread. Upload is opt-in and can retain textures until the file is released.
39
+ */
40
+ export async function preloadMotionAsset(
41
+ source: string | LoadedMotionAsset,
42
+ { uploadImages = false, ...options }: MotionPrepareOptions & { uploadImages?: boolean } = {},
43
+ ): Promise<LoadedMotionAsset> {
44
+ const loaded = typeof source === 'string' ? loadMotionAsset(source, options) : source;
45
+ const warmed = new Map<SkImage, SkImage>();
46
+ const images: [string, SkImage][] = [];
47
+ let changed = false;
48
+ for (const [name, image] of Object.entries(loaded.images)) {
49
+ let ready = warmed.get(image);
50
+ if (!ready) {
51
+ // Neither method is available at the 3.0 peer dependency floor.
52
+ const preloadable = image as SkImage & { makeRasterImage?: () => Promise<SkImage> };
53
+ ready = await preloadable.makeRasterImage?.() ?? image;
54
+ if (uploadImages) {
55
+ const uploadable = ready as SkImage & { makeTextureImage?: () => SkImage };
56
+ try {
57
+ ready = uploadable.makeTextureImage?.() ?? ready;
58
+ } catch {
59
+ // A reduced or unsupported backend can still upload the raster image when drawn.
60
+ }
61
+ }
62
+ warmed.set(image, ready);
63
+ }
64
+ images.push([name, ready]);
65
+ changed ||= ready !== image;
66
+ }
67
+ if (!warmed.size) return loaded;
68
+ const ready = Object.fromEntries(images);
69
+ return { ...loaded, images: ready, drawing: changed ? rebindAssetMeshShaders(loaded.drawing, ready) : loaded.drawing };
70
+ }
@@ -0,0 +1,37 @@
1
+ import type { AssetNode, Scalar } from './asset';
2
+
3
+ export type MeshVertex = readonly [x: Scalar, y: Scalar, u: number, v: number];
4
+ export type MeshPin = {
5
+ triangle: number;
6
+ weights: readonly [number, number, number];
7
+ children?: readonly AssetNode[];
8
+ };
9
+ export type MeshNode = {
10
+ kind: 'mesh';
11
+ image: string;
12
+ vertices: readonly MeshVertex[];
13
+ indices: readonly number[];
14
+ opacity?: Scalar;
15
+ pins?: Readonly<Record<string, MeshPin>>;
16
+ };
17
+
18
+ type Channels = Readonly<Record<string, number>>;
19
+
20
+ export function meshPosition([x, y]: MeshVertex, channels: Channels) {
21
+ 'worklet';
22
+ return { x: typeof x === 'string' ? channels[x]! : x, y: typeof y === 'string' ? channels[y]! : y };
23
+ }
24
+
25
+ export function meshPinPosition(mesh: Pick<MeshNode, 'vertices' | 'indices'>, pin: Pick<MeshPin, 'triangle' | 'weights'>, channels: Channels) {
26
+ 'worklet';
27
+ let x = 0;
28
+ let y = 0;
29
+ const start = pin.triangle * 3;
30
+ for (let i = 0; i < 3; i++) {
31
+ const vertex = meshPosition(mesh.vertices[mesh.indices[start + i]!]!, channels);
32
+ const weight = pin.weights[i]!;
33
+ x += vertex.x * weight;
34
+ y += vertex.y * weight;
35
+ }
36
+ return { x, y };
37
+ }