@stream-io/video-filters-web 0.1.0 → 0.1.2
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/CHANGELOG.md +14 -0
- package/dist/index.cjs.js +19 -11
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.es.js +19 -11
- package/dist/index.es.js.map +1 -1
- package/dist/src/createRenderer.d.ts +1 -1
- package/package.json +1 -1
- package/src/createRenderer.ts +10 -7
- package/src/webgl2/backgroundBlurStage.ts +19 -11
package/package.json
CHANGED
package/src/createRenderer.ts
CHANGED
|
@@ -3,7 +3,7 @@ import { buildWebGL2Pipeline } from './webgl2/webgl2Pipeline';
|
|
|
3
3
|
import { getSegmentationParams, SegmentationLevel } from './segmentation';
|
|
4
4
|
|
|
5
5
|
export type BackgroundFilter = 'blur' | 'image';
|
|
6
|
-
export type BackgroundBlurLevel = 'low' | 'medium' | 'high';
|
|
6
|
+
export type BackgroundBlurLevel = 'low' | 'medium' | 'high' | number;
|
|
7
7
|
export type Renderer = {
|
|
8
8
|
/**
|
|
9
9
|
* Disposes of the renderer.
|
|
@@ -46,13 +46,16 @@ export function createRenderer(
|
|
|
46
46
|
getSegmentationParams(segmentationLevel),
|
|
47
47
|
);
|
|
48
48
|
|
|
49
|
-
const id = setInterval(
|
|
50
|
-
|
|
49
|
+
const id = setInterval(
|
|
50
|
+
() => {
|
|
51
|
+
pipeline.render();
|
|
51
52
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
53
|
+
if (backgroundFilter === 'image') {
|
|
54
|
+
pipeline.updatePostProcessingConfig();
|
|
55
|
+
}
|
|
56
|
+
},
|
|
57
|
+
1000 / (fps <= 0 ? 30 : fps),
|
|
58
|
+
);
|
|
56
59
|
|
|
57
60
|
return {
|
|
58
61
|
dispose: () => {
|
|
@@ -62,12 +62,22 @@ function buildBlurPass(
|
|
|
62
62
|
canvas: HTMLCanvasElement,
|
|
63
63
|
blurLevel: BackgroundBlurLevel,
|
|
64
64
|
) {
|
|
65
|
-
const
|
|
66
|
-
blurLevel === '
|
|
67
|
-
?
|
|
68
|
-
: blurLevel === '
|
|
69
|
-
|
|
70
|
-
|
|
65
|
+
const sigma =
|
|
66
|
+
typeof blurLevel === 'number'
|
|
67
|
+
? blurLevel
|
|
68
|
+
: blurLevel === 'low'
|
|
69
|
+
? 2
|
|
70
|
+
: blurLevel === 'medium'
|
|
71
|
+
? 4
|
|
72
|
+
: 6;
|
|
73
|
+
const windowSize = Math.max(1, Math.floor(sigma * 3));
|
|
74
|
+
const offset = new Array<number>(windowSize).fill(0).map((v, index) => index);
|
|
75
|
+
const variance = sigma ** 2;
|
|
76
|
+
const weights = offset.map((x) => {
|
|
77
|
+
var m = sigma * Math.sqrt(2 * Math.PI);
|
|
78
|
+
var e = Math.exp(-(x ** 2) / (2 * variance));
|
|
79
|
+
return e / m;
|
|
80
|
+
});
|
|
71
81
|
|
|
72
82
|
const fragmentShaderSource = glsl`#version 300 es
|
|
73
83
|
|
|
@@ -80,10 +90,8 @@ function buildBlurPass(
|
|
|
80
90
|
in vec2 v_texCoord;
|
|
81
91
|
out vec4 outColor;
|
|
82
92
|
|
|
83
|
-
const float offset[
|
|
84
|
-
const float weight[
|
|
85
|
-
${weights.join(',')}
|
|
86
|
-
);
|
|
93
|
+
const float offset[${windowSize}] = float[](${offset.map((i) => i.toFixed(10)).join(', ')});
|
|
94
|
+
const float weight[${windowSize}] = float[](${weights.map((i) => i.toFixed(10)).join(', ')});
|
|
87
95
|
|
|
88
96
|
void main() {
|
|
89
97
|
vec4 centerColor = texture(u_inputFrame, v_texCoord);
|
|
@@ -91,7 +99,7 @@ function buildBlurPass(
|
|
|
91
99
|
|
|
92
100
|
vec4 frameColor = centerColor * weight[0] * (1.0 - personMask);
|
|
93
101
|
|
|
94
|
-
for (int i = 1; i <
|
|
102
|
+
for (int i = 1; i < ${windowSize}; i++) {
|
|
95
103
|
vec2 offset = vec2(offset[i]) * u_texelSize;
|
|
96
104
|
|
|
97
105
|
vec2 texCoord = v_texCoord + offset;
|