@snovakow/greenscreen 0.0.0-stage → 0.5.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.
Files changed (78) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +238 -2
  3. package/dist/greenscreen.d.ts +32 -0
  4. package/dist/greenscreen.d.ts.map +1 -0
  5. package/dist/greenscreen.global.js +315 -0
  6. package/dist/greenscreen.global.js.map +1 -0
  7. package/dist/greenscreen.js +250 -0
  8. package/dist/greenscreen.js.map +1 -0
  9. package/dist/greenscreen.min.js +315 -0
  10. package/dist/greenscreen.min.js.map +1 -0
  11. package/dist/index.d.ts +6 -0
  12. package/dist/index.d.ts.map +1 -0
  13. package/dist/index.js +3 -0
  14. package/dist/index.js.map +1 -0
  15. package/dist/pack.d.ts +7 -0
  16. package/dist/pack.d.ts.map +1 -0
  17. package/dist/pack.js +63 -0
  18. package/dist/pack.js.map +1 -0
  19. package/dist/react.d.ts +26 -0
  20. package/dist/react.d.ts.map +1 -0
  21. package/dist/react.js +78 -0
  22. package/dist/react.js.map +1 -0
  23. package/dist/renderer.d.ts +19 -0
  24. package/dist/renderer.d.ts.map +1 -0
  25. package/dist/renderer.js +25 -0
  26. package/dist/renderer.js.map +1 -0
  27. package/dist/settings.d.ts +36 -0
  28. package/dist/settings.d.ts.map +1 -0
  29. package/dist/settings.js +112 -0
  30. package/dist/settings.js.map +1 -0
  31. package/dist/shader.glsl.d.ts +3 -0
  32. package/dist/shader.glsl.d.ts.map +1 -0
  33. package/dist/shader.glsl.js +179 -0
  34. package/dist/shader.glsl.js.map +1 -0
  35. package/dist/shader.wgsl.d.ts +2 -0
  36. package/dist/shader.wgsl.d.ts.map +1 -0
  37. package/dist/shader.wgsl.js +160 -0
  38. package/dist/shader.wgsl.js.map +1 -0
  39. package/dist/source.d.ts +16 -0
  40. package/dist/source.d.ts.map +1 -0
  41. package/dist/source.js +103 -0
  42. package/dist/source.js.map +1 -0
  43. package/dist/three-webgpu.d.ts +20 -0
  44. package/dist/three-webgpu.d.ts.map +1 -0
  45. package/dist/three-webgpu.js +178 -0
  46. package/dist/three-webgpu.js.map +1 -0
  47. package/dist/three.d.ts +27 -0
  48. package/dist/three.d.ts.map +1 -0
  49. package/dist/three.js +81 -0
  50. package/dist/three.js.map +1 -0
  51. package/dist/types.d.ts +4 -0
  52. package/dist/types.d.ts.map +1 -0
  53. package/dist/types.js +4 -0
  54. package/dist/types.js.map +1 -0
  55. package/dist/webgl.d.ts +13 -0
  56. package/dist/webgl.d.ts.map +1 -0
  57. package/dist/webgl.js +183 -0
  58. package/dist/webgl.js.map +1 -0
  59. package/dist/webgpu.d.ts +19 -0
  60. package/dist/webgpu.d.ts.map +1 -0
  61. package/dist/webgpu.js +303 -0
  62. package/dist/webgpu.js.map +1 -0
  63. package/package.json +103 -4
  64. package/src/global.ts +11 -0
  65. package/src/greenscreen.ts +286 -0
  66. package/src/index.ts +5 -0
  67. package/src/pack.ts +73 -0
  68. package/src/react.tsx +126 -0
  69. package/src/renderer.ts +53 -0
  70. package/src/settings.ts +177 -0
  71. package/src/shader.glsl.ts +179 -0
  72. package/src/shader.wgsl.ts +159 -0
  73. package/src/source.ts +105 -0
  74. package/src/three-webgpu.ts +261 -0
  75. package/src/three.ts +114 -0
  76. package/src/types.ts +10 -0
  77. package/src/webgl.ts +191 -0
  78. package/src/webgpu.ts +312 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 snovakow
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,239 @@
1
- # Temporary Holding Version
1
+ # Greenscreen
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ A chroma keyer for the browser, on WebGPU or WebGL2. It keys as [OBS Studio](https://obsproject.com)'s chroma key filter does, and takes the filter's settings in OBS's units, so a key tuned in OBS can be copied across as it is. It takes a video, an image or a canvas, keys a colour out of it, and draws the result into a canvas of its own: see-through where the key was, at the picture's own size.
4
+
5
+ It also has add-ons of its own, which OBS's filter doesn't have. They can take only the key's hue out of edges, mirror the picture, cut it down to a four-cornered shape, and tint it or change its saturation.
6
+
7
+ It has no dependencies, and it works on its own from plain JavaScript, from TypeScript with its own types, from React, and in three.js scenes.
8
+
9
+ ```sh
10
+ npm install @snovakow/greenscreen
11
+ ```
12
+
13
+ | Import | For | Needs |
14
+ |---|---|---|
15
+ | `@snovakow/greenscreen` | Plain JavaScript and TypeScript | nothing |
16
+ | `@snovakow/greenscreen/react` | The `<Greenscreen>` component and `useGreenscreen` hook | React 18 or later |
17
+ | `@snovakow/greenscreen/three` | `GreenscreenMaterial`, for three.js's `WebGLRenderer` | three.js r170 or later |
18
+ | `@snovakow/greenscreen/three/webgpu` | `GreenscreenNodeMaterial`, for three.js's `WebGPURenderer` | three.js r170 or later |
19
+
20
+ The package is ES modules only. React and three.js are optional peer dependencies: install whichever you use.
21
+
22
+ ## Plain JavaScript
23
+
24
+ ```js
25
+ import { Greenscreen } from '@snovakow/greenscreen'
26
+
27
+ const video = document.querySelector('video')
28
+ video.srcObject = await navigator.mediaDevices.getUserMedia({ video: true })
29
+ await video.play()
30
+
31
+ const keyer = new Greenscreen(video)
32
+ document.body.append(keyer.canvas)
33
+ keyer.start()
34
+ ```
35
+
36
+ Green is keyed out by default. `start()` renders on every animation frame. If your page already has a loop, call `keyer.render()` from it instead.
37
+
38
+ The keyer draws into `keyer.canvas`. That canvas keeps its picture between renders, so you can show it on the page, or draw it into another canvas with `drawImage` at any time, for example to composite or record it. Its colours are premultiplied by alpha, which is what `drawImage` expects.
39
+
40
+ To change the key, call `set()` with the settings to change:
41
+
42
+ ```js
43
+ keyer.set({ keyColor: 0x28be3c, similarity: 300, crop: true, corners })
44
+ ```
45
+
46
+ ### Without a build step
47
+
48
+ Two more builds are in `dist`, each the whole core in one file:
49
+
50
+ ```html
51
+ <!-- An ES module -->
52
+ <script type="module">
53
+ import { Greenscreen } from './greenscreen.min.js'
54
+ </script>
55
+
56
+ <!-- A classic script, which sets window.Greenscreen -->
57
+ <script src="./greenscreen.global.js"></script>
58
+ <script>
59
+ const keyer = new Greenscreen(video, { settings: { keyColor: Greenscreen.KEY_COLORS.blue } })
60
+ </script>
61
+ ```
62
+
63
+ The global build puts `DEFAULT_SETTINGS`, `KEY_COLORS`, `RANGES` and `warmUp` on `Greenscreen` itself. An import map can also point the package's name at `dist/index.js`, as [examples/plain](examples/plain) does.
64
+
65
+ ## TypeScript
66
+
67
+ The package ships its own declarations, and the settings and the API are fully typed:
68
+
69
+ ```ts
70
+ import { Greenscreen, RANGES } from '@snovakow/greenscreen'
71
+ import type { Settings } from '@snovakow/greenscreen'
72
+
73
+ const keyer = new Greenscreen(video, { backend: 'webgl' })
74
+ const { min, max, step } = RANGES.similarity
75
+ keyer.set({ similarity: 450 } satisfies Partial<Settings>)
76
+ ```
77
+
78
+ They check under `moduleResolution` `bundler`, `node16` and `nodenext`.
79
+
80
+ In a worker, give the keyer an `OffscreenCanvas` and type it as one: `new Greenscreen<OffscreenCanvas>(frame, { canvas })`.
81
+
82
+ ## React
83
+
84
+ ```tsx
85
+ import { Greenscreen } from '@snovakow/greenscreen/react'
86
+
87
+ <Greenscreen source={video} settings={{ similarity: 450 }} className="picture" />
88
+ ```
89
+
90
+ The component renders a `<div>` and puts the keyer's canvas in it, fitted with CSS `object-fit`. Give the `div` a size with `className` or `style`.
91
+
92
+ Its props are:
93
+ - `source`
94
+ - `settings`, applied over the defaults
95
+ - `backend`
96
+ - `paused`, which stops it rendering by itself
97
+ - `onError`, as the keyer's, which can be a new function on each render
98
+ - `fit`, `'contain'` by default
99
+ - `className` and `style`
100
+
101
+ Its ref is the keyer. It's `null` until the keyer is made, just after the first render.
102
+
103
+ `useGreenscreen(source, settings?, { backend, paused, onError })` gives you the same keyer without the component, for drawing its canvas yourself. It returns `null` on the first render, and disposes the keyer when the component unmounts. StrictMode is safe: each mount gets a keyer, and therefore a canvas, of its own.
104
+
105
+ ## three.js
106
+
107
+ On a mesh, `GreenscreenMaterial` keys its `map` and shows it as `MeshBasicMaterial` would show that map, though it doesn't apply the texture's `offset`, `repeat` or `rotation`:
108
+
109
+ ```js
110
+ import { GreenscreenMaterial } from '@snovakow/greenscreen/three'
111
+
112
+ const map = new THREE.VideoTexture(video)
113
+ map.colorSpace = THREE.SRGBColorSpace
114
+ const material = new GreenscreenMaterial({ map, settings: { similarity: 450 } })
115
+ scene.add(new THREE.Mesh(new THREE.PlaneGeometry(16 / 9, 1), material))
116
+ ```
117
+
118
+ `GreenscreenNodeMaterial`, from `@snovakow/greenscreen/three/webgpu`, does the same for `WebGPURenderer`, on both of its backends. Both materials take `map` and `settings`, and have `set()`, `reset()` and `settings` like the keyer.
119
+
120
+ The texture may be in sRGB or have no colour space. Either way it's keyed as OBS would key a picture of those sRGB values drawn at the mesh's size on screen. The materials read the texture's pixels and blend them themselves, in linear light as OBS's sampler does, so they use no mipmaps. Where the GPU decodes a texture in sRGB, it's a little less precise than the core, so a steep fade can differ from the core's by several levels at hard edges.
121
+
122
+ ## Settings
123
+
124
+ The settings are OBS's for its chroma key filter, in OBS's units, ranges and defaults, followed by the add-ons, which are the library's own. The numbers in OBS's filter dialog can be copied across as they are.
125
+
126
+ ### OBS's chroma key filter
127
+
128
+ | Setting | In OBS | Values | Default | What it does |
129
+ |---|---|---|---|---|
130
+ | `key` | The filter's eye | boolean | `true` | Runs the filter. Off, the picture passes through it unchanged, and only the add-ons apply. |
131
+ | `keyColor` | Key Color Type, Key Color | `0xrrggbb` | `0x00ff00` | The colour taken out. |
132
+ | `similarity` | Similarity | 1 to 1000 | 400 | How near the key a colour must be to be taken out. |
133
+ | `smoothness` | Smoothness | 1 to 1000 | 80 | How far past `similarity` a colour fades back in. |
134
+ | `spill` | Key Color Spill Reduction | 1 to 1000 | 100 | How far past `similarity` a colour that's kept is desaturated, which takes the key's cast off edges. |
135
+ | `opacity` | Opacity | 0 to 1 | 1 | |
136
+ | `contrast` | Contrast | −4 to 4 | 0 | A factor from ⅕ to 5. |
137
+ | `brightness` | Brightness | −1 to 1 | 0 | Added to the colour. |
138
+ | `gamma` | Gamma | −1 to 1 | 0 | An exponent from 2 to ½. |
139
+
140
+ `KEY_COLORS` holds OBS's three presets: `green` is `0x00ff00`, `blue` is `0x0099ff` and `magenta` is `0xff00ff`. Any other colour is OBS's Custom Color. OBS stores colours as `0xbbggrr`, so a Key Color read from OBS's own files has its red and blue the other way round: OBS's blue, `0xFF9900`, is `0x0099ff` here.
141
+
142
+ `similarity`, `smoothness` and `spill` are whole numbers, as in OBS, and any other number is rounded to the nearest.
143
+
144
+ ### Add-ons
145
+
146
+ The add-ons are the library's own, and not in OBS. At their defaults they change nothing. All but `hueSpill` act on the picture OBS's filter gives; `hueSpill` acts inside the filter, in place of one of its steps.
147
+
148
+ | Setting | Values | Default | What it does |
149
+ |---|---|---|---|
150
+ | `hueSpill` | boolean | `false` | Makes spill take out only the key's hue, where OBS's spill desaturates the whole colour. Each colour keeps its luma and its other hues, so a green cast comes off edges and hair, and skin and hair keep their own colour. It reaches as far as `spill`, and takes out as much of the key's hue as OBS would take of the colour. A colour that's near the key itself turns toward its other hues rather than grey: at OBS's defaults, cyan turns sky blue and olive brown. |
151
+ | `flip` | boolean | `false` | Mirrors the keyed picture left to right. |
152
+ | `crop` | boolean | `false` | Cuts the picture down to the shape `corners` make. |
153
+ | `corners` | four `{ x, y }` | the whole picture | Clockwise from the top left. They're in the picture's coordinates, 0 to 1 with y down, and not mirrored by `flip`. The shape must be convex. |
154
+ | `tint` | `0xrrggbb` | `0xffffff` | Multiplies the colour, in linear light. |
155
+ | `saturation` | 0 to 2 | 1 | Mixes the colour with its luma, in linear light. 0 is grey. |
156
+
157
+ `DEFAULT_SETTINGS` holds the defaults, and `RANGES` the limits and a slider step for each number: OBS's sliders' own, for OBS's settings.
158
+
159
+ How settings are handled:
160
+ - **Out of range:** a number outside its range is clamped.
161
+ - **Wrong kind:** a value of the wrong kind throws a `TypeError`.
162
+ - **Undefined or unknown:** an `undefined` value, or a name that isn't a setting, is ignored. You can pass an app's own settings object as it is.
163
+ - **Copied, not kept:** `set()` copies everything it's given, corners included. Changing your object afterwards does nothing until you pass it again.
164
+ - **Snapshots:** `keyer.settings` is a frozen snapshot.
165
+
166
+ ### How it keys
167
+
168
+ The keyer does what OBS's filter does, the filter's [second version](https://github.com/obsproject/obs-studio/blob/master/plugins/obs-filters/data/chroma_key_filter_v2.effect), in the same order:
169
+
170
+ 1. **The picture** is read as OBS holds a source: premultiplied, in linear light, and blended between pixels in linear light. Each pixel's own colour is then divided by its alpha.
171
+ 2. **The distance from the key** is measured between colours' Cb and Cr, as BT.709 has them in studio range, worked out from their sRGB values. It's blurred, out of nine: the pixel itself counts once, and four samples round it count twice each. The samples are one pixel across and half a pixel down, and half a pixel across and one pixel up, each both ways. The settings count in thousandths of this distance, so a `similarity` of 400 is 0.4.
172
+ 3. **Past `similarity`,** the alpha comes in over `smoothness`, and the colour comes back from grey over `spill`, each as the 1.5 power of the way across. Grey is the colour's BT.709 luma. `opacity` multiplies the alpha.
173
+ 4. **The colour** is raised to `gamma`'s exponent and multiplied by `contrast`'s factor, and `brightness` is added, all in linear light. Then it's clamped.
174
+
175
+ After that the add-ons act, and the colour is encoded to sRGB for the canvas. With `hueSpill` on, step 3 takes out only the key's hue rather than greying the colour. It works on the colour's sRGB values, in BT.709's Pb and Pr. The part of the colour's chroma that points the key's way from grey comes out, by as much as OBS would grey it, and its luma is kept.
176
+
177
+ The one difference from OBS is the browser's. A page blends a see-through canvas into what's behind it on sRGB values, where OBS blends a source into its scene in linear light. So a partly see-through pixel mixes differently with what's behind it: at soft edges, and all over at an `opacity` below 1. Opaque pixels come out the same.
178
+
179
+ ## The keyer
180
+
181
+ ```ts
182
+ new Greenscreen(source?, { canvas?, backend?, settings?, onLost?, onRestored?, onError? })
183
+ ```
184
+
185
+ **Construction options:**
186
+ - **`source`:** a `<video>`, `<img>`, `<canvas>`, `OffscreenCanvas`, `ImageBitmap`, `ImageData` or `VideoFrame`. You can set it later. An animated image, such as a GIF, is keyed as a still. For motion, use a video, or set each frame as the source as a `VideoFrame`, which `ImageDecoder` can decode.
187
+ - **`canvas`:** the canvas to draw in. It must be one no context has been taken from. Without it, the keyer makes its own.
188
+ - **`backend`:** `'auto'` (the default), `'webgpu'` or `'webgl'`. See [Backends](#backends).
189
+ - **`onLost`, `onRestored`:** called when the graphics context or device is lost and when it comes back. The keyer redraws on the next render after it's back.
190
+ - **`onError`:** called once for each picture that can't be keyed, with what was thrown. That's a `RangeError` for a picture wider or taller than the GPU's textures go, often 8192 or 16384 pixels, and a `SecurityError` for one from another site without CORS. The canvas is left see-through until a picture can be keyed. Without `onError`, the keyer warns in the console. It isn't called for a source with no picture yet, such as a video still loading, or for a backend that can't be had, which `initialized` reports.
191
+
192
+ **Properties:**
193
+ - **`canvas`:** where the keyed picture is drawn.
194
+ - **`backend`:** `'webgl'`, `'webgpu'`, or `null` until it has one.
195
+ - **`initialized`:** a promise that resolves with the backend, or rejects if it can't be had.
196
+ - **`ready`:** whether `canvas` holds a picture of the current source. It's false while the source's picture can't be keyed; see `onError`.
197
+ - **`frame`:** counts the pictures drawn.
198
+ - **`settings`:** the current settings.
199
+ - **`source`:** can be read and set.
200
+
201
+ **Methods:**
202
+ - **`render()`:** draws, but only when there's something new: a video frame, a changed setting, a new source or size. It's cheap to call on every frame, and returns `ready`.
203
+ - **`start()`, `stop()`:** render on every animation frame, and stop.
204
+ - **`set(patch)`, `reset()`:** change settings, or go back to the defaults.
205
+ - **`invalidate()`:** takes the picture again on the next render. Only an `ImageBitmap`, `ImageData` or `VideoFrame` whose pixels you've changed needs it. Videos are watched for new frames, an image for a new `src`, and a canvas is taken again on every render, as nothing says when it's been drawn on.
206
+ - **`dispose()`:** frees the context or device's resources at once. The keyer and its canvas can't be used again.
207
+
208
+ ### Backends
209
+
210
+ `'auto'` chooses WebGPU where the browser has a GPU adapter for it, and WebGL2 elsewhere. A fallback adapter, which runs WebGPU on the CPU, doesn't count, as WebGL2 is then likely on the GPU. Ask for one by name with `backend: 'webgpu'` or `backend: 'webgl'`.
211
+
212
+ A keyer draws nothing until it has its backend:
213
+ - A WebGL2 keyer has it at once.
214
+ - A WebGPU keyer has it once the device is ready.
215
+ - An `'auto'` keyer has it once it's known which to take.
216
+
217
+ So call `warmUp()` early on, and keyers made after it has resolved draw from their first render. It resolves with the backend `'auto'` comes to. `warmUp('webgpu')` gets WebGPU ready, and rejects where WebGPU isn't available.
218
+
219
+ Every WebGPU keyer on a page shares one device. A WebGL2 keyer has a context of its own, and browsers allow a page only so many, about 16 in Chrome, so dispose keyers you're finished with.
220
+
221
+ ## Demo and checks
222
+
223
+ [demo](demo) is a greenscreen app built on the React entry. It keys the camera, or an image or video you open with File or drop on the page, which stays in your browser. It has a lil-gui panel, with OBS's controls under OBS's names in one folder and the add-ons in another, a preview to pick the key from, and handles to drag the cut's corners. `demo/check` is a page of checks. It holds the defaults, ranges and presets to OBS's, keys a test picture with every part of the library the browser can run, on WebGL2 and on WebGPU, and compares each pixel with the keyer worked out on the CPU. Open it in any browser to see whether the library works there.
224
+
225
+ To work on the library, you need [Node.js](https://nodejs.org) 22.12 or later. In a clone:
226
+
227
+ ```sh
228
+ npm install
229
+ npm run dev # the demo, with the library from its sources
230
+ npm test # the checks and the demo, in headless Chrome
231
+ npm run test:examples # the packed package in the plain, TypeScript and React examples
232
+ npm run probe # what this machine's Chrome offers: WebGL2, and WebGPU's adapters
233
+ ```
234
+
235
+ The checks run in Chrome twice, once on the machine's GPU and once on SwiftShader, its software renderer. Set `CHROME` to use another Chrome. `npm run build` makes the package in `dist` and the demo site in `demo/dist`. `npm run typecheck` and `npm run lint` check the code.
236
+
237
+ ## Licence
238
+
239
+ MIT. The keying follows the maths of OBS Studio's chroma key filter, and the credit for it is OBS's. OBS Studio is GPL-2.0-or-later. The library's code is its own, and none of OBS's code is in this package.
@@ -0,0 +1,32 @@
1
+ import type { Settings } from './settings.js';
2
+ import type { BackendName, Canvas, Source } from './types.js';
3
+ export type BackendChoice = 'auto' | BackendName;
4
+ export interface GreenscreenOptions<C extends Canvas = HTMLCanvasElement> {
5
+ canvas?: C;
6
+ backend?: BackendChoice;
7
+ settings?: Readonly<Partial<Settings>>;
8
+ onLost?: () => void;
9
+ onRestored?: () => void;
10
+ onError?: (error: unknown) => void;
11
+ }
12
+ export declare class Greenscreen<C extends Canvas = HTMLCanvasElement> {
13
+ #private;
14
+ readonly canvas: C;
15
+ readonly initialized: Promise<BackendName>;
16
+ constructor(source?: Source | null, options?: GreenscreenOptions<C>);
17
+ get backend(): BackendName | null;
18
+ get ready(): boolean;
19
+ get frame(): number;
20
+ get settings(): Readonly<Settings>;
21
+ get source(): Source | null;
22
+ set source(source: Source | null);
23
+ set(patch: Readonly<Partial<Settings>>): void;
24
+ reset(): void;
25
+ invalidate(): void;
26
+ render(): boolean;
27
+ start(): void;
28
+ stop(): void;
29
+ dispose(): void;
30
+ }
31
+ export declare function warmUp(backend?: BackendChoice): Promise<BackendName>;
32
+ //# sourceMappingURL=greenscreen.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"greenscreen.d.ts","sourceRoot":"","sources":["../src/greenscreen.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAA;AAE7C,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,YAAY,CAAA;AAa7D,MAAM,MAAM,aAAa,GAAG,MAAM,GAAG,WAAW,CAAA;AAEhD,MAAM,WAAW,kBAAkB,CAAC,CAAC,SAAS,MAAM,GAAG,iBAAiB;IAGtE,MAAM,CAAC,EAAE,CAAC,CAAA;IAEV,OAAO,CAAC,EAAE,aAAa,CAAA;IAEvB,QAAQ,CAAC,EAAE,QAAQ,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC,CAAA;IAEtC,MAAM,CAAC,EAAE,MAAM,IAAI,CAAA;IAEnB,UAAU,CAAC,EAAE,MAAM,IAAI,CAAA;IAIvB,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,OAAO,KAAK,IAAI,CAAA;CACnC;AAUD,qBAAa,WAAW,CAAC,CAAC,SAAS,MAAM,GAAG,iBAAiB;;IAC3D,QAAQ,CAAC,MAAM,EAAE,CAAC,CAAA;IAElB,QAAQ,CAAC,WAAW,EAAE,OAAO,CAAC,WAAW,CAAC,CAAA;IAiB1C,YAAY,MAAM,GAAE,MAAM,GAAG,IAAW,EAAE,OAAO,GAAE,kBAAkB,CAAC,CAAC,CAAM,EAY5E;IA0DD,IAAI,OAAO,IAAI,WAAW,GAAG,IAAI,CAEhC;IAID,IAAI,KAAK,IAAI,OAAO,CAEnB;IAGD,IAAI,KAAK,IAAI,MAAM,CAElB;IAGD,IAAI,QAAQ,IAAI,QAAQ,CAAC,QAAQ,CAAC,CAEjC;IAED,IAAI,MAAM,IAAI,MAAM,GAAG,IAAI,CAE1B;IAGD,IAAI,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,EAM/B;IAID,GAAG,CAAC,KAAK,EAAE,QAAQ,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC,GAAG,IAAI,CAQ5C;IAGD,KAAK,IAAI,IAAI,CAEZ;IAID,UAAU,IAAI,IAAI,CAEjB;IAID,MAAM,IAAI,OAAO,CA+BhB;IAGD,KAAK,IAAI,IAAI,CASZ;IAED,IAAI,IAAI,IAAI,CAGX;IAGD,OAAO,IAAI,IAAI,CASd;CACF;AAKD,wBAAsB,MAAM,CAAC,OAAO,GAAE,aAAsB,GAAG,OAAO,CAAC,WAAW,CAAC,CAIlF"}
@@ -0,0 +1,315 @@
1
+ var Greenscreen=(function(){var e=[-.100644,-.338572,.439216,.501961],t=[.439216,-.398942,-.040274,.501961];function n(n,a){let[o,s,c]=r(n.keyColor);a[0]=e[0]*o+e[1]*s+e[2]*c+e[3],a[1]=t[0]*o+t[1]*s+t[2]*c+t[3],a[2]=1/Math.max(Math.hypot(a[0]-e[3],a[1]-t[3]),.001),a[3]=+!!n.key,a[4]=n.similarity/1e3,a[5]=n.smoothness/1e3,a[6]=n.spill/1e3,a[7]=n.opacity;let{gamma:l,contrast:u}=n;a[8]=l<0?1-l:1/(1+l),a[9]=u<0?1/(1-u):1+u,a[10]=n.brightness,a[11]=0;let d=r(n.tint);return a[12]=i(d[0]),a[13]=i(d[1]),a[14]=i(d[2]),a[15]=n.saturation,a[16]=+!!n.flip,a[17]=0,a[18]=0,a[19]=0,n.corners.forEach((e,t)=>{a[20+t*2]=e.x,a[21+t*2]=e.y}),a[28]=+!!n.crop,a[29]=+!!n.hueSpill,a.fill(0,30),a}function r(e){return[(e>>16&255)/255,(e>>8&255)/255,(e&255)/255]}function i(e){return e<=.04045?e/12.92:((e+.055)/1.055)**2.4}var a=Object.freeze({similarity:{min:1,max:1e3,step:1},smoothness:{min:1,max:1e3,step:1},spill:{min:1,max:1e3,step:1},opacity:{min:0,max:1,step:1e-4},contrast:{min:-4,max:4,step:.01},brightness:{min:-1,max:1,step:1e-4},gamma:{min:-1,max:1,step:.01},saturation:{min:0,max:2,step:.01}}),o=Object.freeze({green:65280,blue:39423,magenta:16711935}),s=Object.freeze([Object.freeze({x:0,y:0}),Object.freeze({x:1,y:0}),Object.freeze({x:1,y:1}),Object.freeze({x:0,y:1})]),c=Object.freeze({key:!0,keyColor:o.green,similarity:400,smoothness:80,spill:100,opacity:1,contrast:0,brightness:0,gamma:0,hueSpill:!1,flip:!1,crop:!1,corners:s,tint:16777215,saturation:1}),l=[`similarity`,`smoothness`,`spill`,`opacity`,`contrast`,`brightness`,`gamma`,`saturation`],u=new Set([`similarity`,`smoothness`,`spill`]);function d(e,t){let n={...e};t.key!==void 0&&(n.key=p(`key`,t.key)),t.hueSpill!==void 0&&(n.hueSpill=p(`hueSpill`,t.hueSpill)),t.flip!==void 0&&(n.flip=p(`flip`,t.flip)),t.crop!==void 0&&(n.crop=p(`crop`,t.crop)),t.keyColor!==void 0&&(n.keyColor=m(`keyColor`,t.keyColor)),t.tint!==void 0&&(n.tint=m(`tint`,t.tint)),t.corners!==void 0&&(n.corners=g(t.corners));for(let e of l){let r=t[e];r!==void 0&&(n[e]=h(e,r))}return Object.freeze(n)}var f=(e,t,n)=>Math.min(Math.max(e,t),n);function p(e,t){if(typeof t!=`boolean`)throw TypeError(`Greenscreen: ${e} must be true or false`);return t}function m(e,t){if(typeof t!=`number`||!Number.isInteger(t)||t<0||t>16777215)throw TypeError(`Greenscreen: ${e} must be a colour from 0x000000 to 0xffffff`);return t}function h(e,t){if(typeof t!=`number`||!Number.isFinite(t))throw TypeError(`Greenscreen: ${e} must be a number`);let n=f(t,a[e].min,a[e].max);return u.has(e)?Math.round(n):n}function g(e){if(!Array.isArray(e)||e.length!==4)throw TypeError(`Greenscreen: corners must be four points`);return Object.freeze(e.map(e=>{let t=typeof e==`object`&&e&&`x`in e?e.x:void 0,n=typeof e==`object`&&e&&`y`in e?e.y:void 0;if(typeof t!=`number`||typeof n!=`number`||!Number.isFinite(t)||!Number.isFinite(n))throw TypeError(`Greenscreen: each corner must have a number x and y`);return Object.freeze({x:f(t,0,1),y:f(n,0,1)})}))}var _=100;function v(e){return`videoWidth`in e?[e.videoWidth,e.videoHeight]:`naturalWidth`in e?[e.naturalWidth,e.naturalHeight]:`displayWidth`in e?[e.displayWidth,e.displayHeight]:[e.width,e.height]}function y(e){console.warn(`Greenscreen: the picture could not be keyed`,e)}var b=class{source;#e=!0;#t=-1;#n=``;#r=0;#i=-1/0;#a=null;#o=!1;constructor(e){this.source=e,this.#s()}#s(){let e=this.source;!(`requestVideoFrameCallback`in e)||this.#o||(this.#r=e.requestVideoFrameCallback(()=>{this.#e=!0,this.#i=performance.now(),this.#s()}))}hasPicture(){let e=this.source;if(`videoWidth`in e)return e.readyState>=2&&e.videoWidth>0&&e.videoHeight>0;if(`naturalWidth`in e)return e.complete&&e.naturalWidth>0;let[t,n]=v(e);return t>0&&n>0}changed(){if(this.#e)return!0;let e=this.source;return`videoWidth`in e?performance.now()-this.#i>_&&e.currentTime!==this.#t:`naturalWidth`in e?e.currentSrc!==this.#n:`getContext`in e}size(){return v(this.source)}taken(){this.#e=!1,this.#a=null;let e=this.source;`videoWidth`in e?this.#t=e.currentTime:`naturalWidth`in e&&(this.#n=e.currentSrc)}failed(){let e=this.source,t=`currentSrc`in e?e.currentSrc:``,n=this.#a!==t;return this.taken(),this.#a=t,n}invalidate(){this.#e=!0}dispose(){this.#o=!0;let e=this.source;this.#r&&`cancelVideoFrameCallback`in e&&e.cancelVideoFrameCallback(this.#r)}};function x(e,t,n,r){return RangeError(`Greenscreen: the picture is ${e}×${t}, and ${n} here takes up to ${r} a side`)}function S(e,t,n){let r=null;return(typeof HTMLCanvasElement<`u`&&e instanceof HTMLCanvasElement||typeof OffscreenCanvas<`u`&&e instanceof OffscreenCanvas)&&(r=e.getContext(t,n)),t===`webgl2`?r instanceof WebGL2RenderingContext?r:null:t===`2d`?typeof CanvasRenderingContext2D<`u`&&r instanceof CanvasRenderingContext2D||typeof OffscreenCanvasRenderingContext2D<`u`&&r instanceof OffscreenCanvasRenderingContext2D?r:null:typeof GPUCanvasContext<`u`&&r instanceof GPUCanvasContext?r:null}var C=`precision highp float;
2
+ precision highp int;
3
+ precision highp sampler2D;
4
+
5
+ uniform sampler2D image;
6
+ uniform vec4 settings[8];
7
+
8
+ in vec2 vUv;
9
+ out vec4 fragColor;
10
+
11
+ // A colour's Cb and Cr from its sRGB values, as OBS's filter has them: BT.709's, in studio range
12
+ const vec3 CB = vec3(-0.100644, -0.338572, 0.439216);
13
+ const vec3 CR = vec3(0.439216, -0.398942, -0.040274);
14
+ const float CHROMA_OFFSET = 0.501961;
15
+ // BT.709's luma, which OBS desaturates to
16
+ const vec3 LUMA = vec3(0.2126, 0.7152, 0.0722);
17
+ // The scales that take B' − Y' and R' − Y' to BT.709's Pb and Pr, for hueSpill
18
+ const vec2 CHROMA_SCALE = vec2(1.8556, 1.5748);
19
+
20
+ float toLinear(float c) {
21
+ return c <= 0.04045 ? c / 12.92 : pow((c + 0.055) / 1.055, 2.4);
22
+ }
23
+
24
+ float toEncoded(float c) {
25
+ return c <= 0.0031308 ? 12.92 * c : 1.055 * pow(c, 1.0 / 2.4) - 0.055;
26
+ }
27
+
28
+ vec3 toLinear(vec3 c) {
29
+ return vec3(toLinear(c.r), toLinear(c.g), toLinear(c.b));
30
+ }
31
+
32
+ vec3 toEncoded(vec3 c) {
33
+ return vec3(toEncoded(c.r), toEncoded(c.g), toEncoded(c.b));
34
+ }
35
+
36
+ // The picture's pixel at a column, and a row from the top, clamped to the picture's edges as OBS's sampler clamps.
37
+ // It's premultiplied, in linear light, as OBS holds a source, whichever way the texture holds it
38
+ vec4 pixelAt(ivec2 at, ivec2 size) {
39
+ at = clamp(at, ivec2(0), size - 1);
40
+ if (settings[4].y > 0.5) at.y = size.y - 1 - at.y;
41
+ vec4 texel = texelFetch(image, at, 0);
42
+ vec3 rgb = settings[4].w > 0.5 ? texel.rgb : toLinear(texel.rgb);
43
+ return vec4(rgb * texel.a, texel.a);
44
+ }
45
+
46
+ // The picture at q, 0 to 1 across and down it, as OBS's linear sampler gives it: the four pixels round q, blended
47
+ vec4 sampleAt(vec2 q, ivec2 size) {
48
+ vec2 f = q * vec2(size) - 0.5;
49
+ vec2 corner = floor(f);
50
+ vec2 w = f - corner;
51
+ ivec2 i = ivec2(corner);
52
+ vec4 above = mix(pixelAt(i, size), pixelAt(i + ivec2(1, 0), size), w.x);
53
+ vec4 below = mix(pixelAt(i + ivec2(0, 1), size), pixelAt(i + ivec2(1, 1), size), w.x);
54
+ return mix(above, below, w.y);
55
+ }
56
+
57
+ // How far a colour's chroma is from the key's, from its sRGB values
58
+ float keyDistance(vec3 encoded) {
59
+ return distance(vec2(dot(encoded, CB), dot(encoded, CR)) + CHROMA_OFFSET, settings[0].xy);
60
+ }
61
+
62
+ // hueSpill's: an sRGB colour's Pb and Pr, and the colour again from its luma and those
63
+ vec2 chromaOf(vec3 encoded) {
64
+ float y = dot(encoded, LUMA);
65
+ return vec2(encoded.b - y, encoded.r - y) / CHROMA_SCALE;
66
+ }
67
+
68
+ vec3 fromLumaChroma(float y, vec2 c) {
69
+ float r = y + c.y * CHROMA_SCALE.y;
70
+ float b = y + c.x * CHROMA_SCALE.x;
71
+ return vec3(r, (y - LUMA.r * r - LUMA.b * b) / LUMA.g, b);
72
+ }
73
+
74
+ // How far inside the cut's side from a to b the point p is, in output pixels. The points are in the picture's
75
+ // coordinates, and size scales them to output pixels once they're subtracted, so a short side between two corners
76
+ // close together keeps its direction. Two corners together leave no side, and a triangle
77
+ float cutSide(vec2 a, vec2 b, vec2 p, vec2 size) {
78
+ vec2 side = (b - a) * size;
79
+ float len = length(side);
80
+ if (len == 0.0) return 1.0e6;
81
+ vec2 q = (p - a) * size;
82
+ return (q.y * side.x - q.x * side.y) / len;
83
+ }
84
+
85
+ // 1 inside the cut, 0 outside, and between across the output pixel a side passes through, so its edges are smooth at
86
+ // any size. The corners must make a convex shape, which a point is inside where it is inside all of its sides. They
87
+ // go clockwise, which puts the inside of each side on its right
88
+ float cutMask(vec2 p, vec2 size) {
89
+ vec2 c0 = settings[5].xy;
90
+ vec2 c1 = settings[5].zw;
91
+ vec2 c2 = settings[6].xy;
92
+ vec2 c3 = settings[6].zw;
93
+ float d = min(
94
+ min(cutSide(c0, c1, p, size), cutSide(c1, c2, p, size)),
95
+ min(cutSide(c2, c3, p, size), cutSide(c3, c0, p, size))
96
+ );
97
+ return clamp(d + 0.5, 0.0, 1.0);
98
+ }
99
+
100
+ void main() {
101
+ // Output pixels to the picture's width and height, for the cut's soft edge
102
+ vec2 size = 1.0 / max(fwidth(vUv), vec2(1e-6));
103
+ vec4 key = settings[0];
104
+ vec4 matte = settings[1];
105
+ vec4 correction = settings[2];
106
+ vec4 extras = settings[3];
107
+ vec4 flags = settings[4];
108
+
109
+ // Where this pixel is on the picture, mirrored by flip
110
+ vec2 p = vec2(flags.x > 0.5 ? 1.0 - vUv.x : vUv.x, vUv.y);
111
+ ivec2 pixels = textureSize(image, 0);
112
+
113
+ vec4 sampled = sampleAt(p, pixels);
114
+ float alpha = sampled.a;
115
+ vec3 rgb = alpha > 0.0 ? sampled.rgb / alpha : vec3(0.0);
116
+
117
+ // OBS's filter
118
+ if (key.w > 0.5) {
119
+ vec2 texel = 1.0 / vec2(pixels);
120
+ vec2 across = vec2(texel.x, 0.5 * texel.y);
121
+ vec2 up = vec2(0.5 * texel.x, -texel.y);
122
+ float around = keyDistance(toEncoded(sampleAt(p - across, pixels).rgb))
123
+ + keyDistance(toEncoded(sampleAt(p + across, pixels).rgb))
124
+ + keyDistance(toEncoded(sampleAt(p - up, pixels).rgb))
125
+ + keyDistance(toEncoded(sampleAt(p + up, pixels).rgb));
126
+ float beyond = (2.0 * around + keyDistance(toEncoded(rgb))) / 9.0 - matte.x;
127
+ alpha *= matte.w;
128
+ alpha *= pow(clamp(beyond / matte.y, 0.0, 1.0), 1.5);
129
+ float kept = pow(clamp(beyond / matte.z, 0.0, 1.0), 1.5);
130
+ if (settings[7].y > 0.5) {
131
+ // The add-on: only the part of the colour's chroma that points the key's way comes out
132
+ vec3 encoded = toEncoded(rgb);
133
+ vec2 toward = (key.xy - CHROMA_OFFSET) * key.z;
134
+ vec2 c = chromaOf(encoded);
135
+ c -= toward * max(dot(c, toward), 0.0) * (1.0 - kept);
136
+ rgb = toLinear(clamp(fromLumaChroma(dot(encoded, LUMA), c), 0.0, 1.0));
137
+ } else {
138
+ rgb = mix(vec3(dot(rgb, LUMA)), rgb, kept);
139
+ }
140
+ rgb = pow(max(rgb, 0.0), vec3(correction.x)) * correction.y + correction.z;
141
+ }
142
+ rgb = clamp(rgb, 0.0, 1.0);
143
+
144
+ // The add-ons
145
+ rgb *= extras.rgb;
146
+ rgb = clamp(mix(vec3(dot(rgb, LUMA)), rgb, extras.w), 0.0, 1.0);
147
+ if (settings[7].x > 0.5) alpha *= cutMask(p, size);
148
+
149
+ if (flags.z < 0.5) rgb = toEncoded(rgb);
150
+ fragColor = vec4(rgb * alpha, alpha);
151
+ }
152
+ `,w=`#version 300 es
153
+ out vec2 vUv;
154
+ void main() {
155
+ vec2 position = vec2(float((gl_VertexID & 1) << 2) - 1.0, float((gl_VertexID & 2) << 1) - 1.0);
156
+ vUv = vec2(position.x + 1.0, 1.0 - position.y) * 0.5;
157
+ gl_Position = vec4(position, 0.0, 1.0);
158
+ }
159
+ `,T={alpha:!0,premultipliedAlpha:!0,antialias:!1,depth:!1,stencil:!1,preserveDrawingBuffer:!0},E=class{backend=`webgl`;#e;#t;#n;#r=null;#i=null;#a=null;#o=0;#s=0;#c=0;#l=!1;constructor(e,t){let n=S(e,`webgl2`,T);if(!n)throw Error(`Greenscreen: WebGL2 is not available on this canvas`);this.#e=e,this.#t=n,this.#n=t,e.addEventListener(`webglcontextlost`,this.#p),e.addEventListener(`webglcontextrestored`,this.#m),this.#u()}get lost(){return this.#l}#u(){let e=this.#t,t=e.createProgram(),n=D(e,e.VERTEX_SHADER,w),r=D(e,e.FRAGMENT_SHADER,`#version 300 es\n${C}`);if(e.attachShader(t,n),e.attachShader(t,r),e.linkProgram(t),!e.getProgramParameter(t,e.LINK_STATUS)&&!e.isContextLost())throw Error(`Greenscreen: the shader didn't link: ${e.getProgramInfoLog(t)}`);e.deleteShader(n),e.deleteShader(r),e.useProgram(t),e.uniform1i(e.getUniformLocation(t,`image`),0),this.#r=t,this.#i=e.getUniformLocation(t,`settings`);let i=e.createTexture();e.activeTexture(e.TEXTURE0),e.bindTexture(e.TEXTURE_2D,i),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_MIN_FILTER,e.LINEAR),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_MAG_FILTER,e.LINEAR),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_WRAP_S,e.CLAMP_TO_EDGE),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_WRAP_T,e.CLAMP_TO_EDGE),e.pixelStorei(e.UNPACK_FLIP_Y_WEBGL,!1),e.pixelStorei(e.UNPACK_PREMULTIPLY_ALPHA_WEBGL,!1),e.pixelStorei(e.UNPACK_COLORSPACE_CONVERSION_WEBGL,e.BROWSER_DEFAULT_WEBGL),this.#a=i,this.#o=0,this.#s=0;let a=e.getParameter(e.MAX_TEXTURE_SIZE),o=e.getParameter(e.MAX_VIEWPORT_DIMS),s=[a,...o instanceof Int32Array?o:[]];this.#c=Math.min(...s.filter(e=>typeof e==`number`))}upload(e,t,n){if(this.#l)return;let r=this.#c;if(t>r||n>r)throw x(t,n,`WebGL2`,r);let i=this.#t;i.activeTexture(i.TEXTURE0),i.bindTexture(i.TEXTURE_2D,this.#a),i.texImage2D(i.TEXTURE_2D,0,i.RGBA8,i.RGBA,i.UNSIGNED_BYTE,this.#f(e,t,n)),this.#o=t,this.#s=n}#d=null;#f(e,t,n){if(typeof ImageBitmap>`u`||!(e instanceof ImageBitmap)||typeof OffscreenCanvas>`u`)return e;let r=this.#d;if(!r){if(r=new OffscreenCanvas(t,n).getContext(`2d`),!r)return e;this.#d=r}let i=r.canvas;return i.width!==t||i.height!==n?(i.width=t,i.height=n):r.clearRect(0,0,t,n),r.drawImage(e,0,0),i}draw(e){if(this.#l||!this.#o||!this.#s)return;let t=this.#t,n=this.#e;(n.width!==this.#o||n.height!==this.#s)&&(n.width=this.#o,n.height=this.#s),t.viewport(0,0,this.#o,this.#s),t.useProgram(this.#r),t.uniform4fv(this.#i,e),t.activeTexture(t.TEXTURE0),t.bindTexture(t.TEXTURE_2D,this.#a),t.drawArrays(t.TRIANGLES,0,3)}clear(){if(this.#l)return;let e=this.#t;e.clearColor(0,0,0,0),e.clear(e.COLOR_BUFFER_BIT)}#p=e=>{e.preventDefault(),this.#l=!0,this.#r=null,this.#a=null,this.#o=0,this.#s=0,this.#n.lost()};#m=()=>{this.#l=!1,this.#u(),this.#n.restored()};dispose(){let e=this.#t;this.#e.removeEventListener(`webglcontextlost`,this.#p),this.#e.removeEventListener(`webglcontextrestored`,this.#m),this.#l||(e.deleteTexture(this.#a),e.deleteProgram(this.#r)),this.#l=!0,e.getExtension(`WEBGL_lose_context`)?.loseContext()}};function D(e,t,n){let r=e.createShader(t);if(!r)throw Error(`Greenscreen: no shader could be made`);if(e.shaderSource(r,n),e.compileShader(r),!e.getShaderParameter(r,e.COMPILE_STATUS)&&!e.isContextLost())throw Error(`Greenscreen: the shader didn't compile: ${e.getShaderInfoLog(r)}`);return r}var O=`
160
+ struct Settings {
161
+ key: vec4f,
162
+ matte: vec4f,
163
+ correction: vec4f,
164
+ extras: vec4f,
165
+ flags: vec4f,
166
+ corners: array<vec4f, 2>,
167
+ switches: vec4f,
168
+ }
169
+
170
+ @group(0) @binding(0) var image: texture_2d<f32>;
171
+ @group(0) @binding(1) var<uniform> settings: Settings;
172
+
173
+ struct Varyings {
174
+ @builtin(position) position: vec4f,
175
+ @location(0) uv: vec2f,
176
+ }
177
+
178
+ // One triangle over the whole canvas, its corners made from their index
179
+ @vertex
180
+ fn vertexMain(@builtin(vertex_index) index: u32) -> Varyings {
181
+ let position = vec2f(f32((index & 1u) << 2u) - 1.0, f32((index & 2u) << 1u) - 1.0);
182
+ var result: Varyings;
183
+ result.position = vec4f(position, 0.0, 1.0);
184
+ result.uv = vec2f(position.x + 1.0, 1.0 - position.y) * 0.5;
185
+ return result;
186
+ }
187
+
188
+ const CB = vec3f(-0.100644, -0.338572, 0.439216);
189
+ const CR = vec3f(0.439216, -0.398942, -0.040274);
190
+ const CHROMA_OFFSET = 0.501961;
191
+ const LUMA = vec3f(0.2126, 0.7152, 0.0722);
192
+ const CHROMA_SCALE = vec2f(1.8556, 1.5748);
193
+
194
+ fn channelToLinear(c: f32) -> f32 {
195
+ return select(pow((c + 0.055) / 1.055, 2.4), c / 12.92, c <= 0.04045);
196
+ }
197
+
198
+ fn channelToEncoded(c: f32) -> f32 {
199
+ return select(1.055 * pow(c, 1.0 / 2.4) - 0.055, 12.92 * c, c <= 0.0031308);
200
+ }
201
+
202
+ fn toLinear(c: vec3f) -> vec3f {
203
+ return vec3f(channelToLinear(c.r), channelToLinear(c.g), channelToLinear(c.b));
204
+ }
205
+
206
+ fn toEncoded(c: vec3f) -> vec3f {
207
+ return vec3f(channelToEncoded(c.r), channelToEncoded(c.g), channelToEncoded(c.b));
208
+ }
209
+
210
+ fn pixelAt(at: vec2i, size: vec2i) -> vec4f {
211
+ let texel = textureLoad(image, clamp(at, vec2i(0), size - 1), 0);
212
+ return vec4f(toLinear(texel.rgb) * texel.a, texel.a);
213
+ }
214
+
215
+ fn sampleAt(q: vec2f, size: vec2i) -> vec4f {
216
+ let f = q * vec2f(size) - 0.5;
217
+ let corner = floor(f);
218
+ let w = f - corner;
219
+ let i = vec2i(corner);
220
+ let above = mix(pixelAt(i, size), pixelAt(i + vec2i(1, 0), size), w.x);
221
+ let below = mix(pixelAt(i + vec2i(0, 1), size), pixelAt(i + vec2i(1, 1), size), w.x);
222
+ return mix(above, below, w.y);
223
+ }
224
+
225
+ fn keyDistance(encoded: vec3f) -> f32 {
226
+ return distance(vec2f(dot(encoded, CB), dot(encoded, CR)) + CHROMA_OFFSET, settings.key.xy);
227
+ }
228
+
229
+ fn chromaOf(encoded: vec3f) -> vec2f {
230
+ let y = dot(encoded, LUMA);
231
+ return vec2f(encoded.b - y, encoded.r - y) / CHROMA_SCALE;
232
+ }
233
+
234
+ fn fromLumaChroma(y: f32, c: vec2f) -> vec3f {
235
+ let r = y + c.y * CHROMA_SCALE.y;
236
+ let b = y + c.x * CHROMA_SCALE.x;
237
+ return vec3f(r, (y - LUMA.r * r - LUMA.b * b) / LUMA.g, b);
238
+ }
239
+
240
+ fn cutSide(a: vec2f, b: vec2f, p: vec2f, size: vec2f) -> f32 {
241
+ let side = (b - a) * size;
242
+ let len = length(side);
243
+ if (len == 0.0) {
244
+ return 1.0e6;
245
+ }
246
+ let q = (p - a) * size;
247
+ return (q.y * side.x - q.x * side.y) / len;
248
+ }
249
+
250
+ fn cutMask(p: vec2f, size: vec2f) -> f32 {
251
+ let c0 = settings.corners[0].xy;
252
+ let c1 = settings.corners[0].zw;
253
+ let c2 = settings.corners[1].xy;
254
+ let c3 = settings.corners[1].zw;
255
+ let d = min(
256
+ min(cutSide(c0, c1, p, size), cutSide(c1, c2, p, size)),
257
+ min(cutSide(c2, c3, p, size), cutSide(c3, c0, p, size))
258
+ );
259
+ return clamp(d + 0.5, 0.0, 1.0);
260
+ }
261
+
262
+ @fragment
263
+ fn fragmentMain(input: Varyings) -> @location(0) vec4f {
264
+ // Taken first, while every pixel takes the same way, as a derivative must be
265
+ let size = 1.0 / max(fwidth(input.uv), vec2f(1e-6));
266
+ let key = settings.key;
267
+ let matte = settings.matte;
268
+ let correction = settings.correction;
269
+ let extras = settings.extras;
270
+
271
+ let p = vec2f(select(input.uv.x, 1.0 - input.uv.x, settings.flags.x > 0.5), input.uv.y);
272
+ let pixels = vec2i(textureDimensions(image));
273
+
274
+ let sampled = sampleAt(p, pixels);
275
+ var alpha = sampled.a;
276
+ var rgb = select(vec3f(0.0), sampled.rgb / alpha, alpha > 0.0);
277
+
278
+ // OBS's filter
279
+ if (key.w > 0.5) {
280
+ let texel = 1.0 / vec2f(pixels);
281
+ let across = vec2f(texel.x, 0.5 * texel.y);
282
+ let up = vec2f(0.5 * texel.x, -texel.y);
283
+ let around = keyDistance(toEncoded(sampleAt(p - across, pixels).rgb))
284
+ + keyDistance(toEncoded(sampleAt(p + across, pixels).rgb))
285
+ + keyDistance(toEncoded(sampleAt(p - up, pixels).rgb))
286
+ + keyDistance(toEncoded(sampleAt(p + up, pixels).rgb));
287
+ let beyond = (2.0 * around + keyDistance(toEncoded(rgb))) / 9.0 - matte.x;
288
+ alpha *= matte.w;
289
+ alpha *= pow(clamp(beyond / matte.y, 0.0, 1.0), 1.5);
290
+ let kept = pow(clamp(beyond / matte.z, 0.0, 1.0), 1.5);
291
+ if (settings.switches.y > 0.5) {
292
+ // The add-on, hueSpill
293
+ let encoded = toEncoded(rgb);
294
+ let toward = (key.xy - CHROMA_OFFSET) * key.z;
295
+ var c = chromaOf(encoded);
296
+ c -= toward * max(dot(c, toward), 0.0) * (1.0 - kept);
297
+ rgb = toLinear(clamp(fromLumaChroma(dot(encoded, LUMA), c), vec3f(0.0), vec3f(1.0)));
298
+ } else {
299
+ rgb = mix(vec3f(dot(rgb, LUMA)), rgb, kept);
300
+ }
301
+ rgb = pow(max(rgb, vec3f(0.0)), vec3f(correction.x)) * correction.y + correction.z;
302
+ }
303
+ rgb = clamp(rgb, vec3f(0.0), vec3f(1.0));
304
+
305
+ // The add-ons
306
+ rgb *= extras.rgb;
307
+ rgb = clamp(mix(vec3f(dot(rgb, LUMA)), rgb, extras.w), vec3f(0.0), vec3f(1.0));
308
+ if (settings.switches.x > 0.5) {
309
+ alpha *= cutMask(p, size);
310
+ }
311
+
312
+ return vec4f(toEncoded(rgb) * alpha, alpha);
313
+ }
314
+ `,k=16,A=null,j=null,M=null,N=null;function P(e){if(A)return A;let t=(async()=>{let n=e??await I(),r=await n.requestDevice({requiredLimits:{maxTextureDimension2D:n.limits.maxTextureDimension2D}});return r.lost.then(()=>{A===t&&(A=null,j=null,M=null,N=null)}),r})();return A=t,t.then(e=>{A===t&&(j=e)},()=>{}),t}function F(){return j}async function I(){let e=typeof navigator>`u`?void 0:navigator.gpu;if(!e)throw Error(`Greenscreen: WebGPU is not available here`);let t=await e.requestAdapter();if(!t)throw Error(`Greenscreen: WebGPU has no adapter here`);return t}function L(){if(M)return M;let e=(async()=>{try{let e=await I();return!e.info.isFallbackAdapter&&(await P(e),!0)}catch{return!1}})();return M=e,e.then(t=>{M===e&&(N=t)}),e}function R(){return typeof navigator>`u`||!navigator.gpu?!1:N}var z=new WeakMap;function B(e,t){let n=z.get(e);n||(n=new Map,z.set(e,n));let r=n.get(t);if(!r){let i=e.createShaderModule({label:`Greenscreen`,code:O});r=e.createRenderPipeline({label:`Greenscreen`,layout:`auto`,vertex:{module:i,entryPoint:`vertexMain`},fragment:{module:i,entryPoint:`fragmentMain`,targets:[{format:t}]},primitive:{topology:`triangle-list`}}),n.set(t,r)}return r}function V(){return navigator.gpu.getPreferredCanvasFormat()}var H=class{backend=`webgpu`;#e;#t;#n;#r;#i;#a;#o;#s;#c=null;#l=null;#u=null;#d=!1;#f=!1;constructor(e,t,n){let r=new OffscreenCanvas(1,1),i=S(r,`webgpu`);if(!i)throw Error(`Greenscreen: WebGPU is not available on this canvas`);let a=S(e,`2d`);if(!a)throw Error(`Greenscreen: this canvas has a context of another kind already`);this.#e=e,this.#t=a,this.#n=r,this.#r=i,this.#i=n,this.#p(t)}get lost(){return this.#d}#p(e){let t=V();this.#a=e,this.#r.configure({device:e,format:t,alphaMode:`premultiplied`,usage:k}),this.#o=B(e,t),this.#s=e.createBuffer({label:`Greenscreen settings`,size:128,usage:72}),this.#c=null,this.#l=null,e.lost.then(()=>this.#m(e))}async#m(e){if(!(this.#f||e!==this.#a)){this.#d=!0,this.#c=null,this.#l=null,this.#i.lost();try{let e=await P();if(this.#f)return;this.#p(e),this.#d=!1,this.#i.restored()}catch{}}}upload(e,t,n){if(this.#d)return;let r=this.#a,i=r.limits.maxTextureDimension2D;if(t>i||n>i)throw x(t,n,`WebGPU`,i);let a=this.#c;a&&a.width===t&&a.height===n||(a?.destroy(),a=r.createTexture({label:`Greenscreen picture`,size:{width:t,height:n},format:`rgba8unorm`,usage:22}),this.#c=a,this.#l=r.createBindGroup({layout:this.#o.getBindGroupLayout(0),entries:[{binding:0,resource:a.createView()},{binding:1,resource:{buffer:this.#s}}]})),r.queue.copyExternalImageToTexture({source:this.#h(e,t,n),flipY:!1},{texture:a,colorSpace:`srgb`,premultipliedAlpha:!1},{width:t,height:n})}#h(e,t,n){if(!(`videoWidth`in e||`displayWidth`in e&&!/^(RGB|BGR)/.test(e.format??``)))return e;let r=this.#u;if(!r){if(r=S(U(),`2d`),!r)return e;this.#u=r}let i=r.canvas;return(i.width!==t||i.height!==n)&&(i.width=t,i.height=n),r.globalCompositeOperation=`copy`,r.drawImage(e,0,0,t,n),i}draw(e){let t=this.#c,n=this.#l;if(this.#d||!t||!n)return;let{width:r,height:i}=t;for(let e of[this.#n,this.#e])(e.width!==r||e.height!==i)&&(e.width=r,e.height=i);let a=this.#a;a.queue.writeBuffer(this.#s,0,e,0,e.length);let o=a.createCommandEncoder(),s=o.beginRenderPass({colorAttachments:[{view:this.#r.getCurrentTexture().createView(),loadOp:`clear`,clearValue:[0,0,0,0],storeOp:`store`}]});s.setPipeline(this.#o),s.setBindGroup(0,n),s.draw(3),s.end(),a.queue.submit([o.finish()]);let c=this.#t;c.globalCompositeOperation=`copy`,c.drawImage(this.#n,0,0)}clear(){let e=this.#e;this.#t.clearRect(0,0,e.width,e.height)}dispose(){this.#f=!0,this.#d=!0,this.#c?.destroy(),this.#s.destroy(),this.#c=null,this.#l=null,this.#u=null,this.#r.unconfigure()}};function U(){return typeof document<`u`?document.createElement(`canvas`):new OffscreenCanvas(1,1)}var W=1e3/60,G=class{canvas;initialized;#e=null;#t=c;#n=new Float32Array(32);#r=new Float32Array(32);#i=!0;#a=null;#o=!1;#s=!1;#c=0;#l=null;#u=!1;#d;#f;#p;constructor(e=null,t={}){this.canvas=t.canvas??q(),this.#d=t.onLost,this.#f=t.onRestored,this.#p=t.onError??y,this.set(t.settings??{}),this.source=e,this.initialized=this.#m(t.backend??`auto`),this.initialized.catch(()=>{})}#m(e){if(e===`webgl`)return this.#h();if(e===`webgpu`)return this.#g();if(e===`auto`){let e=R();return e===null?L().then(e=>e?this.#g():this.#h()):e?this.#g():this.#h()}let t=e;return Promise.reject(TypeError(`Greenscreen: no backend called ${t}`))}#h(){if(this.#u)return Promise.reject(Error(`Greenscreen: disposed before its backend was ready`));try{return this.#e=new E(this.canvas,this.#_),Promise.resolve(`webgl`)}catch(e){return Promise.reject(e)}}#g(){let e=e=>{if(this.#u)throw Error(`Greenscreen: disposed before WebGPU was ready`);return this.#e=new H(this.canvas,e,this.#_),`webgpu`},t=F();if(!t)return P().then(e);try{return Promise.resolve(e(t))}catch(e){return Promise.reject(e)}}#_={lost:()=>{this.#s=!1,this.#o=!1,this.#d?.()},restored:()=>{this.#a?.invalidate(),this.#i=!0,this.#f?.()}};get backend(){return this.#e?.backend??null}get ready(){return this.#s}get frame(){return this.#c}get settings(){return this.#t}get source(){return this.#a?.source??null}set source(e){e!==this.source&&(this.#a?.dispose(),this.#a=e?new b(e):null,this.#o=!1,this.#s=!1)}set(e){this.#t=d(this.#t,e);let t=n(this.#t,this.#r);t.some((e,t)=>e!==this.#n[t])&&(this.#r=this.#n,this.#n=t,this.#i=!0)}reset(){this.set(c)}invalidate(){this.#a?.invalidate()}render(){let e=this.#e,t=this.#a;if(this.#u||!e||e.lost||!t)return!1;let n=this.#i;if(t.hasPicture()&&t.changed()){let[r,i]=t.size();try{e.upload(t.source,r,i),t.taken(),this.#o=!0,n=!0}catch(n){return this.#o=!1,this.#s=!1,t.failed()&&(e.clear(),this.#p(n)),!1}}return!this.#o||!n?this.#s:(e.draw(this.#n),this.#i=!1,this.#s=!0,this.#c+=1,!0)}start(){if(this.#l||this.#u)return;let e=0,t=()=>{e=J(t),this.render()};e=J(t),this.#l={cancel:()=>Y(e)}}stop(){this.#l?.cancel(),this.#l=null}dispose(){this.#u||(this.stop(),this.#u=!0,this.#a?.dispose(),this.#a=null,this.#e?.dispose(),this.#e=null,this.#s=!1)}};async function K(e=`auto`){let t=e===`auto`?await L()?`webgpu`:`webgl`:e;return t===`webgpu`&&B(await P(),V()),t}function q(){if(typeof document<`u`){let e=document.createElement(`canvas`);return e.width=1,e.height=1,e}if(typeof OffscreenCanvas<`u`)return new OffscreenCanvas(1,1);throw Error(`Greenscreen: there is no canvas to draw in here. Give it one with options.canvas`)}function J(e){return typeof requestAnimationFrame==`function`?requestAnimationFrame(e):setTimeout(e,W)}function Y(e){typeof cancelAnimationFrame==`function`?cancelAnimationFrame(e):clearTimeout(e)}return class extends G{static DEFAULT_SETTINGS=c;static KEY_COLORS=o;static RANGES=a;static warmUp=K}})();
315
+ //# sourceMappingURL=greenscreen.global.js.map