@srsergio/taptapp-ar 1.0.74 → 1.0.76

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.
@@ -18,10 +18,10 @@ const getControllerWorker = async () => {
18
18
  }
19
19
  };
20
20
  ControllerWorker = await getControllerWorker();
21
- const DEFAULT_FILTER_CUTOFF = 1.0;
21
+ const DEFAULT_FILTER_CUTOFF = 0.5;
22
22
  const DEFAULT_FILTER_BETA = 0.1;
23
- const DEFAULT_WARMUP_TOLERANCE = 8;
24
- const DEFAULT_MISS_TOLERANCE = 2;
23
+ const DEFAULT_WARMUP_TOLERANCE = 2; // Instant detection
24
+ const DEFAULT_MISS_TOLERANCE = 5; // More grace when partially hidden
25
25
  class Controller {
26
26
  inputWidth;
27
27
  inputHeight;
@@ -22,9 +22,6 @@ export class CropDetector {
22
22
  projectedImage?: undefined;
23
23
  };
24
24
  };
25
- /**
26
- * Scans the ENTIRE frame by downsampling it to cropSize
27
- */
28
25
  _detectGlobal(imageData: any): {
29
26
  featurePoints: any[];
30
27
  debugExtra: {
@@ -24,16 +24,16 @@ class CropDetector {
24
24
  }
25
25
  detectMoving(input) {
26
26
  const imageData = input;
27
- // 🚀 MOONSHOT: Alternate between local crops and GLOBAL scan
28
- // This solves the "not reading the whole screen" issue.
29
- // Every 3 frames, we do a full screen downsampled scan.
30
- if (this.lastRandomIndex % 3 === 0) {
27
+ // 🚀 MOONSHOT: High Frequency Global Scan
28
+ // Scan full screen every 2 frames when searching to guarantee instant recovery
29
+ if (this.lastRandomIndex % 2 === 0) {
31
30
  this.lastRandomIndex = (this.lastRandomIndex + 1) % 25;
32
31
  return this._detectGlobal(imageData);
33
32
  }
34
- // Original moving crop logic for high-detail local detection
33
+ // Local crops (25 grid)
35
34
  const gridSize = 5;
36
35
  const idx = (this.lastRandomIndex - 1) % (gridSize * gridSize);
36
+ // ... rest of logic remains but we hit it less often because global scan is more successful
37
37
  const dx = idx % gridSize;
38
38
  const dy = Math.floor(idx / gridSize);
39
39
  const stepX = this.cropSize / 3;
@@ -45,19 +45,18 @@ class CropDetector {
45
45
  this.lastRandomIndex = (this.lastRandomIndex + 1) % 25;
46
46
  return this._detect(imageData, startX, startY);
47
47
  }
48
- /**
49
- * Scans the ENTIRE frame by downsampling it to cropSize
50
- */
51
48
  _detectGlobal(imageData) {
52
49
  const croppedData = new Float32Array(this.cropSize * this.cropSize);
53
50
  const scaleX = this.width / this.cropSize;
54
51
  const scaleY = this.height / this.cropSize;
55
- // Fast downsample (nearest neighbor is enough for initial feature detection)
52
+ // Better sampling: avoid missing edges by jumping too much
56
53
  for (let y = 0; y < this.cropSize; y++) {
57
54
  const srcY = Math.floor(y * scaleY) * this.width;
58
55
  const dstY = y * this.cropSize;
59
56
  for (let x = 0; x < this.cropSize; x++) {
60
- croppedData[dstY + x] = imageData[srcY + Math.floor(x * scaleX)];
57
+ // Average slightly to preserve gradients
58
+ const sx = Math.floor(x * scaleX);
59
+ croppedData[dstY + x] = (imageData[srcY + sx] + imageData[srcY + sx + 1]) * 0.5;
61
60
  }
62
61
  }
63
62
  const { featurePoints } = this.detector.detect(croppedData);
@@ -123,7 +123,7 @@ class SimpleAR {
123
123
  }
124
124
  this.lastMatrix = worldMatrix;
125
125
  if (!this.filters[targetIndex]) {
126
- this.filters[targetIndex] = new OneEuroFilter({ minCutOff: 1.0, beta: 0.1 });
126
+ this.filters[targetIndex] = new OneEuroFilter({ minCutOff: 0.8, beta: 0.2 });
127
127
  }
128
128
  const flatMVT = [
129
129
  modelViewTransform[0][0], modelViewTransform[0][1], modelViewTransform[0][2], modelViewTransform[0][3],
@@ -165,18 +165,18 @@ class Tracker {
165
165
  matchingPoints.push([bestX, bestY]);
166
166
  continue;
167
167
  }
168
- // Search in projected image
169
- for (let sy = -searchOneSize; sy <= searchOneSize; sy += searchGap) {
168
+ // 🚀 MOONSHOT: Coarse-to-Fine Search for MAXIMUM FPS
169
+ // Step 1: Coarse search (Gap 4)
170
+ const coarseGap = 4;
171
+ for (let sy = -searchOneSize; sy <= searchOneSize; sy += coarseGap) {
170
172
  const cy = sCenterY + sy;
171
173
  if (cy < templateOneSize || cy >= markerHeight - templateOneSize)
172
174
  continue;
173
- for (let sx = -searchOneSize; sx <= searchOneSize; sx += searchGap) {
175
+ for (let sx = -searchOneSize; sx <= searchOneSize; sx += coarseGap) {
174
176
  const cx = sCenterX + sx;
175
177
  if (cx < templateOneSize || cx >= markerWidth - templateOneSize)
176
178
  continue;
177
- let sumI = 0;
178
- let sumI2 = 0;
179
- let sumIT = 0;
179
+ let sumI = 0, sumI2 = 0, sumIT = 0;
180
180
  for (let ty = -templateOneSize; ty <= templateOneSize; ty++) {
181
181
  const rowOffset = (cy + ty) * markerWidth;
182
182
  const tRowOffset = (ty + templateOneSize) * templateSize;
@@ -199,6 +199,43 @@ class Tracker {
199
199
  }
200
200
  }
201
201
  }
202
+ // Step 2: Fine refinement (Gap 1) only around the best coarse match
203
+ if (bestSim > AR2_SIM_THRESH) {
204
+ const fineCenterX = (bestX * scale) | 0;
205
+ const fineCenterY = (bestY * scale) | 0;
206
+ const fineSearch = coarseGap;
207
+ for (let sy = -fineSearch; sy <= fineSearch; sy++) {
208
+ const cy = fineCenterY + sy;
209
+ if (cy < templateOneSize || cy >= markerHeight - templateOneSize)
210
+ continue;
211
+ for (let sx = -fineSearch; sx <= fineSearch; sx++) {
212
+ const cx = fineCenterX + sx;
213
+ if (cx < templateOneSize || cx >= markerWidth - templateOneSize)
214
+ continue;
215
+ let sumI = 0, sumI2 = 0, sumIT = 0;
216
+ for (let ty = -templateOneSize; ty <= templateOneSize; ty++) {
217
+ const rowOffset = (cy + ty) * markerWidth;
218
+ const tRowOffset = (ty + templateOneSize) * templateSize;
219
+ for (let tx = -templateOneSize; tx <= templateOneSize; tx++) {
220
+ const valI = projectedImage[rowOffset + (cx + tx)];
221
+ const valT = templateData[tRowOffset + (tx + templateOneSize)];
222
+ sumI += valI;
223
+ sumI2 += valI * valI;
224
+ sumIT += valI * valT;
225
+ }
226
+ }
227
+ const varI = Math.sqrt(Math.max(0, sumI2 - (sumI * sumI) * oneOverNPixels));
228
+ if (varI < 0.0001)
229
+ continue;
230
+ const sim = (sumIT - (sumI * sumT) * oneOverNPixels) / (varI * varT);
231
+ if (sim > bestSim) {
232
+ bestSim = sim;
233
+ bestX = cx / scale;
234
+ bestY = cy / scale;
235
+ }
236
+ }
237
+ }
238
+ }
202
239
  sims[f] = bestSim;
203
240
  matchingPoints.push([bestX, bestY]);
204
241
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@srsergio/taptapp-ar",
3
- "version": "1.0.74",
3
+ "version": "1.0.76",
4
4
  "description": "AR Compiler for Node.js and Browser",
5
5
  "repository": {
6
6
  "type": "git",
@@ -19,10 +19,10 @@ const getControllerWorker = async () => {
19
19
  };
20
20
  ControllerWorker = await getControllerWorker();
21
21
 
22
- const DEFAULT_FILTER_CUTOFF = 1.0;
22
+ const DEFAULT_FILTER_CUTOFF = 0.5;
23
23
  const DEFAULT_FILTER_BETA = 0.1;
24
- const DEFAULT_WARMUP_TOLERANCE = 8;
25
- const DEFAULT_MISS_TOLERANCE = 2;
24
+ const DEFAULT_WARMUP_TOLERANCE = 2; // Instant detection
25
+ const DEFAULT_MISS_TOLERANCE = 5; // More grace when partially hidden
26
26
 
27
27
  export interface ControllerOptions {
28
28
  inputWidth: number;
@@ -33,20 +33,19 @@ class CropDetector {
33
33
  detectMoving(input) {
34
34
  const imageData = input;
35
35
 
36
- // 🚀 MOONSHOT: Alternate between local crops and GLOBAL scan
37
- // This solves the "not reading the whole screen" issue.
38
- // Every 3 frames, we do a full screen downsampled scan.
39
- if (this.lastRandomIndex % 3 === 0) {
36
+ // 🚀 MOONSHOT: High Frequency Global Scan
37
+ // Scan full screen every 2 frames when searching to guarantee instant recovery
38
+ if (this.lastRandomIndex % 2 === 0) {
40
39
  this.lastRandomIndex = (this.lastRandomIndex + 1) % 25;
41
40
  return this._detectGlobal(imageData);
42
41
  }
43
42
 
44
- // Original moving crop logic for high-detail local detection
43
+ // Local crops (25 grid)
45
44
  const gridSize = 5;
46
45
  const idx = (this.lastRandomIndex - 1) % (gridSize * gridSize);
46
+ // ... rest of logic remains but we hit it less often because global scan is more successful
47
47
  const dx = idx % gridSize;
48
48
  const dy = Math.floor(idx / gridSize);
49
-
50
49
  const stepX = this.cropSize / 3;
51
50
  const stepY = this.cropSize / 3;
52
51
 
@@ -57,29 +56,26 @@ class CropDetector {
57
56
  startY = Math.max(0, Math.min(this.height - this.cropSize - 1, startY));
58
57
 
59
58
  this.lastRandomIndex = (this.lastRandomIndex + 1) % 25;
60
-
61
59
  return this._detect(imageData, startX, startY);
62
60
  }
63
61
 
64
- /**
65
- * Scans the ENTIRE frame by downsampling it to cropSize
66
- */
67
62
  _detectGlobal(imageData) {
68
63
  const croppedData = new Float32Array(this.cropSize * this.cropSize);
69
64
  const scaleX = this.width / this.cropSize;
70
65
  const scaleY = this.height / this.cropSize;
71
66
 
72
- // Fast downsample (nearest neighbor is enough for initial feature detection)
67
+ // Better sampling: avoid missing edges by jumping too much
73
68
  for (let y = 0; y < this.cropSize; y++) {
74
69
  const srcY = Math.floor(y * scaleY) * this.width;
75
70
  const dstY = y * this.cropSize;
76
71
  for (let x = 0; x < this.cropSize; x++) {
77
- croppedData[dstY + x] = imageData[srcY + Math.floor(x * scaleX)];
72
+ // Average slightly to preserve gradients
73
+ const sx = Math.floor(x * scaleX);
74
+ croppedData[dstY + x] = (imageData[srcY + sx] + imageData[srcY + sx + 1]) * 0.5;
78
75
  }
79
76
  }
80
77
 
81
78
  const { featurePoints } = this.detector.detect(croppedData);
82
-
83
79
  featurePoints.forEach((p) => {
84
80
  p.x *= scaleX;
85
81
  p.y *= scaleY;
@@ -163,7 +163,7 @@ class SimpleAR {
163
163
  this.lastMatrix = worldMatrix;
164
164
 
165
165
  if (!this.filters[targetIndex]) {
166
- this.filters[targetIndex] = new OneEuroFilter({ minCutOff: 1.0, beta: 0.1 });
166
+ this.filters[targetIndex] = new OneEuroFilter({ minCutOff: 0.8, beta: 0.2 });
167
167
  }
168
168
 
169
169
  const flatMVT = [
@@ -214,29 +214,25 @@ class Tracker {
214
214
  continue;
215
215
  }
216
216
 
217
- // Search in projected image
218
- for (let sy = -searchOneSize; sy <= searchOneSize; sy += searchGap) {
217
+ // 🚀 MOONSHOT: Coarse-to-Fine Search for MAXIMUM FPS
218
+ // Step 1: Coarse search (Gap 4)
219
+ const coarseGap = 4;
220
+ for (let sy = -searchOneSize; sy <= searchOneSize; sy += coarseGap) {
219
221
  const cy = sCenterY + sy;
220
222
  if (cy < templateOneSize || cy >= markerHeight - templateOneSize) continue;
221
223
 
222
- for (let sx = -searchOneSize; sx <= searchOneSize; sx += searchGap) {
224
+ for (let sx = -searchOneSize; sx <= searchOneSize; sx += coarseGap) {
223
225
  const cx = sCenterX + sx;
224
226
  if (cx < templateOneSize || cx >= markerWidth - templateOneSize) continue;
225
227
 
226
- let sumI = 0;
227
- let sumI2 = 0;
228
- let sumIT = 0;
229
-
228
+ let sumI = 0, sumI2 = 0, sumIT = 0;
230
229
  for (let ty = -templateOneSize; ty <= templateOneSize; ty++) {
231
230
  const rowOffset = (cy + ty) * markerWidth;
232
231
  const tRowOffset = (ty + templateOneSize) * templateSize;
233
232
  for (let tx = -templateOneSize; tx <= templateOneSize; tx++) {
234
233
  const valI = projectedImage[rowOffset + (cx + tx)];
235
234
  const valT = templateData[tRowOffset + (tx + templateOneSize)];
236
-
237
- sumI += valI;
238
- sumI2 += valI * valI;
239
- sumIT += valI * valT;
235
+ sumI += valI; sumI2 += valI * valI; sumIT += valI * valT;
240
236
  }
241
237
  }
242
238
 
@@ -252,6 +248,41 @@ class Tracker {
252
248
  }
253
249
  }
254
250
 
251
+ // Step 2: Fine refinement (Gap 1) only around the best coarse match
252
+ if (bestSim > AR2_SIM_THRESH) {
253
+ const fineCenterX = (bestX * scale) | 0;
254
+ const fineCenterY = (bestY * scale) | 0;
255
+ const fineSearch = coarseGap;
256
+
257
+ for (let sy = -fineSearch; sy <= fineSearch; sy++) {
258
+ const cy = fineCenterY + sy;
259
+ if (cy < templateOneSize || cy >= markerHeight - templateOneSize) continue;
260
+ for (let sx = -fineSearch; sx <= fineSearch; sx++) {
261
+ const cx = fineCenterX + sx;
262
+ if (cx < templateOneSize || cx >= markerWidth - templateOneSize) continue;
263
+
264
+ let sumI = 0, sumI2 = 0, sumIT = 0;
265
+ for (let ty = -templateOneSize; ty <= templateOneSize; ty++) {
266
+ const rowOffset = (cy + ty) * markerWidth;
267
+ const tRowOffset = (ty + templateOneSize) * templateSize;
268
+ for (let tx = -templateOneSize; tx <= templateOneSize; tx++) {
269
+ const valI = projectedImage[rowOffset + (cx + tx)];
270
+ const valT = templateData[tRowOffset + (tx + templateOneSize)];
271
+ sumI += valI; sumI2 += valI * valI; sumIT += valI * valT;
272
+ }
273
+ }
274
+ const varI = Math.sqrt(Math.max(0, sumI2 - (sumI * sumI) * oneOverNPixels));
275
+ if (varI < 0.0001) continue;
276
+ const sim = (sumIT - (sumI * sumT) * oneOverNPixels) / (varI * varT);
277
+ if (sim > bestSim) {
278
+ bestSim = sim;
279
+ bestX = cx / scale;
280
+ bestY = cy / scale;
281
+ }
282
+ }
283
+ }
284
+ }
285
+
255
286
  sims[f] = bestSim;
256
287
  matchingPoints.push([bestX, bestY]);
257
288
  }