@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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stream-io/video-filters-web",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
4
4
  "packageManager": "yarn@3.5.0",
5
5
  "main": "./dist/index.cjs.js",
6
6
  "module": "./dist/index.es.js",
@@ -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
- pipeline.render();
49
+ const id = setInterval(
50
+ () => {
51
+ pipeline.render();
51
52
 
52
- if (backgroundFilter === 'image') {
53
- pipeline.updatePostProcessingConfig();
54
- }
55
- }, 1000 / (fps <= 0 ? 30 : fps));
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 weights =
66
- blurLevel === 'low'
67
- ? [0.227027027, 0.1545945946, 0.1016216216, 0.0340540541, 0.0142162162]
68
- : blurLevel === 'medium'
69
- ? [0.327027027, 0.1945945946, 0.1216216216, 0.0540540541, 0.0162162162]
70
- : [0.627027027, 0.3445945946, 0.2216216216, 0.0540540541, 0.0162162162];
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[5] = float[](0.0, 1.0, 2.0, 3.0, 4.0);
84
- const float weight[5] = float[](
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 < 5; 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;