@zappar/zappar-cv 3.0.1-alpha.13 → 3.0.1-alpha.14

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/README.md CHANGED
@@ -18,8 +18,8 @@ npm i @zappar/zappar-cv
18
18
 
19
19
  You can use our CDN from within your HTML:
20
20
  ```
21
- <script src="https://libs.zappar.com/zappar-cv/3.0.1-alpha.13/zappar-cv.js"></script>
21
+ <script src="https://libs.zappar.com/zappar-cv/3.0.1-alpha.14/zappar-cv.js"></script>
22
22
  ```
23
23
 
24
24
  Or you can download and host our standalone JavaScript bundle:
25
- [https://libs.zappar.com/zappar-cv/3.0.1-alpha.13/zappar-cv.zip](https://libs.zappar.com/zappar-cv/3.0.1-alpha.13/zappar-cv.zip)
25
+ [https://libs.zappar.com/zappar-cv/3.0.1-alpha.14/zappar-cv.zip](https://libs.zappar.com/zappar-cv/3.0.1-alpha.14/zappar-cv.zip)
@@ -0,0 +1,2 @@
1
+ export declare function disposeDrawPlane(): void;
2
+ export declare function drawAxis(gl: WebGLRenderingContext, projectionMatrix: Float32Array, cameraMatrix: Float32Array, targetMatrix: Float32Array): void;
@@ -0,0 +1,172 @@
1
+ import { compileShader, linkProgram } from "./shader";
2
+ let shader;
3
+ let vbo;
4
+ let uvbo;
5
+ let texturesByUrl = {};
6
+ export function disposeDrawPlane() {
7
+ shader = undefined;
8
+ vbo = undefined;
9
+ uvbo = undefined;
10
+ texturesByUrl = {};
11
+ }
12
+ function generate(gl) {
13
+ if (vbo)
14
+ return vbo;
15
+ vbo = gl.createBuffer();
16
+ if (!vbo)
17
+ throw new Error("Unable to create buffer object");
18
+ let coords = [
19
+ 0, 0, 0, 1, 0, 0,
20
+ 0, 0, 0, 0, 1, 0,
21
+ 0, 0, 0, 0, 0, 1
22
+ ];
23
+ gl.bindBuffer(gl.ARRAY_BUFFER, vbo);
24
+ gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(coords), gl.STATIC_DRAW);
25
+ gl.bindBuffer(gl.ARRAY_BUFFER, null);
26
+ return vbo;
27
+ }
28
+ function generateColor(gl) {
29
+ if (uvbo)
30
+ return uvbo;
31
+ uvbo = gl.createBuffer();
32
+ if (!uvbo)
33
+ throw new Error("Unable to create buffer object");
34
+ let coords = [
35
+ 1, 0, 0, 1, 1, 0, 0, 1,
36
+ 0, 1, 0, 1, 0, 1, 0, 1,
37
+ 0, 0, 1, 1, 0, 0, 1, 1
38
+ ];
39
+ gl.bindBuffer(gl.ARRAY_BUFFER, uvbo);
40
+ gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(coords), gl.STATIC_DRAW);
41
+ gl.bindBuffer(gl.ARRAY_BUFFER, null);
42
+ return uvbo;
43
+ }
44
+ export function drawAxis(gl, projectionMatrix, cameraMatrix, targetMatrix) {
45
+ let shader = getShader(gl);
46
+ let v = generate(gl);
47
+ let uvbo = generateColor(gl);
48
+ gl.disable(gl.DEPTH_TEST);
49
+ gl.useProgram(shader.prog);
50
+ gl.uniformMatrix4fv(shader.unif_proj, false, projectionMatrix);
51
+ gl.uniformMatrix4fv(shader.unif_camera, false, cameraMatrix);
52
+ gl.uniformMatrix4fv(shader.unif_matrix, false, targetMatrix);
53
+ gl.bindBuffer(gl.ARRAY_BUFFER, v);
54
+ gl.activeTexture(gl.TEXTURE0);
55
+ gl.bindTexture(gl.TEXTURE_2D, null);
56
+ gl.vertexAttribPointer(shader.attr_position, 3, gl.FLOAT, false, 3 * 4, 0);
57
+ gl.enableVertexAttribArray(shader.attr_position);
58
+ gl.bindBuffer(gl.ARRAY_BUFFER, uvbo);
59
+ gl.vertexAttribPointer(shader.attr_color, 4, gl.FLOAT, false, 4 * 4, 0);
60
+ gl.enableVertexAttribArray(shader.attr_color);
61
+ gl.lineWidth(10);
62
+ gl.drawArrays(gl.LINES, 0, 6);
63
+ gl.disableVertexAttribArray(shader.attr_position);
64
+ gl.disableVertexAttribArray(shader.attr_color);
65
+ gl.bindBuffer(gl.ARRAY_BUFFER, null);
66
+ }
67
+ function generateLocalMatrix() {
68
+ let position = [0, 0, -5];
69
+ return new Float32Array([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, position[0], position[1], position[2], 1]);
70
+ }
71
+ let vertexShaderSrc = `
72
+ #ifndef GL_ES
73
+ #define highp
74
+ #define mediump
75
+ #define lowp
76
+ #endif
77
+
78
+ uniform mat4 projMatrix;
79
+ uniform mat4 cameraMatrix;
80
+ uniform mat4 modelViewMatrix;
81
+ attribute vec4 position;
82
+ attribute vec4 color;
83
+
84
+ varying highp vec4 vColor;
85
+
86
+ void main()
87
+ {
88
+ gl_Position = projMatrix * cameraMatrix * modelViewMatrix * position;
89
+ vColor = color;
90
+ }`;
91
+ let fragmentShaderSrc = `
92
+ #define highp mediump
93
+ #ifdef GL_ES
94
+ // define default precision for float, vec, mat.
95
+ precision highp float;
96
+ #else
97
+ #define highp
98
+ #define mediump
99
+ #define lowp
100
+ #endif
101
+
102
+ varying highp vec4 vColor;
103
+
104
+ void main()
105
+ {
106
+ gl_FragColor = vColor;
107
+ }`;
108
+ function getShader(gl) {
109
+ if (shader)
110
+ return shader;
111
+ let prog = gl.createProgram();
112
+ if (!prog)
113
+ throw new Error("Unable to create program");
114
+ let vertexShader = compileShader(gl, gl.VERTEX_SHADER, vertexShaderSrc);
115
+ let fragmentShader = compileShader(gl, gl.FRAGMENT_SHADER, fragmentShaderSrc);
116
+ gl.attachShader(prog, vertexShader);
117
+ gl.attachShader(prog, fragmentShader);
118
+ linkProgram(gl, prog);
119
+ let unif_proj = gl.getUniformLocation(prog, "projMatrix");
120
+ if (!unif_proj)
121
+ throw new Error("Unable to get uniform location projMatrix");
122
+ let unif_matrix = gl.getUniformLocation(prog, "modelViewMatrix");
123
+ if (!unif_matrix)
124
+ throw new Error("Unable to get uniform location modelViewMatrix");
125
+ let unif_camera = gl.getUniformLocation(prog, "cameraMatrix");
126
+ if (!unif_camera)
127
+ throw new Error("Unable to get uniform location cameraMatrix");
128
+ shader = {
129
+ prog,
130
+ unif_matrix,
131
+ unif_proj,
132
+ unif_camera,
133
+ attr_position: gl.getAttribLocation(prog, "position"),
134
+ attr_color: gl.getAttribLocation(prog, "color")
135
+ };
136
+ return shader;
137
+ }
138
+ function loadTexture(gl, url) {
139
+ if (texturesByUrl[url])
140
+ return texturesByUrl[url];
141
+ let texture = gl.createTexture();
142
+ if (!texture)
143
+ throw new Error("Unable to create texture");
144
+ texturesByUrl[url] = texture;
145
+ gl.bindTexture(gl.TEXTURE_2D, texture);
146
+ // Because images have to be download over the internet
147
+ // they might take a moment until they are ready.
148
+ // Until then put a single pixel in the texture so we can
149
+ // use it immediately. When the image has finished downloading
150
+ // we'll update the texture with the contents of the image.
151
+ const level = 0;
152
+ const internalFormat = gl.RGBA;
153
+ const width = 1;
154
+ const height = 1;
155
+ const border = 0;
156
+ const srcFormat = gl.RGBA;
157
+ const srcType = gl.UNSIGNED_BYTE;
158
+ const pixel = new Uint8Array([0, 0, 255, 255]); // opaque blue
159
+ gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true);
160
+ gl.texImage2D(gl.TEXTURE_2D, level, internalFormat, width, height, border, srcFormat, srcType, pixel);
161
+ const image = new Image();
162
+ image.onload = function () {
163
+ gl.bindTexture(gl.TEXTURE_2D, texture);
164
+ gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true);
165
+ gl.texImage2D(gl.TEXTURE_2D, level, internalFormat, srcFormat, srcType, image);
166
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
167
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
168
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
169
+ };
170
+ image.src = url;
171
+ return texture;
172
+ }
package/lib/version.d.ts CHANGED
@@ -1 +1 @@
1
- export declare const VERSION = "3.0.1-alpha.13";
1
+ export declare const VERSION = "3.0.1-alpha.14";
package/lib/version.js CHANGED
@@ -1 +1 @@
1
- export const VERSION = "3.0.1-alpha.13";
1
+ export const VERSION = "3.0.1-alpha.14";
Binary file
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zappar/zappar-cv",
3
- "version": "3.0.1-alpha.13",
3
+ "version": "3.0.1-alpha.14",
4
4
  "description": "Zappar's core computer vision library, supporting image, face and world tracking.",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",